Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

pine script - Why don't the BOLLINGER (BB) lines overlap the candles?

I have a script in Pine Script and I have a problem because at some point my drawn lines BB and MACD do not overlap the chart candles, so I get an error when I want to check the price above the Bollinger line

//@version=4
// Bollinger Bands on MACD
study(title="Bollinger Bands on MACD", shorttitle="BB on MACD", scale=scale.none, overlay=true)
src = input(close, title="Source")

// 1 - Compute MACD
fastLength = input(12, minval=1, title="MACD Fast EMA Length")
slowLength=input(26, minval=1, title="MACD slow EMA Length")
signalSmoothing=input(9, minval=1, title="MACD Signal Smoothing")

fastEMA = ema(src, fastLength)
slowEMA = ema(src, slowLength)
macd = fastEMA - slowEMA
signal = ema(macd, signalSmoothing)
hist = macd - signal

// 2 - Compute Bollinger Bands on MACD
lengthBB = input(20, minval=1, title="BB Length")
basis = sma(macd, lengthBB)
mult = input(2.0, minval=0.001, maxval=50, title="BB Std Dev Mult. Factor")
upperBB = basis + (mult * stdev(macd, lengthBB))
lowerBB = basis - (mult * stdev(macd, lengthBB))

// Plot colors
bar_grow_above = #26A69A
bar_grow_below = #FFCDD2
bar_fall_above = #B2DFDB
bar_fall_below = #EF5350
bar_macd = #0094ff
bar_signal = #ff6a00


// 3 - Plotting
// 3.1 - Bollinger Bands on MACD
plot(basis, color= color.green, style=plot.style_line, title="BB base line" )
p1 = plot(upperBB, color= color.green, style=plot.style_line, title="BB Upper")
p2 = plot(lowerBB, color= color.lime, style=plot.style_line, title="BB Lower")
fill(p1, p2, color=#9932CC, title="BB Background", transp=95)

// 3.2 - MACD
plot(hist, title="MACD Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? bar_grow_above : bar_fall_above) : (hist[1] < hist ? bar_grow_below : bar_fall_below) ), transp=75 ) // "Deep Pink" color
plot(signal, color= color.orange, style=plot.style_line, title="Signal")
plot(macd, color= color.blue, style=plot.style_line, title="MACD") // "Deep Sky Blue" color


bbCondition= close < lowerBB 
plotshape( bbCondition, title="SYGNA?", location=location.top, color=color.red, transp=0, style=shape.triangledown, text="Do something")

Am i doing something wrong? how can I fix it? My condition is:

bbCondition = close <lowerBB
plotshape (bbCondition, title = "SIGNAL", location = location.top, color = color.red, transp = 0, style = shape.triangledown, text = "Do something")

unfortunately does not work

question from:https://stackoverflow.com/questions/66062018/why-dont-the-bollinger-bb-lines-overlap-the-candles

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

1 Answer

0 votes
by (71.8m points)
  1. When you assign close < lowerBB - it will compare the asset's price with the lower bollinger band of the MACD, not with the on-chart bollingers. Replace the close with macd variable.
  2. Simple comparison with < will lead to consecutive signals, use the crossover/crossunder functions to get the signal only on crossovers.

//@version=4
// Bollinger Bands on MACD
study(title="Bollinger Bands on MACD", shorttitle="BB on MACD", scale=scale.none, overlay=true)
src = input(close, title="Source")

// 1 - Compute MACD
fastLength = input(12, minval=1, title="MACD Fast EMA Length")
slowLength=input(26, minval=1, title="MACD slow EMA Length")
signalSmoothing=input(9, minval=1, title="MACD Signal Smoothing")

fastEMA = ema(src, fastLength)
slowEMA = ema(src, slowLength)
macd = fastEMA - slowEMA
signal = ema(macd, signalSmoothing)
hist = macd - signal

// 2 - Compute Bollinger Bands on MACD
lengthBB = input(20, minval=1, title="BB Length")
basis = sma(macd, lengthBB)
mult = input(2.0, minval=0.001, maxval=50, title="BB Std Dev Mult. Factor")
upperBB = basis + (mult * stdev(macd, lengthBB))
lowerBB = basis - (mult * stdev(macd, lengthBB))

// Plot colors
bar_grow_above = #26A69A
bar_grow_below = #FFCDD2
bar_fall_above = #B2DFDB
bar_fall_below = #EF5350
bar_macd = #0094ff
bar_signal = #ff6a00


// 3 - Plotting
// 3.1 - Bollinger Bands on MACD
plot(basis, color= color.green, style=plot.style_line, title="BB base line" )
p1 = plot(upperBB, color= color.green, style=plot.style_line, title="BB Upper")
p2 = plot(lowerBB, color= color.lime, style=plot.style_line, title="BB Lower")
fill(p1, p2, color=#9932CC, title="BB Background", transp=95)

// 3.2 - MACD
plot(hist, title="MACD Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? bar_grow_above : bar_fall_above) : (hist[1] < hist ? bar_grow_below : bar_fall_below) ), transp=75 ) // "Deep Pink" color
plot(signal, color= color.orange, style=plot.style_line, title="Signal")
plot(macd, color= color.blue, style=plot.style_line, title="MACD") // "Deep Sky Blue" color

bbCondition= cross(macd, lowerBB) 
plotshape(bbCondition ? true : na, title="SYGNA?", location=location.top, color=color.red, transp=0, style=shape.triangledown, text="Do something")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...