alex deralu
alex deralu

Reputation: 599

Pinescript: How to check if a specific condition occured in last 14 days for every candle?

I have a pinescript indicator and I want to plot lines when that indicators conditions are satisfied in last 14 candles and there is specific tsi and stochastic rsi conditions being met.

Indicators conditions

if buy_goodf or buy_greatf or buy_incrediblef
    line.new(bar_index[0], low-lineheightbottom*LineLength, bar_index[0], high+lineheighttop*LineLength, color=color.new(color.lime, 50), width=linewidth)


if sell_goodf or sell_greatf or sell_incrediblef
    line.new(bar_index[0], low-lineheightbottom*LineLength, bar_index[0], high+lineheighttop*LineLength, color=color.new(color.red, 50), width=linewidth)

So this part works well, I tested that lines are showing correctly.

But now I want to plot a blue line at every candle where:-

  1. In the last 14 candles there has been a buy line ( that is buy_goodf or buy_greatf or buy_incrediblef has satisfied.

  2. There is tsi and stochrsi are a certain way.

The only problem I am having is with the first point - how to check for every candle if in its last 13 candles there have a buy condition or not.

I tried coding it using a for loop like this, but this does not give me signals.

for i = 0 to 13
    if (buy_goodf or buy_greatf or buy_incrediblef) and tsi_value>tsi_ema and tsi_value[1]<tsi_ema[1] and k[1]<d[1] and k>d
        line.new(bar_index[0], low-lineheightbottom*LineLength, bar_index[0], high+lineheighttop*LineLength, color=color.new(color.blue, 50), width=linewidth)

Is this the correct way of doing it?

Upvotes: 1

Views: 1628

Answers (1)

bbnm
bbnm

Reputation: 1094

I'm not sure if this is what you're looking for. But this code checks if there has been an EMA crossover in the last 14 days, if there is it will plot true over the candles. You can check the image below.

plot(ema(close, 20))
plot(ema(close, 50), color=color.white)

EMACrossover = crossover(ema(close, 20), ema(close, 50))

plotshape(EMACrossover, text="EMA Crossover", location=location.belowbar)

conditionOccurredDuringLookbackPeriod(condition, lookbackPeriod) =>
    bool conditionOccurred = false
    for i = 1 to lookbackPeriod
        if condition[i]
            conditionOccurred := true
    conditionOccurred

EMACrossoverOccurredDuringLast14Candles = conditionOccurredDuringLookbackPeriod(EMACrossover, 14)

plotshape(EMACrossoverOccurredDuringLast14Candles, text="True")

See this image

Upvotes: 1

Related Questions