Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

[FYI] Strategy results with executions but no trades

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

    [FYI] Strategy results with executions but no trades

    Hi,

    I've encountered the curious case where my strategy generated a report with summary, chart, orders, executions and all but the graphs and trades were empty.
    This happens when a global reference of SystemPerformance is kept in the strategy itself or an object that was created in Sate.SetDefaults, my guess would be that this blocks the trades from getting updated.
    Local variables should be fine.
    Last edited by MojoJojo; 07-14-2020, 09:11 PM.

    #2
    Hello MojoJojo, thanks for your post.

    Do you have an example you could share? Maybe I could suggest an alternative to correct this.

    I look forward to hearing from you.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello Chris,

      in my previous tests it was enough to assign SystemPerformance to a global variable.
      Since I can't compile an code at the moment, this is an untested example.

      Code:
          public class SampleMABracket : Strategy {
              private SMA smaFast;
              private SMA smaSlow;
              private SystemPerformance sysPerf;
      
              protected override void OnStateChange() {
                  if (State == State.SetDefaults) {
                      Description = "Sample MA Bracket";
                      Name = "SampleMABracket";
                      Fast = 10;
                      Slow = 25;
                      // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
                      // See the Help Guide for additional information
                      IsInstantiatedOnEachOptimizationIteration = false;
                      sysPerf = SystemPerformance; //only occurs when created here
                  } else if (State == State.DataLoaded) {
                      smaFast = SMA(Fast);
                      smaSlow = SMA(Slow);
                  }
              }
      
              protected override void OnBarUpdate() {
                  if (CurrentBar < BarsRequiredToTrade)
                      return;
      
                  if (CrossAbove(smaFast, smaSlow, 1)) {
                          EnterLong();
                  } else if (CrossBelow(smaFast, smaSlow, 1)) {
                          EnterShort();
                  }
              }
      
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
              public int Fast { get; set; }
      
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
              public int Slow { get; set; }
          }
      Of course this is not very useful a more relevant and already refactored code to not run into the issue looks like this:

      Code:
          public class NTMetrics {
              private bool isAll;
              private bool isLong;
              private Strategy strategy;
      
              public NTMetrics(Strategy strategy, bool isAll, bool isLong) {
                  this.strategy = strategy;
                  this.isAll = isAll;
                  this.isLong = isLong;
              }
      
              public TradesPerformance GetPerformance() {
                  if(isAll) {
                      return strategy.SystemPerformance.AllTrades.TradesPerformance;
                  } else if(isLong) {
                      return strategy.SystemPerformance.LongTrades.TradesPerformance;
                  } else {
                      return strategy.SystemPerformance.ShortTrades.TradesPerformance;
                  }
              }
              public double NetProfit { get { return GetPerformance().NetProfit; } }
          }
      It doesn't keep a direct reference but reads performance data dynamically.
      Maybe 1 in 100000 NT users will encounter this problem in the future and find this information helpful.
      It's simple to avoid, finding out why is the harder part.

      Edit: moved creation of reference to State.SetDefaults, otherwise trades are logged normally
      Last edited by MojoJojo; 07-14-2020, 09:13 PM.

      Comment


        #4
        Hi MojoJojo, thanks for the reply.

        I do not have access to the underlying code, but I would have to assume this happens because of the entire Strategy.SystemPerformance object is deleted and created again, or given a new reference in some way that we don't see through NinjaScript.

        Thanks again for sharing.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris,

          it turns out that the secret ingredient is creating the reference in State.SetDefaults for trades not to be logged. So it's quite an extraordinary combinations of factors and can be safely ignored.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by zacharydw00, Today, 05:14 PM
          0 responses
          7 views
          0 likes
          Last Post zacharydw00  
          Started by shahabjet1, Today, 04:47 PM
          0 responses
          13 views
          0 likes
          Last Post shahabjet1  
          Started by aligator, Today, 04:32 PM
          0 responses
          11 views
          0 likes
          Last Post aligator  
          Started by timko, Today, 04:02 PM
          0 responses
          10 views
          0 likes
          Last Post timko
          by timko
           
          Started by asmmbillah, Today, 03:37 PM
          2 responses
          18 views
          0 likes
          Last Post asmmbillah  
          Working...
          X