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

Learning Ninjascript details

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

    Learning Ninjascript details

    I have some intro programming courses under my belt from college years ago, and I've gone through the entire help file for Ninjatrader, but still have a lot of questions. What other sources would you recommend to learn more of the inner-workings of Ninjascript?

    For an example of the types of questions I have: in the tutorial https://ninjatrader.com/support/help...ion_logic4.htm
    you have the line
    Plot0.Set(SMA(SMAPeriod)[0] + myDataSeries[0]);

    1. Is Plot0.Set( ) equivalent to Value.Set( ). Do they both accomplish the same thing? And if so, how would I set the values for Plot1 using the Values collection? Would it be Values[1][0].Set( )?

    2. Since you have "SMA(SMAPeriod)[0]", I assume you could have SMA(SMAPeriod)[2] and so forth. Which makes me ask, is each call to SMA(SMAPeriod) calculating an array of SMA values. And if so, if we are only interested in the current value, isn't it inefficient to re-calculate all the other values each time?

    Ninjatrader seems like a great platform, so hopefully can find a detailed reference to learn it well!
    Last edited by YD777; 07-14-2016, 06:45 AM.

    #2
    Hello,
    If you have not already I would recommend going through our educational resources on the forum specifically the reference samples and tips sections.
    There are also partners that offer NinjaScript education. If you would like more information on these partners please let me know and our partner affiliate can provide you with some more information.

    Plot0.Set and Value.Set do achieve the same for your plot. You can set your plot by doing PlotName.Set() or Value.Set. If you have a second plot named Plot1 you can set it by doing Plot1.Set() or you could use Values[1].Set()

    If you are calling SMA(Period) multiple times in OnBarUpdate that is going to instantiate it multiple times which can cause extra load that you do not need. You could help reduce the load and get around this by instantiating the SMA(Period) in the OnStartUp() event method.,

    So instead of using this:
    Code:
    protected override void OnBarUpdate()
    {
            Print(SMA(20)[0]);
            Print(SMA(20)[1]);
            Print(SMA(20)[2]);
    }
    You would instead use this:
    Code:
    #Variables
            private SMA mySMA;
    #endregion
    
    
    protected override void OnStartup()
    {
            mySMA = SMA(20);
    }
    
    protected override void OnBarUpdate()
    {
           Print(mySMA[0];
           Print(mySMA[1];
           Print(mySMA[2];
    }
    If we can be of any other assistance please let us know.
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      Thanks. That is helpful for my first question, but I don't think I worded my second question the best. Let me try again for that second question:

      If I have SMA(SMAPeriod)[3] in a script, does the script go and calculate SMA(SMAPeriod) for all bars, and then return the value 3 bars ago, or does it just specifically calculate SMA for 3 bars ago only?

      Comment


        #4
        Hello,
        The Entire SMA series is calculated and then is cached when you call it.
        The series is cached though so the next time you call it the calculation is quicker.
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          Thanks again. Last question:

          Indicators on my chart are calculated for each bar and stored in a DataSeries, correct? So how would I access a historical value of my indicator without simply recalculating the value?

          Let's say SMA is on my chart, and my second indicator wants to know what the value of SMA was 3 bars ago. How would it access this value rather than recalculate it?

          Comment


            #6
            Hello,
            You would not be able to pull the values for the SMA that is calculating that was added to your chart. You would only be able ot get teh values fo rthe SMA by calling its indicator method.
            Cody B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by YD777 View Post
              Thanks again. Last question:

              Indicators on my chart are calculated for each bar and stored in a DataSeries, correct? So how would I access a historical value of my indicator without simply recalculating the value?

              Let's say SMA is on my chart, and my second indicator wants to know what the value of SMA was 3 bars ago. How would it access this value rather than recalculate it?
              The Value object, (and its brethren Values[x]) are each a collection. Like all collections, the values are calculated, then held indexed in the collection object. So to refer to values inside the collection, you use the index. It does not mean that the value is recalculated: it is read from the collection using the index, just as it is done with all collections.

              Comment


                #8
                Originally posted by koganam View Post
                The Value object, (and its brethren Values[x]) are each a collection. Like all collections, the values are calculated, then held indexed in the collection object. So to refer to values inside the collection, you use the index. It does not mean that the value is recalculated: it is read from the collection using the index, just as it is done with all collections.
                Thank you, Koganam, this was the impression I was getting from what is in the help files. So I ask you, if I had 3 indicators in a strategy:

                Code:
                protected override void Initialize()
                {
                	Add(Indicator1());
                        Add(Indicator2());
                	Add(SMA(20));
                
                	...
                }
                And lets say in OnBarUpdate( ) I wanted see if SMA 3 bars ago was above X, would the expression to access that SMA value be:

                Code:
                Values[2][3] > X
                And am I'm I correct in thinking that is not equivalent to
                Code:
                SMA(20)[3] > X
                since the latter calculates the SMA values all over again?
                Last edited by YD777; 07-14-2016, 08:02 PM.

                Comment


                  #9
                  Hello YD777,

                  When adding an indicator to a chart this does create DataSeries collections for these. To access these from the indicator that was added to the chart, you will need a handle (a variable that is the type of the indicator) to store the instance of the indicator to.

                  For example:

                  Code:
                  In #region Variables:
                  private SMA mySMA;
                  
                  In Initialize():
                  mySMA = SMA(14);
                  Add(mySMA);
                  
                  In OnBarUpdate():
                  Print(mySMA[0].ToString());
                  if (mySMA[0] > X)
                  {
                  // execute code
                  }
                  Values, are the plot values from within an indicator internally.
                  If you were to add a plot with Add(new Plot(Color.Green, "myPlot")); this would create an index in Values that you can set and this will draw the plot on the chart.
                  http://ninjatrader.com/support/helpG...nt7/values.htm
                  http://ninjatrader.com/support/helpGuides/nt7/add.htm
                  Chelsea B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by f.saeidi, Today, 08:01 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post f.saeidi  
                  Started by Rapine Heihei, Today, 07:51 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post Rapine Heihei  
                  Started by frslvr, 04-11-2024, 07:26 AM
                  5 responses
                  96 views
                  1 like
                  Last Post caryc123  
                  Started by algospoke, 04-17-2024, 06:40 PM
                  6 responses
                  49 views
                  0 likes
                  Last Post algospoke  
                  Started by arvidvanstaey, Today, 02:19 PM
                  4 responses
                  11 views
                  0 likes
                  Last Post arvidvanstaey  
                  Working...
                  X