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

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 Klaus Hengher, Yesterday, 03:13 AM
      2 responses
      15 views
      0 likes
      Last Post Klaus Hengher  
      Started by Sebastian - TwinPeaks, Yesterday, 01:31 PM
      2 responses
      13 views
      0 likes
      Last Post Sebastian - TwinPeaks  
      Started by wbennettjr, 07-15-2017, 05:07 PM
      16 responses
      2,530 views
      1 like
      Last Post eladlevi  
      Started by Human#102, Yesterday, 09:54 AM
      2 responses
      8 views
      0 likes
      Last Post Human#102  
      Started by Patlpp, 08-16-2021, 03:10 PM
      10 responses
      498 views
      0 likes
      Last Post Joerg
      by Joerg
       
      Working...
      X