While debugging in VS, I realized that once the trailingStop is set, every time a new position is opened, it closes out immediately with a trail stop exit. So only the first entry works as expected and every other order gets stopped out immediately (at the same price as the entry fill price). The following code shows how I enter the trailing stop orders and how I try to cancel them. (only showing select relevant code here)
if (quickScalpOrder != null && scalpTrailingStopActivated == false && (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) - calculatedRTSlippage >= 70.00)) { Print("Quick Scalp Trail Stop Set" + quickScalpOrder.ToString()); double stopLossVal = Instrument.MasterInstrument.Round2TickSize((Close[0] - Position.AvgPrice) / 2); SetTrailStop("quickScalp", CalculationMode.Ticks, stopLossVal, false); scalpTrailingStopActivated = true; } protected override void OnExecution(IExecution execution) { if (execution.Name == "Trail stop" && execution.Order.FromEntrySignal == "quickScalp") { Print("Quick Scalp Trailing Stop Hit **** " + execution.ToString()); Print("\r\n"); CancelOrder(execution.Order); quickScalpOrder = null; } else if (execution.Name == "Trail stop" && execution.Order.FromEntrySignal == "wideScalp") { Print("Wide Scalp Trailing Stop Hit **** " + execution.ToString()); Print("\r\n"); CancelOrder(execution.Order); wideScalpOrder = null; } if (quickScalpOrder == null && wideScalpOrder == null) { CancelAllOrders(); breakEvenStopActivated = false; scalpTrailingStopActivated = false; wideTrailingStopActivated = false; breakEvenStopLossVal = 0.0; } }
Comment