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

如何在Pine Script中获取交叉值并按指定阈值过滤绘制?

基于快慢随机震荡指标的交易策略与指标绘制问题

我是结构化编程语言新手,正在基于快慢两条随机震荡指标(Stochastic Oscillator)编写指标与交易策略,需依据两条指标的交叉(crossover)/下穿(crossunder)值、快慢指标数值判断开平仓条件,具体逻辑如下:

结构化英语逻辑

IF crossover is below 25 OR slow stoch oscil is below 20 THEN
   Open long position
ELSE
   IF fast stoch oscil is above 90 THEN
      Close long position
   ELSE
      Do nothing
   ENDIF
ENDIF
IF crossunder is above 75 OR slow stoch oscil is above 80 THEN
   Open short position
ELSE
   IF fast stoch oscil is below 10 THEN
      Close short position
   ELSE
      Do nothing
   ENDIF
ENDIF
EXIT

伪代码

crossover = (stoch_slow, stoch_fast)
crossunder = (stoch_slow, stoch_fast)
long
  if crossover < 25 or stoch_slow < 20
    close_long
      if long == true and stoch_fast > 90
short
  if crossunder > 75 or stoch_slow > 80
    close_short
     if short == true and stoch_fast < 10

我查阅了Pine Script用户手册相关章节未找到类似示例,目前仅能实现所有交叉/下穿点的绘制,代码如下:

当前实现代码

//@version=5
indicator(title="Stochastic oscillator-SlowFast", shorttitle="Stoch-SF", overlay=false)
// Input and calculate values
periodKs = input.int(10)
smoothKs = input.int(10)
ks = ta.sma(ta.stoch(close, high, low, periodKs), smoothKs)
periodKf = input.int(6)
smoothKf = input.int(3)
kf = ta.sma(ta.stoch(close, high, low, periodKf), smoothKf)
// Plot crossover/under circles
long = plot(ta.crossover(ks, kf)? kf : na, color=color.teal, linewidth=3, style=plot.style_circles)
short = plot(ta.crossunder(ks, kf)? kf : na, color=color.red, linewidth=3, style=plot.style_circles)
// Plot Stochastics lines
plot(ks, color=color.yellow)
plot(kf, color=color.white)
hline(18)
hline(36)
hline(64)
hline(82)

后续我找到另一种标记交叉/下穿的方式,但仍无法解决布尔值与浮点型值的类型统一问题:

后续尝试代码片段

co = ta.crossover(ks, kf)
cu = ta.crossunder(ks, kf)
// Colour the background when a crossover/under happens
backgroundColour = if co
    color.new(color.teal, 80)
else if cu
    color.new(color.orange, 80)
bgcolor(backgroundColour)

核心需求

如何获取交叉/下穿的具体数值并仅绘制符合指定阈值的交叉点?


解决方案

1. 核心思路

ta.crossover()和ta.crossunder()返回布尔值,要获取交叉发生时的指标数值,只需在条件成立时取对应指标值,否则返回na(浮点型兼容值),以此解决类型统一问题。同时在条件中加入阈值判断,只保留符合要求的交叉点用于绘制。

2. 完整实现代码

//@version=5
indicator(title="Stochastic oscillator-SlowFast Strategy", shorttitle="Stoch-SF-Strategy", overlay=false)

// 输入参数
periodKs = input.int(10, title="Slow K Period")
smoothKs = input.int(10, title="Slow K Smoothing")
periodKf = input.int(6, title="Fast K Period")
smoothKf = input.int(3, title="Fast K Smoothing")

// 计算快慢随机指标
ks = ta.sma(ta.stoch(close, high, low, periodKs), smoothKs)
kf = ta.sma(ta.stoch(close, high, low, periodKf), smoothKf)

// 筛选符合阈值的交叉/下穿点并获取数值
// 多头交叉:慢线穿快线且数值<25
long_cross_val = ta.crossover(ks, kf) and kf < 25 ? kf : na
// 空头下穿:慢线下穿快线且数值>75
short_cross_val = ta.crossunder(ks, kf) and kf > 75 ? kf : na

// 开平仓逻辑
long_entry = long_cross_val != na or ks < 20
long_exit = ta.position_size > 0 and kf > 90
short_entry = short_cross_val != na or ks > 80
short_exit = ta.position_size < 0 and kf < 10

// 执行交易
if (long_entry)
    strategy.entry("Long", strategy.long)
if (long_exit)
    strategy.close("Long")

if (short_entry)
    strategy.entry("Short", strategy.short)
if (short_exit)
    strategy.close("Short")

// 绘制符合要求的交叉点
plot(long_cross_val, color=color.teal, linewidth=3, style=plot.style_circles, title="Valid Long Cross")
plot(short_cross_val, color=color.red, linewidth=3, style=plot.style_circles, title="Valid Short Cross")

// 绘制指标线与阈值线
plot(ks, color=color.yellow, title="Slow Stoch")
plot(kf, color=color.white, title="Fast Stoch")
hline(20, "Long Entry Threshold", color=color.teal)
hline(25, "Long Cross Threshold", color=color.teal, linestyle=hline.style_dashed)
hline(75, "Short Cross Threshold", color=color.red, linestyle=hline.style_dashed)
hline(80, "Short Entry Threshold", color=color.red)
hline(10, "Short Exit Threshold", color=color.blue)
hline(90, "Long Exit Threshold", color=color.blue)

代码说明

  • 用ta.crossover(ks, kf) and kf < 25筛选出数值低于25的多头交叉点,仅此时返回交叉点的kf值,否则返回na,绘制时只会显示符合条件的点。
  • 完全按照给定的结构化英语实现交易逻辑,所有用于绘制的变量均为浮点型(含na),彻底解决类型不匹配问题。
  • 新增对应阈值线,直观展示条件触发区间。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 17:10:25