add more strategies

This commit is contained in:
jackyu66git
2025-07-21 20:31:25 +08:00
parent d75c202975
commit 6907ce7d8c
6 changed files with 379 additions and 69 deletions
+22 -25
View File
@@ -1,32 +1,30 @@
//@version=6
strategy("布林带ATR反转策略", shorttitle="BBB_ATR", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, calc_on_every_tick=true)
//@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)
atr_multiplier = input.float(3.0, "ATR乘数(轨道)", minval=1.0, maxval=10.0, step=0.1)
atr_stop_multiplier = input.float(3.0, "ATR乘数(止损)", minval=0.5, maxval=5.0, step=0.1)
atr_length = input.int(9, "ATR计算周期", minval=5, maxval=50)
bb_length = input.int(41, "布林带长度", minval=10, maxval=200)
bb_mult = input.float(2.3, "布林带倍数", minval=1.0, maxval=10.0, step=0.1)
atr_length = input.int(11, "ATR计算周期", minval=5, maxval=90)
atr_mult = input.float(3, "止损ATR倍数", minval=0.5, maxval=10.0, step=0.1)
// 显示设置
show_bands = input.bool(true, "显示布林带")
show_signals = input.bool(true, "显示信号")
// 计算移动平均线(中线)
bb_middle = ta.sma(close, bb_length)
// 计算布林带(保持标准差计算)
bb_basis = ta.ema(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(仅用于止损)
atr_value = ta.atr(atr_length)
// 计算上下轨
bb_upper = bb_middle + (atr_multiplier * atr_value)
bb_lower = bb_middle - (atr_multiplier * atr_value)
// 显示布林带
plot(show_bands ? bb_middle : na, "中线", color=color.blue, linewidth=2)
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]
@@ -35,10 +33,10 @@ 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
short_take_profit = low < bb_lower
// 做多止盈条件:价格突破上轨
long_take_profit = close > bb_upper
long_take_profit = close-atr_value*0.5 > bb_upper
// 记录入场价格和止损位
var float long_entry_price = na
@@ -51,12 +49,12 @@ if strategy.position_size == 0
if long_condition
strategy.entry("做多", strategy.long)
long_entry_price := close
long_stop_loss := close - (atr_stop_multiplier * atr_value)
long_stop_loss := close - (atr_mult * atr_value)
if short_condition
strategy.entry("做空", strategy.short)
short_entry_price := close
short_stop_loss := close + (atr_stop_multiplier * atr_value)
short_stop_loss := close + (atr_mult * atr_value)
// 多头仓位管理
if strategy.position_size > 0
@@ -99,21 +97,20 @@ if show_signals
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, "布林带ATR反转策略", text_color=color.black, bgcolor=color.gray)
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, "ATR轨道乘数", text_color=color.black)
table.cell(info_table, 1, 2, str.tostring(atr_multiplier), 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_stop_multiplier), 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)