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

[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 bmartz, 03-12-2024, 06:12 AM
          4 responses
          32 views
          0 likes
          Last Post bmartz
          by bmartz
           
          Started by Aviram Y, Today, 05:29 AM
          4 responses
          12 views
          0 likes
          Last Post Aviram Y  
          Started by algospoke, 04-17-2024, 06:40 PM
          3 responses
          28 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by gentlebenthebear, Today, 01:30 AM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by cls71, Today, 04:45 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X