Files
tradingview/三重滤网均线交易策略.pine
T

260 lines
12 KiB
Plaintext

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © 三重滤网均线交易策略
//@version=6
strategy("三重滤网均线交易策略", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=100000)
// ==================== 参数设置 ====================
// 均线参数
ema156_length = input.int(156, "大趋势均线周期 (EMA156)", minval=1, group="均线设置")
ema52_length = input.int(52, "小趋势均线周期 (EMA52)", minval=1, group="均线设置")
// MACD参数
macd_fast = input.int(12, "MACD快线周期", minval=1, group="MACD设置")
macd_slow = input.int(26, "MACD慢线周期", minval=1, group="MACD设置")
macd_signal = input.int(9, "MACD信号线周期", minval=1, group="MACD设置")
// 策略参数
confirm_bars = input.int(3, "金叉/死叉后确认K线数", minval=1, maxval=10, group="策略设置")
breakout_bars = input.int(10, "突破EMA52确认K线数", minval=1, maxval=20, group="策略设置")
risk_reward_ratio = input.float(2.0, "盈亏比", minval=1.0, maxval=5.0, step=0.5, group="策略设置")
// 显示设置
show_ema156 = input.bool(true, "显示EMA156", group="显示设置")
show_ema52 = input.bool(true, "显示EMA52", group="显示设置")
show_signals = input.bool(true, "显示信号标记", group="显示设置")
// ==================== 计算均线 ====================
ema156 = ta.ema(close, ema156_length)
ema52 = ta.ema(close, ema52_length)
// ==================== 计算MACD ====================
[macd_line, signal_line, hist] = ta.macd(close, macd_fast, macd_slow, macd_signal)
// MACD金叉和死叉
macd_golden_cross = ta.crossover(macd_line, signal_line)
macd_death_cross = ta.crossunder(macd_line, signal_line)
// ==================== 趋势判断 ====================
// 大趋势:价格相对于EMA156的位置
big_trend_bullish = close > ema156 // 大级别多头趋势
big_trend_bearish = close < ema156 // 大级别空头趋势
// 小趋势:价格相对于EMA52的位置
small_trend_bullish = close > ema52 // 小级别多头趋势
small_trend_bearish = close < ema52 // 小级别空头趋势
// ==================== 金叉/死叉后趋势确认 ====================
// 记录最近的金叉/死叉位置
var int bars_since_golden = na
var int bars_since_death = na
var float golden_cross_price = na
var float death_cross_price = na
if macd_golden_cross
bars_since_golden := 0
golden_cross_price := close
else if not na(bars_since_golden)
bars_since_golden := bars_since_golden + 1
if macd_death_cross
bars_since_death := 0
death_cross_price := close
else if not na(bars_since_death)
bars_since_death := bars_since_death + 1
// 检查金叉后confirm_bars根K线是否持续上涨(收盘价逐步走高或维持)
golden_cross_confirmed = false
if not na(bars_since_golden) and bars_since_golden >= confirm_bars and bars_since_golden <= confirm_bars + 5
// 检查金叉后的K线是否按上涨趋势运行
trend_up_after_golden = true
for i = 1 to confirm_bars
if close[bars_since_golden - i] < golden_cross_price
trend_up_after_golden := false
break
golden_cross_confirmed := trend_up_after_golden
// 检查死叉后confirm_bars根K线是否持续下跌
death_cross_confirmed = false
if not na(bars_since_death) and bars_since_death >= confirm_bars and bars_since_death <= confirm_bars + 5
trend_down_after_death = true
for i = 1 to confirm_bars
if close[bars_since_death - i] > death_cross_price
trend_down_after_death := false
break
death_cross_confirmed := trend_down_after_death
// ==================== 价格站稳EMA52确认 ====================
// 检查价格是否在近期突破并站稳EMA52
price_above_ema52_stable = true
for i = 0 to math.min(breakout_bars - 1, bar_index)
if close[i] < ema52[i]
price_above_ema52_stable := false
break
price_below_ema52_stable = true
for i = 0 to math.min(breakout_bars - 1, bar_index)
if close[i] > ema52[i]
price_below_ema52_stable := false
break
// 检测EMA52突破
ema52_breakout_up = ta.crossover(close, ema52)
ema52_breakout_down = ta.crossunder(close, ema52)
// 近期是否有EMA52突破
recent_ema52_breakout_up = false
recent_ema52_breakout_down = false
for i = 0 to breakout_bars - 1
if ema52_breakout_up[i]
recent_ema52_breakout_up := true
if ema52_breakout_down[i]
recent_ema52_breakout_down := true
// ==================== 计算回调低点/高点作为止损 ====================
// 寻找最近的回调低点(用于多头止损)
var float swing_low = na
lookback_period = 20
swing_low := ta.lowest(low, lookback_period)
// 寻找最近的回调高点(用于空头止损)
var float swing_high = na
swing_high := ta.highest(high, lookback_period)
// ==================== 入场条件 ====================
// 多头入场条件:
// 1. 大级别为多头趋势(价格在EMA156上方)
// 2. 小级别刚从空头转多头(价格突破EMA52)
// 3. MACD金叉已确认(金叉后K线继续上涨)
// 4. 价格站稳EMA52
long_condition = big_trend_bullish and
small_trend_bullish and
(golden_cross_confirmed or (not na(bars_since_golden) and bars_since_golden <= breakout_bars)) and
(recent_ema52_breakout_up or price_above_ema52_stable) and
macd_line > signal_line
// 空头入场条件(反向操作):
// 1. 大级别为空头趋势(价格在EMA156下方)
// 2. 小级别刚从多头转空头(价格跌破EMA52)
// 3. MACD死叉已确认
// 4. 价格站稳EMA52下方
short_condition = big_trend_bearish and
small_trend_bearish and
(death_cross_confirmed or (not na(bars_since_death) and bars_since_death <= breakout_bars)) and
(recent_ema52_breakout_down or price_below_ema52_stable) and
macd_line < signal_line
// ==================== 计算止损止盈 ====================
// 多头止损止盈
long_stop_loss = swing_low
long_risk = close - long_stop_loss
long_take_profit = close + long_risk * risk_reward_ratio
// 空头止损止盈
short_stop_loss = swing_high
short_risk = short_stop_loss - close
short_take_profit = close - short_risk * risk_reward_ratio
// ==================== 执行交易 ====================
// 避免重复开仓
var bool in_long_position = false
var bool in_short_position = false
// 更新持仓状态
if strategy.position_size > 0
in_long_position := true
in_short_position := false
else if strategy.position_size < 0
in_long_position := false
in_short_position := true
else
in_long_position := false
in_short_position := false
// 多头入场
if long_condition and not in_long_position and long_risk > 0
strategy.entry("多头", strategy.long)
strategy.exit("多头止盈止损", "多头", stop=long_stop_loss, limit=long_take_profit)
// 空头入场
if short_condition and not in_short_position and short_risk > 0
strategy.entry("空头", strategy.short)
strategy.exit("空头止盈止损", "空头", stop=short_stop_loss, limit=short_take_profit)
// ==================== 绘制均线 ====================
plot(show_ema156 ? ema156 : na, "EMA156 大趋势", color=color.new(color.orange, 0), linewidth=2)
plot(show_ema52 ? ema52 : na, "EMA52 小趋势", color=color.new(color.blue, 0), linewidth=1)
// ==================== 绘制趋势背景 ====================
// 大趋势背景色
bg_color = big_trend_bullish ? color.new(color.green, 93) : big_trend_bearish ? color.new(color.red, 93) : na
bgcolor(bg_color, title="大趋势背景")
// ==================== 绘制信号标记 ====================
// 多头信号
plotshape(show_signals and long_condition and not in_long_position[1] ? low : na,
title="多头信号", style=shape.triangleup, location=location.belowbar,
color=color.new(color.green, 0), size=size.small, text="买入")
// 空头信号
plotshape(show_signals and short_condition and not in_short_position[1] ? high : na,
title="空头信号", style=shape.triangledown, location=location.abovebar,
color=color.new(color.red, 0), size=size.small, text="卖出")
// MACD金叉标记
plotshape(show_signals and macd_golden_cross ? low : na,
title="MACD金叉", style=shape.circle, location=location.belowbar,
color=color.new(color.lime, 0), size=size.tiny)
// MACD死叉标记
plotshape(show_signals and macd_death_cross ? high : na,
title="MACD死叉", style=shape.circle, location=location.abovebar,
color=color.new(color.fuchsia, 0), size=size.tiny)
// ==================== 绘制止损止盈线 ====================
plot(strategy.position_size > 0 ? long_stop_loss : na, "多头止损", color=color.red, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size > 0 ? long_take_profit : na, "多头止盈", color=color.green, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? short_stop_loss : na, "空头止损", color=color.red, style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? short_take_profit : na, "空头止盈", color=color.green, style=plot.style_linebr, linewidth=1)
// ==================== 信息面板 ====================
var table info_panel = table.new(position.top_right, 2, 8, bgcolor=color.new(color.black, 80), border_width=1)
if barstate.islast
table.cell(info_panel, 0, 0, "指标", text_color=color.white, text_size=size.small)
table.cell(info_panel, 1, 0, "状态", text_color=color.white, text_size=size.small)
table.cell(info_panel, 0, 1, "大趋势(EMA156)", text_color=color.white, text_size=size.small)
table.cell(info_panel, 1, 1, big_trend_bullish ? "📈 多头" : "📉 空头",
text_color=big_trend_bullish ? color.lime : color.red, text_size=size.small)
table.cell(info_panel, 0, 2, "小趋势(EMA52)", text_color=color.white, text_size=size.small)
table.cell(info_panel, 1, 2, small_trend_bullish ? "📈 多头" : "📉 空头",
text_color=small_trend_bullish ? color.lime : color.red, text_size=size.small)
table.cell(info_panel, 0, 3, "MACD状态", text_color=color.white, text_size=size.small)
table.cell(info_panel, 1, 3, macd_line > signal_line ? "金叉状态" : "死叉状态",
text_color=macd_line > signal_line ? color.lime : color.red, text_size=size.small)
table.cell(info_panel, 0, 4, "持仓状态", text_color=color.white, text_size=size.small)
position_text = strategy.position_size > 0 ? "多头持仓" : strategy.position_size < 0 ? "空头持仓" : "空仓"
position_color = strategy.position_size > 0 ? color.lime : strategy.position_size < 0 ? color.red : color.gray
table.cell(info_panel, 1, 4, position_text, text_color=position_color, text_size=size.small)
table.cell(info_panel, 0, 5, "EMA156", text_color=color.white, text_size=size.small)
table.cell(info_panel, 1, 5, str.tostring(ema156, "#.##"), text_color=color.orange, text_size=size.small)
table.cell(info_panel, 0, 6, "EMA52", text_color=color.white, text_size=size.small)
table.cell(info_panel, 1, 6, str.tostring(ema52, "#.##"), text_color=color.blue, text_size=size.small)
table.cell(info_panel, 0, 7, "盈亏比", text_color=color.white, text_size=size.small)
table.cell(info_panel, 1, 7, "1:" + str.tostring(risk_reward_ratio, "#.#"), text_color=color.yellow, text_size=size.small)
// ==================== 警报条件 ====================
alertcondition(long_condition and not in_long_position[1], title="多头入场信号", message="三重滤网策略:多头入场信号触发!大趋势多头,小趋势回调完成,MACD金叉确认")
alertcondition(short_condition and not in_short_position[1], title="空头入场信号", message="三重滤网策略:空头入场信号触发!大趋势空头,小趋势反弹完成,MACD死叉确认")
alertcondition(macd_golden_cross, title="MACD金叉", message="MACD金叉出现")
alertcondition(macd_death_cross, title="MACD死叉", message="MACD死叉出现")