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

Two account`s, two chart`s, same strategy same SystemPerformance results

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

    Two account`s, two chart`s, same strategy same SystemPerformance results

    Hi, I have the following problem:
    same strategy (different id of the strategy) on two different charts and different accounts giving the same SystemPerformance results each time.
    how can i use these two different strategies and their values separately?

    for each chart and account separately (CumProfit = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit)

    Click image for larger version  Name:	Screenshot_3.jpg Views:	0 Size:	325.8 KB ID:	1146028Click image for larger version  Name:	Screenshot_4.jpg Views:	0 Size:	325.9 KB ID:	1146029
    Last edited by sidlercom80; 03-11-2021, 09:16 AM.
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    #2
    Hello sidlercom80,

    Thank you for your post.

    I note there are two different lines in that panel, one just labeled P&L and one labeled P&L Intraday. On the left chart both are visible but on the right only P&L intraday is visible and giving the same value as the PnL line on the second chart, so the results aren't quite the same between the two charts.

    Can you provide a small code sample of how you're plotting both of those two lines?

    Thanks in advance; I look forward to assisting you further.

    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi _Kate, it's not directly about the plot, they are just showing. I load a strategy A. in chart A. and one ( the same strategy but different ID) in another chart B.
      so as follows::

      Chart A. Acoount A. with Strategy ID=226934909 (should be CumProfit A. = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit)
      Chart B. Account B. with Strategy ID=226934910 (should be CumProfit B. = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit)

      If the same strategy is loaded twice, each time with a different ID (it is then two different strategies) on a different account, the system performance values should also only be calculated on the associated account and not totally on both accounts
      sidlercom80
      NinjaTrader Ecosystem Vendor - Sidi Trading

      Comment


        #4
        Hi, _Kate I know what you mean now I just don't know the solution yet, do you have an idea?
        the line is drawn in an indicator, the value for the line comes from the strategy. the indicator is loaded with the strategy.

        Code in Strategy:
        Code:
        //Add P&L Panel Plot Indicator
        sidiStrategyPerformancePlot = SidiStrategyPerformancePlot();
        
        if (sidiStrategyPerformancePlot != null)
        {
           sidiStrategyPerformancePlot.Strategy = this; // Create an indicator which is set for a panel plot. The indicator will use this strategies references PanelPlot series to plot.    
           AddChartIndicator(sidiStrategyPerformancePlot);
        }
        Code in Indicator:
        Code:
        protected override void OnBarUpdate()
        {
        //Grab the Panel Plot value for the current bar and plot it.
        double cumProfit = Strategy.CumProfitPanelPlot.GetValueAt(CurrentBar) ;
        
        if (cumProfit != 0)
        priorCumProfit = cumProfit;
        
        
        CumProfitPlot[0] = cumProfit != 0 ? cumProfit : priorCumProfit;
         }

        the idea is from Sample: Plotting from within a NinjaScript Strategy

        https://ninjatrader.com/support/foru...trategy?t=6651
        Last edited by sidlercom80; 03-11-2021, 10:29 AM.
        sidlercom80
        NinjaTrader Ecosystem Vendor - Sidi Trading

        Comment


          #5
          after further testing: it looks as if the indicator that is loaded from the strategy and is supposed to apply only to this strategy is used for all subsequent strategies, although the indicator for all subsequent strategies are loaded separately. i don't understand that
          sidlercom80
          NinjaTrader Ecosystem Vendor - Sidi Trading

          Comment


            #6
            Originally posted by sidlercom80 View Post
            after further testing: it looks as if the indicator that is loaded from the strategy and is supposed to apply only to this strategy is used for all subsequent strategies, although the indicator for all subsequent strategies are loaded separately. i don't understand that
            Sounds like the auto-generated code at the bottom of the Indicator is forcing the
            reuse of a cached indicator instance.

            Update the indicator to accept a throwaway argument which can be made completely unique.

            Then, pass a completely unique argument into the indicator from the strategy.

            Comment


              #7
              Hi bltdavid, thank you for your tip, unfortunately i don't know exactly how to do it. do you have another tip, or an example?

              I think you mean here somewhere:
              Code:
              public partial class Indicator : Gui.NinjaScript.IndicatorRenderBase
              {
              private MyIndicators.SidiStrategyPerformancePlot[] cacheSidiStrategyPerformancePlot;
              public MyIndicators.SidiStrategyPerformancePlot SidiStrategyPerformancePlot()
              {
              return SidiStrategyPerformancePlot(Input);
              }
              
              public MyIndicators.SidiStrategyPerformancePlot SidiStrategyPerformancePlot(ISeries<double> input)
              {
              if (cacheSidiStrategyPerformancePlot != null)
              for (int idx = 0; idx < cacheSidiStrategyPerformancePlot.Length; idx++)
              if (cacheSidiStrategyPerformancePlot[idx] != null && cacheSidiStrategyPerformancePlot[idx].EqualsInput(input))
              return cacheSidiStrategyPerformancePlot[idx];
              return CacheIndicator(new MyIndicators.SidiStrategyPerformancePlot(), input, ref cacheSidiStrategyPerformancePlot);
              }
              }
              Last edited by sidlercom80; 03-11-2021, 11:33 AM.
              sidlercom80
              NinjaTrader Ecosystem Vendor - Sidi Trading

              Comment


                #8
                Hello sidlercom80,

                Thanks for your replies.

                I believe what bltdavid is saying is to add a parameter to the indicator that it doesn't really use for calculations but that you can use a unique value for so the indicator isn't being called with exactly the same values from each strategy. So, say, add a property called "StrategyID" to the indicator and then you can use GetAtmStrategyUniqueId to generate a unique value (even though you're not using it with ATM orders, it just generates a unique value so you can use that) and pass that unique value to the to the indicator as StrategyID so it's not caching the same indicator instance for each strategy.



                I'd give that a try and see what results you get, because it does seem like values are coming from a cached version of the indicator.

                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Hi _Kate, thank you for your answer! i know roughly what you mean, but unfortunately not how i should implement it exactly. is there no example?
                  sidlercom80
                  NinjaTrader Ecosystem Vendor - Sidi Trading

                  Comment


                    #10
                    Hello sidlercom80,

                    Thank you for your reply.

                    I found that GetAtmStrategyUniqueId() only works in State.RealTime, so in lieu of that since each strategy instance already has a unique id assigned to that we can use that as the unique value we pass to the indicator. I've worked up a simple example that's just a revised version of SampleStrategyPlot that's attached.

                    Please let us know if we may be of further assistance to you.
                    Attached Files
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Kate View Post
                      I believe what bltdavid is saying is to add a parameter to the indicator that it doesn't really use for calculations but that you can use a unique value for so the indicator isn't being called with exactly the same values from each strategy. So, say, add a property called "StrategyID" ...
                      Exactly.

                      The idea is the property must become an argument when calling the indicator.

                      So, just add a new public NinjaScriptProperty to the indicator, name doesn't matter, I usually use
                      strings for these properties.

                      NinjaTrader's automatic code generation converts all public properties with the NinjaScriptProperty
                      attribute into arguments that are passed to the indicator. This is the critical secret sauce to making
                      this work -- your goal is to add a new argument to your indicator. It's considered a "throwaway" or
                      dummy argument because you don't care about or use the value -- you need this argument because
                      it's mere presence is what helps you subvert the code caching of indicator instances -- that is, this
                      technique is how you turn that caching off.

                      Ok, so far so good.
                      You're almost done -- that's it for the indicator -- now for calling it from the strategy.

                      Obviously, when calling the indicator from your Strategy, you are forced to account for this new
                      string argument. Well, just make the string value unique. Using GUID is one way to do that.

                      A unique string value in the argument list should thwart the internal NinjaTrader caching code that
                      seeks to reuse previously saved indicator instances.

                      Fire up NT7 and study the NinjaTrader automatically generated code at the bottom of each
                      indicator -- NT8 is doing something very similar but the code is more internalized.
                      Last edited by bltdavid; 03-11-2021, 03:36 PM. Reason: fix typos

                      Comment


                        #12
                        Hi _Kate, hi bltdavid, thank you very much for your help! i have now implemented it as _Kate has shown in the example, unfortunately without success.
                        As can be seen in the picture, the two charts have different strategies, but the same values are used alternately for both strategies.

                        Click image for larger version  Name:	Screenshot_2.jpg Views:	0 Size:	376.0 KB ID:	1146245Click image for larger version  Name:	Screenshot_3.jpg Views:	0 Size:	413.5 KB ID:	1146246

                        Strategy:
                        Code:
                        protected override void OnStateChange()
                        {
                        ...
                           else if (State == State.Configure)
                             {
                               string strategyID = this.Id.ToString();
                               sidiStrategyPerformancePlot = SidiStrategyPerformancePlot(strategyID);
                        
                                  if (sidiStrategyPerformancePlot != null)
                                     {
                                        sidiStrategyPerformancePlot.Strategy = this;
                                        AddChartIndicator(sidiStrategyPerformancePlot);
                                      }
                              }
                        }
                        
                        protected override void OnBarUpdate()
                        {
                           CumProfit = SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit
                           CumProfitPanelPlot[0] = CumProfit;
                           double dummy = sidiStrategyPerformancePlot != null ? sidiStrategyPerformancePlot[0] : 0;
                        }

                        Indicator:
                        Click image for larger version  Name:	Screenshot_5.jpg Views:	0 Size:	158.8 KB ID:	1146247

                        I have also made a short video:
                        video, sharing, camera phone, video phone, free, upload


                        do you have any other ideas?
                        Last edited by sidlercom80; 03-12-2021, 05:08 AM.
                        sidlercom80
                        NinjaTrader Ecosystem Vendor - Sidi Trading

                        Comment


                          #13
                          I think I have found the problem. I use a public property for the value to save it:

                          Code:
                            [XmlIgnore, Browsable(false)]
                            public double CumProfit
                                  {
                                      get { return cumProfit; }
                                      private set
                                      {
                                          if (value != cumProfit)
                                          {
                                              cumProfit = value;
                                              NotifyPropertyChanged("CumProfit");
                                              NotifyPropertyChanged("CumProfitString");
                                          }
                                      }
                                  }
                          Code:
                          CumProfitPanelPlot[0] = CumProfit;
                          If I do this directly without a public property, it works:
                          Code:
                          CumProfitPanelPlot[0] = SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit;
                          but that would mean that i can only use the strategy once at a time, which is not the solution! does anyone have an idea how i can use all the public properties in the strategy one at a time per strategy? the variables and values in strategy A are to be used only for strategy A. and those for strategy B. only for strategy B. a separate instance for one strategy each. i thought all values saved in a strategy are only used for this strategy and if another strategy is opened, the values are also newly created.
                          Last edited by sidlercom80; 03-12-2021, 06:00 AM.
                          sidlercom80
                          NinjaTrader Ecosystem Vendor - Sidi Trading

                          Comment


                            #14
                            Problem solved! I'm an idiot I forgot that i had declared a static variable, removed "static", now it works. thanks again for your patience and help, i have learned a lot again

                            Code:
                            private static double cumProfit = 0;
                            sidlercom80
                            NinjaTrader Ecosystem Vendor - Sidi Trading

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by rocketman7, Today, 02:12 AM
                            3 responses
                            19 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by trilliantrader, 04-18-2024, 08:16 AM
                            7 responses
                            27 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by samish18, 04-17-2024, 08:57 AM
                            17 responses
                            64 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by briansaul, Today, 05:31 AM
                            1 response
                            13 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by PaulMohn, Today, 03:49 AM
                            1 response
                            12 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Working...
                            X