Files
tradingview/bollinger_band_strategy_new.pine
2025-09-28 11:48:03 +08:00

133 lines
5.9 KiB
Plaintext

//@version=5
strategy("布林带反转策略", shorttitle="BB_REV", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, calc_on_every_tick=true)
// 输入参数
bb_length = input.int(90, "布林带长度", minval=10, maxval=200)
bb_mult = input.float(3.0, "布林带倍数", minval=1.0, maxval=10.0, step=0.1)
atr_length = input.int(14, "ATR计算周期", minval=5, maxval=50)
atr_mult = input.float(1.0, "止损ATR倍数", minval=0.5, maxval=5.0, step=0.1)
// 显示设置
show_bands = input.bool(true, "显示布林带")
show_signals = input.bool(true, "显示信号")
// 计算布林带(保持标准差计算)
bb_basis = ta.sma(close, bb_length)
bb_dev = bb_mult * ta.stdev(close, bb_length)
bb_upper = bb_basis + bb_dev
bb_lower = bb_basis - bb_dev
// 计算ATR(仅用于止损)
atr_value = ta.atr(atr_length)
// 显示布林带
plot(show_bands ? bb_basis : na, "中线", color=color.blue, linewidth=2)
plot(show_bands ? bb_upper : na, "上轨", color=color.red, linewidth=2)
plot(show_bands ? bb_lower : na, "下轨", color=color.green, linewidth=2)
// 交易条件
// 做空条件:价格突破上轨
short_condition = close > bb_upper and close[1] <= bb_upper[1]
// 做多条件:价格跌破下轨
long_condition = close < bb_lower and close[1] >= bb_lower[1]
// 做空止盈条件:价格跌破下轨
short_take_profit = close < bb_lower
// 做多止盈条件:价格突破上轨
long_take_profit = close > bb_upper
// 记录入场价格和止损位
var float long_entry_price = na
var float short_entry_price = na
var float long_stop_loss = na
var float short_stop_loss = na
// 执行交易逻辑
if strategy.position_size == 0
if long_condition
strategy.entry("做多", strategy.long)
long_entry_price := close
long_stop_loss := close - (atr_mult * atr_value)
if short_condition
strategy.entry("做空", strategy.short)
short_entry_price := close
short_stop_loss := close + (atr_mult * atr_value)
// 多头仓位管理
if strategy.position_size > 0
// 止盈:价格突破上轨
if long_take_profit
strategy.close("做多", comment="多头止盈")
long_entry_price := na
long_stop_loss := na
// 止损:价格跌破止损位
else if close <= long_stop_loss
strategy.close("做多", comment="多头止损")
long_entry_price := na
long_stop_loss := na
// 空头仓位管理
if strategy.position_size < 0
// 止盈:价格跌破下轨
if short_take_profit
strategy.close("做空", comment="空头止盈")
short_entry_price := na
short_stop_loss := na
// 止损:价格突破止损位
else if close >= short_stop_loss
strategy.close("做空", comment="空头止损")
short_entry_price := na
short_stop_loss := na
// 显示信号
if show_signals
if long_condition and strategy.position_size == 0
label.new(bar_index, low, "做多", color=color.green, style=label.style_label_up, size=size.normal, textcolor=color.white)
if short_condition and strategy.position_size == 0
label.new(bar_index, high, "做空", color=color.red, style=label.style_label_down, size=size.normal, textcolor=color.white)
if long_take_profit and strategy.position_size > 0
label.new(bar_index, high, "多头止盈", color=color.green, style=label.style_label_down, size=size.small, textcolor=color.white)
if short_take_profit and strategy.position_size < 0
label.new(bar_index, low, "空头止盈", color=color.red, style=label.style_label_up, size=size.small, textcolor=color.white)
// 显示止损线
plot(strategy.position_size > 0 and not na(long_stop_loss) ? long_stop_loss : na, "多头止损", color=color.red, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 and not na(short_stop_loss) ? short_stop_loss : na, "空头止损", color=color.red, style=plot.style_linebr, linewidth=1)
// 信息表格
if barstate.islast
var table info_table = table.new(position.top_right, 2, 10, bgcolor=color.white, border_width=1)
table.cell(info_table, 0, 0, "布林带反转策略", text_color=color.black, bgcolor=color.gray)
table.cell(info_table, 1, 0, "", text_color=color.black, bgcolor=color.gray)
table.cell(info_table, 0, 1, "布林带长度", text_color=color.black)
table.cell(info_table, 1, 1, str.tostring(bb_length), text_color=color.black)
table.cell(info_table, 0, 2, "布林带倍数", text_color=color.black)
table.cell(info_table, 1, 2, str.tostring(bb_mult), text_color=color.black)
table.cell(info_table, 0, 3, "ATR止损倍数", text_color=color.black)
table.cell(info_table, 1, 3, str.tostring(atr_mult), text_color=color.black)
table.cell(info_table, 0, 4, "当前ATR", text_color=color.black)
table.cell(info_table, 1, 4, str.tostring(math.round(atr_value, 4)), text_color=color.black)
table.cell(info_table, 0, 5, "上轨价位", text_color=color.black)
table.cell(info_table, 1, 5, str.tostring(math.round(bb_upper, 2)), text_color=color.black)
table.cell(info_table, 0, 6, "下轨价位", text_color=color.black)
table.cell(info_table, 1, 6, str.tostring(math.round(bb_lower, 2)), text_color=color.black)
table.cell(info_table, 0, 7, "当前价格", text_color=color.black)
table.cell(info_table, 1, 7, str.tostring(math.round(close, 2)), text_color=color.black)
table.cell(info_table, 0, 8, "仓位状态", text_color=color.black)
position_text = strategy.position_size > 0 ? "多头" : strategy.position_size < 0 ? "空头" : "空仓"
table.cell(info_table, 1, 8, position_text, text_color=color.black)
table.cell(info_table, 0, 9, "价格位置", text_color=color.black)
price_position = close > bb_upper ? "上轨之上" : close < bb_lower ? "下轨之下" : "轨道之间"
table.cell(info_table, 1, 9, price_position, text_color=color.black)