Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Do nothing until initial bars have loaded?

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

    Do nothing until initial bars have loaded?

    I'm sure there is a simple way to do this, but I'm struggling a bit.

    I want my indicator to do NOTHING, until the initial bars that will load on the chart happen. I.e., let's say I have the data series set to n-number of bars, or a start date... I'd like nothing to occur until those initial bars are loaded.

    I've come up with things that sort of work, but don't do what I want.

    E.g....

    I set a global variable initially to false called 'startScript'

    I then set at the top of my OnBarUpdate() like so:
    Code:
    if(!startScript)
      return;
    In OnStateChange(), I tried each of these(individually):
    Code:
    else if (State == State.DataLoaded)
    {
      startScript = true;
    }
    Code:
    else if (State == State.Transition)
    {
      startScript = true;
    }
    Code:
    else if (State == State.Realtime)
    {
      startScript = true;
    }
    The State.DataLoaded begins processing on the first bar the plots on the chart, which is not helpful.

    The State.Transition is close, but it doesn't do anything until a bar actually COMPLETES for the first time after the script is instantiated.

    The State.Realtime operates in a similar fashion to State.Transition.

    I.e., I don't want my script to WAIT until a bar is completed after all the bars are loaded....


    I also tried using the a BarCount in State.DataLoaded, which would allow me to put a return statement in if the CurrentBar is less that that bar count I captured in State.DataLoaded, but this didn't work the way I wanted either.

    So if all of the above was confusing the way I explained it... I would just like the indicator's FIRST calculation to be run as soon as all the bars are loaded onto the chart, irrespective of being connected to a data feed. And then run normally after that. is such a thing possible?

    #2
    Hello forrestang,

    Thanks for your post.

    Scripts will start processing OnBarUpdate when State.Historical is reached, and OnBarUpdate will start iterating from bar 0 until the script transitions to processing realtime bars. State.Historical also comes after State.DataLoaded. OnBarUpdate does not start processing until all of the chart data is loaded and the script starts processing historical data.

    When calculations require several bars, those calculations should not be made until the script has processed that many bars. This would be the recommended solution to have something calculated as soon as it can and to have that calculation made as new bars form. Please see below.



    We look forward to assisting.

    Comment


      #3
      Thanks for the reply Jim, I understand that portion, but that isn't really what my issue is.

      Just a quick explainer, I am doing an indicator that has a drawing object that the user can manually drag around on the chart(I have that bit working)... and based on the location of that drawing, it will draw items based on prior values. The beginning of that can be found HERE. So I really don't want anything to happen until the bars have loaded. Most of my logic will occur in OnRender().

      With most indicators, the iterations start on bar 0, and runs forward on each bar of OnBarUpdate() until it gets to the most recent bar on the chart.

      What I am wondering.. is there a simple way to have a calculation NOT occur, until the last bar is loaded on the chart... and THEN run a calculation.

      I.e. for example... imagine you wanted to simply draw a vertical line on a chart. If you had the code:
      Code:
      protected override void OnBarUpdate()
      {
          Draw.VerticalLine(this, CurrentBar+"xLoc", 0, Brushes.Yellow, DashStyleHelper.Dot, 1, true);    
      }
      This will draw a line on EACH iteration of onBarUpdate() from bar 0 to the last bar on the chart. My question is, is there a way to NOT draw this line until the LAST bar on the chart is loaded... RIGHT before the state switches to State.Realtime?

      Also of note, I know you could remove the 'CurrentBar' string identifier and it would only show the last vertical line, but the goal is simple to have something that DOESN'T happen until all the INITIAL bars on the chart have loaded.

      Comment


        #4
        Hello forrestang,

        If you want to prevent calculating in OnBarUpdate until the last historical bar is processed, you could do something like the following:

        Code:
        if (State == State.Historical && CurrentBar == Bars.Count-2)
            Draw.Dot(this, "tag", true, 0, High[0], Brushes.White);
        Alternatively, you could try calling your code in OnStateChange > State.Realtime as opposed to flipping a bool and waiting for OnBarUpdate to iterate again.

        We look forward to assisting.

        Comment


          #5
          Thanks Jim, this is exactly what I needed, much appreciated sir!

          Comment


            #6
            Jim,
            This is an example of what I was trying to do.

            The reason I wanted nothing to happen until after the bars have initially loaded, is I was trying to sync up this cursor(vertical line), with the right-most bar on initial load. I couldn't think of a way to do this w/o waiting for that initial load to take place first.

            You can see when this indicator loads, it initially loads with the cursor at the right most bar, and updates as new tics come in(you can see the green projection moving).

            Then if desired, I can grab the line, drag it backwards, and it will show the historical projections, and at that point won't update, as it is processing the historical bars.

            I can if I want, drag it back to the right most bar, and it will then start updating again, and follow each new bar, as the vertical line will stay LOCKED on the most recent bar, until I manually drag it off the right-most bar.

            THe projection is unimportant, I was more interested in the coding exercise of this.


            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            576 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