0

Apologies if the below sounds elementary but basically I would like to plot a shape/signal whenever candlestick that fulfills condition A cross under candlestick that fulfills condition B (within a lookback period).

Example:

Condition A = low < low[1] and close > low[1] Condition B = close [1] < low[2] and open < close

Is there a way for me to do it while also adding a lookback period (example, 5 bars, so Condition A candlestick will look to the left 5 bars to see whether it cross any Condition B candlestick)?

Thanks!!

Crossover and lookback periods

1 Answers1

0

inferring that you want to get a signal if ConditionA is true now, and conditionB has been true in 1 of the last X candles

you have two options to do this:

1- hardcoding:

ConditionA = low < low[1] 
ConditionB = close[1] < low[2] 
signal = ConditionA and (ConditionB[1] or ConditionB[2] or ConditionB[3] or ConditionB[4] or ConditionB[5])

this is not an optimal solution as you won't have control over the lookback period.

2- Suggested Method: using a for loop

lookback = 10
conditionA = low < low[1] 
conditionB = close[1] < low[2] 
signal = false
if conditionA
    for i = 1 to lookback 
        if conditionB[i]
            signal := true
            break

this method is better since you can use an input for your look-back period and make it dynamic.

you can then use

plotshape()

to plot a shape when the "Signal" variable is true

Edit: for the crossover check, edit the "if conditionB[i]" line to the following.

if conditionB[i] and open[0] > close[i] and close[0] < close[i] 
Indi Quant
  • 28
  • 6
  • Thanks! I think it's getting close. Is there a way to code the signal is true only when candle with Condition A CROSSES a candle with Condition B? Right now, the signal is true whenever Condition A appears after Condition B but would like the crosses :). Thanks!! – donsterrrrr Nov 02 '22 at 13:56
  • @donsterrrrr I'm not sure what you mean by "cross" here, if you mean Candle A should open above the close of candle B and close below the close of Candle B or something like that, then you can add that check in the for loop, if conditionB[i] is true, then add another if that checks if open[0] (A) is above or below close[i] (B) and take it from there. – Indi Quant Nov 02 '22 at 14:07
  • Yes, @Indi Quant, you put it rightly. Apologies as my pine script knowledge is bare minimum. Would you be able to share the exact script like what you did above? Really appreciate it! – donsterrrrr Nov 02 '22 at 17:33
  • @donsterrrrr I edited the answer to add the crossover line of code, try to enhance your pine script knowledge and ask right questions, with all due respect, this is not a platform to get free development done, you could hire a developer if you need more advanced code. – Indi Quant Nov 02 '22 at 20:24