Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with SMA Crossover Strategy with Trailing ATR Stops

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

    Help with SMA Crossover Strategy with Trailing ATR Stops

    Hi Folks, good morning. I have developed what I think is a SMA Crossover Strategy with trailing ATR stops, (see code below). My issue is the trailing ATR stops do not seem to work. When I back-test my strategy, the strategy only exits at end of day (EOD). I never see the strategy exit using the ATR trailing stops on the chart no matter what parameter I have for ATR multiplier or ATR period.

    Any help would be greatly appreciated.




    //This namespace holds Strategies in this folder and is required. Do not change it.

    namespace NinjaTrader.NinjaScript.Strategies

    {

    public class SampleMACrossoverWithATRstops : Strategy

    {

    private SMA smaFast;

    private SMA smaSlow;

    private ATR atr;

    private double atrStopValueLong;

    private double atrStopValueShort;


    protected override void OnStateChange()

    {

    if (State == State.SetDefaults)

    {

    Description = "MA Crossover with ATR Trailing Stops";

    Name = "SampleMACrossoverWithATRstops";

    Fast = 9;

    Slow = 21;

    AtrPeriod = 14;

    AtrMultiplier = 2;

    IsInstantiatedOnEachOptimizationIteration = false;



    // Adding session close exit properties

    IsExitOnSessionCloseStrategy = true;

    ExitOnSessionCloseSeconds = 30; // Exits 30 seconds before session close

    }

    else if (State == State.DataLoaded)

    {

    smaFast = SMA(Fast);

    smaSlow = SMA(Slow);

    atr = ATR(AtrPeriod);



    smaFast.Plots[0].Brush = Brushes.Snow;

    smaSlow.Plots[0].Brush = Brushes.Yellow;


    AddChartIndicator(smaFast);

    AddChartIndicator(smaSlow);

    }

    }


    protected override void OnBarUpdate()

    {

    if (CurrentBars[0] < Slow) return;


    double atrValue = atr[0] * AtrMultiplier;


    if (CrossAbove(smaFast, smaSlow, 1))

    {

    EnterLong();

    atrStopValueLong = Close[0] - atrValue;

    Draw.Line(this, "AtrTrailLong" + CurrentBar, CurrentBar, atrStopValueLong, CurrentBar - 1, atrStopValueLong, Brushes.Green);

    Draw.ArrowUp(this, "EnterLongArrow" + CurrentBar, false, 0, Low[0] - 2 * TickSize, Brushes.Green);

    }

    else if (CrossBelow(smaFast, smaSlow, 1))

    {

    EnterShort();

    atrStopValueShort = Close[0] + atrValue;

    Draw.Line(this, "AtrTrailShort" + CurrentBar, CurrentBar, atrStopValueShort, CurrentBar - 1, atrStopValueShort, Brushes.Red);

    Draw.ArrowDown(this, "EnterShortArrow" + CurrentBar, false, 0, High[0] + 2 * TickSize, Brushes.Red);

    }


    // Update the trailing stop for LONG positions

    if (Position.MarketPosition == MarketPosition.Long)

    {

    if (Close[0] - atrValue > atrStopValueLong)

    {

    atrStopValueLong = Close[0] - atrValue;

    ExitLongStopMarket(atrStopValueLong, "ATR Trailing Stop Long");

    Draw.ArrowDown(this, "ExitLongArrow" + CurrentBar, false, 0, High[0] + TickSize, Brushes.Red);

    }

    }


    // Update the trailing stop for SHORT positions

    if (Position.MarketPosition == MarketPosition.Short)

    {

    if (Close[0] + atrValue < atrStopValueShort)

    {

    atrStopValueShort = Close[0] + atrValue;

    ExitShortStopMarket(atrStopValueShort, "ATR Trailing Stop Short");

    Draw.ArrowUp(this, "ExitShortArrow" + CurrentBar, false, 0, Low[0] - TickSize, Brushes.Green);

    }

    }

    }

    #2
    Hello joedtrades1982,

    Thank you for your post.

    Please take a look at this example script which may be helpful to you, which demonstrates placing a trailing entry order 2 ticks above the ATRTrailing indicator's Upper plot.

    Hello, the indicator I use plots the ATR with a period of 10 and multiplier of 4 as a dotted line on the chart (image attached). Using this, if the price reaches within 2 ticks to this level, it gives a signal for a breakout. My question is, how would I be able to create a strategy that trails an entry 2 ticks above this level



    Are you seeing any errors in the log tab of the Control Center? If so, what is the full error message?

    In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.

    Below is a link to a forum post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

    https://ninjatrader.com/support/foru...121#post791121

    Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders. Include labels for all values and comparison operators.

    Let me know if you need any assistance creating a print or enabling TraceOrders.

    Save the output from the output window to a text file and provide this with your reply.

    I'll be happy to assist with analyzing the output.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    50 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X