4/14/2025 10:43:00 AM,Default,Strategy 'RKV8StopOrder/340960992' submitted an order that generated the following error 'Order rejected'. Strategy has sent cancel requests attempted to close the position and terminated itself.,
4/14/2025 10:43:00 AM,Order,xxx Buy order stop price must be above last trade price[Order parameters are invalid] affected Order: BuyToCover 2 StopMarket @ 18868.5,
4/14/2025 10:43:00 AM,Order,Order='8734009144/xxxxx' Name='StopShort' New state='Rejected' Instrument='MNQ JUN25' Action='Buy to cover' Limit price=0 Stop price=18868.5 Quantity=2 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='Order rejected' Native error='Buy order stop price must be above last trade price[Order parameters are invalid]',
Here is my code.
// Manage position and stops
if (Position.MarketPosition == MarketPosition.Flat)
{
// Reset counters when flat
barsSinceEntry = 0;
isFirstBarAfterEntry = false;
// Cancel all stop orders when flat
ExitLongStopMarket(0, true, 0, 0, "StopLong", "LongEntry");
ExitShortStopMarket(0, true, 0, 0, "StopShort", "ShortEntry");
}
else
{
// Increment bars since entry
barsSinceEntry++;
// Check if we need to transition from initial stop to trailing stop
if (barsSinceEntry > 1 && isFirstBarAfterEntry)
{
isFirstBarAfterEntry = false;
if (Position.MarketPosition == MarketPosition.Long)
{
// Replace initial stop with trailing stop
double stopPrice = RK1.TrailingStop[0] - 20;
ExitLongStopMarket(0, true, Position.Quantity, stopPrice, "StopLong", "LongEntry");
}
else if (Position.MarketPosition == MarketPosition.Short)
{
// Replace initial stop with trailing stop
double stopPrice = RK1.TrailingStop[0] + 20;
ExitShortStopMarket(0, true, Position.Quantity, stopPrice, "StopShort", "ShortEntry");
}
}
// On subsequent bars, continue updating the trailing stop
else if (barsSinceEntry > 1 && !isFirstBarAfterEntry)
{
if (Position.MarketPosition == MarketPosition.Long)
{
double stopPrice = RK1.TrailingStop[0] - 20;
ExitLongStopMarket(0, true, Position.Quantity, stopPrice, "StopLong", "LongEntry");
}
else if (Position.MarketPosition == MarketPosition.Short)
{
double stopPrice = RK1.TrailingStop[0] + 20;
ExitShortStopMarket(0, true, Position.Quantity, stopPrice, "StopShort", "ShortEntry");
}
}
}
}
// Handle order execution events to set the initial stop
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
// Call the base method first
base.OnExecutionUpdate(execution, executionId, price, quantity, marketPosition, orderId, time);
// Set initial stop based on entry price
if (marketPosition == MarketPosition.Long && execution.Name == "LongEntry")
{
double initialStopPrice = price - 65;
ExitLongStopMarket(0, true, Position.Quantity, initialStopPrice, "StopLong", "LongEntry");
// Reset the counter and set the flag to transition to trailing stop on next bar
barsSinceEntry = 0;
isFirstBarAfterEntry = true;
}
else if (marketPosition == MarketPosition.Short && execution.Name == "ShortEntry")
{
double initialStopPrice = price + 65;
ExitShortStopMarket(0, true, Position.Quantity, initialStopPrice, "StopShort", "ShortEntry");
// Reset the counter and set the flag to transition to trailing stop on next bar
barsSinceEntry = 0;
isFirstBarAfterEntry = true;
}
}

Comment