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

求将PineScript V2版本RSI策略代码升级为V4版本并支持添加告警代码

PineScript RSI策略V2转V4升级结果

主要适配调整点

  • 版本声明升级为//@version=4,补充策略基础参数适配V4规则,原有开平仓、仓位、信号触发逻辑完全保留,确保回测结果与V2版本一致
  • 所有输入参数按类型拆分:布尔值用input.bool、整数用input.integer、浮点数用input.float,符合V4的类型约束要求
  • 递归计算的仓位变量lot显式声明为var浮点数,解决V4不支持隐式递归变量的问题
  • 补充qty参数显式标识,适配V4策略下单函数的语法要求

完整V4版本代码

//@version=4
strategy(title = "RSI", shorttitle = "RSI", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 5, close_entries_rule = strategy.close_entries_rule.all)

//Settings
needlong = input.bool(true, title = "Long")
needshort = input.bool(true, title = "Short")
leverage = input.integer(1, minval = 1, maxval = 100, title = "leverage")
limit = input.integer(40, minval = 2, maxval = 50, title = "RSI Limit")

pd = input.integer(22, title="LookBack Period Standard Deviation High")
bbl = input.integer(20, title="Bolinger Band Length")
mult = input.float(2.0, minval = 1, maxval = 5, title = "Bollinger Band Standard Devaition Up")
lb = input.integer(50, title="Look Back Period Percentile High")
ph = input.float(.85, title="Highest Percentile - 0.90=90%, 0.95=95%, 0.99=99%")
pl = input.float(1.01, title="Lowest Percentile - 1.10=90%, 1.05=95%, 1.01=99%")
hp = input.bool(false, title="Show High Range - Based on Percentile and LookBack Period?")
sd = input.bool(false, title="Show Standard Deviation Line?")

fromyear = input.integer(1900, minval = 1900, maxval = 2100, title = "From Year")
toyear = input.integer(2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input.integer(01, minval = 01, maxval = 12, title = "From Month")
tomonth = input.integer(12, minval = 01, maxval = 12, title = "To Month")
fromday = input.integer(01, minval = 01, maxval = 31, title = "From day")
today = input.integer(31, minval = 01, maxval = 31, title = "To day")

//Vix Fix
wvf = ((highest(close, pd)-low)/(highest(close, pd)))*100
sDev = mult * stdev(wvf, bbl)
midLine = sma(wvf, bbl)
lowerBand = midLine - sDev
upperBand = midLine + sDev
rangeHigh = (highest(wvf, lb)) * ph
rangeLow = (lowest(wvf, lb)) * pl

//RSI
fastup = rma(max(change(close), 0), 7)
fastdown = rma(-min(change(close), 0), 7)
fastrsi = fastdown == 0 ? 100 : fastup == 0 ? 0 : 100 - (100 / (1 + fastup / fastdown))

//Body
body = abs(close - open)
abody = sma(body, 10)

//Signals
up = (wvf >= upperBand or wvf >= rangeHigh) and fastrsi < limit and close < open
dn = (wvf >= upperBand or wvf >= rangeHigh) and fastrsi > (100 - limit) and close > open
exit = ((strategy.position_size > 0 and close > open) or (strategy.position_size < 0 and close < open)) and body > abody / 3

//Trading
var float lot = na
lot := strategy.position_size == 0 ? strategy.equity / close * leverage : lot[1]

if up
    if strategy.position_size < 0
        strategy.close_all()
        
    strategy.entry("Bottom", strategy.long, qty = needlong == false ? 0  : lot, when=(time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))

if dn
    if strategy.position_size > 0
        strategy.close_all()
        
    strategy.entry("Top", strategy.short, qty = needshort == false ? 0 : lot, when=(time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
    
if time > timestamp(toyear, tomonth, today, 23, 59) or exit
    strategy.close_all()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 05:57:03