Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing indicator variable from a strategy

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

    Accessing indicator variable from a strategy

    Is there some reading that describes how one writes code to access the current value of a variable generated in an Indicator from a Strategy? This variable would be a new one and not origianlly built by the original coder for public access.

    For example, if I create a double variable called "My_Double_Variable" in the PSAR indicator, what code do I have to add to the PSAR indicator and my strategy to move the current value of "My_Double_Variable" as it exists in PSAR into My_Strategy.

    I recognize that the code in My Strategy would look something like:
    Var_Strat = ParabolicSAR(0.02,0.2,0.02).My_Double_Variable;

    I have the same question if the variable in question were bool or int.

    Thanks!

    Larry
    Last edited by epcompany; 07-10-2009, 07:56 AM.

    #2
    Hey Larry, have you seen the sample reference titled: Exposing indicator values that are not plots?
    AustinNinjaTrader Customer Service

    Comment


      #3
      Austin,
      Thanks for the reference. I had not seen it and will read it with care.

      My problem, if you will allow, is that I'm looking for a cook book that says if you want that, do this. Your example provides an example of structure without giving me the rules of development. Are the "rules" written somewhere?

      Also, it seems that the example sets my variable into a series class allowing access to historical values. I only want the current value and don't care about any others. I've seen indicators such as TSSuperTrend chew up massive amounts of memory storing useless historical data.

      All I want is to get at the current value of my new indicator variable in My Strategy. It can't be that complicated but the easy button seems to be elusive.

      Thanks for any additional pointer you may give me!

      Larry

      Comment


        #4
        Larry, the indicator in the example I sent you can do this without a problem.

        Upon import, the sample adds an indicator and a strategy to your NinjaScript collection. The indicator is called SampleBoolSeries, but it also exposes a double value--exactly what you're trying to do.

        At line 37 in SampleBoolSeries the double called exposedVariable is declared. Then it is assigned a value later in OnBarUpdate() at line 94. The last important section is in properties, where the exposedVariable becomes retrievable at lines 116-122.

        Then line 63 in the strategy (SampleBoolSeriesStrategy) retreives the value from the indicator.

        Please let us know if you have any other questions.
        Last edited by NinjaTrader_Austin; 07-13-2009, 08:55 AM.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Austin,
          Thanks for the patience and the help!

          I appreciate it.

          Larry

          Comment


            #6
            Austin,
            Following the Yellow Brick Road worked fine. I now can port values around at will, Thanks.

            On to the next problem:

            I'm using five minute bars. I can watch my value change over in the indicator. It does not transfer to the strategy for "a while".

            If I set both the strategy and indicator to
            CalculateOnBarClose = true;

            The changing value of the indicator shows up in the strategy one five minute bar later.

            If I set both the strategy and indicator to
            CalculateOnBarClose = false;

            The changed value of the indicator shows up in the strategy around two minutes and thirty eight seconds later (about half a bar).

            The latency is a strategy killer. What am I doing wrong?

            Thanks!

            Larry


            PS: As this message got too long, I will post the added code in the next reply.

            Comment


              #7
              Austin,

              To understand this, you need to read the previous post as this is a continuation.

              Here's the code I added:

              In variables:
              private int trade_up;
              private int trade_down;

              In Initialize()
              CalculateOnBarClose = false; // or true for the other case

              In OnBarUpdate()
              if (!trend[0] && trend[1])
              {
              ...
              trade_down =
              1;
              trade_up =
              0;

              ....

              }

              Similar code for the reverse.

              In Properties
              [Browsable(false)]
              [XmlIgnore()]
              publicint Trade_up
              {
              get { Update(); return trade_up; }}

              [Browsable(
              false)]
              [XmlIgnore()]
              publicint Trade_down
              {
              get { Update(); return trade_down; }}

              In Strategy:
              gt_trade_up = TSSuperTrend(Close,7,MovingAverageType.TEMA,2.618,7,SuperTrendMode.ATR).Trade_up;
              gt_trade_down = TSSuperTrend(Close,
              7,MovingAverageType.TEMA,2.618,7,SuperTrendMode.ATR).Trade_down;

              Comment


                #8
                epcompany, thanks for the detailed posts - is there any CalculateOnBarClose setting in your referenced / called indicators? If yes, please remove all of those and just set this from the strategy Initialize().
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Bertrand,
                  How interesting!

                  Let me make sure I understand. Even though I have set CalculateOnBarClose to false in both the strategy and the indicator, you want me to REMOVE the CalculateOnBarClose statement from the TSSuperTrend indicator and any indicators that might be called by it.

                  Then I use a single CalculateOnBarClose statement in my strategy and set it according to how I want it used.

                  Is that correct?

                  Thanks,

                  Larry

                  Comment


                    #10
                    Yes, this is correct - there's a known bug in NinjaTrader 6.5 where set CalculateOnBarClose values would not be overridden as expected, so as workaround we advise to set this only in the 'mother' strategy and remove any references to this in the called indicators.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Thanks a bunch!

                      Happy trading.

                      Larry

                      PS: I can't wait for NT7 to come out

                      Comment


                        #12
                        That worked beautifully!

                        Thanks to both Austin and Bertrand!

                        Larry

                        Comment


                          #13
                          Continuing the porting variables tread:

                          Notice in the previous that I might be porting more than one variable from the indicator to the strategy. In fact, I port four.

                          I'm using the { get { Update(); return xxxx; }} procedure from the samples you provided.

                          If I use the statement for each variable, the indicator gets run once, as you would expect, each time I go "get" a variable.

                          This has the effect of

                          1)eating a lot of compute cycles as the indicator gets run four times a tick,
                          and
                          2) giving me slightly different numbers as the time base for one can be a tick or more later than the last one.

                          Is there a way to "get" more than one variable at a time with just one "{ get { Update(); return xxxx; }}" such that the result is generated from the same { get { Update(); return xxxx; }} pass through the variable?

                          Thanks,

                          Larry

                          Comment


                            #14
                            Larry, you should only call update as you need it because it is a forced OnBarUpdate() call. An idea would be to use a flag to check if it has already been updated for this OnBarUpdate, and if it already has been updated, then don't call it again until a new bar.

                            Of course, it would be different for CalculateOnBarClose = false.

                            Basically, you can just optimize your code for calling Update() when you need it. Otherwise, you've correctly noted that an unoptimized indicator/strategy would eat a lot of cycles.

                            An idea I had for you:
                            Instead of calling four individual variables, why not just call one variable that is actually four stuck together? You could, for example, have all the logic run like usual and have the indicator generate your four values in an intermediate state, then concatenate it all together. So instead of calling variable valueA and then valueB and then valueC and D, you could just call one "variable" that returns all the values seperated by a character, like this "valueA|valueB|valueC|valueD."

                            Hopefully that is enough information to get the ball rolling. Let us know if you have anymore questions.
                            AustinNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by wftrrdt, Today, 05:01 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post wftrrdt
                            by wftrrdt
                             
                            Started by wellbeingcbd, Today, 04:41 AM
                            0 responses
                            1 view
                            0 likes
                            Last Post wellbeingcbd  
                            Started by piydasuknu, Today, 03:45 AM
                            0 responses
                            3 views
                            0 likes
                            Last Post piydasuknu  
                            Started by ajdfkgjiojjhfihjg, Today, 03:20 AM
                            0 responses
                            3 views
                            0 likes
                            Last Post ajdfkgjiojjhfihjg  
                            Started by keravitahere, Today, 01:56 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post keravitahere  
                            Working...
                            X