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

Accessing chart indicators from a strategy

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

    Accessing chart indicators from a strategy

    Hi,

    I've seen a few posts on accessing indicators that are on a chart from within a strategy that has subsequently been attached to the chart. While it seems to be possible to access the chart indicators from the indicators collection, some users have reported that this sometimes works and sometimes doesn't. One indicated that it only worked consistently as long as the indicators were added after the strategy. Is this true, as in, would this be expected behavior?

    If so, then my question would be, if you use AddChartIndicator() in a strategy to add the desired indicators, would those indicators reliably be accessible to the strategy's NinjaScript via the indicators collection?

    A follow-up question would be about doing things in the right order. If an indicator is added via AddChartIndicator in the DataLoaded state change, at what point and in which block of code is it safe to loop through the indicators collection, set a local variable of the type of the indicator to the instance added to the chart, and begin accessing its values?

    And the last piece, that I can think of, is would there be any need to call Update() on the indicators in the strategy's OnBarUpdate() method before accessing their values? I'm thinking not since they're on the chart and theoretically would be in sync with the strategy, but, don't want to assume anything.

    Thanks,

    Gordon

    #2
    Hello Gordon,

    Thanks for your post.

    It is possible to add indicators to the chart by the convenience method of addChartIndicator(), these indicators do not necessarily have to accessed by the strategy. They can be added for just a visual use on the chart without the strategy using them within the code.

    It is possible to access other chart indicator values through the indicators collection, but this would not guarantee synchronization and would not be the recommended/supported approach for using indicators in a strategy.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,

      Thank you. If one uses addChartIndicator() from within a strategy and then accesses those same indicators in the strategy code (after getting references to them) will synchronization be guaranteed? If so, could you please point out where in the strategy lifecycle you should loop through the chart indicators to set the references?

      Regards,

      Gordon

      Comment


        #4
        Hello Gordon,

        Thanks for your reply.

        I've attached an example strategy that shows adding a private instance of an EMA to the strategy to use in the strategy coding as well as to add to the chart for display purposes. The strategy will output the current bar value of the EMA to the New>Ninjascript output window.

        ExampleAddChartIndicator.zip



        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul, I am using Addchartindicator() within a strategy. The Indicator has plenty of plots. I just want to display 1 or 2. How do I do that?

          Comment


            #6
            Hello guw75,

            Thanks for your post.

            You would need to set the plots that you do not wish to see to transparent.

            Please see the example of setting a plot brush here: https://ninjatrader.com/support/help..._strategie.htm

            With multiple plots, the first plot would be [0], the 2nd plot would be [1] the 3rd would [2], etc. etc. You can find out the order by either looking at the code to see the sequence of AddPlots() in the code or you can look in the indicator panel with the indicator applied to a chart and that will show them in the order. For example, the standard MACD indicator has 3 plots. When looking at the indicator panel they are in the order of MACD, Avg, Diff. So if you wanted to hide the Diff plot:

            MACD(12, 26, 9).Plots[2].Brush = Brushes.Transparent;
            AddChartIndicator(MACD(12, 26, 9))


            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Thanks a lot.
              What I noticed, the order is important:
              Instead of:
              MACD(12, 26, 9).Plots[2].Brush = Brushes.Transparent;
              AddChartIndicator(MACD(12, 26, 9))



              coding:
              AddChartIndicator(MACD(12, 26, 9));
              MACD(12, 26, 9).Plots[2].Brush = Brushes.Transparent

              will hide the Diff plot in the data box but not in the chart.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by rtwave, 04-12-2024, 09:30 AM
              4 responses
              31 views
              0 likes
              Last Post rtwave
              by rtwave
               
              Started by yertle, Yesterday, 08:38 AM
              7 responses
              29 views
              0 likes
              Last Post yertle
              by yertle
               
              Started by bmartz, 03-12-2024, 06:12 AM
              2 responses
              22 views
              0 likes
              Last Post bmartz
              by bmartz
               
              Started by funk10101, Today, 12:02 AM
              0 responses
              6 views
              0 likes
              Last Post funk10101  
              Started by gravdigaz6, Yesterday, 11:40 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Working...
              X