I am working on converting a ThinkScript indicator into PineScript. I am currently having problems with converting ThinkScript's barNumber() function to PineScript. I think I know what to use as its equivalent but I am not sure I understand the barNumber() even after reading the documentation and examples.
This indicator is basically used as an entry/exit indicator. I think what the code is doing by utilizing the barNumber() is removing the signal if a new signal is plotted, but if that new signal is invalidated then it reverts to the previous signal.
Here is the part of code I am confused with the first few defs have more meat behind them just it is irrelevant to explain them they should all return as floats (def stateUp through def linDev):
def bar = barNumber();
def stateUp;
def stateDn;
def atrCCI;
def price;
def linDev;
def CCI = if linDev == 0
then 0
else (price - avg(price, length)) / linDev / 0.05;
def MT1 = if CCI > 0
then max(MT1[1], hl2 - ATRCCI)
else (min(MT1[1], hl2 + ATRCCI)
def state = if close > ST and close > MT1 then StateUp
else if close < ST and close < MT1 then StateDn
else State[1];
def newState = HighestAll(if state <> state[1] then bar else 0);
The code uses a lot of conditional statements, here are some of the other usages for this code:
CSA = if bar >= newState then MT1 else Double.NaN;
signal = if bar >= newState and state == stateUp and. . .
Is there an easy way to approach this in PineScript?
Thank you for the help!