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
957 views
in Technique[技术] by (71.8m points)

algorithmic trading - Highlight candle which is near to moving average on pine script

I am trying to identify whether a candle is near the moving average(not touching the MA). The candle that touching the moving average is working perfectly. How can I highlight the candle which is near to the MA?

Here is my current code

sma20 = sma(close, 20)
bullishEC = close > open[1] and close[1] < open[1]
maLongCondition = close > sma20 and (cross(sma20,low) or cross(sma20,close))
buy = bullishEC and maLongCondition
barcolor( buy ? color.yellow : na, title="bullish")

Thank you.

question from:https://stackoverflow.com/questions/65651592/highlight-candle-which-is-near-to-moving-average-on-pine-script

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

1 Reply

0 votes
by (71.8m points)

You can calculate the desired area with sma20 value + your desired percentage:

colorAreaPercentage = input(1 , "Colored area precentage" , input.integer , minval = 0)
sma20 = sma(close, 20)
barColor =  close > sma20 and close < sma20 + (sma20 * colorAreaPercentage /100) ? color.fuchsia : close < sma20 and close > sma20 - (sma20 * colorAreaPercentage / 100) ? color.orange : na
if ((high >= sma20 and low <= sma20) or high == sma20 or low == sma20)
    barColor := na
barcolor(barColor)

This will color 1% above the moving average and 1% below the moving average

Edit:

if ((high >= sma20 and low <= sma20) or high == sma20 or low == sma20)
    barColor := na

This part of code will leave the touching candles uncolored.


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

...