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

Communicating between Strategy and AddChartIndicators

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

    Communicating between Strategy and AddChartIndicators

    As I understand it if I use AddChartIndicator to add an indicator with variables that are public I can see those values within the Strategy that added it. Does the reverse hold true ?
    in other words, for values that are public in the Strategy can the Indicator that was added see them. My guess is not and for this case I am forced to still use Static vars.

    Thanks for any insight.

    #2
    Hello JerryWar,

    Thank you for your note.

    Unfortunately, it would not work the same way because they would have to be passed in as an overload. Because Strategies host the indicator, you would need to pass any data in using the overloads you create using that specific indicator's public properties.

    It would be possible to do this if the indicator has the instance of the strategy that has been passed by a public variable that has been assigned to the type of the strategy. This would be undocumented however.

    Please let us know if any further questions come up.

    Comment


      #3
      Chris, Thanks for your response.
      Are you saying that if the strategy reads a public var from Indicator #1 it can then pass it to
      indicator #2 as variable defined as a NinjaScript property for Indicator #2 ?
      and if this is the case, Do you have to somehow force Indicator #2 to update its inputs or will this happen just by referencing Indicaotor #2 in the Strategy onBarupdate().

      Comment


        #4
        Hello JerryWar,

        Thank you for your reply.

        You may pass the value from the first indicator to your second indicator provided it has been read first in the order of operations. Any value that you are passing to the second indicator would need to be done so before the strategy calls OnBarUpdate() so that the value can be used in the logic.

        Using the overloads of the indicator is the suggested approach because this will ensure the cache is being used so that if you passed the same value the next bar, it would use the cached indicator.

        Please let us know if any further questions come up.

        Comment


          #5
          Chris
          So will something like this work ok ?

          // excerpt from proposed Strategy code

          else if (State == State.Configure)
          {
          AddChartIndicator ( Indicator1());
          // AddChartIndicator ( Indicaotr2(1)); // Instead of feeding dummy parameter
          // will this ensure History is properly filled in.
          AddChartIndicator ( Indicator2(Indicator1().Osc[0]));
          }
          }

          protected override void OnBarUpdate()
          {
          // Update Indicator 2 values before Strategy is processed.
          Indicator2(Indicator1().Osc[0]);
          //
          // Process Strategy OnBarUpdate
          //

          }

          Comment


            #6
            Hello JerryWar,

            Thank you for the reply.

            Based on what we discussed in this thread prior to your most recent response, your sample code would not accomplish this. What I was describing earlier was passing the parameter value of indicator 1 into indicator 2 using the overload of that particular indicator.

            I would like to make sure I fully understand your request so that I may best assist. It sounds like you are describing passing your first indicator into the second indicator using the input series. If I have misunderstood your request, could you explain more about what you are trying to accomplish?

            I look forward to your reply.

            Comment


              #7
              I need to get a calculated value from Indicator #1 that is public into the strategy on each bar both Historical and Realtime. This I can do in the strategy by reading it.
              value = Indicator1.Osc[0]
              next I need to get that value into the second AddChartIndicator Indicator2 but it needs to constantly update.
              Creating a public NinjaScriptProperty input for Indicator2 didn't work, the input doesn't update. What ever the value is in State.Configured is the last value it sees . It doesn't see the Indicator1.Osc[0] changes I am trying to pass it.

              Comment


                #8
                Chris, I don't understand your example or your explanation.
                double sma = mySMA(12)[0];
                double sma2 = mySMA2(sma)[0];

                I was unaware an indicator could have a Type or a return value.
                Could you please elaborate?

                Comment


                  #9
                  Hello JerryWar,

                  Thank you for the reply.

                  When you have declared your variables containing both indicators, you can then use the variable for the first indicator in the syntax of the second indicator like so:

                  Indicator1 = Bollinger(0.5, 12);

                  Indicator2 = SMA(Indicator1.Upper, 12);

                  I have attached a custom strategy to this post I created as an example entitled "JerryWarExample". Please review the code and try running the strategy with the NinjaScript Output Window open to view the Print Statements. This should assist you with creating the instances of both indicators in your code and then calling those specific instances in OnBarUpdate() to retrieve their values.

                  Please let us know if we may provide further assistance.

                  JerryWarExample.zip

                  Comment


                    #10
                    Chris,
                    I was using AddChartIndicator in State.Configure.
                    Can you confirm that creating an Instance and then passing that name to AddChartIndicator will not create multiple copies of the indicator ? CPU load is a concern.
                    In the meantime I will give this a try. I appreciate you getting back with an example before the long weekend.

                    Comment


                      #11
                      Hello JerryWar,

                      Thank you for the reply.

                      In the sample code that I provided, the instance of the indicator is created with AddChartIndicator before OnBarUpdate() is called and therefore it is only the single instance of each. CPU load should not be a concern with this approach.

                      Please let us know if any additional questions come up.

                      Comment


                        #12
                        What you did with your example was to pass a Series to the SMA as the default Input Series which is a double. I need to pass either an int Series or one element from the series but there is a problem in doing so as I get an error telling me that an object reference is not set to an instance of an object. Does the method in your sample only work for a series double passed into the Default input and in which case I cannot create alternate series type to pass in ? essentially a trick to use the default input to get data in ?

                        Comment


                          #13
                          Hello JerryWar,

                          Thank you for the reply.

                          An input series will always be a Series<double>. The sample I provided in my earlier post would only be able to pass in an int series if the int series was added as a public property. If you wish to use an int as an overload for your indicator, you would need a public property in your indicator that's an int series. Indicators by default have 1 overload set which always includes an input series.

                          For example, let's say you had MyIndicator()
                          and MyIndicator(Series<double> input)

                          Any [NinjaScriptProperty] you create, would stack in the () overload and the (series<double>) overload sets.

                          If you have an int property, the indicator would now have:

                          MyIndicator(int myInt)

                          MyIndicator(Series<double> input, int myInt)

                          Please let us know if you have any further questions.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by RookieTrader, Today, 09:37 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post RookieTrader  
                          Started by KennyK, 05-29-2017, 02:02 AM
                          3 responses
                          1,282 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Started by AttiM, 02-14-2024, 05:20 PM
                          11 responses
                          184 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by fernandobr, Today, 09:11 AM
                          1 response
                          3 views
                          0 likes
                          Last Post NinjaTrader_Erick  
                          Started by timmbbo, Today, 08:59 AM
                          1 response
                          3 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Working...
                          X