本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © blackcat1402
//@version=6
indicator('[blackcat] L1 Dynamic Multi-Layer Bollinger Bands', shorttitle = 'L1 DMBB', overlay = true, max_labels_count = 500)
// Function to calculate Bollinger Bands
calculate_bollinger_bands(close_price, period, multiplier1, multiplier2) =>
// Calculate the middle band as the simple moving average of the close price over the specified period
middle_band = ta.sma(close_price, period)
// Calculate the upper band as the middle band plus the specified multiplier times the standard deviation of the close price over the period
upper_band = middle_band + multiplier1 * ta.stdev(close_price, period)
// Calculate the lower band as the middle band minus the specified multiplier times the standard deviation of the close price over the period
lower_band = middle_band - multiplier1 * ta.stdev(close_price, period)
// Calculate the upper outer band as the middle band plus the second specified multiplier times the standard deviation of the close price over the period
upper_outer_band = middle_band + multiplier2 * ta.stdev(close_price, period)
// Calculate the lower outer band as the middle band minus the second specified multiplier times the standard deviation of the close price over the period
lower_outer_band = middle_band - multiplier2 * ta.stdev(close_price, period)
// Determine the fill color based on whether the middle band is greater than its previous value
fill_color = middle_band > middle_band[1] ? color.new(color.green, 80) : color.new(color.red, 80)
// Return the calculated bands and fill color
[middle_band, upper_band, lower_band, upper_outer_band, lower_outer_band, fill_color]
// Define the period and multipliers
period = 21
multiplier1 = 1.382
multiplier2 = 2.56
// Calculate the Bollinger Bands
[middle_band, upper_band, lower_band, upper_outer_band, lower_outer_band, fill_color] = calculate_bollinger_bands(close, period, multiplier1, multiplier2)
// Plot the middle band with a white color
plot(middle_band, color = color.new(color.white, 0))
// Fill the area between the upper band and lower band with the defined fill color
fill(plot(upper_band), plot(lower_band), color = fill_color)
// Plot the upper band, upper outer band, lower band, and lower outer band with red and blue colors respectively
plot(upper_band, color = color.new(color.red, 0))
plot(upper_outer_band, color = color.new(color.red, 0))
plot(lower_band, color = color.new(color.blue, 0))
plot(lower_outer_band, color = color.new(color.blue, 0))
// Add buy label when the price crosses below the lower band
var label buyLabel = na
if ta.crossover(close, lower_band)
// label.delete(buyLabel)
buyLabel := label.new(x = bar_index, y = low, text = 'BUY', style = label.style_label_up, color = color.green, textcolor = color.white, size = size.small)
buyLabel
// Add sell label when the price crosses above the upper band
var label sellLabel = na
if ta.crossunder(close, upper_band)
// label.delete(sellLabel)
sellLabel := label.new(x = bar_index, y = high, text = 'SELL', style = label.style_label_down, color = color.red, textcolor = color.white, size = size.small)
sellLabel
// Alerts for buy and sell signals
alertcondition(ta.crossover(close, lower_band), title = 'Buy Alert', message = 'Price crossed below the lower band. Buy signal.')
alertcondition(ta.crossunder(close, upper_band), title = 'Sell Alert', message = 'Price crossed above the upper band. Sell signal.')