Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Creating an indicator

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

    Creating an indicator

    Hello NT,

    Could someone help me with creating an indicator? I am not that experienced and it seems a little confusing for me.

    I am trying to create the following:

    1. MovingAvg(High, 3) - (Avg True Range(3)*1)

    2. MovingAvg (Low, 3) + (Avg True Range (3)*1)

    #2
    Hello,

    Thank you for your post.

    There are Moving Average and ATR methods you can easily reference in your code:

    Moving Average - Simple (SMA)
    Average True Range (ATR)

    If you would like this plotted in the chart, you'd need to first set up the Plots in your Initialize() method. This method is called only once when adding your indicator to the chart.

    Code:
        protected override void Initialize()
                    {
                        Add(new Plot(Color.Green, "Upper"));
                        Add(new Plot(Color.Red,     "Lower"));
                        
                        Overlay = true;
                      }
    We are also Initializing Overlay = true; to ensure these plots are drawn on the price panel.

    Next, we can work in OnBarUpdate() which is called on each incoming tick and used to recalculate these values.

    At this step, we will first need to store your calculations as a double value which can then be set in the plot. We are using the SMA() and ATR() methods as per your calculations requirements.

    Code:
    protected override void OnBarUpdate()
                    {
                        
                        double ma1 = (SMA(High, 3)[0] - ATR(3)[0] * 1);
                        double ma2 = (SMA(Low, 3)[0] + ATR(3)[0] * 1);
    Now that we have stored these calculations, we can set them in your plots:

    Code:
    Upper.Set(ma1);
    Lower.Set(ma2);
    }
    Finally, we can setup the Plots as a public DataSeries that will allow you to change the properties of the plot from the UI

    Code:
            #region Properties
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Upper
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Lower
            {
                get { return Values[1]; }
            }
    
            #endregion
    Hopefully this will help produce what you're looking for.

    I would also like to add that the indicator you are creating is similar to our KeltnerChannel indicator. I'd suggest taking a look at the code in Tools--> Edit NinjaScript--> Indicator--> KeltnerChannel

    I'm also providing some general educational material to help you get started with working with indicators.

    You will find language references to all of the methods and functions you will be using. You will also see a tutorial section which will help you create your first indicator and get you started with some of these concepts.

    A link to our Help Guide can be found below:
    http://www.ninjatrader.com/support/helpGuides/nt7/index.html?distribution.htm

    I am also linking you to the Educational Resources section of the Help Guide to help you get started with NinjaScript:
    http://www.ninjatrader.com/support/helpGuides/nt7/index.html?educational_resources.htm

    We also have some Reference Samples online as well as some Tips and Tricks for both indicators and strategies:

    These samples can be downloaded, installed and modified from NinjaTrader.

    Please let me know if there is anything else I can do for you.
    Last edited by NinjaTrader_Matthew; 05-18-2012, 01:11 PM.
    MatthewNinjaTrader Product Management

    Comment


      #3
      I did the 1st two steps and saved. Everything is fine thus far.

      1. Where do input step 3 below? Do I add that where the color of the plots in step 1 that you told me?

      2. The 4th step does that goes in the area of line 48 thru 63 (Properties).

      3. Are there any videos to watch for future purposes?

      Comment


        #4
        Hello,

        Step 3 would go directly under step 2. All of this information is put in OnBarUpdate() between the opening ({) and closing braces (})

        Code:
        protected override void OnBarUpdate()                 
        {                                          
        
            double ma1 = (SMA(High, 3)[0] - ATR(3)[0] * 1);                    
            double ma2 = (SMA(Low, 3)[0] + ATR(3)[0] * 1);
        
             Ma1.Set(ma1); 
             Ma2.Set(ma2); 
        }
        The color for the Plots go above OnBarUpdate() in the Initialize() method


        Yes, the last step goes into in the Properties region.

        We have a few videos for you to watch

        Learning NinjaScript:

        Download NinjaTrader FREE at http://www.ninjatrader.comThis video is a NinjaScript tutorial on creating a strategy based on overbought/oversold conditions of...


        Automated Strategy Development Webinar:

        Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Mr. Matthew,

          I did everything you told me to do and saved it. I am getting an error message (Error on generating Indicator). Below that it has the following

          CS0201 line 45, col 13
          CS1061 line 45, col 19
          CS0103 line 45, col 38
          CS0103 line 48, col 6
          CS0103 line 49, col 6

          Comment


            #6
            Can you copy and paste the entire code you tried using?
            MatthewNinjaTrader Product Management

            Comment


              #7



              protectedoverridevoid Initialize()
              {
              Add(
              new Plot(Color.Green, "Upper"));
              Add(
              new Plot(Color.Red, "Lower"));

              Overlay =
              true;
              }
              ///<summary>
              /// Called on each bar update event (incoming tick)
              ///</summary>
              protectedoverridevoid OnBarUpdate()
              {
              double ma1 = (SMA(High, 3)[0] - ATR(3)[0] * 1);
              double ma2 = (SMA(Low, 3)[0] + ATR(3)[0] * 1);
              Ma1.Set(ma1);
              Ma2.Set(ma2);
              }

              Comment


                #8
                Sorry, there was a typo in the code I typed before. Rather than using Ma1.Set(ma1), please use Upper.Set(ma1);

                Code:
                    protected override void Initialize()
                            {
                            Add(new Plot(Color.Green, "Upper"));
                            Add(new Plot(Color.Red, "Lower"));
                
                            Overlay = true;
                            }
                            ///<summary>
                            /// Called on each bar update event (incoming tick)
                            ///</summary>
                            protected override void OnBarUpdate()
                            {
                            double ma1 = (SMA(High, 3)[0] - ATR(3)[0] * 1);
                            double ma2 = (SMA(Low, 3)[0] + ATR(3)[0] * 1);
                            Upper.Set(ma1);
                            Lower.Set(ma2);
                            }
                
                        #region Properties
                        [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                        [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                        public DataSeries Upper
                        {
                            get { return Values[0]; }
                        }
                
                        [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                        [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                        public DataSeries Lower
                        {
                            get { return Values[1]; }
                        }
                
                        #endregion
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  Hey Matt,

                  I did exactly what you told me and I have an error message for line 45, col 13,19, & 38.

                  The codes are cs0201, cs1061, & cs0103

                  Sorry I am not good at this but I do appreciate your help.

                  Comment


                    #10
                    Matt,

                    Line 45 would be double ma1 = (SMA(High, 3) [0] - ATR (3) [0] * 1):

                    Should there be any spacing? however, there is spacing the 2nd formula and no error messages came up

                    Comment


                      #11
                      The spacing should not matter.

                      From your code do you have a colon or semicolon? It should end with a semicolon.

                      I've attached a .zip file which contains a version of this indicator which should compile, and should also result in the image attached when applied to a chart.
                      Attached Files
                      MatthewNinjaTrader Product Management

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by sidlercom80, 10-28-2023, 08:49 AM
                      167 responses
                      2,260 views
                      0 likes
                      Last Post jeronymite  
                      Started by warreng86, 11-10-2020, 02:04 PM
                      7 responses
                      1,361 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by Perr0Grande, Today, 08:16 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post Perr0Grande  
                      Started by elderan, Today, 08:03 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post elderan
                      by elderan
                       
                      Started by algospoke, Today, 06:40 PM
                      0 responses
                      10 views
                      0 likes
                      Last Post algospoke  
                      Working...
                      X