I have a question about ATR Bands in FineScript.
Find out the value of the upper closing price of the atr band and the value of the /lower closing price when entering the strategy, and then TP and stop-loss from that value...Is that possible?
I have a question about ATR Bands in FineScript.
Find out the value of the upper closing price of the atr band and the value of the /lower closing price when entering the strategy, and then TP and stop-loss from that value...Is that possible?
Sure, it is possible. You need to use the var
keyword to store the value of the upper/lower band at the time of your entry. Then later use that variable as your TP/SL.
var float atr_upper_band = na
atr_upper_band := entry_condition ? upper_band_value : atr_upper_band // Store the upper band value if it is a new entry. Keep the old value otherwise
The key here is to use var
keyword so their values don't change unless you specifically do it.
var is the keyword used for assigning and one-time initializing of the variable.\
Normally, a syntax of assignment of variables, which doesn’t include the keyword var, results in the value of the variable being overwritten with every update of the data. Contrary to that, when assigning variables with the keyword var, they can “keep the state” despite the data updating, only changing it when conditions within if-expressions are met.