I'm trying to plot a triangle if within the previous 6 candlesticks, there are at least 2 times where a moving crossover occurs. I have the following script but i don't know how to code the if statement. thanks alot in advance for any advice!
//@version=4
study(title="Crossover", overlay=true,resolution="")
first = ema(close, 5)
seconds = ema(close, 13)
third = sma(close,21)
fourth = sma(close,34)
fifth = sma(close, 55)
sixth = sma(close,89)
plot(first, title="EMA 5", color=color.red, linewidth=1, transp=0)
plot(seconds, title="Ema 13", color=color.aqua, linewidth=1, transp=0)
plot(third, title="SMA 21", color=color.orange, linewidth=2, transp=0)
plot(fourth, title="SMA 34", color=color.blue, linewidth=2, transp=0)
plot(fifth, title="SMA 55", color=color.black, linewidth=2, transp=0)
plot(sixth, title="SMA 89", color=color.purple, linewidth=2, transp=0)
long1 = (first > seconds) and crossover(first,third)
long2 = (first > third) and crossover(first, seconds)
long3 = (first > fourth) and (first > seconds) and (first > third) and cross(first,fourth)
// if long1 or long2 or long3 is true 2 times in the previous 6 six candles then plot it ("long")
xxxxxx
plotshape(series=long, title="L", style=shape.triangleup, location=location.belowbar, color=color.green, text="L", size=size.small)
See How can I count the occurrences of a condition in the last x bars? for an example.
Use the barssince
function:
LENGTH = 6
count1 = barssince(long1 or long2 or long3)
count2 = count1[count1 + 1]
plotshape(count1 + count2 < LENGTH - 1, title="L", style=shape.triangleup, location=location.belowbar, color=color.green, text="L", size=size.small)
UPDATE
Actually, my solution is a bit over-designed, and I would recommend sum
as suggested by Bjorn Mistiaen:
LENGTH = 6
count = sum(long1 or long2 or long3 ? 1 : 0, LENGTH)
plotshape(count >= 2, title="L", style=shape.triangleup, location=location.belowbar, color=color.green, text="L", size=size.small)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.