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

请求TradingView双均线交叉策略(5MA/20MA)浮盈锁定止损Pine脚本

双均线交叉+固定点位利润锁定策略

策略逻辑

  • 采用5周期均线(5MA)与20周期均线(20MA)的交叉信号开仓:上交叉做多,下交叉做空
  • 持仓期间,当未实现盈利产生后,在入场价上方(做空)或下方(做多)固定点位设置止损单,锁定已有利润,不再等待后续均线交叉信号平仓

Pine Script 代码实现

//@version=5
strategy("双均线交叉+固定点位利润锁定", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// 均线参数设置
fastLength = input.int(5, title="短期均线周期")
slowLength = input.int(20, title="长期均线周期")
profitLockPoints = input.int(100, title="利润锁定点位(点)")

// 计算均线
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)

// 交叉信号
longCondition = ta.crossover(fastMA, slowMA)
shortCondition = ta.crossunder(fastMA, slowMA)

// 入场价记录
var float entryPrice = na
if (longCondition)
    entryPrice := close
    strategy.entry("做多", strategy.long)
if (shortCondition)
    entryPrice := close
    strategy.entry("做空", strategy.short)

// 利润锁定止损计算
longStop = entryPrice - profitLockPoints * syminfo.mintick
shortStop = entryPrice + profitLockPoints * syminfo.mintick

// 浮盈判断与止损触发
// 做多时,当前价格高于入场价(有浮盈),触发止损
if (strategy.position_size > 0 and close > entryPrice)
    strategy.exit("多单止损锁定", "做多", stop=longStop)
// 做空时,当前价格低于入场价(有浮盈),触发止损
if (strategy.position_size < 0 and close < entryPrice)
    strategy.exit("空单止损锁定", "做空", stop=shortStop)

// 绘制均线
plot(fastMA, color=color.new(color.blue, 0), title="5MA")
plot(slowMA, color=color.new(color.red, 0), title="20MA")

代码关键说明

  • 参数可调整:通过输入框可修改均线周期、利润锁定点位,适配不同交易品种
  • 入场价追踪:用var变量记录每次开仓的入场价,确保止损计算基于实际入场成本
  • 浮盈触发条件:仅当持仓产生浮盈时才激活利润锁定止损,避免刚开仓就触发止损
  • 止损逻辑:做多时止损设为入场价减锁定点位,做空时设为入场价加锁定点位,锁定已有利润

内容的提问来源于stack exchange,提问作者Hirofumi Tachibana

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 19:03:18