I have an issue to declare
SetStopLoss(CalculationMode.Ticks, stopLossTicks)
The problem is that the stopLossTicks is not a fixed but obtained from the ATR(4)[0]. Every bar the value of stopLossTicks change.
I've put the logic in OnBarUpdate() to get a dynamic StopLoss Modification to BreakEven and then TrailingStop based on the attached file.
Do you have any idea how can i give the stopLossTicks variable into
State.Configure?
protected override void OnStateChange() { if (State == State.SetDefaults) { Description = ........ AddPlot(new Stroke(Brushes.Lime, 2), PlotStyle.Hash, "ProfitTarget"); AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Line, "StopLoss"); } else if (State == State.Configure) { SetStopLoss(CalculationMode.Ticks, stopLossTicks); } } protected override void OnBarUpdate() { int ATR_VAL = (int)(ATR(4)[0]/ TickSize); int stopLossTicks = (int)(ATR_VAL * 2.6); if ( Position.MarketPosition == MarketPosition.Flat && Close[1] < Close[0]) { EnterLongLimit(1, Close[0], "LongEntry"); } switch (Position.MarketPosition) { case MarketPosition.Flat: SetStopLoss(CalculationMode.Ticks, stopLossTicks); previousPrice = 0; stopPlot = 0; break; case MarketPosition.Long: if (previousPrice == 0) { stopPlot = Position.AveragePrice - stopLossTicks * TickSize; // initial stop plot level } if (Close[0] > Position.AveragePrice + 5 * TickSize && previousPrice == 0) { initialBreakEven = Position.AveragePrice + 3 * TickSize; SetStopLoss(CalculationMode.Price, initialBreakEven); previousPrice = Position.AveragePrice; stopPlot = initialBreakEven; } else if (previousPrice != 0 && GetCurrentAsk() > previousPrice + 7 * TickSize ) { newPrice = previousPrice + 2 * TickSize; / SetStopLoss(CalculationMode.Price, newPrice); previousPrice = newPrice; stopPlot = newPrice; } StopLoss[0] = stopPlot; break; default: break; } }
Comment