Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Session searching

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

    Session searching

    Hi,
    I need your help to develop my custom indicator.:
    I have a chart with only RTH SESSION with break at EOD (14 candles for each session - 30 minutes bar). I have to start from the current RTH and go 10 session back from the current and 20 session back. I have to exclude holidays (so, i check if the bars in the session are < 14) and saturdays/sundays. Once done, I have to print a text at 20th session back and 10th session back from the current.
    The current RTH (at the closing of it) would to be the start of my counting.

    The indicator has to be refreshed each session end and not each bar.

    I don't know how to do.

    Thank you

    #2
    Hello Davide999,

    Thanks for your post.

    To draw text on the first bar of a session either 10 or 20 sessions back you could consider looping from 0 barsAgo through CurrentBar and increment an integer counter each time IsFirstBarOfSessionByIndex() is true.

    IsFirstBarOfSessionByIndex(): https://ninjatrader.com/support/help...ionbyindex.htm

    Note that there is no GetFirstBarOfSession(int sessionsAgo) method available at this time. You vote was already added to this feature request from a previous ticket.

    Are you sure that you don't want to just draw the text on the first bar of the session as it happens instead of waiting for 20 sessions to pass and going back and drawing it?

    Please let me know if I may assist further.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon,
      I would draw a text 10 session and 20 session back from the current..each day.
      I have done this loop:

      Code:
                      for(int i = 0; i < CurrentBar; i++){
      
                      if (Bars.IsFirstBarOfSessionByIndex(i)) {
      
                          CountSessione +=1;}}​
      Please, can you help me to identify where i have to draw the text.

      I would to obtain this result, and each end of day session the text have to translate on the right at the next session (if the next session has < 14 candles, an example if it is an holiday I have to exclude them from the count):
      Click image for larger version

Name:	image.png
Views:	131
Size:	364.2 KB
ID:	1244350

      Thanks
      Last edited by Davide999; 04-03-2023, 11:51 AM.

      Comment


        #4
        Hello Davide999,

        Thanks for your note.

        The code you shared loops from bar 0 to the current bar and increments the counter when Bars.IsFirstBarOfSessionByIndex is true. When the counter in your script is equal to 10, this indicates that this session is the 10th session. When the counter is equal to 20, this indicates the session is the 20th session.

        You could create a condition that checks if the counter in your script (CountSessione) is equal to 10 and call Draw.Text() to draw text on the chart when the session counter is equal to 10. You would also create a condition that checks if CountSessione is equal to 20 and call Draw.Text().

        Draw.Text(): https://ninjatrader.com/support/help.../draw_text.htm

        Please let me know if I may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Yes, but i find the text on the extreme left (year 2022) of the chart...it is wrong:

          Code:
              
                          for(int i = 0; i <= CurrentBar; i++){
          
                          if (Bars.IsFirstBarOfSessionByIndex(i)) {
          
                              CountSessione +=1;
                              CandleTime = Time[i];
                          }
          
                          if (CountSessione == 10){
          
                              Testo10 = Draw.Text(this, "AutomaticTextNumberSession10", true, "10", 0, High[0]+100, 0, Brushes.Yellow, new NinjaTrader.Gui.Tools.SimpleFont("Arial", 40) {Bold = true}, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 50);
          
                          }}​
          Click image for larger version  Name:	image.png Views:	0 Size:	301.9 KB ID:	1244354

          Comment


            #6
            I have to check from NOW, so the current session, to back (once arrived to 20 session back ...stop):
            Click image for larger version

Name:	image.png
Views:	103
Size:	272.9 KB
ID:	1244356

            Comment


              #7
              Hi Brandon,
              The script that you suggests to do draw a text at the last session on the left of the chart and this is wrong. I need to start from the current RTH, go back 10 and 20 session and draw a text on the 10th and on the 20th session at the center of the session itself. At the end of a session, the script has to draw the text one session next (so, "translate" the text from a session to the following session).

              Please, see my screen.

              Thanks

              Comment


                #8
                Hello Davide999,

                Thanks for your note.

                You would need to reverse your loop if you want to count from the current bar on the chart back to the first bar on the chart. Currently, your script is looping from bar 0 to the CurrentBar (leftmost bar to the rightmost bar) and instead you should be looping from the CurrentBar (rightmost bar) back to bar 0 (leftmost bar) and increment you counter when Bars.IsFirstBarOfSessionByIndex() is true.

                You could consider using ChartBars.ToIndex and ChartBars.FromIndex to loop through the rendered bars on the chart similar to what is seen in the IsFirstBarOfSessionByIndex() help guide sample code.

                ChartBars.ToIndex: https://ninjatrader.com/support/help...rs_toindex.htm
                ChartBars.FromIndex: https://ninjatrader.com/support/help..._fromindex.htm
                IsFirstBarOfSessionByIndex: https://ninjatrader.com/support/help...ionbyindex.htm

                Please let me know if I may assist further.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  Ok Brandon, this is logically correct but can I do a for in this way?

                  Code:
                                   for (int barIndex = ChartBars.ToIndex; barIndex >= ChartBars.FromIndex; barIndex--){
                  
                                      if (Bars.IsFirstBarOfSessionByIndex(barIndex)) {
                  
                                          CountSessione +=1;
                                          CandleTime = Time[barIndex];
                  
                                      }
                                      if(CountSessione == 10){
                  
                                          Print(CandleTime);
                  
                                      Testo10 = Draw.Text(this, "AutomaticTextNumberSession10", true, "10", CandleTime.AddMinutes(-30), High[0]+100, 0, Brushes.Yellow, new NinjaTrader.Gui.Tools.SimpleFont("Arial", 40) {Bold = true}, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 50);
                                      }
                                  }​
                  I try that but it is not working. I also obtain this error "Indicator 'SessionNumbering': Error on calling 'OnBarUpdate' method on bar 1: 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."
                  Last edited by Davide999; 04-03-2023, 01:29 PM.

                  Comment


                    #10
                    Hello Davide999,

                    Thanks for your note.

                    After further research on this, ChartBars.ToIndex could only be used as the starting point for your loop. Using ToIndex/FromIndex would loop through visible bars which change based on what you are looking at. This also means you'd be looping through all bars in the chart every OnRender() call.

                    To count backward starting at the CurrentBar on the chart, you should loop from CurrentBar back to bar 0. You would also need to add a break in your loop after the last session you want to find so that you aren't looping over the entire series. For example, the code might look something like this.

                    Code:
                    for (int i = CurrentBar; i >= 0; i--)
                    {
                        //logic
                    
                        if(CountSessione == 10)
                        {
                            //store i to a variable so you know what bar index it was
                            break;
                        }
                    }
                    
                    //do something with the bar index you found
                    ​Note that you might need to add additional custom logic to the script after the loop to make sure those sessions were found. For example, assuming you only load 3 sessions, the script might throw an error depending on what you are trying to do after the loop.

                    Please let me know if I may assist further.
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #11
                      Hi Brandon,
                      Thank you but doesn't work yet. Here my code:
                      Code:
                      protected override void OnBarUpdate(){    
                      
                                      if (CurrentBar < 1) return;
                      
                                          //logic
                                          for (int i = CurrentBar; i >= 0; i--)
                                          {
                                              if (Bars.IsFirstBarOfSessionByIndex(i)) CountSessione +=1;
                      
                                                  if (CountSessione == 10)
                                                  {
                                                     Print(CountSessione);
                                                  //store i to a variable so you know what bar index it was
                                                  BarIndex = i;
                                                  break;
                      
                      
                                              }
                                      }
                      
                                      Testo10 = Draw.Text(this, "AutomaticTextNumberSession10", true, "10", Time[BarIndex], High[0]+100, 0, Brushes.Yellow, new NinjaTrader.Gui.Tools.SimpleFont("Arial", 40) {Bold = true}, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 50);
                                              ​
                      And here the result ("10" could be drawed on the RED CROSS):

                      Click image for larger version  Name:	image.png Views:	0 Size:	1,017.9 KB ID:	1244378
                      I think that the problem is that ON BAR UPDATE execute the loop n times and the firstbarofsessionbyindex remain always at the first session? Do you think?
                      Last edited by Davide999; 04-03-2023, 02:47 PM.

                      Comment


                        #12
                        Hello Davide999,

                        Thanks for your note.

                        You will need to make sure to run this loop when OnBarUpdate() reached the end of the data that is loaded on the chart.

                        The way it is currently being run, the script will run the loop for every bar.

                        A condition should be added to the script that checks f the CurrentBar processing is equal to 2 subtracted from Count (the total count or the last historical bar). The condition would look like this.

                        if (CurrentBar < Count - 2)
                        return;

                        As a general rule of thumb, you never want to run a loop that goes over a large number of bars for every bar as that severely impacts performance when you use a series that has a large number of bars, such as a tick series.

                        A reverse loop then needs to be run from Realtime data so its getting 'X' number of sessions from now (the current bar on the chart).

                        In the loop, increment a counter by 1 when Bars.IsFirstBarOfSessionByIndex() is true.

                        Then, check if the counter is equal to 10 to indicate 10 sessions back from the current bar, save the bar index to a variable, and call break; to break the loop.

                        Outside of the loop, you would subtract the bar index you saved to a variable from the CurrentBar value to get your barsAgo value. This barsAgo value would then be used for your Draw.Text() method to draw the text on the chart 10 sessions back from the current bar.

                        I have created an example script demonstrating this that you could view.

                        Note that you might also want to be doing this check once per session if possible, such as on the first bar of the session, to improve the performance depending on the bars type selected.

                        Please let me know if I may assist further.
                        Attached Files
                        Last edited by NinjaTrader_BrandonH; 04-04-2023, 07:05 AM.
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment


                          #13
                          ​Brandon,
                          Thank you very much for your support. I appreciate that! It is working correctly. Sorry for my bad programming but I'm not an expert and i don't know the ninjascript class etc..
                          Here the results with my personalization:

                          See you soon and thank you!
                          ​​Click image for larger version  Name:	image.png Views:	0 Size:	349.2 KB ID:	1244409

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          558 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          324 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
                          545 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          547 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X