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

Best Practices When Using Indicator Values

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

    Best Practices When Using Indicator Values

    I'm developing a strategy that uses values from a custom indicator for entries/exits etc. I notice that in most of the samples, outside the strategy indicators are referenced inline each time a value is needed. That is, if we are using the value of an EMA at various points within our code you will see EMA(xxx)[x] in every spot it is needed. Is this the preferred technique? The reason I ask is that I went down the road of making an internal data series within the strategy, setting it equal to the external indicator once, and then using it internally for all calculations. Is this any more efficient or am I just adding extra code for no reason?

    Thanks

    #2
    Hello RandomTask,

    Thanks for your post!

    The best practice would be to create an instance of an indicator and assign that instance of that indicator to an indicator call with the parameters you need in State.DataLoaded or State.Historical. The instance of the indicator can then be accesses for historical values just like you can with another Series<double> object.

    Code:
    private SMA mySma;
     
    protected override void OnStateChange()
    {
      // when the indicator begins processing
      // save an instance of the SMA indicator with the desired input   
      if (State == State.Historical)
      {
        mySma = SMA(20);
      }
    }
     
    protected override void OnBarUpdate()
    {
      // use the referenced mySMA throughout the lifetime of the script
      if (Close[0] > mySma[0])
      {
        Print(mySma[0]);
        EnterLongLimit(mySma[0]);
        Draw.Dot(this, Time[0].ToString(), false, 0, mySma[0], Brushes.DarkGreen);
      }
    }
    This best practice and others can be publicly referenced here: https://ninjatrader.com/support/help...tm#Performance

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Awesome. Thank you.

      Comment


        #4
        It seems that when I try this with a custom indicator I get the following error in the compiler:

        private MyCustomIndicator myInd;

        'NinjaTrader.NinjaScript.Strategies.Strategy.MyCus tomIndicator(double, double, int)' is a 'method' but is used like a 'type'

        Comment


          #5
          Hello RandomTask,

          I do not see an error taking this approach. I have attached an example strategy and indicator to demonstrate.

          Please let me know if you are receiving any compiler errors with this Strategy and Indicator on your end.
          Attached Files
          JimNinjaTrader Customer Service

          Comment


            #6
            That one works just fine.

            It ended up being a namespace issue. My custom indicators are in a custom namespace and so in order for the variable declaration to work I needed to do this:

            private NinjaTrader.NinjaScript.Indicators.MyCustomNamespa ce.MyCustomIndicator myInd;

            Thank you.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Rapine Heihei, Today, 08:19 PM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by Rapine Heihei, Today, 08:25 PM
            0 responses
            4 views
            0 likes
            Last Post Rapine Heihei  
            Started by f.saeidi, Today, 08:01 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by Rapine Heihei, Today, 07:51 PM
            0 responses
            6 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  
            Working...
            X