Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to pass double from strategy to indicator?

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

    How to pass double from strategy to indicator?

    I'm wanting to display cumulative profits as an indicator. How would I do this? Set a public property on the indicator class?

    Many Thanks!

    #2
    Hello Skechers,

    You can see the sample in the link below which uses an indicator to plot strategy values.

    Comment


      #3
      Many Thanks, Jesse.

      Comment


        #4
        Read through the code but am not following it quite:
        1. How do I declare the variable in the strategy code?
        2. How do I declare the strategy method in the indicator code?
        3. How do I get the value of Home from #2?
        Many Thanks!

        Comment


          #5
          I worked through it a little, but the retrieved variable value is always 0 (zero).

          Code in strategy (where shared variable is stored):
          Code:
          namespace NinjaTrader.NinjaScript.Strategies
          {
              public partial class MySharedVariables: NinjaTrader.NinjaScript.AddOnBase
              {
                  public static double CumulativeProfit
                  { get; set; }
              }
          }
          Code in Indicator OnBarUpdate to assign value to shared variable:
          Code:
          NinjaTrader.NinjaScript.Strategies.MySharedVariables.CumulativeProfit = DayNetProfit;
          Code in indicator OnBarUpdate to retrieve the value of shared variable
          Code:
                  protected override void OnBarUpdate()
                  {
                      CumulativeProfitPlot[0] = NinjaTrader.NinjaScript.Strategies.MySharedVariables.CumulativeProfit;
                      Print("Cumulative Profit (from Indicator) " + CumulativeProfitPlot[0]); // This is always 0 (zero)
                  }
          ​

          Comment


            #6
            Hello Skechers,

            making a double requires extra logic for that to work, you can instead just use the sample exactly as it is to pass values. If you need to pass more than one value you would duplicate the logic in that sample.

            The strategy exposes a Series<double> which does not require extra logic to expose to the indicator. The indicator reads the 0 bars ago value of the series and plots it.



            Comment


              #7
              The sample code retrieves a variable from the indicator. I'm wanting to do the reverse. I'm just not understanding the code I guess to transform it to what I need.

              Comment


                #8
                Hello Skechers,

                It looks like you have misunderstood the sample, the sample code does not retrieve any values from the indicator. It does the reverse like you are asking.

                Please look at the indicators that come in the sample and see how they are using a reference to the strategy to get values from the strategy.

                This is the code which retrieves the value from the strategy and has the indicator plot it:

                Code:
                Plot[0] = Strategy.OverlayPlot.GetValueAt(CurrentBar);

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CarlTrading, 03-31-2026, 09:41 PM
                1 response
                43 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by CarlTrading, 04-01-2026, 02:41 AM
                0 responses
                20 views
                0 likes
                Last Post CarlTrading  
                Started by CaptainJack, 03-31-2026, 11:44 PM
                0 responses
                30 views
                1 like
                Last Post CaptainJack  
                Started by CarlTrading, 03-30-2026, 11:51 AM
                0 responses
                47 views
                0 likes
                Last Post CarlTrading  
                Started by CarlTrading, 03-30-2026, 11:48 AM
                0 responses
                38 views
                0 likes
                Last Post CarlTrading  
                Working...
                X