You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script策略编写求助:基于EMA、MA的比例出入场规则实现

修正后可直接运行的Pine Script代码

//@version=4
strategy("低溢价比率趋势策略", overlay = true, default_qty_type = strategy.cash, default_qty_value = 10000, initial_capital = 100000)

// 可自定义参数
sma_period = input.int(50, title = "SMA周期")
ema_period = input.int(20, title = "EMA周期")
entry_discount_pct = input.float(10.0, title = "入场价格较EMA低的百分比(%)", step = 0.5)
exit_premium_pct = input.float(10.0, title = "离场价格较EMA高的百分比(%)", step = 0.5)

// 指标计算
smaClose = sma(close, sma_period)
emaClose = ema(close, ema_period)

// 条件逻辑
long_entry_condition = emaClose < smaClose and close <= emaClose * (1 - entry_discount_pct / 100)
long_exit_condition = emaClose > smaClose and close >= emaClose * (1 + exit_premium_pct / 100)

// 交易执行
strategy.entry("做多入场", strategy.long, when = long_entry_condition)
strategy.close("做多入场", when = long_exit_condition)

// 指标绘制
plot(smaClose, color = color.new(color.yellow, 0), linewidth = 3, title = "SMA均线")
plot(emaClose, color = color.new(color.blue, 0), linewidth = 1, title = "EMA均线")

// 出入场信号标记
plotshape(long_entry_condition, title = "入场信号", location = location.belowbar, color = color.green, style = shape.labelup, text = "多", textcolor = color.white, size = size.small)
plotshape(long_exit_condition, title = "离场信号", location = location.abovebar, color = color.red, style = shape.labeldown, text = "平", textcolor = color.white, size = size.small)

修改说明

  • 新增了可调节参数模块,你可以直接在交易界面的参数设置面板调整均线周期、入场折价比例、离场溢价比例,不需要修改代码本身
  • 把文字化的策略条件转换成了Pine Script可识别的数值逻辑:
    • 价格较EMA低指定比例的逻辑为close <= emaClose * (1 - 目标百分比 / 100)
    • 价格较EMA高指定比例的逻辑为close >= emaClose * (1 + 目标百分比 / 100)
  • 补充了出入场信号的K线标记,方便回测的时候直观看到触发点位
  • 默认添加了回测初始资金设置,你可以根据自身需求调整对应数值

内容的提问来源于stack exchange,提问作者joshua wilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 03:03:02