Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

loop to find specific time bar

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

    loop to find specific time bar

    Hello,

    Is there a way i can get index bar for a specific time like 9:30 or 4pm ? The loop will return each bar number for 9:30.
    Ty

    #2
    Hello frankduc,

    Thank you for your post.

    It sounds like what you are looking for is ChartBars.GetBarIdxByTime(ChartControl chartControl, DateTime time):



    If this is not what you're looking for, could you clarify what you're trying to do?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Kate,

      I try to list all the barindex number of every bar at 9:30 from the chart. For both OBU and OR.

      i tried :

      Code:
      MyDateTime = new DateTime(9, 30, 0);
      
      Print(ChartBars.GetBarIdxByTime(ChartControl, MyDateTime));
      Return MyDateTime does not exist in the context

      ty

      Comment


        #4
        Hello frankduc,

        Thank you for your reply.

        You're not properly formatting your DateTime. If you do it like you've got it there, it thinks you're using month, day, year, not hours, minutes, seconds.

        However, this should work:

        Code:
        DateTime MyDateTime = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 09, 30, 0);
        
        Print(ChartBars.GetBarIdxByTime(ChartControl, MyDateTime));
        Please let us know if we may be of further assistance to you.

        Comment


          #5
          Hello Kate,

          I have a strange behaviour and maybe you can enlight me.

          I have a strange print, here it is:

          2021-01-22 15:59:00 : 3840,25 - 3839 = 1,25
          time3120
          time3119
          dif19,75
          CP3846,25
          CP3826,5
          Can3852,5
          2021-01-22 16:00:00 : 3839,75 - 3832,75 = 7
          time3510
          time3510
          dif0
          CP3844,5
          CP3844,5
          Can3844,5
          time3510
          time3510
          dif0

          As you can see Time3120 time 3119 which represent 9;30 and 16:00 jump to time3510
          time3510. You can see the result in the chart attach.
          At this specific place the gap wont close. Any idea why? Is there bad prints? At some bar if i put the cursor under 9:30 there is only 9:31.

          Code:
          if(CurrentBar < 1) return;
          
          
          DateTime MyDateTime9 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 09, 30, 0);
          DateTime MyDateTime16 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day-1, 16, 00, 0);
          
          Print("time"+ChartBars.GetBarIdxByTime(ChartContro l, MyDateTime9));
          Print("time"+ChartBars.GetBarIdxByTime(ChartContro l, MyDateTime16));
          
          
          double closePrice16 = Bars.GetClose(Bars.GetBar(MyDateTime16));
          double closePrice9 = Bars.GetClose(Bars.GetBar(MyDateTime9));
          
          double difCP = closePrice16 - closePrice9;
          
          Print("dif"+difCP);
          Print("CP"+closePrice16);
          Print("CP"+closePrice9);
          
          
          double candle = Close[0] + difCP;
          
          Print("Can"+candle);
          
          Value[0] = candle;

          TY
          Attached Files

          Comment


            #6
            Hello frankduc,

            Thank you for your reply.

            I note the issue is occurring on Sundays, and is because you're trying to just subtract 1 day, which means it's trying to check for the 1600 bar on Saturday when no data exists. since you want to refer to the closing price from the previous Friday, you need to check if the current day is Sunday and if so reference the Friday data instead like the below:

            Code:
            protected override void OnBarUpdate()
            {
            if(CurrentBar < 1) return;
            
            DateTime MyDateTime9 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 08, 30, 0);
            DateTime MyDateTime16 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day-1, 15, 00, 0);
            
            if(MyDateTime9.DayOfWeek == DayOfWeek.Sunday)
            {
            MyDateTime16 = MyDateTime16.AddDays(-2);
            }
            
            // logic continues here
            }
            Please let us know if we may be of further assistance to you.

            Comment


              #7
              Hallo frankduc, hallo Kate,

              that is not enough. You also have to check for holidays.

              Best regards
              Goldy

              Comment


                #8
                Hi Kate and Goldy,

                That is not the problem. If it was about weekends, on a 5 days chart without weekends the indicator would return the right answer. But look at the 5 days chart attach, it is not.

                Look at 27 janv at 16:00 there is a gap where the yellow line is misplaced.

                The calculation should of been made on the new series of close price and not the actual serie. the new close at 16:00 is 3795.75 (yellow line) -22 = 3773.75 for 9:30 (where the YL should be) but instead it calculated on the close of the actual serie and return 3749.

                How am i going to fix that now. Any suggestion appreciated for the help.

                TY

                Attached Files

                Comment


                  #9
                  Hello again,

                  I think i found the solution but it wont apply for some strange reasons.

                  If i apply += to double difCP = closePrice16 - closePrice9; it should work, but anyone can tell me why OBU wont let me do it?

                  Code:
                  double difCplus = 0;
                  
                  double closePrice16 = Bars.GetClose(Bars.GetBar(MyDateTime16));
                  double closePrice9 = Bars.GetClose(Bars.GetBar(MyDateTime9));
                  
                  double difCP = closePrice16 - closePrice9;
                  
                  difCplus += difCP;
                  difCplus is Printed does not return a cumulative sum.

                  -2,10,-2

                  it should return -2, 8, 6

                  TY

                  Comment


                    #10
                    Hello frankduc,

                    Thank you for your reply.

                    Is this line within OnBarUpdate?


                    double difCplus = 0;

                    If so, the results you're seeing are because you're resetting difCplus to zero with every time OnBarUpdate is triggered, so it's not cumulating that value.

                    I would suggest definining difCplus at the class level and then setting the initial 0 value in OnStateChange in State.SetDefaults:

                    Code:
                    public class aFrankducTest : Indicator
                    {
                    [B]private double difCplus;[/B]
                    protected override void OnStateChange()
                    {
                    if (State == State.SetDefaults)
                    {
                    Description = @"Enter the description for your new custom Indicator here.";
                    Name = "aFrankducTest";
                    Calculate = Calculate.OnBarClose;
                    IsOverlay = true;
                    DisplayInDataBox = true;
                    DrawOnPricePanel = true;
                    DrawHorizontalGridLines = true;
                    DrawVerticalGridLines = true;
                    PaintPriceMarkers = true;
                    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive = true;
                    AddPlot(Brushes.Orange, "MyPlot");
                    
                    [B]difCplus = 0;[/B]
                    }
                    else if (State == State.Configure)
                    {
                    }
                    }
                    Please let us know if we may be of further assistance to you.
                    Last edited by NinjaTrader_Kate; 02-01-2021, 09:48 AM.

                    Comment


                      #11
                      Kate,

                      Sorry to annoy you with that again. You were right, make it private the cumulation works.

                      But its working to well. It cumulates the same diffence every minute instead of only those betteen 16:00 and 9:30.

                      time390 // this is 16:00
                      time389 // this is 9:30
                      dif-2 // this the difference bettween 3794.75-3792.75
                      CP3792,75 // this is price at 16:00
                      CP3794,75 // this is price at 9:30
                      DP-706 // finally cumulative sum 706
                      Can3098,5
                      time390
                      time389
                      dif-2
                      CP3792,75
                      CP3794,75
                      DP-708 // cum 708
                      Can3095,75
                      time390
                      time389
                      dif-2
                      CP3792,75
                      CP3794,75
                      DP-710

                      It should be cumulating only once when it loop thought a 16-9:30. Why is repeating every minute?

                      TY

                      Comment


                        #12
                        Hello frankduc,

                        Thank you for your reply.

                        Can you post your code as it currently is so that I can best understand the issue? Are you wanting the cumulative value to reset at some point? I'm a little confused as to your wanted behavior.

                        Thanks in advance; I look forward to assisting you further.

                        Comment


                          #13
                          Kate,

                          The goal is to eliminate the gap between each day by narrowing the difference there is between 4pm and the next day 9:30. If the gap is 10 points lower. Than you incease from 10 points all prices till 4pm next day. The chart is RTH. Session begin at 9:30 end at 4pm.

                          Code:
                          namespace NinjaTrader.NinjaScript.Indicators
                          {
                          public class Heikeitest : Indicator
                          {
                          private double difCplus = 0;
                          protected override void OnStateChange()
                          {
                          if (State == State.SetDefaults)
                          {
                          Description = @"Enter the description for your new custom Indicator here.";
                          Name = "Heikeitest";
                          Calculate = Calculate.OnBarClose;
                          IsOverlay = true;
                          DisplayInDataBox = true;
                          DrawOnPricePanel = true;
                          DrawHorizontalGridLines = true;
                          DrawVerticalGridLines = true;
                          PaintPriceMarkers = true;
                          ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                          //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                          //See Help Guide for additional information.
                          IsSuspendedWhileInactive = true;
                          AddPlot(Brushes.Yellow, "candle");
                          
                          }
                          else if (State == State.Configure)
                          {
                          }
                          }
                          
                          protected override void OnBarUpdate()
                          {
                          
                          if(CurrentBar < 1) return;
                          
                          
                          DateTime MyDateTime9 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 09, 30, 0);
                          DateTime MyDateTime16 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day-1, 16, 00, 0);
                          
                          
                          
                          Print("time"+ChartBars.GetBarIdxByTime(ChartContro l, MyDateTime9));
                          Print("time"+ChartBars.GetBarIdxByTime(ChartContro l, MyDateTime16));
                          
                          
                          
                          double closePrice16 = Bars.GetClose(Bars.GetBar(MyDateTime16));
                          double closePrice9 = Bars.GetClose(Bars.GetBar(MyDateTime9));
                          
                          double difCP = closePrice16 - closePrice9;
                          
                          
                          
                          difCplus += difCP;
                          
                          Print("dif"+difCP);
                          Print("CP"+closePrice16);
                          Print("CP"+closePrice9);
                          Print("DP"+ difCplus);
                          
                          double candle = Close[0] + difCplus;
                          
                          Print("Can"+candle);
                          
                          
                          Value[0] = candle;
                          
                          
                          
                          }
                          }
                          }
                          I also get that message : Indicator 'Heikeitest': Error on calling 'OnBarUpdate' method on bar 5070: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                          difCplus += difCP; should only cumulate between 4 and 9:30 . if you print you will see that it cumulate every minute. Should cumulating only when there is a gap.

                          if there is a -2 first day and 8 next day, 2 the third day
                          the cumulation should give -2, 6 , 8...
                          instead it is showing in OW -2,-2,-2-2,6,6,6 ... and cumulate the return for evey minute.

                          TY
                          Attached Files

                          Comment


                            #14
                            Hello frankduc,

                            Thank you for your reply.

                            Your script as written doesn't have any restrictions on when you're calculating these values - it will recalculate on each iteration through OnBarUpdate. If you want to only calculate them once per session, say on the 9:30 am opening bar of the session, you could do so with a SessionIterator using Bars.IsFirstBarOfSession:

                            protected override void OnBarUpdate()
                            {
                            if(CurrentBar < 1) return;
                            if(Bars.IsFirstBarOfSession)
                            {
                            DateTime MyDateTime9 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 09, 30, 0);
                            DateTime MyDateTime16 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day-1, 16, 00, 0);

                            Print("time"+ChartBars.GetBarIdxByTime(ChartContro l, MyDateTime9));
                            Print("time"+ChartBars.GetBarIdxByTime(ChartContro l, MyDateTime16));

                            double closePrice16 = Bars.GetClose(Bars.GetBar(MyDateTime16));
                            double closePrice9 = Bars.GetClose(Bars.GetBar(MyDateTime9));

                            double difCP = closePrice16 - closePrice9;

                            difCplus += difCP;

                            Print("dif"+difCP);
                            Print("CP"+closePrice16);
                            Print("CP"+closePrice9);
                            Print("DP"+ difCplus);
                            }
                            double candle = Close[0] + difCplus;

                            Print("Can"+candle);


                            Value[0] = candle;
                            }



                            Please let us know if we may be of further assistance to you.

                            Comment


                              #15
                              Kate,
                              I know what the trouble is now.

                              This part of the code:

                              Code:
                              DateTime MyDateTime9 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 09, 30, 0);
                              DateTime MyDateTime16 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day-1, 16, 00, 0);
                              
                              double closePrice16 = Bars.GetClose(Bars.GetBar(MyDateTime16));
                              double closePrice9 = Bars.GetClose(Bars.GetBar(MyDateTime9));
                              
                              double difCP = closePrice16 - closePrice9;
                              
                              double candle = Close[0] + difCP;
                              return the difference between prices between 4pm and 9:30 every time it cross those hours on the chart.

                              If it cross 4-9:30 the first time it returned lets say -2

                              Than it apply that -2 to all the prices from that time untill it cross the next 4-9:30.

                              Next 4-9:30 it cross if it returned for example +4, it apply +4 to all close prices until the next 4-9:30.

                              Its ajusting the prices only by comparing one day to the previous.

                              What i want is the first -2 beeing applied to all prices till the end of the chart. (right now its stopping at the next 4-9:30)

                              Than starting the second cross at 4-9:30 apply over the +4 to the close prices (it will become +2) till the end of the chart.

                              and it goes on with the next cross at 4-9:30. ...


                              The way i see it we need to change
                              Code:
                              DateTime MyDateTime9 = new DateTime(Time[0].Year,Time[0].Month, Time[0].Day, 09, 30, 0);
                              that part of the code.

                              Because the series start at 0 till 389-390 and the next cross is at 779-780 meaning 389 + 389 +1. Maybe i dont need the hours, i just need to change the time by the bar number (index).

                              How do we use Time by bar without the hours?

                              TY

                              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
                              347 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