Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting the last bars of two previous closed sessions

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

    Getting the last bars of two previous closed sessions

    I'm writing an indicator that needs to reference the last bar of the two closed sessions preceding the current one. Let's have three arbitrary sessions, A, B, and C, defined in a TradingHours Template. For each candle in session C, I need to access the last bar of sessions B and A, respectively. I've used the Time value, but it soon gets challenging because of holidays and other calendar-related quirks.

    Is there any other way I don't know for getting a sure reference to those two bars?​

    #2
    Hello Gianpiero,

    Because you always start at bar 0 in processing you can just use a couple of variables for this type of use case. As the script processes it will eventually process the first bar of a session. At that point you store the priorSessionValue variable to the priorPriorSessionValue to shift the value. At the same time you get the previous session price and store that to the priorSessionValue. For each new session this is repeated shifting the newly found value to priorSessionValue and then priorSessionValue to priorPriorSessionValue.

    Here is a small example of how you would do that. The easiest way to see how this works is to enable the session break lines on the chart and use a large timeframe like 60 minute bars. You can then plot the results so you can compare the plot values to previous session close prices.

    Code:
    private double priorPriorSessionValue;
    private double priorSessionValue;
    protected override void OnBarUpdate()
    {
        if(CurrentBar == 0)
        {
            priorSessionValue = Close[0];
            priorPriorSessionValue = Close[0];
        }
    
        if(CurrentBar < 1) return;
    
        if(Bars.IsFirstBarOfSession)
        {
            priorPriorSessionValue = priorSessionValue;
            priorSessionValue = Close[1];
        }
    
        Values[0][0] = priorSessionValue;
        Values[1][0] = priorPriorSessionValue;
    
    }

    Comment


      #3
      Thank you, Jesse, you helped me a lot!

      Comment


        #4
        Hi Jesse!

        Another bit of help. How can each bar know in which session it sits in? I still don't get how to reference TradingHours templates. I can print them looping through the table, but I don't know how to access each row value to store them somwhere. I've had a view at the methods of TradingHoursSession, and I saw a 'to-array<>' or something like that, but I don't know how to use it and how to structure the code around it.

        Comment


          #5
          Hello Gianpiero,

          In the sample i provided you would know which session the data was from based on the name of the variable. The script always executes in the same order so you can shift the value of each variable to continuously have a set variable for each session.

          You can know which session a bar is in by using the IsFirstBarOfSession property during processing. If the bar is the first bar of the session then any bar after that is in the same session, any bar before that is in the previous session.

          The trading hours templates would only give you the hours defined within the trading hours template but wouldn't let you know if a specific bar is within a specific session, those hours could exist on any day.




          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          331 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          549 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          550 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X