I think this could be a really simple question im just missing something here.
Im trying to set the Profit Targets and Stop Loss for Long or Short position within OnExecutionUpdate. However the below code works only for Long position and does not activate the stoploss or profit target for the short positions in the backtest they are are not recognised at all unlike the long position stops.
I have followed the various samples from the help guide and sample strategy codes from "sampleOnOrderUpdate" but i noticed that all the samples and examples only cover long positions and i cant find any examples where it combines both long and short anywhere. So i have given it a go below: What am i doing wrong?
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) { if (entryOrder != null && entryOrder == execution.Order && entryOrder.IsLong) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) { if (EnableProfitTarget) { targetOrder = ExitLongLimit(1, true, execution.Order.Filled, execution.Order.AverageFillPrice + (InitialProfitTarget * TickSize), "Initial TP", "Long Entry"); } if (EnableStopLoss) { stopOrder = ExitLongStopMarket(1, true, execution.Order.Filled, execution.Order.AverageFillPrice - (InitialStopLoss * TickSize), "Initial SL", "Long Entry"); } } if (execution.Order.OrderState != OrderState.PartFilled) entryOrder = null; } if (entryOrder != null && entryOrder == execution.Order && entryOrder.IsShort) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) { if (EnableProfitTarget) { targetOrder = ExitShortLimit(1, true, execution.Order.Filled, execution.Order.AverageFillPrice - (InitialProfitTarget * TickSize), "Initial TP", "[COLOR=#FF0000][B]Long Entry[/B][/COLOR]"); } if (EnableStopLoss) { stopOrder = ExitShortStopMarket(1, true, execution.Order.Filled, execution.Order.AverageFillPrice + (InitialStopLoss * TickSize), "Initial SL", "[COLOR=#FF0000][B]Long Entry[/B][/COLOR]"); } } if (execution.Order.OrderState != OrderState.PartFilled) entryOrder = null; } if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order)) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled) { stopOrder = null; targetOrder = null; } } }
Thankyou
Comment