PineEA / 策略无需编译 19 6

EMA 金叉 + ATR 追踪止损策略

XAUOF 官方·3 小时前发布

EMA 金叉进场,ATR 动态止损,可选只做多,自带回测参数

EMA 金叉 + ATR 追踪止损策略
ema_crossover_trailing_stop_strategy.pine113
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//@version=6
strategy("EMA金叉死叉追踪止损策略", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// ==================== 参数设置 ====================
emaFastLen = input.int(9, "快速EMA周期", minval=1, tooltip="较短周期的EMA,对价格变化更敏感")
emaSlowLen = input.int(21, "慢速EMA周期", minval=1, tooltip="较长周期的EMA,代表趋势方向")
atrLen = input.int(14, "ATR周期", minval=1, tooltip="用于计算波动性的周期")
atrMult = input.float(2.0, "ATR止损倍数", minval=0.1, step=0.1, tooltip="止损距离 = ATR × 此倍数,推荐1.5-3.0")
riskReward = input.float(2.0, "风险回报比", minval=0.1, step=0.1, tooltip="止盈距离是止损距离的倍数,推荐2.0-3.0")
longOnly = input.bool(false, "仅做多", tooltip="开启后只做多单,不做空单")
riskPercent = input.float(1.0, "每单风险百分比", minval=0.1, maxval=100, step=0.1, tooltip="每笔交易风险占账户权益的百分比,推荐1%-2%")

// ==================== 计算指标 ====================
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
atr = ta.atr(atrLen)

// ==================== 交易信号 ====================
longCondition = ta.crossover(emaFast, emaSlow)
shortCondition = ta.crossunder(emaFast, emaSlow)

// ==================== 持仓状态跟踪 ====================
var float entryPrice = na       // 入场价
var float stopLoss = na          // 当前止损价
var float takeProfit = na        // 止盈价
var int positionType = 0         // 1=多头, -1=空头, 0=无持仓

// 检测当前持仓
bool inLong = strategy.position_size > 0
bool inShort = strategy.position_size < 0
bool inPosition = inLong or inShort

// ==================== 开仓逻辑 ====================
// 多头开仓
if longCondition and not inPosition
    entryPrice := close
    stopLoss := entryPrice - atr * atrMult
    takeProfit := entryPrice + (entryPrice - stopLoss) * riskReward
    
    // 计算仓位:风险金额 / 每手风险金额
    riskAmount = strategy.equity * riskPercent / 100
    riskPerShare = entryPrice - stopLoss
    // 防止除以零
    if riskPerShare > 0
        qty = riskAmount / riskPerShare
        strategy.entry("多单", strategy.long, qty=qty)
        positionType := 1
        label.new(bar_index, low, "买入\n" + str.tostring(entryPrice, "#.##"), 
                  color=color.new(color.green, 0), 
                  textcolor=color.white, 
                  style=label.style_label_up, 
                  size=size.small)

// 空头开仓
if shortCondition and not inPosition and not longOnly
    entryPrice := close
    stopLoss := entryPrice + atr * atrMult
    takeProfit := entryPrice - (stopLoss - entryPrice) * riskReward
    
    // 计算仓位
    riskAmount = strategy.equity * riskPercent / 100
    riskPerShare = stopLoss - entryPrice
    if riskPerShare > 0
        qty = riskAmount / riskPerShare
        strategy.entry("空单", strategy.short, qty=qty)
        positionType := -1
        label.new(bar_index, high, "卖出\n" + str.tostring(entryPrice, "#.##"), 
                  color=color.new(color.red, 0), 
                  textcolor=color.white, 
                  style=label.style_label_down, 
                  size=size.small)

// ==================== 追踪止损逻辑 ====================
// 多头追踪止损:止损只能上移不能下移
if inLong and positionType == 1
    newStopLoss = close - atr * atrMult
    if not na(stopLoss) and newStopLoss > stopLoss
        stopLoss := newStopLoss
    strategy.exit("多单止损止盈", "多单", stop=stopLoss, limit=takeProfit)

// 空头追踪止损:止损只能下移不能上移
if inShort and positionType == -1
    newStopLoss = close + atr * atrMult
    if not na(stopLoss) and newStopLoss < stopLoss
        stopLoss := newStopLoss
    strategy.exit("空单止损止盈", "空单", stop=stopLoss, limit=takeProfit)

// 平仓后重置状态
if not inPosition and positionType != 0
    positionType := 0
    entryPrice := na
    stopLoss := na
    takeProfit := na

// ==================== 绘图 ====================
// 绘制均线
plot(emaFast, "快速EMA", color=color.new(color.aqua, 0), linewidth=2)
plot(emaSlow, "慢速EMA", color=color.new(color.orange, 0), linewidth=2)

// 绘制当前止损线
plot(inPosition ? stopLoss : na, "追踪止损", 
     color=color.new(color.red, 20), 
     style=plot.style_linebr, 
     linewidth=2)

// 绘制止盈线
plot(inPosition ? takeProfit : na, "止盈位", 
     color=color.new(color.green, 20), 
     style=plot.style_linebr, 
     linewidth=2)

// 背景色:多头持仓浅绿,空头持仓浅红
bgcolor(inLong ? color.new(color.green, 95) : inShort ? color.new(color.red, 95) : na)

作品说明

思路说明

这是一个基于 EMA 金叉死叉的趋势跟踪策略。快线上穿慢线时做多,下穿时做空。止损用 ATR 的倍数动态设置,随着价格向有利方向移动,止损会自动跟进(追踪止损),但永远不会往不利方向回退。止盈根据风险回报比自动计算。仓位大小根据账户权益和单笔风险百分比动态调整,确保每笔交易的风险可控。

使用说明

安装方法

  1. 打开 TradingView,进入任意图表
  2. 点击底部"Pine 编辑器"
  3. 复制上述完整代码粘贴进去
  4. 点击"添加到图表"

参数调整

  • 快速EMA周期(默认9):越小越灵敏,信号越多但假信号也多;推荐 5-12
  • 慢速EMA周期(默认21):越大趋势越平滑,推荐 20-50
  • ATR周期(默认14):计算波动性的窗口,通常保持 14
  • ATR止损倍数(默认2.0):倍数越大止损越宽松,不易被扫,但单次亏损更大;震荡市场用 1.5-2.0,趋势市场可用 2.5-3.0
  • 风险回报比(默认2.0):止盈是止损的倍数,2.0 表示盈亏比 1:2;激进可用 1.5,保守可用 3.0
  • 仅做多:牛市或不想做空时勾选
  • 每单风险百分比(默认1%):单笔交易最大亏损占账户的比例,新手建议 0.5%-1%,熟练后可用 1%-2%

信号识别

  • 图表上水色线是快速EMA,橙色线是慢速EMA
  • 出现绿色"买入"标签代表做多开仓,红色"卖出"标签代表做空开仓
  • 红色虚线是当前追踪止损位,会随价格向有利方向移动但不会回撤
  • 绿色虚线是止盈位
  • 背景浅绿色表示持有多单,浅红色表示持有空单

注意事项

  1. 这是趋势跟踪策略,适合有明确趋势的市场,震荡行情会频繁止损
  2. 追踪止损会自动保护利润,不需要手动平仓
  3. 回测时注意佣金和滑点设置是否符合实际
  4. 仓位根据风险百分比自动计算,确保每笔亏损可控
  5. 建议先在模拟盘测试,熟悉策略特性后再用于实盘
  6. 可结合更大周期(如日线、周线)的趋势方向过滤信号,提高胜率

评论

后可以留言,比如反馈用着怎么样、想加什么功能。

还没有人评论,来抢个沙发。