I've been working a problem that has plagued me for some time. I get the following error many times while testing my strategy in Playback/Market Replay.
It doesn't happen for all trades but for the ones it does happen to it's very consistent. I've added many Print() statements to try and find where the problem is. This is what I've come up.
Here is a snippet of the relevant code:
In OnBarUpdate():
EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
Print("\nBefore SetStopLoss, Close[0]("+Close[0]+") + stopLoss("+stopLoss+") = " + (Close[0]+stopLoss));
SetStopLoss("ShortEntry", CalculationMode.Price, Close[0]+stopLoss, false);
Print("\nAfter SetStopLoss, Close[0] + stopLoss =" + (Close[0] + stopLoss));
SetProfitTarget("ShortEntry", CalculationMode.Price, Close[0] - takeProfit);
Print("\nOpen = "+Open[0]+", High = "+High[0]+", Low = "+Low[0]+", Close[0] = "+Close[0]);
if (order.OrderState == OrderState.Filled) Print($"\n{Time[0]}: Order Filled: {order.Name} for {order.Quantity} units at {order.AverageFillPrice}\n");
if (order.OrderState == OrderState.Submitted) Print($"\n{Time[0]}: Order Submitted: {order.Name} for {order.Quantity} units, at {order.LimitPrice}(l)/{order.StopPrice}(s)");
4/26/2024 9:35:30 AM: Order Filled: ShortEntry for 1 units at 17721.75
4/26/2024 9:35:30 AM: Order Submitted: Stop loss for 1 units, at 0(l)/17721.25(s)
Strategy 'AS/282061273' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
Disabling NinjaScript strategy 'AS/282061273'
Before SetStopLoss, Close[0](17723.75) + stopLoss(11.9473272927223) = 17735.6973272927
After SetStopLoss, Close[0] + stopLoss =17735.6973272927
Open = 17734, High = 17736.25, Low = 17723, Close[0] = 17723.75
If I change the SetStopLoss and manually write the 'value/price' 17735.75, the error does not occur, as I would expect.
SetStopLoss("ShortEntry", CalculationMode.Price, 17735.75, false);
4/26/2024 9:35:30 AM: Order Filled: ShortEntry for 1 units at 17721.75
4/26/2024 9:35:30 AM: Order Submitted: Stop loss for 1 units, at 0(l)/17735.75(s)
4/26/2024 9:35:30 AM: Order Submitted: Profit target for 1 units, at 17693.25(l)/0(s)
Before SetStopLoss, Close[0](17723.75) + stopLoss(11.9473272927223) = 17735.6973272927
After SetStopLoss, Close[0] + stopLoss =17735.6973272927
Open = 17734, High = 17736.25, Low = 17723, Close[0] = 17723.75
Issue with stop loss
Error..orders can't be placed below the market
Reading through them has helped me understand things better and given me some best practices (such as move the EnterShort() below the SetStopLoss() and SetProfitTarget()), but I don't think I see anything that addresses the above behavior specifically.
Thank you in advance for your help,
Michael

Comment