Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Checking if there's enough data to start a strategy

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

    Checking if there's enough data to start a strategy

    Hello,

    I'm working on a strategy that will detect a gap on the S&P500 index, then enter a trade depending on the gap direction. When I'm backtesting with downloaded Market Replay data, it seems to work fine. However, when I apply the strategy to a live chart it stops at one of my debug print statements. See the code below.

    Is there something I need to change when checking if there's enough data? Or do I even need that line?

    Thanks,
    MJ


    Code:
    protected override void OnStateChange()
    {
    if (State == State.Historical)
    {
    }
    else if (State == State.Configure)
    {
    // Add data series...
    AddDataSeries("MES 06-24", BarsPeriodType.Second, 3);
    AddDataSeries("^SP500", BarsPeriodType.Minute, 5);
    }
    }
    
    protected override void OnBarUpdate()
    {
    // Check for the S&P 500 series to perform gap analysis...
    if (BarsInProgress == 2)
    
    // Ensure there's enough data
    if (CurrentBars[0] < 1 || CurrentBars[2] < 1)
    Print("Not enough data to analyze.")
    return; // Not enough data to analyze.







    #2
    Hello devatechnologies,

    There are no curly braces after the if condition so the next command is a one line action block.

    if (CurrentBars[0] < 1 || CurrentBars[2] < 1) // no curly braces so next command is a one line action block
    Print("Not enough data to analyze."); // this line is a one line action block that will only print if the condition is true
    return; // Not enough data to analyze. // this is not within the action block of the if statement and will always return

    Use curly braces for the action block if you want the action to only occur if the condition is true.

    if (CurrentBars[0] < 1 || CurrentBars[2] < 1)
    {
    Print("Not enough data to analyze.");
    return; // Not enough data to analyze.
    }​


    This would fall under general C# education which is pre-requisite to coding NinjaScripts.
    https://support.ninjatrader.com/s/ar...th-NinjaScript
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Aha. Thanks for the quick reply.

      It had previously been a one-liner
      Code:
      if (CurrentBars[0] < 1 || CurrentBars[2] < 1) return;
      Which I broke when I added the print statement.

      I'll try it on live data tomorrow.

      -MJ

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      50 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
      69 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