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 fx.practic, 10-15-2013, 12:53 AM
      5 responses
      5,404 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by Shai Samuel, 07-02-2022, 02:46 PM
      4 responses
      95 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by DJ888, Yesterday, 10:57 PM
      0 responses
      8 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by MacDad, 02-25-2024, 11:48 PM
      7 responses
      159 views
      0 likes
      Last Post loganjarosz123  
      Started by Belfortbucks, Yesterday, 09:29 PM
      0 responses
      8 views
      0 likes
      Last Post Belfortbucks  
      Working...
      X