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

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;
    
    }
    JesseNinjaTrader Customer Service

    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.




          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by DavidHP, Today, 07:56 AM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by kujista, Today, 06:23 AM
          3 responses
          7 views
          0 likes
          Last Post kujista
          by kujista
           
          Started by Mindset, Yesterday, 02:04 AM
          2 responses
          18 views
          0 likes
          Last Post NinjaTrader_RyanS  
          Started by f.saeidi, Today, 08:03 AM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by samish18, 04-17-2024, 08:57 AM
          15 responses
          53 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Working...
          X