1

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?

STH
  • 27
  • 4

1 Answers1

0

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.

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Thank you, teacher! May I ask you one more question? – STH May 09 '23 at 00:18
  • -------------------------------------------------- The above is before the change. The contents below are after the change. Is it right to change it like this? ------------------------------------------------- – STH May 09 '23 at 00:22
  • Yes but you should use only one `strategy.exit()`. You can combine your TP and SL in one `strategy.exit()`. Also, for TP, you should use the `limit` argument. `strategy.exit("LongExit", "Long", stop=atr_lower_band , limit=atr_upper_band)` – vitruvius May 09 '23 at 04:43