Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Encode the synchronized start of multiple time frames.

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

    Encode the synchronized start of multiple time frames.

    Good afternoon,

    I'm trying to program a strategy that, on a 1-second chart, sends a buy order when the 1-second, 1-minute, and up to 300-minute bars start within the same second.
    To do this, I used the following code:
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Second, 1);
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    AddDataSeries(Data.BarsPeriodType.Minute, 2);
    AddDataSeries(Data.BarsPeriodType.Minute, 3);
    AddDataSeries(Data.BarsPeriodType.Minute, 4);
    .
    .
    .
    AddDataSeries(Data.BarsPeriodType.Minute, 300);
    }
    protected override void OnBarUpdate()
    {
    if(CurrentBars[300] < 2)
    {return;}
    if(BarsInProgress == 0 && IsFirstTickOfBar)
    {
    EndBar1sec = true;
    }
    if(BarsInProgress == 1 && IsFirstTickOfBar)
    {
    EndBar1min = true;
    }
    .
    .
    .

    if(BarsInProgress == 300 && IsFirstTickOfBar)
    {
    EndBar300min = true;
    }
    if(EndBar1sec && BarEnd1min)
    {
    if(EndBar1sec && EndBar2min)
    {
    .
    .
    .
    if(EndBar1sec && EndBar300min)
    {
    EnterLong();
    EndBar1sec = false;
    EndBar1min = false;
    .
    .
    .
    EndBar300min = false;
    }
    EndBar2min = false;
    }
    EndBar1min = false;
    EndBar1sec = false;
    }


    But I find that, when nesting the 300 If conditions, there must be a limit because they aren't being collected correctly and the program is behaving quite erratically.
    Is there a limit to nesting If conditions in NinjaTrader?
    If so, what alternative do I have to program the simultaneous start condition for 1 second, 1 minute, etc. Up to 300 minutes in minute intervals (1, 2, 3,..., 298, 299, and 300)?
    Thanks in advance.​

    #2
    Originally posted by BIOK.NT View Post
    Good afternoon,

    I'm trying to program a strategy that, on a 1-second chart, sends a buy order when the 1-second, 1-minute, and up to 300-minute bars start within the same second.
    To do this, I used the following code:
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Second, 1);
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    AddDataSeries(Data.BarsPeriodType.Minute, 2);
    AddDataSeries(Data.BarsPeriodType.Minute, 3);
    AddDataSeries(Data.BarsPeriodType.Minute, 4);
    .
    .
    .
    AddDataSeries(Data.BarsPeriodType.Minute, 300);
    }
    protected override void OnBarUpdate()
    {
    if(CurrentBars[300] < 2)
    {return;}
    if(BarsInProgress == 0 && IsFirstTickOfBar)
    {
    EndBar1sec = true;
    }
    if(BarsInProgress == 1 && IsFirstTickOfBar)
    {
    EndBar1min = true;
    }
    .
    .
    .

    if(BarsInProgress == 300 && IsFirstTickOfBar)
    {
    EndBar300min = true;
    }
    if(EndBar1sec && BarEnd1min)
    {
    if(EndBar1sec && EndBar2min)
    {
    .
    .
    .
    if(EndBar1sec && EndBar300min)
    {
    EnterLong();
    EndBar1sec = false;
    EndBar1min = false;
    .
    .
    .
    EndBar300min = false;
    }
    EndBar2min = false;
    }
    EndBar1min = false;
    EndBar1sec = false;
    }


    But I find that, when nesting the 300 If conditions, there must be a limit because they aren't being collected correctly and the program is behaving quite erratically.
    Is there a limit to nesting If conditions in NinjaTrader?
    If so, what alternative do I have to program the simultaneous start condition for 1 second, 1 minute, etc. Up to 300 minutes in minute intervals (1, 2, 3,..., 298, 299, and 300)?
    Thanks in advance.​
    sooo when you use the addDateSeries() like this you have to remind that barsInprogress 0 is the default time frame. :

    AddDataSeries(Data.BarsPeriodType.Second, 1); //barsInprogress == 1
    AddDataSeries(Data.BarsPeriodType.Minute, 1); //barsInprogress == 2

    AddDataSeries(Data.BarsPeriodType.Minute, 2); //barsInprogress == 3
    AddDataSeries(Data.BarsPeriodType.Minute, 3);//barsInprogress == 4
    AddDataSeries(Data.BarsPeriodType.Minute, 4);//barsInprogress == 5

    What you could do is to store a date time object for each of these and check in your barsinprogress == 0 if all the 5 date times.seconds are the same. Just store this in an array of datetime. then in the barsinprgoress == 0 loop over the datetimes and see if they are equal for the if condition. Should be very little code.

    P.S. Also, just create a forloop for the AddDataSeries.

    psudeo code:

    for i in range 300
    addDataSeries(
    Data.BarsPeriodType.Minute, i);​​
    Last edited by mw_futures; 04-10-2025, 06:11 AM.

    Comment


      #3
      Hello mw_futures!

      Thank you very much for your contribution.
      Could you write me a code example of how to traverse the arrays for two time frames and how to structure it?​

      Comment


        #4
        Hello BIOK.NT,

        You can just use the built in Times series to access the time of each series from BarsInProgress 0

        if(BarsInProgress == 0)
        {
        Print(Times[1][0] ) ; // the 1 is the bars in progress of the series you wanted the time from
        }

        Regarding using looping, we do not suggest using any type of logic to variably control AddDataSeries as it may not work in all tools. What you have now with the 5 AddDataSeries statements is the way that would be suggested to add secondary series.

        Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        116 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        61 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        40 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        43 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        82 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X