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

Basic coding script questions

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

    Basic coding script questions

    I have a few basic questions concerning code scripting. Please note that I am an almost total newbie, have read some of the NT Tutorials and am trying to get into this subject. I opened the code for NT's "SMA" indicator to formulate my questions:

    Each indicator code script seems to have three basic sections.

    1. The first section, when expanded, lists some declarations (like using System.Diagnostics;
    using System.Drawing;etc etc.) - I do not change these. Correct?

    2. The second section contains - in this case for the SMA indicator - three sub sections:

    2.0. First it states the name of the indicator with: "public class SMA : Indicator". This is where I give my indicator a name, correct?

    2.1. It then has a sub-section "Variables" which begins with #region and ends with #endregion:
    private int period = 14; Once the indicator building is complete, this is what shows up as the default value. Correct?

    2.2. Now come the sub-sections called
    "protected override void Initialize()" and
    "protected override void OnBarUpdate()". No question on these because I believe there is plenty of explanations in the NT Help section which I will study.

    2.3. Now comes a sub-section called "#region Properties". Why is the earlier subsection "private int" but this one "public int"? Where can I read something about the difference between the two?

    3. The third section is entitled NT generated code. Neither change nor remove. I take it to mean just that: I do not need to mess with it at all. It is created when I am done in Section 1 and 2 above and push the COMPILE button. Correct?

    Lastly, one probably more advanced question: I want to evenutally create a multi time frame script. In which of the above three section do I need to put the Syntax "BarsArray [int index]"?

    sandman

    #2
    sandman,

    I am happy to assist.

    1. The first section, when expanded, lists some declarations (like using System.Diagnostics;
    using System.Drawing;etc etc.) - I do not change these. Correct?
    This is where you can import types used in other namespaces, etc. in the .NET framework. You typically don't need to change this.

    2. The second section contains - in this case for the SMA indicator - three sub sections:

    2.0. First it states the name of the indicator with: "public class SMA : Indicator". This is where I give my indicator a name, correct?
    Yes, you can put your indicator name here. It must be unique. This line basically defines a class named SMA that inherits the "Indicator" class.

    2.1. It then has a sub-section "Variables" which begins with #region and ends with #endregion:
    private int period = 14; Once the indicator building is complete, this is what shows up as the default value. Correct?
    Yes, this would be where you define the private variables, or public variables default settings. The public variables, aka "User Defined Inputs", can be tied to these private variable and are located in the region titled "Properties".

    2.2. Now come the sub-sections called
    "protected override void Initialize()" and
    "protected override void OnBarUpdate()". No question on these because I believe there is plenty of explanations in the NT Help section which I will study.
    Ok, let me know if you have more questions here.

    2.3. Now comes a sub-section called "#region Properties". Why is the earlier subsection "private int" but this one "public int"? Where can I read something about the difference between the two?
    Public means that the variable is accessible outside of the "Class". Private means that its only accessible from within the "Class". A "Class" is a way in Object Oriented code to sort of compartmentalize things and reduce complexity. In NinjaTrader, each indicator is its own "Class".



    3. The third section is entitled NT generated code. Neither change nor remove. I take it to mean just that: I do not need to mess with it at all. It is created when I am done in Section 1 and 2 above and push the COMPILE button. Correct?
    There is no need to modify this section. Any modifications are automatically handled here for you.

    Lastly, one probably more advanced question: I want to evenutally create a multi time frame script. In which of the above three section do I need to put the Syntax "BarsArray [int index]"?
    This can only be used in the "OnBarUpdate()" section.

    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Adam, that helps in getting me oriented.

      sandman

      Comment


        #4
        Adam.

        I do have another immediate question. I am using one custom made multi time frame indicator already. It takes 1,2,3 and 5 minute inputs.

        When I now apply this indicator on a 4Range chart it still works/shows up. I wonder what it actually now works on, and I believe someone told me that NinjaTrader's program would in this case "replace" the "1-minute" condition with "4-range" while maintaining the 2,3 and 5-conditions. Is that correct? (I hope my question makes sense).

        sandman

        Comment


          #5
          Sandman,

          With multi-series code OnBarUpdate() is called for each separate series including the one on the chart. So your understanding is correct.

          The BarsInProgress can be used to check which series called the OnBarUpdate() method. If you shift the chart from 1 minute to 4-range for example, in either case BarsInProgress will be 0 when that series calls the OnBarUpdate() method.
          Adam P.NinjaTrader Customer Service

          Comment


            #6
            Thank you.

            Comment


              #7
              Hi.

              Here is my next question concerning multiple timeframes for an indicator.

              I used the strategy wizard to create certain conditions for one timeframe. One of these conditions is that the EMA13 needs to be larger than the EMA13 one bar ago and the LinReg4 needs to be larger than the LinReg4 one bar ago (rising).

              The code then looks like this:

              A. // Condition set 1
              if (EMA(13)[0] + 1 > EMA(13)[1]
              && LinReg(4)[0] > LinReg(4)[1]

              Now if I want to add two more datseries (for example 3 and 5 minute) I put this into the code:

              a. Add(PeriodType.Minute, 3);
              b. Add(PeriodType.Minute, 5);

              From what I have seen in the code section these additional dataseries are referenced through the BarsArray in the code with
              [1] for the 3min
              and
              [2] for the 5min, with
              [0] being for the primary data series.

              But that meaning seems to collide with the [1] above under A. where it means 1 bar ago.

              My question is how does the line of code under A. above have to be written so it applies for the 3min and 5min inputs?

              sandman

              Comment


                #8
                Code:
                if (EMA(BarsArray[1], 13)[0] + 1 > EMA(BarsArray[1], 13)[1]
                            && LinReg(4)[0] > LinReg(4)[1]
                In the above code sample I use BarsArray[1] which is the secondary data series. So we compare the EMA of the secondary series in this example.

                Let me know if any questions.

                Comment


                  #9
                  I see. So you actually write out "BarsArray[1]". Got it, I think I will try it out now, and if I run into something, will get back to you. Meantime, thank you!

                  sandman

                  Comment


                    #10
                    My Pleasure.

                    Let me know if questions as you get into it.

                    Remember, BarsArray[0] is the Primary and then you count up from there.

                    Dont forget to use BarsInProgress to specify where your code is being run.

                    -Brett

                    Comment


                      #11
                      I see. So you actually write out "BarsArray[1]". Got it, I think I will try it out now, and if I run into something, will get back to you. Meantime, thank you!

                      sandman

                      Comment


                        #12
                        Brett.

                        Thank you! I tried it out with various indicators and time frames. Works like a charm. I think I am getting the hang of it.

                        Now, the next thing I ran into is this. I already used in the conditions this one:

                        && Close[0] < SMA(20)[0],
                        meaning Closing Price of the Primary must be smaller than the SMA20 of the Primary. That worked quite fine.

                        What I want to express now is that the Closing Price of the Primary must be smaller than the Opening Price of the secondary and tertiary. I tried this

                        && Close[0] < Open(BarsArray[1])
                        && Close[0] < Open(BarsArray[2])

                        but it ain't working. What am I doing wrong? Please correct me.

                        sandman

                        Comment


                          #13
                          Hello,

                          Open/Close/High/Low uses a slightly different way to access its values.

                          For Example:

                          Opens[1][0];

                          This would return the open for the secondary data series for the current bar. It does this due to the 's' and the additoinal [1].

                          Accessing the Price Data in a Multi-Bars NinjaScript

                          Much more detail at the following link in the category above:



                          -Brett

                          Comment


                            #14
                            Got it, thank you very much. Already put it to use and it works nicely!

                            sandman

                            Comment


                              #15
                              A moving average can also be expressed in degrees of its slope, like for example +45 or -2. Does NinjaTrader provide an indicator for this (if so which one is it?), or are these only available through 3rd Party Vendors?

                              sandman

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by samish18, Today, 01:01 PM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by WHICKED, Today, 12:56 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cre8able, Today, 01:16 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post cre8able  
                              Started by chbruno, 04-24-2024, 04:10 PM
                              2 responses
                              47 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by WHICKED, Today, 12:45 PM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X