How to reference the lowest low at last buy signal

Viewed 24

I have an indicator in Pine Script whose buy signal is BuyCondition. I am trying to reference the lowest within 20 bars back of the last BuyCondition. In my head, I thought the following would work, however it gives the study error "Invalid value of the 'length' argument (0.0) in the "lowest" function. It must be > 0."

Pine script:

RRR = input.float(1.5, title='Risk Reward Ratio')
LookbackForLowest = input.int(20, title='Lowest Low LookBack')
SinceLastBought = ta.barssince(BuyCondition)
BuyClosePrice = ta.valuewhen(BuyCondition == 1, close, 1)
LookbackFromNow = (SinceLastBought + LookbackForLowest)
LowestLow = ta.lowest(low, LookbackFromNow)[1]
StopLoss = close<LowestLow
Exit = (BuyClosePrice - LowestLow) * RRR


//SELL CONDITIONS
//1. Stop loss at recent low
//2. RRR

SellCondition1 = close<LowestLow
SellCondition2 = close>Exit
SellCondition = SellCondition1 or SellCondition2

Is anyone able to help me please?

1 Answers

You might make this small modifiction, barssince will return na if the condition is not yet found.

LookbackForLowest = input.int(20, title='Lowest Low LookBack')
SinceLastBought = ta.barssince(BuyCondition)
BuyClosePrice = ta.valuewhen(BuyCondition == 1, close, 1)
LookbackFromNow = (SinceLastBought + LookbackForLowest)
LowestLow =nz(ta.lowest(low, math.max(1,LookbackFromNow))[1])
StopLoss = close<LowestLow
Exit = (BuyClosePrice - LowestLow) * RRR

Related