Cone8
 ( 22.94% )
- ago
@Glitch - run on TQQQ. Only the first position is sold on the same bar, but both should be.

CODE:
using System; using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Data; using WealthLab.PowerPack; namespace WealthScript5 { public class SameBarExit : UserStrategyBase {       // **** EXAMPLE FOR TQQQ ONLY **** public override void Initialize(BarHistory bars) {          PlotStopsAndLimits(3);          StartIndex = 1;          _nso = new(bars);          PlotStopsAndLimits(3); } public override void Execute(BarHistory bars, int idx)       {     if (!HasOpenPosition(bars, PositionType.Long)) {             if (bars.DateTimes[idx] == _dte)             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0);                               PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, _nso[idx] * 0.98, 1);                            } } else {                Position p = FindOpenPosition(0);             if (p != null)                if (idx - p.EntryBar > 10)                   _t = PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose, 0, "MOC - 0");             p = FindOpenPosition(1);             if (p != null)                if (idx - p.EntryBar > 10)                   _t = PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose, 0, "MOC - 1");                   } } public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) {          t.AutoProfitTargetPrice = executionPrice * 1.20; } Transaction _t;       DateTime _dte = new DateTime(2025, 4, 4);       NextSessionOpen _nso; } }
0
41
Solved
3 Replies

Reply

Bookmark

Sort
Glitch8
 ( 10.37% )
- ago
#1
Thanks, I logged the issue.
0
Glitch8
 ( 10.37% )
- ago
#2
I'm seeing the second entry being placed at a limit order that fills at 36.015. TQQQ opened that day at 36.75, so above that limit price. So based on daily data only we can't be sure that the market didn't open, shoot up to the day's high, come back down to fill the lower limit, then go back up again. This is why the backtester doesn't fill that same day exit.
1
Best Answer
Cone8
 ( 22.94% )
- ago
#3
Makes sense, thanks.
Just thinking - out of the box and scope - but it'd be cool if granular processing could solve those intra-bar limit and stop order ambiguities.
0

Reply

Bookmark

Sort