- ago
Suppose I open three SPY positions in this order:
Transaction transaction = PlaceTrade(bars, TransactionType.Buy , OrderType.Market);
transaction.Quantity = 1200;
transaction = PlaceTrade(bars, TransactionType.Buy , OrderType.Market);
transaction.Quantity = 1300;
transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);
transaction.Quantity = 1100;

How can I close the first position using PlaceTrade?
I currently use ClosePosition, but when the market moves fast I get +- few shares than the shares actually bought by Fidelity. By using PlaceTrade I control the number of shares both when entering and closing the pos.

Your help is appreciated.

0
322
Solved
8 Replies

Reply

Bookmark

Sort
- ago
#1
It'd be more convenient to close it if you create the three instances rather than reuse one.
1
Glitch8
 ( 12.10% )
- ago
#2
Yes, you need to pass the INSTANCE of the Transaction to close, and you're losing your instances by re-assigning values to that "transaction" variable each time you call PlaceTrade.
0
- ago
#3
Thank you Glitch, but I still don't know how to do it. Given the example below, would you be kind enough to write the line that would close the first transaction?

CODE:
public override void Execute(BarHistory bars, int bar) {          int barTime = bars.DateTimes[bar].Hour * 100 + bars.DateTimes[bar].Minute;          Transaction transaction1, transaction2, transaction3; if (!HasOpenPosition(bars, PositionType.Long)) {             if (barTime == 940)             {                transaction1 = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                transaction1.Quantity = 1200;                transaction2 = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                transaction2.Quantity = 1300;                transaction3 = PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                transaction3.Quantity = 1100;             } } else {             if (barTime > 1555)             {                // Close the first position.             }                } }


0
Glitch8
 ( 12.10% )
- ago
#4
OK there was a missing piece we neglected to lay out here, apologies for that! We need to use Position Tags to "tag" each position, we can then use FindPosition to find the appropriate Position to close. So, really you don't need separate Transaction variables like Eugene and I thought.

Here I'm closing the second position we opened:

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript2 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { }       public override void Execute(BarHistory bars, int bar)       {          int barTime = bars.DateTimes[bar].Hour * 100 + bars.DateTimes[bar].Minute;          Transaction transaction1, transaction2, transaction3;          if (!HasOpenPosition(bars, PositionType.Long))          {             if (barTime == 940)             {                transaction1 = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 1);                transaction1.Quantity = 11;                transaction2 = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 2);                transaction2.Quantity = 22;                transaction3 = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 3);                transaction3.Quantity = 33;             }          }          else          {             if (barTime > 1555)             {                Position position = FindOpenPosition(2);                ClosePosition(position, OrderType.Market);             }          }       } //declare private variables below } }
0
- ago
#5
QUOTE:
we can then use FindPosition

You mean FindOpenPosition. I looked up the example given for FindOpenPosition, but that raises another question. When tagging a position with the PlaceTrade method, how can one select integer tags that are unique? I don't want to mistakenly select the same integer twice for two different positions.

I suppose we could use "Positions.Count" as our position tag-number generator and that would insure a unique numerical tag for each created position. Is that what everyone is doing? If so, that approach should be included in the FindOpenPosition example in the Quick Reference.
0
Glitch8
 ( 12.10% )
- ago
#6
Yes, I meant FindOpenPosition!
0
- ago
#7
Thanks. Using integer Tag with PlaceTrade instead of ClosePosition works too.
CODE:
Transaction transaction = PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, 1, ""); transaction.Quantity = (int)position.Quantity;


Note: ClosePosition will close all open positions with the same tag value. which was a bonus for me.
1
Best Answer
- ago
#8
QUOTE:
ClosePosition will close all open positions with the same tag value....

Interesting. So one can use the same integer tag value on different positions that need to be closed together. I didn't know that! I like that feature.
0

Reply

Bookmark

Sort