I have a issue with strategy.
When the trade is placed (in this example, a Long trade), only the entry order appears initially, but the SL and TP orders do not. After the 5-minute bar closes, the SL and TP orders are finally placed. However, I cannot afford to be without a stop loss for those 5 minutes.
So what I did next was place the SL and TP orders together with entryOrder = EnterLong. They do appear immediately, but after the 5-minute bar closes, the SL and TP orders disappear, leaving only the entry order
What is wrong? How can I resolve it? Is it okay if I use 2 times the order SL and TP, one with entryorder (MarketPosition Flat) and other in MarketPosition.Long?
Note 1: The strategy must be used with Calculate.OnBarClose and for this example we are gonna use timeframe 5 minutes, Long trade
Note 2: When I take the SAME trade at Playback, it takes SL and TP order at the same time Entry order, even when I use Calculate.OnBarClose. Exacly same trade, same instrument, same time. But in "Real" SL and TP doesnt match with entry order
Thanks for help
This is the code, I will show you the code in a simple way
Variables
private double stopLoss;
private double entryPrice;
private double takeProfitPrice;
private Order stopOrder = null;
private Order entryOrder = null;
private Order takeOrder = null;
;
if (Position.MarketPosition == MarketPosition.Flat)
{
stopLoss = indicator.TriggerUp - (Value / tickValue) * TickSize;
}
if (Position.MarketPosition == MarketPosition.Flat && BullishTrigger)
{
entryOrder = EnterLong(Convert.ToInt32(1), "LongEntry");
}
if (Position.MarketPosition == MarketPosition.Long)
{
stopOrder = ExitLongStopMarket(0, true, entryOrder.Quantity, stopLoss, "StopLoss", "LongEntry");
takeOrder = ExitLongLimit(0, true, 1, takeProfitPrice, "TakeProfit", "LongEntry");
}
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
if (order.Name.Contains("Entry"))
{
entryOrder = order;
if (order.OrderState == OrderState.Cancelled)
{
entryOrder = null;
}
}
if (order.Name == "StopLoss")
{
stopOrder = order;
if (order.OrderState == OrderState.Cancelled)
{
stopOrder = null;
}
}
if (order.Name == "TakeProfit")
{
takeOrder = order;
if (order.OrderState == OrderState.Cancelled)
{
takeOrder = null;
}
if (order.OrderState == OrderState.Filled)
{
targetFilled = true;
}
}
}

Comment