- ago
In build 41 video you explained how to close part of an open position by assigning value to 'exit' transaction, but I couldn't apply the logic.
in below example, I bought 1000 shares by setting transaction quantity. Then I want to sell only 100 shares. can you point me to an example or show me how to to do that?

CODE:
if (!HasOpenPosition(bars, PositionType.Long)) {             Transaction trans= PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             trans.Quantity = 1000;              } else { // Sell 10% of open position.          }
0
320
Solved
3 Answers

Reply

Bookmark

Sort
- ago
#1
I tried to do this:
CODE:
ClosePosition(LastOpenPosition, OrderType.Market);             if(Backtester.TransactionLog.Count > 0)             {                Backtester.TransactionLog[Backtester.TransactionLog.Count - 1].Quantity = 100;             }

but it generated 10 positions for each one I opened.
0
- ago
#2
Wealth-Lab creates open positions in the past automatically when a position is split by selling a part of it. This is by design.

You can do it like that or for example, by splitting the last position (in a single position scenario) like this:
CODE:
PlaceTrade(bars, TransactionType.Sell, OrderType.Market).Quantity = LastPosition.Quantity * 0.10;
1
Best Answer
- ago
#3
Thank you. Your suggestion is much cleaner than my code.
1

Reply

Bookmark

Sort