- ago
I am using a strategy with a partial position closure and I would like to understand if it is normal that when it is triggered, my buy order is executed in several parts.

I assume that this is intentional for the backtest, but in the Strategy Monitor I would have only one position. Can someone confirm this ?

I took the strategyKnife Juggler as an example.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript16 {    public class MyStrategy : UserStrategyBase    {       private bool halfSold = false;       public MyStrategy() : base()       {          StartIndex = 20;       }       public override void Initialize(BarHistory bars)       {          source = bars.Close;          pct = 2.00;          pct = (100.0 - pct) / 100.0;          multSource = source * pct;          PlotStopsAndLimits(3);          PlotStopsAndLimits(3);          foreach (IndicatorBase ib in _startIndexList)             if (ib.FirstValidIndex > StartIndex)                StartIndex = ib.FirstValidIndex;       }       public override void Execute(BarHistory bars, int idx)       {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          if (foundPosition0 == null)          {             Backtester.CancelationCode = 1;             val = multSource[idx];             _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, val, 0, "Buy " + 2.00 + "% below " + source.Description);             halfSold = false;          }          else          {             double profitTarget6Pct = foundPosition0.EntryPrice * 1.06;             if (!halfSold && bars.Close[idx] >= profitTarget6Pct && foundPosition0.Quantity > 1)             {                double quantityToSell = Math.Max(1, foundPosition0.Quantity * 0.5);                Transaction partialSellTransaction = PlaceTrade(bars, TransactionType.Sell, OrderType.Market, quantityToSell, 0, "Sell half at 6% profit");                partialSellTransaction.Quantity = quantityToSell;                halfSold = true;             }             if (idx - foundPosition0.EntryBar + 1 >= 20)             {                ClosePosition(foundPosition0, OrderType.Market, 0, "Sell after 20 bars");                halfSold = false;             }             else if (foundPosition0.ProfitPctAsOf(idx) >= 15.0)             {                ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * 1.15, "Sell at 15% profit tgt");                halfSold = false;             }          }       }       public override void NewWFOInterval(BarHistory bars)       {          source = bars.Close;          pct = 2.00;          pct = (100.0 - pct) / 100.0;          multSource = source * pct;       }       private double pct;       private double val;       private TimeSeries source;       private TimeSeries multSource;       private Transaction _transaction;       private List<IndicatorBase> _startIndexList = new List<IndicatorBase>();    } }


I understood that splitPosition is deprecated.

Also, when I change market order by limit for the 6% the logic of the strategy becomes different.
0
166
Solved
3 Replies

Reply

Bookmark

Sort
Cone8
 ( 4.83% )
- ago
#1
Re: I understood that splitPosition is deprecated.
It's not required. When you exit a fraction of a position, as this script does, WealthLab splits the original position for you. When that happens, the position that you're working with becomes 2 positions - or however many required depending on the Position.Quantity that you exit.

QUOTE:
Also, when I change market order by limit for the 6% the logic of the strategy becomes different.
What did expect?
0
- ago
#2
So, during backtesting, there is a look-ahead bias that causes my initial buy position to be split when there are multiple closings.

QUOTE:
What did expect?

I expected my order to become a limit order by changing order.market to order.limit. However, the entire logic of the strategy becomes incomprehensible.
0
Cone8
 ( 4.83% )
- ago
#3
QUOTE:
So, during backtesting, there is a look-ahead bias

No such thing is happening.

When it's time to exit half the position, half the shares are sold. Nothing more, just like in real trading. That happens in the code above when
1. You haven't already split the shares, and,
2. bars.Close[idx] >= profitTarget6Pct is true, and
3. The position is greater than 1 share.

But now we have to keep track of 2 positions because half the shares are going to be sold, probably at a different price. Both positions have the same basis (entry price) because all the shares were bought at the same time.

Even on a tax form you have to keep track of it that way. 🤷‍♂️
1
Best Answer

Reply

Bookmark

Sort