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

请求将TradingView V2策略脚本转为V5并添加买卖预警

PineScript V2转V5策略脚本 + Pineconnector Webhook预警实现

转换后的完整V5脚本

//@version=5
strategy(title='Sniper & Strategy by CSBender_', shorttitle='Sniper & Strategy by CSBender_', overlay=true, pyramiding=0, initial_capital=2000, currency=currency.USD, default_qty_type=strategy.cash, default_qty_value=1000)

//============ Signal Generator ==================================//
Period = input.timeframe("720", title="Signal Timeframe")
ch1 = request.security(syminfo.tickerid, Period, open, barmerge.lookahead_off)
ch2 = request.security(syminfo.tickerid, Period, close, barmerge.lookahead_off)

longCondition = ta.crossover(ch2, ch1)
shortCondition = ta.crossunder(ch2, ch1)

if (longCondition)
    strategy.entry("Long", strategy.long)
plotshape(longCondition, style=shape.labelup, color=color.lime, text="Buy", textcolor=color.black, location=location.belowbar)

if (shortCondition)
    strategy.entry("Short", strategy.short)
plotshape(shortCondition, style=shape.labeldown, color=color.red, text="Sell", textcolor=color.black)

//============ Directional Projection ==================================//
channel3 = input.bool(false, title="Connect Projection High/Low")
tf2 = input.timeframe("1", title="Trend Projection TF / Mins/D/W")
M2 = input.string("ATR", title="Point & Figure Method")
P2 = input.float(13.00, title="Box Size")
W2 = input.int(1, title="Reversal Amount")

// 映射Point & Figure方法到V5枚举
pf_method = M2 == "ATR" ? pf_type.atr : pf_type.close
pf2 = pointfigure(syminfo.tickerid, tf2, pf_method, W2, P2)
spfc2 = request.security(pf2, tf2, close, barmerge.lookahead_off)

p22 = plot(channel3 ? spfc2 : spfc2 == nz(spfc2[1]) ? spfc2 : na, color=color.blue, linewidth=2, style=plot.style_linebr, title="Projection", offset=0)

//==================== Colour Bar ======================
mysignal = ta.ema(close, 13) - ta.ema(close, 26)
barcolor(mysignal > mysignal[1] ? color.green : color.red)

//============ Trend Colour EMA 1 ==================================//
src0 = close
len0 = input.int(13, minval=1, title="EMA 1 Length")
ema0 = ta.ema(src0, len0)
direction = ta.rising(ema0, 2) ? 1 : ta.falling(ema0, 2) ? -1 : 0
plot_color = direction > 0 ? color.lime : direction < 0 ? color.red : na
plot(ema0, title="EMA", style=plot.style_line, linewidth=2, color=plot_color)

//============ EMA 2 ==================================//
src02 = close
len02 = input.int(21, minval=1, title="EMA 2 Length")
ema02 = ta.ema(src02, len02)
direction2 = ta.rising(ema02, 2) ? 1 : ta.falling(ema02, 2) ? -1 : 0
plot_color2 = direction2 > 0 ? color.lime : direction2 < 0 ? color.red : na
plot(ema02, title="EMA Signal 2", style=plot.style_line, linewidth=2, color=plot_color2)

//============= Hull MA ==================================//
show_hma = input.bool(false, title="Display Hull MA Set:")
hma_src = input(close, title="Hull MA's Source:")
hma_base_length = input.int(8, minval=1, title="Hull MA's Base Length:")
hma_length_scalar = input.int(5, minval=0, title="Hull MA's Length Scalar:")

hullma(src, length) => 
    wma(2*ta.wma(src, math.floor(length/2)) - ta.wma(src, length), math.round(math.sqrt(length)))

plot(not show_hma ? na : hullma(hma_src, hma_base_length + hma_length_scalar*6), color=color.black, linewidth=2, title="Hull MA")

//============ Supertrend Generator ==================================//
Factor = input.int(1, minval=1, title="Trend Transition Signal")
Pd = input.int(1, minval=1, maxval=100, title="ATR Period")

var float TrendUp = na
var float TrendDown = na

Up = ta.hl2() - Factor * ta.atr(Pd)
Dn = ta.hl2() + Factor * ta.atr(Pd)

TrendUp := close[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up
TrendDown := close[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn

Trend = close > TrendDown[1] ? 1 : close < TrendUp[1] ? -1 : nz(Trend[1], 0)
plotarrow(Trend == 1 and Trend[1] == -1 ? Trend : na, title="Up Entry Arrow", colorup=color.lime, maxheight=1000, minheight=50, transp=85)
plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na, title="Down Entry Arrow", colordown=color.red, maxheight=1000, minheight=50, transp=85)

//============ Trend Generator ==================================//
slow = 8
fast = 5
vh1 = ta.ema(ta.highest(ta.avg(low, close), fast), 5)
vl1 = ta.ema(ta.lowest(ta.avg(high, close), slow), 8)
e_ema1 = ta.ema(close, 1)
e_ema2 = ta.ema(e_ema1, 1)
e_ema3 = ta.ema(e_ema2, 1)
tema = 1 * (e_ema1 - e_ema2) + e_ema3
e_e1 = ta.ema(close, 8)
e_e2 = ta.ema(e_e1, 5)
dema = 2 * e_e1 - e_e2
signal = tema > dema ? math.max(vh1, vl1) : math.min(vh1, vl1)
is_call = tema > dema and signal > low and (signal - signal[1] > signal[1] - signal[2])
is_put = tema < dema and signal < high and (signal[1] - signal > signal[2] - signal[1])

plotshape(is_call ? 1 : na, title="BUY ARROW", color=color.green, text="B", style=shape.arrowup, location=location.belowbar)
plotshape(is_put ? -1 : na, title="SELL ARROW", color=color.red, text="S", style=shape.arrowdown)

//============ Webhook Alert Conditions ==================================//
alertcondition(longCondition, title="Long Signal", message='{"action": "BUY", "symbol": "{{ticker}}", "volume": 0.1}')
alertcondition(shortCondition, title="Short Signal", message='{"action": "SELL", "symbol": "{{ticker}}", "volume": 0.1}')

关键转换说明

  • 版本与语法更新:将版本声明改为//@version=5,所有技术分析函数(如crossover、ema)前添加ta.前缀,数学函数(如max、sqrt)前添加math.前缀
  • 时间框架处理:使用input.timeframe()替代字符串输入,request.security()添加barmerge.lookahead_off避免未来函数风险,同时用syminfo.tickerid替换旧版tickerid
  • Point & Figure适配:将字符串方法映射为V5的pf_type枚举,调整pointfigure()参数顺序符合新规范
  • 变量持久化:Supertrend的TrendUp/TrendDown使用var关键字初始化,确保跨K线保留状态
  • Alert配置:新增alertcondition()定义买卖信号触发规则,消息体采用JSON格式适配Pineconnector

Pineconnector Webhook配置步骤

  1. 在TradingView中打开该策略,点击右上角「Alert」按钮
  2. 触发条件选择「Any alert() function call」
  3. 勾选「Webhook URL」,填写Pineconnector提供的MT5对接地址
  4. 保存alert配置,确保Pineconnector已在本地运行并完成MT5授权

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:10:34