I'm running into a weird issue. I have used the following code to sell half a position if a condition is reached, and reset the trailing stop to a new level:
However what is happening is the strategy is entering the position multiple times on the same date, each time at 1/2 the value of the previous entry (see below). I did not change any of the entry parameters or code, which has worked fine before I made this change.
CODE:
Transaction t = PlaceTrade(bars, TransactionType.Sell, OrderType.Market); t.Quantity = LastPosition.Quantity * 0.5; Backtester.CancelationCode = 1; CloseAtTrailingStop(foundPosition0, TrailingStopType.PercentHL, sllongaboveV2.AsDouble, "Sell at " + sllongaboveV2.AsDouble + "% trailing stop loss after 10% gain");
However what is happening is the strategy is entering the position multiple times on the same date, each time at 1/2 the value of the previous entry (see below). I did not change any of the entry parameters or code, which has worked fine before I made this change.
Rename
When you "split" the position like this, the effect creates 2 positions on the entry date. You exit one and the other is left for the logic that follows or the next bar. If the exit logic doesn't keep track that part of the original position was sold, it just keeps doing it again.
Here's a demo of one way to do it by keeping track that the position was split using a boolean _positionSplit .
Here's a demo of one way to do it by keeping track that the position was split using a boolean _positionSplit .
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript9 { public class SplitDemo : UserStrategyBase { TimeSeries _sma; bool _positionSplit; public override void Initialize(BarHistory bars) { _sma = SMA.Series(bars.Close, 50); PlotStopsAndLimits(2); } public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { if (bars.Close.CrossesOver(_sma, idx)) { Transaction t = PlaceTrade(bars, TransactionType.Buy, OrderType.Market); _positionSplit = false; } } else { Position p = LastOpenPosition; // stop loss ClosePosition(p, OrderType.Stop, p.EntryPrice * 0.9, "10% Loss"); // 20% profit target ClosePosition(p, OrderType.Limit, p.EntryPrice * 1.20, "20% Profit"); // Sell 60% of the shares after a 8% closing profit if (!_positionSplit) { if (p.ProfitPctAsOf(idx) > 8) { Transaction t = PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, "8% Profit"); t.Quantity = p.Quantity * 0.6; _positionSplit = true; } } } } } }
If I'm reading this right (And correct me if I'm wrong.), but isn't the time-of-life (ToL) of the _positionSplit Boolean for the duration of the backtest for this specific stock? Of course, the Buy code resets it to False each time a new position is bought, so it applies specifically to a given position, but once that position is split, this code will never allow it to be split a second time regardless of how high it goes.
I'm just wondering if that's a good thing or a bad thing? I suppose that depends on your trading method. One could change that (I would).
Thanks for this short and direct example.
I'm just wondering if that's a good thing or a bad thing? I suppose that depends on your trading method. One could change that (I would).
Thanks for this short and direct example.
The logic here is to split the original position once only after achieving an 8% MFE.
If you want more splits, then you need different logic.
If you want more splits, then you need different logic.
Your Response
Post
Edit Post
Login is required