Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with simple strategy

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

    Help with simple strategy

    So I am just beginning to learn how to play around with the ninjascript wizard.

    What I am attempting to do is code an entry system - Ideally what I would like is an entry system that when the price reaches 5 ticks below my indicator I go short, and then when it goes 5 ticks above my indicator I go long. To practice I just figured I would use a SMA as my indicator. Unfortunately when I click finish on the ninjascript it comes up with an error.

    anyways how do I upload my code to get it into one of those handy boxes so it doesnt take up an extraordinary amount of space?

    #2
    Hello thorpc,

    Welcome to the NinjaTrader Support Forums!

    You may following the instructions on the following post to see where the NinjaScript errors are coming from.


    As for the "handy boxes" taking up space I am not quite sure what you are referring to could you please clarify?

    Happy to be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      Ok I got the error fixed thanks alot! Unfortunately It is still not doing what I think it should be doing - what I mean by a handy little box is those boxes that people use to post code in so my post isnt 130 lines long

      Comment


        #4
        ok well here is the code

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
        if (CrossAbove(GetCurrentAsk() + Offset * TickSize, SMA(SMAlookback), 1))
        EnterLong(DefaultQuantity, "");
        else if (CrossBelow(GetCurrentBid() + Offsetneg * TickSize, SMA(SMAlookback), 1))
        EnterShort(DefaultQuantity, "");


        Basically what I would like to happen is lets say my sma is at 95.05 when the price gets to 95.00 I want it to short, if the price gets to 95.10 I want it to go long. so my Offset value is set at 5 and Offsetneg is -5. Unfortunately it is not doing this. Am I going down the right trackÉ

        Comment


          #5
          Hello thorpc,

          Glad to hear that it was resolved.

          You can use the Wrap Code button when you press the "Post Replay" to be able to generate this. This will bring up the [ CODE ] and [ /CODE ] (without spaces inbetween the brackets) to section off the text inbetween to have a scroll bar if too long.

          See attached screenshot below.
          Attached Files
          JCNinjaTrader Customer Service

          Comment


            #6
            well I debugged the thing so that their isnt any errors but it still is not accomplishing what I was hoping it would do - if you could take a look at the code to see what it is telling you that would be great

            Code:
            #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.Gui.Chart;
            using NinjaTrader.Strategy;
            #endregion
            
            // This namespace holds all strategies and is required. Do not change it.
            namespace NinjaTrader.Strategy
            {
                /// <summary>
                /// bam
                /// </summary>
                [Description("bam")]
                public class MA2 : Strategy
                {
                    #region Variables
                    // Wizard generated variables
                    private int sMAlookback = 50; // Default setting for SMAlookback
                    private int offset = 5; // Default setting for Offset
                    private int offsetneg = -5; // Default setting for Offsetneg
                    // User defined variables (add any user defined variables below)
                    #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()
                    {
                        Add(SMA(SMAlookback));
                        Add(SMA(SMAlookback));
            
                        CalculateOnBarClose = false;
                    }
            
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
                        if (CrossAbove(GetCurrentAsk() + Offset * TickSize, SMA(SMAlookback), 1))
                            EnterLong(DefaultQuantity, "");
                        else if (CrossBelow(GetCurrentBid() + Offsetneg * TickSize, SMA(SMAlookback), 1))
                            EnterShort(DefaultQuantity, "");
                    }
            
                    #region Properties
                    [Description("")]
                    [GridCategory("Parameters")]
                    public int SMAlookback
                    {
                        get { return sMAlookback; }
                        set { sMAlookback = Math.Max(1, value); }
                    }
            
                    [Description("")]
                    [GridCategory("Parameters")]
                    public int Offset
                    {
                        get { return offset; }
                        set { offset = Math.Max(1, value); }
                    }
            
                    [Description("")]
                    [GridCategory("Parameters")]
                    public int Offsetneg
                    {
                        get { return offsetneg; }
                        set { offsetneg = Math.Max(1, value); }
                    }
                    #endregion
                }
            }
            Ideally what I would like is this - lets say my SMA is at 95.05 and the price is well below it. When the price hits 95.00(5 ticks below sma) I would like to short it, if the price continues up to 95.10(5 ticks above) I would like it to reverse and go long. Or vice versa - if the price was well above 95.10 and going down, when it gets to 95.10(5 ticks above SMA) and then if the price continued to go down at 95.00 it would reverse my position and I would go short.

            hope that makes sense :P

            Comment


              #7
              Hello thorpc,

              You logic seems to be good, but I do see one item

              Code:
              [Description("")]
                      [GridCategory("Parameters")]
                      public int Offsetneg
                      {
                          get { return offsetneg; }
                          set { offsetneg = [COLOR="Red"]Math.Max(1, value)[/COLOR]; }
                      }
              When you code the following as a public variable instead of returning -5 it will return 1 as that is the Max (Math.Max) value, you may want to change that first number to the largest negative number that you would like it to be set to.

              If you get results that you do not expect you may want to use the Print() statement to verify what values you are getting.


              Happy to be of further assistance.
              JCNinjaTrader Customer Service

              Comment


                #8
                Thanks for the help,

                As of right now regardless of which direction the price is coming from it only sells short - IE: if SMA is at 95.05 and price is 95.3 as it falls at 95.1 it shorts, (ideally thats when I want it going long) and if the price is below, like lets say 94.8 as it comes up it doesnt short at 95.00 but shorts at 95.10

                here is a screenshot of my log

                and just so you can see what I did with the code here it is too - I didnt know the ideal place to put in the print command so I just stuck it in their.

                Code:
                #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.Gui.Chart;
                using NinjaTrader.Strategy;
                #endregion
                
                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Strategy
                {
                    /// <summary>
                    /// bam
                    /// </summary>
                    [Description("bam")]
                    public class MA2 : Strategy
                    {
                        #region Variables
                        // Wizard generated variables
                        private int sMAlookback = 50; // Default setting for SMAlookback
                        private int offset = 5; // Default setting for Offset
                        private int offsetneg = -5; // Default setting for Offsetneg
                        // User defined variables (add any user defined variables below)
                        #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()
                        {
                            Add(SMA(SMAlookback));
                            Add(SMA(SMAlookback));
                
                            CalculateOnBarClose = false;
                        }
                
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                            if (CrossAbove(GetCurrentAsk() + Offset * TickSize, SMA(SMAlookback), 1))
                                EnterLong(DefaultQuantity, "");
                            else if (CrossBelow(GetCurrentBid() + Offsetneg * TickSize, SMA(SMAlookback), 1))
                                EnterShort(DefaultQuantity, "");
                			if (Close[0] > Open[0])
                {
                     Print("Code has entered If statement. Close: "+ Close[0] + " Open: " + Open[0]);
                     // Do something
                }
                
                        }
                		
                		
                
                        #region Properties
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int SMAlookback
                        {
                            get { return sMAlookback; }
                            set { sMAlookback = Math.Max(50, value); }
                        }
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int Offset
                        {
                            get { return offset; }
                            set { offset = Math.Max(5, value); }
                        }
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int Offsetneg
                        {
                            get { return offsetneg; }
                            set { offsetneg = Math.Max(-5, value); }
                        }
                        #endregion
                    }
                }
                Attached Files

                Comment


                  #9
                  Hello thorpc,

                  You are using the CurrentBid and CurrentAsk for you conditions. You may want to setup your Print() statements as follows.

                  Code:
                  Print("LongCurrentAsk: "+GetCurrentAsk()+" Offset+Tick: "+(Offset * TickSize)+" SMA: "+SMA(SMAlookback)[0]);
                  Print("Calculated value: "+(GetCurrentAsk() + Offset * TickSize));
                  
                  Print("ShortCurrentBid: "+GetCurrentBid()+" Offsetneg+Tick: "+(Offsetneg * TickSize)+" SMA: "+SMA(SMAlookback)[0]);
                  Print("Calculated value: "+(GetCurrentBid() + Offsetneg * TickSize));
                  
                   if (CrossAbove(GetCurrentAsk() + Offset * TickSize, SMA(SMAlookback), 1))
                                  EnterLong(DefaultQuantity, "");
                   else if (CrossBelow(GetCurrentBid() + Offsetneg * TickSize, SMA(SMAlookback), 1))
                                  EnterShort(DefaultQuantity, "");
                  To see the values that you are calculating on. Note that you may have to open up the Output Window (under Control Center -> Tools) to view the Output text.

                  Also, you may want to change Calculate on bar close to true so you can read the Output Window easily.
                  JCNinjaTrader Customer Service

                  Comment


                    #10
                    The problem with calculating on bar close is that orders only would go into effect if the bar has gone through my indicator and closed (or is my knowledge faulty in how this works?), whereas I need orders to activate depending on where the current price is. Theoretically I could have a number of reverses in the same bar as it is being formed and I need to be able to account for that.

                    but you are right the output window is extremely hard to read - would it be better instead of bid/ask I simply use median for both long and short conditions?

                    Comment


                      #11
                      Hello thorpc,

                      Yes, your understanding of Calculate on bar close (COBC) is correct.

                      If you would like to keep COBC false (The only reason I suggested this is to see the values that you are getting), then you may want place the Print() statements inside of their respected "if" conditions to minimize the amount of time it is Printing to see the values.
                      JCNinjaTrader Customer Service

                      Comment


                        #12
                        Ok, what in the print statement am I looking for and / or do you need to see to understand what is happening?

                        Comment


                          #13
                          Hello thorpc,

                          You are checking the values that you to ensure that your condition is setup like how you want it.

                          For example on the CL the following Output.
                          ShortCurrentBid: 93.03 Offsetneg+Tick: -0.05 SMA: 92.9791999999999
                          Calculated value: 92.98

                          The current bid is 93.03 and with the offset value being -0.05 you get the Calculated value of 92.98 which does cross below the SMA which is currently 92.97.

                          Let me know if that makes sense.
                          JCNinjaTrader Customer Service

                          Comment


                            #14
                            OK so I changed it from false to true to better understand output window.

                            Code:
                            LongCurrentAsk: 92.01 Offset+Tick: 0.05 SMA: 92.1494
                            Calculated value: 92.06
                            ShortCurrentBid: 91.99 Offsetneg+Tick: -0.05 SMA: 92.1494
                            Calculated value: 91.94
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            System.ArgumentNullException: Value cannot be null.
                            Parameter name: brush
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
                               at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
                               at NinjaTrader.Indicator.jtRangeMarker7.Plot(Graphics graphics, Rectangle bounds, Double min, Double max)
                            LongCurrentAsk: 92.31 Offset+Tick: 0.05 SMA: 92.1364
                            Calculated value: 92.36
                            ShortCurrentBid: 92.3 Offsetneg+Tick: -0.05 SMA: 92.1364
                            Calculated value: 92.25
                            System.ArgumentNullException: Value cannot be null.

                            This is the output code - I started copying at the end of one bar where it did this

                            LongCurrentAsk: 92.01 Offset+Tick: 0.05 SMA: 92.1494
                            Calculated value: 92.06
                            ShortCurrentBid: 91.99 Offsetneg+Tick: -0.05 SMA: 92.1494
                            Calculated value: 91.94

                            at the end of the next bar it printed this, although I had to delete abunch of what looked like that error message that was repeating over and over again because too many characters

                            LongCurrentAsk: 92.31 Offset+Tick: 0.05 SMA: 92.1364
                            Calculated value: 92.36
                            ShortCurrentBid: 92.3 Offsetneg+Tick: -0.05 SMA: 92.1364
                            Calculated value: 92.25

                            as you can see the ask/bid had moved through the SMA - unfortunately no orders were placed at all.


                            Here is the full code that I am working with so you can see where I plugged in the print statements like how you said

                            Code:
                            #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.Gui.Chart;
                            using NinjaTrader.Strategy;
                            #endregion
                            
                            // This namespace holds all strategies and is required. Do not change it.
                            namespace NinjaTrader.Strategy
                            {
                                /// <summary>
                                /// bam
                                /// </summary>
                                [Description("bam")]
                                public class MA2 : Strategy
                                {
                                    #region Variables
                                    // Wizard generated variables
                                    private int sMAlookback = 50; // Default setting for SMAlookback
                                    private int offset = 5; // Default setting for Offset
                                    private int offsetneg = -5; // Default setting for Offsetneg
                                    // User defined variables (add any user defined variables below)
                                    #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()
                                    {
                                        Add(SMA(SMAlookback));
                                        Add(SMA(SMAlookback));
                            
                                        CalculateOnBarClose = true;
                                    }
                            
                                    /// <summary>
                                    /// Called on each bar update event (incoming tick)
                                    /// </summary>
                                    protected override void OnBarUpdate()
                                    {
                            			Print("LongCurrentAsk: "+GetCurrentAsk()+" Offset+Tick: "+(Offset * TickSize)+" SMA: "+SMA(SMAlookback)[0]);
                            			Print("Calculated value: "+(GetCurrentAsk() + Offset * TickSize));
                            			
                            			Print("ShortCurrentBid: "+GetCurrentBid()+" Offsetneg+Tick: "+(Offsetneg * TickSize)+" SMA: "+SMA(SMAlookback)[0]);
                            			Print("Calculated value: "+(GetCurrentBid() + Offsetneg * TickSize));
                            			
                                        if (CrossAbove(GetCurrentAsk() + Offset * TickSize, SMA(SMAlookback), 1))
                                            EnterLong(DefaultQuantity, "");
                                        else if (CrossBelow(GetCurrentBid() + Offsetneg * TickSize, SMA(SMAlookback), 1))
                                            EnterShort(DefaultQuantity, "");
                                    }
                            		
                            		
                            
                                    #region Properties
                                    [Description("")]
                                    [GridCategory("Parameters")]
                                    public int SMAlookback
                                    {
                                        get { return sMAlookback; }
                                        set { sMAlookback = Math.Max(50, value); }
                                    }
                            
                                    [Description("")]
                                    [GridCategory("Parameters")]
                                    public int Offset
                                    {
                                        get { return offset; }
                                        set { offset = Math.Max(5, value); }
                                    }
                            
                                    [Description("")]
                                    [GridCategory("Parameters")]
                                    public int Offsetneg
                                    {
                                        get { return offsetneg; }
                                        set { offsetneg = Math.Max(-5, value); }
                                    }
                                    #endregion
                                }
                            }

                            Comment


                              #15
                              Hello thorpc,

                              Judging by your Output value it looks like your Condition was hit. You can verify this by Placing a Print() statement inside of your conditions so that you can tell that NinjaTrader is running that part of the code.

                              You may want to make sure that the order is not getting ignored if you are already in a trade using the NinjaTrader Managed Approach. This can be done by placing "TraceOrders = true;" inside of the Initialize() method so that NinjaTrader will Output Order information easily.


                              With this you will see when you condition is hit and how NinjaTrader is going to process the order.

                              Happy to be of further assistance.
                              JCNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              648 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              369 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
                              573 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              575 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X