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

如何编写Stochastic指标超买区K穿D时做多的Pine Script策略

Stochastic指标Pine Script策略实现

以下是适配你需求的完整Pine Script v5代码,核心入场条件已做注释标注:

//@version=5
strategy("Stochastic交易策略", overlay=false, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// 定义Stochastic参数
periodK = input.int(14, title="K周期")
periodD = input.int(3, title="D周期")
smoothK = input.int(3, title="K平滑周期")
overbought = input.int(80, title="超买阈值")
oversold = input.int(20, title="超卖阈值")

// 计算Stochastic的K、D值
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)

// 绘制指标线和超买超卖线
plot(k, title="K线", color=color.new(color.blue, 0))
plot(d, title="D线", color=color.new(color.red, 0))
hline(overbought, "超买线", color=color.green)
hline(oversold, "超卖线", color=color.orange)

// 核心入场条件(你需要的逻辑在这里)
// 多头:K上穿D,且交叉时K处于超买区间
longCondition = ta.crossover(k, d) and k >= overbought
// 空头:K下穿D,且交叉时K处于超卖区间
shortCondition = ta.crossunder(k, d) and k <= oversold

// 下单执行
if (longCondition)
    strategy.entry("多头", strategy.long)

if (shortCondition)
    strategy.entry("空头", strategy.short)

关键逻辑说明

  • ta.crossover(k, d)是Pine Script内置的上穿判断函数,返回布尔值,代表当前周期K值刚好从下往上突破D值
  • 用and逻辑符连接区间判断条件k >= overbought,即可限定交叉必须发生在超买区间才会触发信号,同理适配空头的超卖区间判断
  • 超买超卖阈值、指标周期都可以在参数面板自行调整,不需要修改核心代码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 07:54:03