I'm developing an indicator to track the state of an algorithm. I'm creating a global variable to track the state of that algorithm, but the state seems to be getting recreated on every new tick.
For example, I have something like this:
var bool in_trade = false // have we triggered a buy/sell?
var string trade_pattern = na // which pattern triggered the trade
var int trade_direction = na // is it a long or short trade?
var float trade_entry = na // the price we went long/short
var float trade_target = na // take-profit
var float trade_stop = na // stop-loss
if (...condition...)
in_trade := true
trade_pattern := ...
(etc)
when a trade entry is triggered, I'm setting all those variables and I'm drawing the TP and SL on the screen. However, what I'm observing is that if price reverses against us and drops below the entry price, it reverts back to a "we haven't entered the trade state"
Is there a way in pinescript to create a global variable that I can control outside of tick recalculations?