Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator slow to load / Limit historical bars

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

    Indicator slow to load / Limit historical bars

    I have an indicator that for the sake of simplicity looks for a certain bar pattern and draws an arrow on the chart.

    As well as this I have a user parameter that allows the user specify the number of previous arrows to show on the chart at all times. So if the user chooses '8'. Once the chart loads the previous 8 times this pattern occurred will display an arrow.

    In the code I achieve this by simply Changing the Arrow Drawing Tag each time a pattern occurs by Appending a Number which increases +1 each time the pattern occurs and then I remove old drawings like this::

    RemoveDrawObject("MyIndicator Arrow" + (NumberCounter - MaxArrowsToShow));

    As real time data continues the chart will only ever show the last 8 arrows, every time a new arrow/pattern occurs the oldest arrow will be deleted.

    Now this works perfect. BUT the problem is the script still runs through ALL the bars on the chart when the indicator is initialized. This could be a lot depending on the user chart settings. As such the indicator takes a good few seconds to load and while loading the chart freezes.

    I realize I can prevent the indicator running on Historical bars altogether. But is it possible in my scenario to limit the amount of historical bars my indicator checks over?

    For example, based on my data I know this pattern occurs at least once every 25 bars. So if the user has a setting of 8 arrows to show, I'd want the indicator to only run on the previous 200 bars (8 x 25). But it seems like in order to do this I'd have to know the total number of bars on the chart before the chart has loaded?

    If the chart has 6000 bars, I want to end up this:

    if (CurrentBars[0] < 5800)
    return;

    But is this possible?​

    #2
    Hello several,

    You can run the logic on the last historical bar and loop through a few previous bars if you want to draw objects on the last few bars.

    if (State == State.Historical && CurrentBar == Count - 2)


    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks but would this mean I need to duplicate all my indicator code inside this condition? to run it on the last x bars and then I would need another condition to run it realtime with the same code?

      Comment


        #4
        Hello several,

        If the code on the last historical bar is setting previous bars values this would be the same code as setting the current bars value in real-time.

        However, you could choose to put the code in a method if you want to run the same exact code from multiple branching commands.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          ok. So this works inside on bar update. I don't even need to check for if State.Historical right?

          Code:
                      if (CurrentBars[0] < ((Count - 2) - (MaxArrowstoShow * 25)))
                          return;
          
          // All my code​

          Comment


            #6
            Hello several,

            The count increases with every added bar in real-time. You will need to check if the state is historical to trigger an action on the last historical bar using the code I have suggested.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Right. I understand that but in my case I simply want my code to be run on that last X bars so it if the Count increases in real time the result is the same. It doesn't seem to matter whether they are historical or realtime. The code is working unless I am missing something.

              So if I have 500 historical bars at initialization, the code will run over from bar 300 onwards. Then as realtime bars come and the Count increases the code will run on the new bars. And the oldest Arrows will be deleted as new arrows are created.
              Last edited by several; 01-22-2025, 03:36 PM.

              Comment


                #8
                Hello several,

                To clarify, are you wanting to draw objects over the last few historical bars and then every new real-time bar?

                Or are you trying to draw and re-draw objects over the last few bars on every new bar both historical and real-time?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello several,

                  To clarify, are you wanting to draw objects over the last few historical bars and then every new real-time bar?
                  If the user has a setting of 10 arrows to show, when the indicator is turned it I want it to show only 10 historical arrows. But as realtime bars load and the pattern occurs, it will draw a new arrow and the oldest arrow will be deleted. And so on. There will only ever be 10 arrows on the chart at any time.

                  However, previously even if the user had set only to show 10 arrows, the code would still check over ALL the historical bars. This is what I wanted to stop. So it only checks over enough historical bars to show 10 arrows. Not have the code run over 50,000 bars to show 10 arrows.

                  Or are you trying to draw and re-draw objects over the last few bars on every new bar both historical and real-time?
                  No I'm not trying to redraw on old bars. Only delete.

                  Comment


                    #10
                    Hello several,

                    "If the user has a setting of 10 arrows to show, when the indicator is turned it I want it to show only 10 historical arrows. But as realtime bars load and the pattern occurs, it will draw a new arrow and the oldest arrow will be deleted. And so on. There will only ever be 10 arrows on the chart at any time."

                    You can move a drawing object to a new bar by re-using the tag name. You can remove a drawing object by supplying the tag name to RemoveDrawObject().

                    However, I may have misunderstood your initial inquiry. I had thought you were trying to improve the performance by not drawing in historical until the last historical bar (as there wouldn't be any need to draw and remove those objects in historical). On the last historical bar you can draw objects on the last few bars (like any of the last 10 or whatever you want). From there, on each new bar, you would draw new objects on new bars or move objects that no longer need to appear on previous bars to a new bar.

                    The number of objects is not what I am asking. I am asking when do you want to draw them?

                    Imagine a chart that has 20 historical bars loaded on it.

                    Do you want to draw or re-draw (move to a new bar) objects on each of these 20 historical bars and then remove them?
                    Or do you want to wait until the last historical bar is processed and then for the last few bars (such as the last 10 bars) draw objects over those bars to improve the loading performance of the script?


                    "However, previously even if the user had set only to show 10 arrows, the code would still check over ALL the historical bars. This is what I wanted to stop."

                    This is what I am directing you to do in post # 2. Stop running the logic on all of the historical bars and run the logic on the last historical bar and draw any objects that need to show when the chart first loads. From there you can draw or move objects on each new real-time bar.


                    "No I'm not trying to redraw on old bars. Only delete."

                    If you are deleting an object and then generating a new drawing object of the same object type, like a dot or a rectangle, on a later bar, re-drawing the object to move it to a new bar would be more efficient than deleting an object and generating a new object.

                    That said, your comment doesn't help with when the objects should be drawn.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I simplified the problem for the purpose of the question.

                      In reality the indicator is finding series of candlestick patterns and drawing objects under the bars. There's 4 patterns in each 'set' and the indicator draws 4 arrows under each pattern in the set. After the 4th pattern in the set is found. The set it determined to be complete and it moves onto the next set.

                      If there's a series of 3 patterns, but then the 4th is interrupted by the 1st pattern in the next set, this incomplete set of 3 arrows gets removed as soon as the 1st pattern of the next set is detected. Only completed previous sets will be shown. I'm using a combination of remove object and renaming the tag.

                      When I asked the question I was under the impression that as an indicator is loaded, it is loaded sequentially and it doesn't know the total number of bars on the chart until they had finished loading but you essentially solved my problem with 'Count', I didn't know this variable existed. As this knows the total number of bars on the chart when the indicator starts loading."But it seems like in order to do this I'd have to know the total number of bars on the chart before the chart has loaded?"

                      So my question was based on the fact that even though the user has set the parameter to only show the previous 10 sets. The code was still checking the all the bars on the chart when it didn't need to. If there's 10,000 bars then there's no need for it to check the first 9500 bars because 10 pattern sets will occur in the previous last bars or so.

                      Thanks

                      Comment

                      Latest Posts

                      Collapse

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