When I look at the historical backtest data everything is fine. When we get to realtime markets I have to keep watching it due to it not placing my stop for some reason....
Please help me out, because it is driving me bonkers... Even with over a decade of c# and trading experience...
Image 1: It placed the short limit, it entered and ones that happens it should place my ExitShortStopMarket... But it didn't do it.... I had to manually close once again because on realtime data it does not do it.
Image 2: once the order happened and the realtime bars became historical it "magically" placed everything perfect....
So how is my code executed?
onbarupdate() checks entry conditions and places short / long limits. Examples below:
string entryId = Time[0].ToString("dddd:HH:mm") + "-" + tradeManager.CurrentSessionFirstPresentation.Size;
tradeManager.EntryOrder = EnterShortLimit(0, true, contractsToUseInShort, entry, entryId);
tradeManager.placedOrders.Add(tradeManager.EntryOr der);
tradeManager.StoplossOrder = ExitShortStopMarket(0, true, contractsToUseInShort, tradeManager.CurrentStoploss, "Stoploss", "");
tradeManager.placedOrders.Add(tradeManager.Stoplos sOrder);
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
int quantity, int filled, double averageFillPrice,
Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
{
if (error != ErrorCode.NoError)
{
Print($"{Time[0]}: Order '{order.Name}' error: {error}");
}
else if (tradeManager.EntryOrder != null)
{
if (order.OrderState == OrderState.Rejected)
{
if (Position.MarketPosition == MarketPosition.Long)
{
ExitLong(int.MaxValue);
}else if(Position.MarketPosition == MarketPosition.Short)
{
ExitShort(int.MaxValue);
}
else
{
ExitAllOrders();
}
} else if (orderState == OrderState.Filled && order.Name == tradeManager.EntryOrder.Name)
{
tradeManager.HaveBeenInATrade = true;
if (Position.MarketPosition == MarketPosition.Long)
{
if (GetCurrentBid() < tradeManager.CurrentStoploss)
{
ExitAllOrders();
}
else
{
CalculateNewTpAndSl(true, false, false);
}
}
else if (Position.MarketPosition == MarketPosition.Short)
{
if (GetCurrentAsk() > tradeManager.CurrentStoploss)
{
ExitAllOrders();
}
else
{
CalculateNewTpAndSl(false, false, false);
}
}
}
}
}
CalculateNewTpAndSl() makes sure the TP and SL are placed. Aka this does not happen on real time data...
private void CalculateNewTpAndSl(bool isLong, bool firstTpHit, bool breakevenPlaced)
{
//removed the most logic of the code but in short long limit orders are placed and the stoploss is being placed with the method below. For shorts the other exitshortstopmarket() is used
tradeManager.StoplossOrder = ExitLongStopMarket(0, true, int.MaxValue, tradeManager.CurrentStoploss, "Stoploss", "");
tradeManager.placedOrders.Add(tradeManager.StoplossOrder);
}

Comment