How to trigger "state" to save/process while current bar is forming?

Hi. I’ve made a fairly simple custom indicator that pops up a graphic overlaid on the main chart when a calculated value is within a certain range, and once it moves back out of the range, the “state” is reset and graphics removed.

Everything works great if the current candle closes with the value within range, but if the graphic/reset trigger happens mid-bar and then moves back out, the reset never actually happens because it appears to be that the “state” changes do not get ‘saved’ until the close of the bar.

What I’d really like to know is: what is the code to “save” when the conditions are met, and not have to wait until the close of the bar?

I am needing some volume profile stuff, so the Input needs to be set to BARs, i think? At least, changing that seems to deactivate any profile() data from coming through.


Hello @chrickso,
Could you please provide the code you’re using for this indicator? Thanks.


Not sure if I understand you 100%…but I think I have faced a similar situation in the past. To solve it, I just declare a global variable and use that from with in the calculator class. Not sure how much of a hack this is and may not work in future versions, of course.

const meta = require("./tools/meta");

// declare it here
let myGlobalState = false;

class myIndicator {

   map(d, i, history) {

      // use it here
      myGlobalState = ....;


      return { ... };

1 Like