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

Sharing dataseries created in Strategy to an indicator

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

    Sharing dataseries created in Strategy to an indicator

    Hello,
    I have created an indicator that uses 2 dataseries (BarsType and Volumetric). I want to call this indicator from a Strategy.
    I am having trouble as I call for example Low[0] with an exception saying "Out of bounds", like there is no data in the Low series.
    I tried to use for example BarsArray[0].GetClose(0) - same issue.

    The indicator on its own works well, and I add the additional data series during State.Configure.
    When creating the indicator from the strategy, I add the series during State.Configure in the strategy. But none of the dataseries seem to be loading when the indicator is running. The Count seems correct, just accessing the data returns an exception.

    Help will be very appreciated,
    Thank you,
    laurent

    #2
    Hello Laurentvan,

    Thank you for your post.

    From the Help Guide on AddDataSeries():

    "Should your script be the host for other scripts that are creating indicators and series dependent resources in State.DataLoaded, please make sure that the host is doing the same AddDataSeries() calls as those hosted scripts would. For further reference, please also review the 2nd example below and the 'Adding additional Bars Objects to NinjaScript' section in Multi-Time Frame & Instruments"



    This means your strategy script that is hosting the indicator should have all the same AddDataSeries() calls that your indicator has.

    Please let us know if you have any further questions.​
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Thank you Gaby, much appreciated.
      I do make the same AddSeries calls in the strategy (and not a second time in the indicator).
      I am not sure I am instanciating the indicator well from within the strategy - the behaviour is odd.

      For example, on currentBar 0, I can access Close[0], but I cannot access Low[0]. It is like the Low series are not filled with data. In debug, I can see that supposedly there are lots of samples but the Count value is 0. Really not sure what I am missing.

      Again, the indicator on its own is working.
      There are 2 ways to apparently create the indicator from within the strategy, one with passing a Series as the first argument, one without. I had passed ¨Close¨ as the series, do I need to pass in multiple series? or leave that argument empty?

      Thanks again
      laurent

      Comment


        #4
        Hello Laurentvan,

        If the indicator needs a series as an argument, you should pass that in.

        Do you have a currentbars check for all added data series?



        Additionally, since your script has added data series, I recommend you use Closes and Lows to specify the barsSeriesIndex you want this information from.






        I also recommend you debug the script using prints to determine exactly which line is causing the index out of bounds error. To identify the invalid index, I highly recommend printing the time of the bar, the index being used, and the size of the collection.

        For a Series barsAgo value, CurrentBar is the size of the collection.

        For an array or collection, the .Length or .Count() is the size of the collection.​

        The help guide discusses ‘Make sure you have enough bars in the data series you are accessing’ .
        NinjaScript > Educational Resources > Tips > Make sure you have enough bars in the data series you are accessing

        Please let me know if I can assist further. ​
        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          Thank you so much for your answer. It turns out Visual Studio debugging was breaking on another line (since NT 8.1.2.1 there are problems with breakpoints with VS Studio, I have to disable "Require to match source" and it may break on different lines).
          The issue actually is that my 2nd series (I use AddVolumetric in strategy State.Configure) isn't loaded and I try to access it with BarsArray[1].BarsType - the out of bounds error is BarsArray[1] (there is only 1 BarsArray). When I use the indicator alone and call AddVolumetric in State.Configure it doesn´t have the issue.
          Any idea as to why I don´t see the data series when loaded from the strategy?
          Thanks again,
          laurent

          Comment


            #6
            Actually I am starting to wonder if there isn´t a restriction when instantiating a custom indicator from a strategy.
            When using the custom indicator on its own, I can add AddVolumetric in state.configure, and the indicator can work with both BarsArray[0] and BarsArray[1].
            When instantiating the indicator from the strategy, it takes the Series as an input and can´t add more series in State.Configure.
            Is there a way to pass both series to the indicator from the strategy? I feel that may be the issue,
            Thanks
            laurent

            Comment


              #7
              Hello Laurentvan,

              Thank you for your response.

              Were you able to narrow done which line in particular is causing the error? If so, can you share the entire line here?

              I am able to call BarsArray[1].BarsType when using AddVolumetric() in a strategy. Please see the attached test script.
              Attached Files
              Gaby V.NinjaTrader Customer Service

              Comment


                #8
                The issue I believe is that I call BarsArray[1].BarsType in the OnBarUpdate of the Indicator.

                Here is my scenario:

                Strategy:
                in state.configure I call AddVolumetric
                in state.DataLoaded I instanciate my custom indicator (no data series passed - hence likely BarsArray[0] from the strategy)

                Indicator:
                in OnBarUpdate, I am trying to access BarsArray[1].BarsType but when debugging I see that BarsArray size is only 1

                To reproduce, you would need to create a custom indicator, e.g IndicatorTest, instantiate it in state.DataLoaded (myindicatorTest = indicatorTest(<parameters if any>)) and access BarsArray[1] from the OnBarUpdate function in IndicatorTest.

                I could create that sample to see as well but thought I'd explain the issue I'm getting. It's like the instantiated indicator cannot access the complete BarsArray of the strategy, only the BarsArray[0] that is passed during instantiation,

                Thanks
                laurent

                Comment


                  #9
                  Hello Laurentvan,

                  Thank you for your response.

                  You say you believe the error is the BarsArray[1] call in the indicator, have you debugged the script to identify which line is for certain is causing the error?

                  Are you debugging the indicator, or the strategy?

                  If this error is happening in the indicator script, please note that the indicator also has to have an added data series call like AddVolumetric in order to be able to access BarsArray[1].


                  If possible, provide a reduced version of your script for testing. Remove all code that is not necessary for reproducing this behavior.
                  Gaby V.NinjaTrader Customer Service

                  Comment


                    #10
                    It seems like the problem was indeed I wasn´t adding AddVolumetric in BOTH the strategy and the indicator... I thought I could only call it once.
                    Now it is working.
                    Thanks again for your help

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by fx.practic, 10-15-2013, 12:53 AM
                    5 responses
                    5,403 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by Shai Samuel, 07-02-2022, 02:46 PM
                    4 responses
                    94 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by DJ888, Yesterday, 10:57 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by MacDad, 02-25-2024, 11:48 PM
                    7 responses
                    158 views
                    0 likes
                    Last Post loganjarosz123  
                    Started by Belfortbucks, Yesterday, 09:29 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post Belfortbucks  
                    Working...
                    X