Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtest of strategy generates no trades

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

    Backtest of strategy generates no trades

    Hi, I am new to ninjatrader and have ported a few strategies to ninjascript, however none of them generate any trades using the strategy analyzer, while the default strategies generate trades normally. This is the source code for one of the strategies that generates on trades:

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class Breakout : Strategy
    {
    protected override void OnBarUpdate()
    {
    double min = MIN(Low, 7)[1];
    double high = MAX(High, 7)[1];
    double sma = SMA(200)[0];
    double close = Close[0];
    if (close < min && close > sma)
    EnterLong();
    else if (Close[1] > high)
    EnterShort();

    }
    }
    }


    #2
    Hello Jamie K,

    Thank you for your post.

    To get a better understanding of why nothing happens when you test in the Strategy Analyzer, please answer all of the following questions:
    • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
    • Do you see similar results when running the same test on the SampleMaCrossOver strategy in NinjaTrader with the same settings as your strategy?
    • Who are you connected to? This is displayed in green in the lower-left corner of the Control Center window.
    • Are you connected to your data feed provider when running this test?
    • What instrument(s) (and expiry if applicable) have you selected?
    • What Data Series Type have you selected? Example: Tick, Minute, Day
    • What From and To date is selected?
    • If you open a chart for the same instrument(s) and the same date range, then right-click the chart and select 'Reload all historical data' is the historical data showing on the chart?
    • Is your strategy a multi-instrument or multi-time frame strategy?
    • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
    ​I look forward to your response.

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post
      Hello Jamie K,

      Thank you for your post.

      To get a better understanding of why nothing happens when you test in the Strategy Analyzer, please answer all of the following questions:
      • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
      • Do you see similar results when running the same test on the SampleMaCrossOver strategy in NinjaTrader with the same settings as your strategy?
      • Who are you connected to? This is displayed in green in the lower-left corner of the Control Center window.
      • Are you connected to your data feed provider when running this test?
      • What instrument(s) (and expiry if applicable) have you selected?
      • What Data Series Type have you selected? Example: Tick, Minute, Day
      • What From and To date is selected?
      • If you open a chart for the same instrument(s) and the same date range, then right-click the chart and select 'Reload all historical data' is the historical data showing on the chart?
      • Is your strategy a multi-instrument or multi-time frame strategy?
      • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
      ​I look forward to your response.
      1. Version 8.1.2.1 64-bit
      2. No, I don't see similar results for SampleMaCrossOver when backtesting, SampleMaCrossOver generates 132trades while my strategy generates 0 trades
      3. I am connected to simulation
      4. I am connected to simulation when running the backtest
      5. I selected $SP500, Day
      6. From 2010-01-01 to 2024-07-02
      7. If I select $SP500, Day, and select "reload all historical data", the data is showing on chart
      8. My strategy is not multi-instrument and not multi-time frame, please read the source code if you haven't
      9. When backtesting, I received the below error in the log tab of the control center,
      2024-07-03 9:34:20 AM Default Strategy 'CriticalTradingBreakout': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

      Comment


        #4
        Hello,

        Thank you for your response.

        Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart

        This message is indicating the specific index requested from a collection does not exist. Indexes must be a non-negative number and less than the size of the collection. The error may be indicating the index requested is larger than the number of elements in the collection.

        Code:
        // require BarsInProgress 0 to have 3 bars processed, require BarsInProgress 1 to have 4 bars processed
        if (CurrentBars[0] < 3 || CurrentBars[1] < 4)
        return;
        
        Print(Times[0][3]); // this does not cause an error as CurrentBars[0] is equal to or greater than 3
        
        Print(Times[1][4]); // this does not cause an error as CurrentBars[1] is equal to or greater than 4
        
        Print(Times[0][5]); // this line of code will cause an invalid index error. When CurrentBars[0] is 4 there is no bar 5 bars ago
        In the case of barsAgo values with Series, any barsAgo index must be less than CurrentBar (the total number of bars is the size of a Series collection).

        ​The help guide discusses ‘Make sure you have enough bars in the data series you are accessing’ .
        NinjaScript > Educational Resources > Tips > Make sure you have enough bars in the data series you are accessing


        Try adding a CurrentBar check to the top of OnBarUpdate() to ensure you have enough bars before trying to access any data.

        https://ninjatrader.com/support/help...currentbar.htm

        Comment


          #5
          Originally posted by NinjaTrader_Gaby View Post
          Hello Jamie K,

          Thank you for your post.

          To get a better understanding of why nothing happens when you test in the Strategy Analyzer, please answer all of the following questions:
          • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
          • Do you see similar results when running the same test on the SampleMaCrossOver strategy in NinjaTrader with the same settings as your strategy?
          • Who are you connected to? This is displayed in green in the lower-left corner of the Control Center window.
          • Are you connected to your data feed provider when running this test?
          • What instrument(s) (and expiry if applicable) have you selected?
          • What Data Series Type have you selected? Example: Tick, Minute, Day
          • What From and To date is selected?
          • If you open a chart for the same instrument(s) and the same date range, then right-click the chart and select 'Reload all historical data' is the historical data showing on the chart?
          • Is your strategy a multi-instrument or multi-time frame strategy?
          • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
          ​I look forward to your response.
          Thanks for pointing me to the log where I can see the errors, I am able to fix my strategies

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          31 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          124 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          64 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          41 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