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

How to add indicator to non-primary bars object within a strategy

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

    How to add indicator to non-primary bars object within a strategy

    Hello,

    I am toying with the idea of implementing a multi-bars strategy.

    I would like to Add() indicators to both the primary bars object and to a non-primary bars object from within the strategy in order to expose variables calculated in the indicators to the strategy.

    These indicators are non-standard and were written by me, so they are not available within the strategy via standard Ninjascript indicator method calls.

    I know how to add an indicator to the primary bars object of a strategy and to expose the indicator's variables to the calling strategy as illustrated in the following NT reference example:



    However, I do not find any info on how to add an indicator to a non-primary bars object within a strategy in order to expose its variables to the calling strategy in a similar way.

    Does anyone know how to do this?

    Thanks in advance.
    Last edited by flonkle; 09-15-2014, 02:25 PM.

    #2
    If you've made your own indicator, you will still have a separate input argument to the indicator method to specify a supplementary data series available to the strategy using BarsArray[idx]

    In the same manner you would assign this to a NinjaTrader system indicator, you can do so for your custom indicator from your strategy:

    Code:
            protected override void Initialize()
            {         
                            //add secondary data series to strategy
    			Add(PeriodType.Minute, 1);
            }
            protected override void OnBarUpdate()
            {
                            //use the added BarsArray as an input for the indicator
    			double myThing = this.MyBaconIndicator([B]BarsArray[1][/B], 12)[0];
            }
    Please let me know if this is not exactly what you were looking for.
    MatthewNinjaTrader Product Management

    Comment


      #3
      NT_Matthew,

      Thanks for the reply.

      Your solution will probably work for one of the indicators I want to use. It can be calculated from a call to OnBarUpdate() using only historical price data captured in the price bars of the chart.

      However, I have another indicator that I may want to use that cannot be calculated this way. It calculates information in the OnMarketData() method, and the information it uses can only be captured in real-time as it is not preserved in price-bar data for a chart.

      Also, I need to make sure that this indicator that calculates its values in the OnMarketData() method is applied to the OnMarketData() calls for the proper instrument. Given that I'll be using a multi-instrument strategy to call the indicator (the different bars objects will be generated from separate instruments), I need to separate out the OnMarketData() calls from the different bars objects to assure that the indicator is calculated correctly using the proper data.

      If I absolutely have to, I can always copy and paste my indicator code directly into the strategy code and alter the indicator code slightly to get things done. (This is true if BarsInProgress is updated properly according to which bars object calls the OnMarketData() method in the same way that BarsInProgress is updated according to which bars object calls the OnBarUpdate() method. I haven't tried this.)

      However, this is poor software design and certainly does not lead to re-usability if I ever want to do something similar in the future.

      That's why I was trying to just Add() the indicator to a non-primary bars object from the strategy that calls it, in order to create something re-useable that doesn't involve me copying and pasting indicator code into my strategy whole-cloth whenever I want to do something like this.

      So, I ask again, is this sort of thing possible, or is the solution you previously posted the only way of tying my indicator to a non-primary bars object when the indicator is called from within a multi-bars strategy?
      Last edited by flonkle; 09-16-2014, 11:08 AM.

      Comment


        #4
        Flonkle,

        Thank you for your reply.

        OnMarketData() is a global call for that instrument. Meaning, any data coming in will call OnMarketData() for any working indicators and strategies attached to that instrument.

        This will apply to the method that Matthew outlined in his post, where you add an indicator with a different instrument. If Market data comes in for that instrument, it will call OnMarketData().

        Let me know if I can be of further assistance.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          NT_Cal,

          Come on man... your reply is a total non sequitur.

          I know that OnMarketData() will be called by any instrument that is feeding data to an indicator or strategy. The question is whether or not BarsInProgress is updated to reflect which instrument calls OnMarketData() in the same way that BarsInProgress is updated to reflect which instrument calls OnBarUpdate().

          But don't even worry about that now. That was really just a sidebar.

          I continue to ask you guys, is there any way to bind the indicator that I call from my multi-bars strategy to a non-primary bars object similar to the way that I can bind an indicator called from within a strategy to the primary bars object using Add() in the following reference sample:



          If I can bind my indicator to a non-primary bars object in a way similar to this, then I won't have to worry about BarsInProgress at all, and I won't have to copy and paste indicator code whole-cloth into my strategy code in order to achieve my goal.

          Add() doesn't have an overload that will allow me to explicitly specify a bars object to which the added indicator will be bound. Nor does my indicator constructor have an overload that allows me to explicitly specify the bars object to which it will be bound.

          So, I'm hoping there is some other way to explicitly bind the indicator (which I am instantiating and calling from within a multi-series strategy) to a non-primary bars object, that is similar to the way the indicator in the above reference sample is bound (implicitly) to the primary bars object of the strategy that instantiates and calls it.

          (NT_Matthew's proposed solution will work for some cases but not others.)

          If this kind of binding can't be done, I'd like to know that, too.

          (I'm curious about hearing from you guys on this because even the techniques displayed in the code of your above reference sample do not seem to be documented anywhere else. I 'm hoping that you guys have another gem that will help me do something similar to Add()-ing an indicator to a non-primary bars object from within a calling strategy that I just haven't been able to find.)

          Again, thanks in advance for your help with this specific question.
          Last edited by flonkle; 09-18-2014, 10:04 AM.

          Comment


            #6
            Just to put the question to rest, I checked to see if BarsInProgress is updated to reflect which bars object calls OnMarketData(), and BarsInProgres is updated in this way.

            I also checked to see if I could pass in something like BarsArray[1] (as an object that would potentially be accepted as implementing the IDataSeries interface properly by the compiler) to my indicator constructor in order to specify the bars object to which the indicator should be bound.

            I was surprised that this code even compiled, but it did.

            However, there were run-time errors that prevented this approach from working.

            If I coded something like:

            Code:
            protected override void Initialize() 
            {
                 // code here to Add other instrument(s) to the strategy
                 Add(myIndicator(BarsArray[1]));
            }
            I got a run-time error stating that the 'BarsArray' property can't be accessed from within 'Initialize' method. (This restriction is well-documented, but I'd thought I'd give this approach a shot anyway.)

            And if I tried to move the above Add() statement into the OnStartUp() method like this:

            Code:
            protected override void OnStartUp()
            {
                Add(myIndicator(BarsArray[1]));
            }
            I got a run-time error stating that the 'Add' method only can be called from within 'Initialize' method.

            So, even though this code compiles, it doesn't work due to run-time errors.
            Last edited by flonkle; 09-18-2014, 02:58 PM.

            Comment


              #7
              Flonkle,

              The Add() method for indicators is going to be a visual representation. You cannot change the data series that this uses.

              The method that Matthew supplied is good for getting the calculated values for the different data series on the indicators -
              http://www.ninjatrader.com/support/f...ead.php?t=3572

              However, if you are looking to get a visual representation of the indicator with the different data series you would need to create a placeholder script for plotting, add that script to the strategy.-
              http://www.ninjatrader.com/support/f...ead.php?t=6651
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                NT_Cal,

                You are just wrong about the Add() method only being used to generate visual representations. If you Add() an indicator to a strategy as demonstrated in the following code that I have referenced NUMEROUS times within this thread, then you are doing so in order to expose variables calculated by the indicator to the calling strategy:



                (Even if you won't bother to look at the code referenced above, just read its description page. The first line of the description page for the above code reads: "There may be cases where you want to have your indicator calculate non-plotted values that you will want to access when using this indicator inside of another indicator or strategy..." This line alone should let you know that you are dealing with something other than visual representations here.)

                (And since the indicators I want to Add() are custom indicators, I can calculate the variables within these indicators in any way I see fit. I'm not trying to set any indicator values from within the strategy. I only want to access/read these indicator values from within the strategy. The above NT sample code that I have referenced repeatedly in my posts makes this abundantly clear.)

                I want to Add() an indicator to a non-primary bars object from within a strategy in order to expose the variables of the indicator (that are calculated using data from a non-primary bars object) to the calling strategy. I want to do this in order to avoid duplicating code. I want to write the code ONCE, in the indicator, and then call this indicator from any number of strategies that might need access to the information/variables the indicator calculates. If I can't do this, then I will be forced to duplicate the code that calculates these variables in any and all strategies that need access to this very same information. It is very poor software design to duplicate code in this way.

                The link you posted to sample code about Synchronizing a DataSeries to a non-primary bars object is interesting, but it doesn't help. Even in that code, all the calculations are being performed within the strategy. I need the calculations to be performed within the indicator.

                Also, I may want to calculate variables that represent more than one value per price bar. For example, I may want my indicator to generate an array of values for each price bar. In such case, no type of IDataSeries will allow me to store/expose this information, directly. However, if the indicator exposes this array variable to the calling strategy at the close of each price bar, then I can easily grab this array at each time-step and do with it what I will in the strategy (including storing it in a data structure within the strategy that will allow for "historical" access to these array values at a later time-step).

                In any case, thank you for your time. I think we're pretty much done here.

                Like it or not, it looks like I'm going to be forced to duplicate code across any strategies that need to access a given variable when that variable is calculated on data from a non-primary bars object.
                Last edited by flonkle; 09-25-2014, 01:04 AM.

                Comment


                  #9
                  Originally posted by flonkle View Post
                  NT_Cal,

                  You are just wrong about the Add() method only being used to generate visual representations. If you Add() an indicator to a strategy as demonstrated in the following code that I have referenced NUMEROUS times within this thread, then you are doing so in order to expose variables calculated by the indicator to the calling strategy:



                  (Even if you won't bother to look at the code referenced above, just read its description page. The first line of the description page for the above code reads: "There may be cases where you want to have your indicator calculate non-plotted values that you will want to access when using this indicator inside of another indicator or strategy..." This line alone should let you know that you are dealing with something other than visual representations here.)

                  (And since the indicators I want to Add() are custom indicators, I can calculate the variables within these indicators in any way I see fit. I'm not trying to set any indicator values from within the strategy. I only want to access/read these indicator values from within the strategy. The above NT sample code that I have referenced repeatedly in my posts makes this abundantly clear.)

                  I want to Add() an indicator to a non-primary bars object from within a strategy in order to expose the variables of the indicator (that are calculated using data from a non-primary bars object) to the calling strategy. I want to do this in order to avoid duplicating code. I want to write the code ONCE, in the indicator, and then call this indicator from any number of strategies that might need access to the information/variables the indicator calculates. If I can't do this, then I will be forced to duplicate the code that calculates these variables in any and all strategies that need access to this very same information. It is very poor software design to duplicate code in this way.

                  The link you posted to sample code about Synchronizing a DataSeries to a non-primary bars object is interesting, but it doesn't help. Even in that code, all the calculations are being performed within the strategy. I need the calculations to be performed within the indicator.

                  Also, I may want to calculate variables that represent more than one value per price bar. For example, I may want my indicator to generate an array of values for each price bar. In such case, no type of IDataSeries will allow me to store/expose this information, directly. However, if the indicator exposes this array variable to the calling strategy at the close of each price bar, then I can easily grab this array at each time-step and do with it what I will in the strategy (including storing it in a data structure within the strategy that will allow for "historical" access to these array values at a later time-step).

                  In any case, thank you for your time. I think we're pretty much done here.

                  Like it or not, it looks like I'm going to be forced to duplicate code across any strategies that need to access a given variable when that variable is calculated on data from a non-primary bars object.
                  This one has come up before. The idea you need to use is in this post: http://www.ninjatrader.com/support/f...52&postcount=3.

                  You might want to read the entire thread

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by DJ888, 04-16-2024, 06:09 PM
                  6 responses
                  18 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by Jon17, Today, 04:33 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post Jon17
                  by Jon17
                   
                  Started by Javierw.ok, Today, 04:12 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post Javierw.ok  
                  Started by timmbbo, Today, 08:59 AM
                  2 responses
                  10 views
                  0 likes
                  Last Post bltdavid  
                  Started by alifarahani, Today, 09:40 AM
                  6 responses
                  41 views
                  0 likes
                  Last Post alifarahani  
                  Working...
                  X