Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

counter - Plotting single instance of when price condition is met. How to plot marker on only the first instance of price closing below moving average?

I am learning pine script and trying to create my first simple indicator based on the crossing of two Moving Averages. I have successfully created the plotting of a triangle in both long and short directions when the 20 MA crosses the 50 MA and vice versa, however I am now trying to plot the exit point based on the closing price closing below/above the 50 MA.

So I will focus on the long condition: A long condition is plotted successfully when the 20 MA crosses above the 50 MA. I then want an exit condition to be plotted when the closing price closes below the 50 MA again. My current code works, BUT, it plots every single instance of this so I have hundreds of 'exit' markers for every entry marker.

What do I need to do so that there is only one 'Exit Long' marker for each long condition? I obviously need to somehow count the occurrences and stop the code after 1 occurrence but I cannot seem to work out the syntax for it.

Any help is greatly appreciated.

    //@version=4
study(title="Moving Averages", shorttitle="MA20/50", overlay=true)

//defining moving averages
len2 = input(20, minval=1, title="Length")
src2 = input(close, title="Source")
out2 = sma(src2, len2)
len = input(50, minval=1, title="Length")
src = input(close, title="Source")
out = sma(src, len)

//plotting moving averages
plot(out, color=color.black, title="MA50")
plot(out2, color=color.blue, title="MA20")

//define long condition and plot marker for entry
longcondition = crossover(out2,out)
plotshape(series=longcondition,title="Long",style=shape.triangleup, location=location.belowbar, color=color.green, text="LONG", size=size.small)

//define close condition and plot marker for exit
closelong = close<=out 
plotshape(closelong,title="Exit Long", style=shape.triangleup, location=location.belowbar, color=color.red, text="EXIT LONG", size=size.small)

image shows long entry and multiple long exits

question from:https://stackoverflow.com/questions/65859597/plotting-single-instance-of-when-price-condition-is-met-how-to-plot-marker-on-o

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...