Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limit number of bars to retrieve on a chart

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

    Limit number of bars to retrieve on a chart

    I have an opening range indicator I am developing and need to restrict it to the current day. If I put it on a 5 day chart it computes all 5 day opening ranges and uses lots of computing power. What do I need to put in my code to limit that to just the current day regardless if I have a 5, 10 day chart ? When I limit the data to a one day chart all is good.

    #2
    Hi set2win, thanks for posting.

    We have the SessionIterator class for this purpose:


    On the page, you will find a short example. On the first bar of the session calculate the trading session. If the session is today's session you can begin to process the indicator, otherwise return from OnBarUpdate.

    Kind regards,
    -ChrisL

    Comment


      #3
      So in my code how do I qualify today session? DateTime tradingDay = sessionIterator.ActualTradingDayExchange; if tradingDay == ???

      Comment


        #4
        I can't get it to not calculate other days in the chart, If I give it a 5 day chart it sits there calculating for a long time, if one day chart its fast and what I want


        Here is code:

        protected override void OnBarUpdate()
        {
        DateTime dt = Time[0];

        if (Bars.IsFirstBarOfSession)
        {
        sessionIterator.GetNextSession(Time[0], true);
        // store the desired session information
        tradingDay = sessionIterator.ActualTradingDayExchange;
        DateTime beginTime = sessionIterator.ActualSessionBegin;
        DateTime endTime = sessionIterator.ActualSessionEnd;

        dtStartTime = new DateTime (dt.Year, dt.Month, dt.Day, dtStartTime.Hour, dtStartTime.Minute, dtStartTime.Second);
        dtEndTime = new DateTime (dt.Year, dt.Month, dt.Day, dtEndTime.Hour, dtEndTime.Minute, dtEndTime.Second);
        }

        if (CurrentBars[0] < 10)
        return;


        if (BarsPeriod.BarsPeriodType == BarsPeriodType.Minute && sessionIterator.IsInSession(DateTime.Now, true, true) )
        {


        if( BarsPeriod.Value == 1)
        {
        if (ToTime(Time[0]) < ToTime(dtStartTime))
        {
        dHighestPrice = 0.0;
        dLowestPrice = 0.0;
        }
        else if (ToTime(Time[0]) == ToTime(dtStartTime))
        {
        dHighestPrice = High[0];
        dLowestPrice = Low[0];
        dMidPrice = dLowestPrice + (dHighestPrice-dLowestPrice)/2.0;
        dUpperRange = dHighestPrice + dRangeValue;
        dLowerRange = dLowestPrice - dRangeValue;
        }
        else if (ToTime(Time[0]) > ToTime(dtStartTime) && ToTime(Time[0]) <= ToTime(dtEndTime))
        {

        /// Computations
        if(High[0] > dHighestPrice)
        dHighestPrice = High[0];
        if(Low[0] < dLowestPrice)
        dLowestPrice = Low[0];

        Comment


          #5
          Hi set2win, If you choose to use the Session Iterator, you can use the last value you get from DateTime beginTime = sessionIterator.ActualSessionBegin; and start plotting as long as Time[0] > beginTime (see DateTime.Compare()). Or you can detect the current day from the DateTime.Now property when the script starts up:

          Code:
          if(Time[0].Year == DateTime.Now.Year && Time[0].Month == DateTime.Now.Month && Time[0].Day == DateTime.Now.Day)
          {
              BackBrush = Brushes.Red; //or use a boolean so you can start drawing once this becomes true
          }
          Kind regards,
          -ChrisL

          Comment


            #6
            This code is derived from ecosytem opening range, i think the issue is to identity the dtStartTime / dtEndTime as of today only so if for example I want to use 930am to 940am as the opening range it must be those times for today only.

            adding :
            if (if (BarsPeriod.BarsPeriodType == BarsPeriodType.Minute && Time[0].Year == DateTime.Now.Year && Time[0].Month == DateTime.Now.Month && Time[0].Day == DateTime.Now.Day)

            Limits the display to today only but it still seems to take a long time calculating on a 5 day series
            Last edited by set2win; 04-11-2022, 03:28 PM.

            Comment


              #7
              Hi set2win, just to confirm, you are wanting to modify this opening range indicator from the ecosystem so it only calculates the values for the current day? If that is the case I can have a look at the indicator tomorrow and let you know what to change. If possible please link to the exact indicator you are using.

              Kind regards,
              -ChrisL

              Comment


                #8
                Here is the original code, it will show opening range for as many days as you load. I only want today. and I don't want it calculating any bars other than today. So if I add it to a 5 day chart, it will calculate present day only.
                Attached Files

                Comment


                  #9
                  Hi set2win, I was able to get it to calculate on today's day only using the condition we discussed yesterday. Please see attached.
                  Attached Files

                  Comment


                    #10
                    Hello
                    I would like to ask for help with a possible modification to the indicator "Opening Range Indicator".
                    I would like it to draw the range between two hours for the current day (00:01 - 09:30) and the previous day (00:01 - 09:30) extending the two lines from the previous day on the current day chart.
                    Only the previous day and the current day.
                    Thank you

                    Click image for larger version  Name:	RANGE.jpg Views:	1 Size:	116.8 KB ID:	1200174
                    Last edited by oviejo; 05-06-2022, 08:30 AM.

                    Comment


                      #11
                      Hi oviejo thanks for posting. I will not be able to make any further custom changes to this script. The forum threads remain open indefinitely so members of the community can contribute.

                      Best regards,
                      -ChrisL

                      Comment


                        #12
                        Thanks Chris L., I'll wait to see if another member of the community can help me.

                        Comment

                        Latest Posts

                        Collapse

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