Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help make stops a varable

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

    Help make stops a varable

    I got this strategy working correctly but I want to make the profit and trailing stop a variable like the sma, so when I load the strategy I can modify the stops. Now I have to do it in the program. I would like to make the 20 and 15 ticks a variable. Can someone show me how?

    thanks
    //
    // 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 with Profit and Traling Stop.")]
    public class MACOWPATS : 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));


    // Submits a profit target order 20 ticks away from the avg entry price
    SetProfitTarget(CalculationMode.Ticks, 20);

    // Sets a 15 tick trailing stop for an open position
    SetTrailStop(CalculationMode.Ticks, 15);
    CalculateOnBarClose = false;
    }

    /// <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
    larrylwill, the easiest way to do this would be to re-create the strategy in the Strategy Wizard and then specify your profit target and stop loss values as user inputs, and then use these user inputs/variables in the stop loss/profit target section of the Strategy Wizard. Once you have the Wizard generate your strategy, you can look at the code and see exactly how it is done. Please let me know if you have any other questions.
    AustinNinjaTrader Customer Service

    Comment


      #3
      yes you are right, I figured it out, thank you

      Comment


        #4
        I had made a simple strategy that would buy and sell and had a profit stop and trailing stop. It worked ok, then I wanted to add the profit and trailing stop as a variable that I could set when I loaded the strategy like the moving averages.

        I was advised to use the strategy wizard to learn how. I did that and got a working variable for the profit and trailing stop. I then attempted to combine the 2 and this is what I ended up with. It compiles without errors but when I run it, it sets the trailing stop at the buy or sell price.
        I tried to generate the complete program with the wizard but I never could get it to work, it would always crash.
        Would someone look at it and tell me what I did wrong?



        //
        // 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 COWS : Strategy
        {
        #region Variables
        private int fast = 10;
        private int slow = 25;
        private int proftStop = 20; // Default setting for ProftStop
        private int trailStop = 10; // Default setting for TrailStop
        #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.Green;
        SMA(Slow).Plots[0].Pen.Color = Color.Red;

        Add(SMA(Fast));
        Add(SMA(Slow));
        SetProfitTarget("", CalculationMode.Ticks, 0);
        SetTrailStop("", CalculationMode.Ticks, 0, false);

        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); }
        }
        [Description("")]
        [GridCategory("Parameters")]
        public int ProftStop
        {
        get { return proftStop; }
        set { proftStop = Math.Max(1, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int TrailStop
        {
        get { return trailStop; }
        set { trailStop = Math.Max(1, value); }
        }
        #endregion
        }
        }

        Comment


          #5
          Never Mind I found the problem.

          SetProfitTarget("", CalculationMode.Ticks, 0);
          SetTrailStop("", CalculationMode.Ticks, 0, false);

          Needs to be:

          SetProfitTarget("", CalculationMode.Ticks, proftStop);
          SetTrailStop("", CalculationMode.Ticks, trailStop, false);

          Comment


            #6
            larrylwill, glad to hear you found the solution.
            AustinNinjaTrader Customer Service

            Comment


              #7
              Now I want to find a way to move up the profit stop based on the how apart the MA's are.

              Comment


                #8
                Hi again, please take a look at the reference sample that demonstrates how to adjust profit target and stop loss orders to get started - http://www.ninjatrader-support.com/vb/showthread.php?t=3222.
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  that's what I used for the profit and trailing stop, I just stopped reading when I got it to work.
                  thanks again

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  647 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  368 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  108 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  571 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  573 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X