Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATR based Stop Loss

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    ATR based Stop Loss

    I am trying to have a ATR based stop loss. For example, if I entered a long EURUSD position at 1.5600 at close of bar, and ATR(14) at close of bar is 0.0020, my static stop loss will be 1.5600 - 0.0020 = 1.5580.

    Under Initialize(), I included the following:

    SetStopLoss("", CalculationMode.Ticks, ATR(14)[0], false);

    However, when I tried to run the strategy, the log says:

    "Failed to call method "Initialize for strategy 'SimpleMACross': ATR[barsAgo]: barsAgo out of valid range 0 through -2, was 0.


    I would appreciate any advice.

    #2
    a) please make sure there is historical data available for the instrument you backtest by e.g. pulling up a chart while connected.
    b) try moving your SetStopLoss statement to the OnBarUpdate method

    Comment


      #3
      Hi Dierk,

      Thanks. Your statement (b) works.

      Here's my sample for readers facing the same problem:

      if (CrossAbove(EMA(14), EMA(24), 1))
      {
      EnterLong(1, "");
      SetStopLoss("", CalculationMode.Price, Close[0]-ATR(14)[0], false);
      }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      65 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      35 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      59 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      62 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      51 views
      0 likes
      Last Post CarlTrading  
      Working...
      X