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 NullPointStrategies, Today, 05:17 AM
                0 responses
                48 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                126 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                66 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                42 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X