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

请求协助将TrendAlert指标从PineScript 2升级至PineScript 5

PineScript 2转v5:TrendAlert指标Heiken Ashi部分修复方案

问题背景

需要将TradingView上rstraat开发的TrendAlert指标从PineScript 2升级至PineScript 5,已完成基础转换,但Heiken Ashi蜡烛相关代码出现报错,无法正常运行。

原PineScript 2代码

//@version=2
study("TrendAlert", overlay=false)

// Trend and direction determination Step 1 en Step 2 method conform ZoneRecovery from 4x-dat.com Joseph Nemeth 
// https://www.youtube.com/watch?v=YxDAYtlQjBM
// step 1 LongTerm timefram/resolution (LT) determine general direction 
//• color last Heikenahsi bar gives is the general direction 
// step 2 MidTerm timeframe/resolution determine trend
//• color last Heikenahsi bar must match Direction step 1 and
//• slope of 20EMA in direction step 1 and 
//• price relation to EMA20: above = up, below=down 
// Step 1 AND step 2 => TrendAlert (-1 for Short, 1 for Long, 0 for No trend)

//Heiken Ashi Candles
data = heikenashi(tickerid)
resLT = input(title="Res LT", type=resolution, defval="D")
resMT = input(title="Res MT", type=resolution, defval="240")


LTo = security(data, resLT, open)
LTc = security(data, resLT, close)
MTo = security(data, resMT, open)
MTc = security(data, resMT, close)

LTlong = LTc > LTo 
LTshort = LTc < LTo 
MTema20 = ema(MTc,20)
MTema20delta= change(MTema20)

MTlong = MTc > MTo and MTc > MTema20 and MTema20delta > 0
MTshort = MTc < MTo and MTc < MTema20 and MTema20delta < 0



Long = MTlong and LTlong
Short = MTshort and LTshort

plot(Long ? 1 : Short ? -1: 0, title="TrendAlert", color= Long ? lime : Short ? red : gray, style=columns )

已完成的PineScript 5代码(报错版本)

//@version=5
indicator("TrendAlert", overlay=false)

// Trend and direction determination Step 1 en Step 2 method conform ZoneRecovery from 4x-dat.com Joseph Nemeth
// https://www.youtube.com/watch?v=YxDAYtlQjBM
// step 1 LongTerm timefram/resolution (LT) determine general direction
//• color last Heikenahsi bar gives is the general direction
// step 2 MidTerm timeframe/resolution determine trend
//• color last Heikenahsi bar must match Direction step 1 and
//• slope of 20EMA in direction step 1 and
//• price relation to EMA20: above = up, below=down
// Step 1 AND step 2 => TrendAlert (-1 for Short, 1 for Long, 0 for No trend)

//Heiken Ashi Candles
data = ta.heikenashi(tickerid)
resLT = input(title="Res LT", type=input.resolution, defval="D")
resMT = input(title="Res MT", type=input.resolution, defval="240")

LTo = security(syminfo.tickerid, resLT, open)
LTc = security(syminfo.tickerid, resLT, close)
MTo = security(syminfo.tickerid, resMT, open)
MTc = security(syminfo.tickerid, resMT, close)

LTlong = LTc > LTo
LTshort = LTc < LTo
MTema20 = ta.ema(MTc,20)
MTema20delta= ta.change(MTema20)

MTlong = MTc > MTo and MTc > MTema20 and MTema20delta > 0
MTshort = MTc < MTo and MTc < MTema20 and MTema20delta < 0

Long = MTlong and LTlong
Short = MTshort and LTshort

plotshape(Long ? 1 : Short ? -1: 0, title="TrendAlert", color= Long ? color.lime : Short ? color.red : color.gray, style=shape.columns )

报错信息

  • Error at 15:22 Undeclared identifier 'tickerid'
  • Error at 15:8 Could not find function or function reference 'ta.heikenashi'

修复后的完整PineScript 5代码

//@version=5
indicator("TrendAlert", overlay=false)

// Trend and direction determination Step 1 en Step 2 method conform ZoneRecovery from 4x-dat.com Joseph Nemeth
// step 1 LongTerm timefram/resolution (LT) determine general direction 
//• color last Heikenahsi bar gives is the general direction 
// step 2 MidTerm timeframe/resolution determine trend
//• color last Heikenahsi bar must match Direction step 1 and
//• slope of 20EMA in direction step 1 and 
//• price relation to EMA20: above = up, below=down 
// Step 1 AND step 2 => TrendAlert (-1 for Short, 1 for Long, 0 for No trend)

resLT = input(title="Res LT", type=input.resolution, defval="D")
resMT = input(title="Res MT", type=input.resolution, defval="240")

// 获取不同周期的Heiken Ashi开收盘价,v5通过request.security的heikinashi参数实现
[LTo, LTc] = request.security(syminfo.tickerid, resLT, [open, close], heikinashi=true)
[MTo, MTc] = request.security(syminfo.tickerid, resMT, [open, close], heikinashi=true)

LTlong = LTc > LTo 
LTshort = LTc < LTo 
MTema20 = ta.ema(MTc, 20)
MTema20delta = ta.change(MTema20)

MTlong = MTc > MTo and MTc > MTema20 and MTema20delta > 0
MTshort = MTc < MTo and MTc < MTema20 and MTema20delta < 0

Long = MTlong and LTlong
Short = MTshort and LTshort

plot(Long ? 1 : Short ? -1 : 0, title="TrendAlert", color=Long ? color.lime : Short ? color.red : color.gray, style=plot.style_columns)

错误原因与修复说明

  1. tickerid变量错误:PineScript v5中已移除tickerid,替换为syminfo.tickerid用于获取当前交易标的ID
  2. ta.heikenashi函数不存在:v5中没有单独生成Heiken Ashi数据的函数,而是通过request.security添加heikinashi=true参数,直接获取对应周期的Heiken Ashi开收盘价
  3. plotshape替换为plot:原v2的plot(style=columns)在v5中对应plot(style=plot.style_columns),无需使用plotshape,更符合原指标逻辑

同类功能脚本推荐

  • Zone Recovery v5版本:遵循相同的趋势判定逻辑,由原开发者适配至PineScript 5,直接支持多周期Heiken Ashi与EMA趋势过滤
  • Heiken Ashi Trend Filter:基于Heiken Ashi蜡烛与EMA的趋势判定指标,已原生适配PineScript 5,支持自定义周期与参数

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 00:32:16