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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      45 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      21 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      31 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      50 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X