Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding traling stop

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

    Adding traling stop

    Could somebody instruct me how to add a variable trailing stop to this basic strategy?


    //
    // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Simple moving average cross over strategy.
    /// </summary>
    [Description("Simple moving average cross over strategy.")]
    public class SimpleMACrossOver : Strategy
    {
    #region Variables
    private int fast = 10;
    private int slow = 25;
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    SMA(Fast).Plots[0].Pen.Color = Color.Orange;
    SMA(Slow).Plots[0].Pen.Color = Color.Green;

    Add(SMA(Fast));
    Add(SMA(Slow));

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick).
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CrossAbove(SMA(Fast), SMA(Slow), 1))
    EnterLong();
    else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
    EnterShort();
    }

    #region Properties
    /// <summary>
    /// </summary>
    [Description("Period for fast MA")]
    [GridCategory("Parameters")]
    public int Fast
    {
    get { return fast; }
    set { fast = Math.Max(1, value); }
    }

    /// <summary>
    /// </summary>
    [Description("Period for slow MA")]
    [GridCategory("Parameters")]
    public int Slow
    {
    get { return slow; }
    set { slow = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Hi larrylwill,

    For ease of use there is method SetTrailStop(). You can use it with mode ticks or percent and it will trail the market by that value. You can add this to the scripts Initialize() method.


    If you're looking for more control, you can use SetStopLoss(). This can be used to dynamically set a stop loss value based on the conditions you define. You can also use it base stop losses from your own calculated value, with mode price.

    This sample can help with a dynamic set stop loss movement after a profit trigger:
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      thank you , that's what I was looking for.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      649 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      370 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      109 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      574 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      576 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X