Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy that analyzes @ end of bar, but also does intra-bar calculations

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

    Strategy that analyzes @ end of bar, but also does intra-bar calculations

    I'm working on a strategy that is going to analyze in 1-2 minute bars (haven't decided which yet) and also perform simpe calculations on every price tick inside those bars. The logic will flow something like this ->

    1. Monitor price on 1 minute intervals
    2. When the close of a minute indicates an investment opportunity in the next 60 seconds,
    3. 'Zoom-in' and focus on tick-by-tick changes to select the best time to take a position

    Now, I found the SampleEnterOnceExitExitEveryTick in the Reference Samples section, which is similar to what I'll be doing but opposite. Instead of taking a position at the close of a bar and looking for the best time to exit, I want to look for the best time to take a position in a bar and exit at the end of it. I think converting the code for my use won't be too difficult, but I do have some questions about what some of it means.

    PHP Code:
    if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Flat) 
    
    I understand this is checking for the first tick of each bar, but what does the second part of this if statement check for?

    Also, although it says FirstTickOfBar, it actually means the last tick of the previous bar, right? That way the calculation you run in that if statement would apply run on the bar that just closed.

    #2
    Originally posted by milemke08 View Post
    PHP Code:
    if (FirstTickOfBar && Position.MarketPosition == MarketPosition.Flat) 
    
    I understand this is checking for the first tick of each bar, but what does the second part of this if statement check for?

    Also, although it says FirstTickOfBar, it actually means the last tick of the previous bar, right? That way the calculation you run in that if statement would apply run on the bar that just closed.
    The second half of that expression checks the strategy position. Position is detailed here:


    FirstTickOfBar and closing of previous bar are same event, but reference is on the new bar, such that [0] refers to the just-opened bar and [1] is the just-closed bar. This property only has relevance in real time trading. It's true for every historical (backtest) bar.

    Regarding your multiseries strategy, I would have a look at the bar referencing model to get an idea how different bar series inter-relate. This is detailed in the following link (how bar data is referenced):


    The main things to keep in mind about this is that bars are referenced differently in real time compared to historical, and our historical referencing is conservative. You cannot use lower time frame bars to "peek" into the future when accessing their values from higher tame frames.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      I haven't started writing it just yet, I'm still working out the pseudo-code and logic right now but from what I just read on what you posted I think the easiest route would be to have 2 different bar series in my strategy. One that is a 1 min interval and the other that is a tick-by-tick interval. This way I can monitor the 1 minute interval series and when it fits into my investment parameters I can switch to looking at the tick-by-tick to find the best time to buy in.

      Comment


        #4
        in y'alls sample SampleMultiTimeFrameOrders :

        PHP Code:
        protected override void Initialize()
                {
                    // Add a 5 minute Bars object to the strategy
                    Add(PeriodType.Minute, 5);
                    
                    // Add a 15 minute Bars object to the strategy
                    Add(PeriodType.Minute, 15);
                    
                    CalculateOnBarClose = true;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                    /* OnBarUpdate() method will execute this portion of the code when incoming ticks are on the
                    secondary bar object. */
                    if (BarsInProgress == 1)
                    {
                        /* Checks if the 5 period SMA is decreasing in the secondary bar series (5min) and if it is below the 10
                        period SMA in the tertiary bar series (15min). */
                        if (SMA(5)[0] < SMA(5)[1] && SMA(5)[0] < SMA(BarsArray[2], 10)[0])
                        {
                            /* Exit the long position entered from the 15min bar object on a more granular time period.
                            This allows for more control in the management of your positions and can be used to improve
                            exit timing of your trades. */
                            ExitLong("Exit Long from 5min", "Enter Long from 15min");
                        }
                    }
                    
                    /* OnBarUpdate() method will execute this portion of the code when incoming ticks are on the
                    tertiary bar object (15min). */
                    if (BarsInProgress == 2)
                    {
                        // Checks if the 25 period SMA is greater than the 50 period SMA on the 15min.
                        if (SMA(25)[0] > SMA(50)[0])
                        {
                            /* Enter long for 1 contract on the 15min bar object based on the barsInProgress parameter.
                            A value of 0=primary bars, 1=secondary bars, 2=tertiary bars */
                            EnterLong(2, 1, "Enter Long from 15min");
                        }
                    } 
        
        how does BarsInProgress indicate which time interval to perform what calculations on? Like, how does BarsInProgress == 1 mean the 5 minute interval and BarsInProgress == 2 mean the 15 min one?

        Comment


          #5
          It's in order.

          See Add() here.



          I usually declare public static int variables, something like this:

          Code:
          public static int ES_5MIN = 1; 
                  public static int ES_15MIN = 2;
          so
          Code:
                  if (BarsInProgress == 1)
          becomes
          Code:
                  if (BarsInProgress == ES_5MIN)
          Originally posted by milemke08 View Post
          in y'alls sample SampleMultiTimeFrameOrders :

          PHP Code:
          protected override void Initialize()
                  {
                      // Add a 5 minute Bars object to the strategy
                      Add(PeriodType.Minute, 5);
                      
                      // Add a 15 minute Bars object to the strategy
                      Add(PeriodType.Minute, 15);
                      
                      CalculateOnBarClose = true;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                      /* OnBarUpdate() method will execute this portion of the code when incoming ticks are on the
                      secondary bar object. */
                      if (BarsInProgress == 1)
                      {
                          /* Checks if the 5 period SMA is decreasing in the secondary bar series (5min) and if it is below the 10
                          period SMA in the tertiary bar series (15min). */
                          if (SMA(5)[0] < SMA(5)[1] && SMA(5)[0] < SMA(BarsArray[2], 10)[0])
                          {
                              /* Exit the long position entered from the 15min bar object on a more granular time period.
                              This allows for more control in the management of your positions and can be used to improve
                              exit timing of your trades. */
                              ExitLong("Exit Long from 5min", "Enter Long from 15min");
                          }
                      }
                      
                      /* OnBarUpdate() method will execute this portion of the code when incoming ticks are on the
                      tertiary bar object (15min). */
                      if (BarsInProgress == 2)
                      {
                          // Checks if the 25 period SMA is greater than the 50 period SMA on the 15min.
                          if (SMA(25)[0] > SMA(50)[0])
                          {
                              /* Enter long for 1 contract on the 15min bar object based on the barsInProgress parameter.
                              A value of 0=primary bars, 1=secondary bars, 2=tertiary bars */
                              EnterLong(2, 1, "Enter Long from 15min");
                          }
                      } 
          
          how does BarsInProgress indicate which time interval to perform what calculations on? Like, how does BarsInProgress == 1 mean the 5 minute interval and BarsInProgress == 2 mean the 15 min one?

          Comment


            #6
            The primary series is always BarsInProgress == 0. This is the series the script is applied to. As Sledge writes, each additional series is assigned BIP value according to the order of Add() methods within Initialize().
            Ryan M.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            666 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            376 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            110 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            575 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            580 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X