Tradingview Pine Script v2版本策略代码升级至v5版本技术咨询
Pine Script v2 转 v5 适配完成代码
//@version=5 strategy("通道突破交易策略v5", overlay=true, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=1000, commission_type=strategy.commission.percent, commission_value=0.1) //Settings needlong = input.bool(true, title="long") needshort = input.bool(true, title="short") needstops = input.bool(false, title="stops") stoppercent = input.int(5, minval = 1, maxval = 50, title = "Stop, %") usefastsma = input.bool(true, title="Use fast MA Filter") fastlen = input.int(5, minval = 1, maxval = 50, title = "fast MA Period") slowlen = input.int(21, minval = 2, maxval = 200, title = "slow MA Period") bars = input.int(2, minval = 0, maxval = 3, title = "Bars Q") needbg = input.bool(false, title = "Need trend Background?") needex = input.bool(true, title = "Need extreme? (crypto/fiat only!!!)") fromyear = input.int(1900, minval = 1900, maxval = 2100, title = "From Year") toyear = input.int(2100, minval = 1900, maxval = 2100, title = "To Year") frommonth = input.int(01, minval = 01, maxval = 12, title = "From Month") tomonth = input.int(12, minval = 01, maxval = 12, title = "To Month") fromday = input.int(01, minval = 01, maxval = 31, title = "From day") today = input.int(31, minval = 01, maxval = 31, title = "To day") src = close //PriceChannel 1 lasthigh = ta.highest(src, slowlen) lastlow = ta.lowest(src, slowlen) center = (lasthigh + lastlow) / 2 //PriceChannel 2 lasthigh2 = ta.highest(src, fastlen) lastlow2 = ta.lowest(src, fastlen) center2 = (lasthigh2 + lastlow2) / 2 //Trend trend = 0 trend := low > center and low[1] > center[1] ? 1 : high < center and high[1] < center[1] ? -1 : trend[1] //Bars bar = close > open ? 1 : close < open ? -1 : 0 redbars = bars == 0 ? 1 : bars == 1 and bar == -1 ? 1 : bars == 2 and bar == -1 and bar[1] == -1 ? 1 : bars == 3 and bar == -1 and bar[1] == -1 and bar[2] == -1 ? 1 : 0 greenbars = bars == 0 ? 1 : bars == 1 and bar == 1 ? 1 : bars == 2 and bar == 1 and bar[1] == 1 ? 1 : bars == 3 and bar == 1 and bar[1] == 1 and bar[2] == 1 ? 1 : 0 //Fast RSI fastup = ta.rma(ta.max(ta.change(close), 0), 2) fastdown = ta.rma(-ta.min(ta.change(close), 0), 2) fastrsi = fastdown == 0 ? 100 : fastup == 0 ? 0 : 100 - (100 / (1 + fastup / fastdown)) //CryptoBottom mac = ta.sma(close, 10) len = math.abs(close - mac) len_sma = ta.sma(len, 100) bar_max = ta.max(open, close) bar_min = ta.min(open, close) //Signals up1 = trend == 1 and (low < center2 or not usefastsma) and redbars == 1 dn1 = trend == -1 and (high > center2 or not usefastsma) and greenbars == 1 up2 = high < center and high < center2 and bar == -1 and needex dn2 = low > center and low > center2 and bar == 1 and needex up3 = close < open and len > len_sma * 3 and bar_min < bar_min[1] and fastrsi < 10 ? 1 : 0 //Lines plot(center2, color = color.red, linewidth = 3, title = "Fast MA") plot(center, color = color.blue, linewidth = 3, title = "Slow MA") //Background col = not needbg ? na : trend == 1 ? color.new(color.lime, 80) : color.new(color.red, 80) bgcolor(col) //Trading stoplong = 0.0 stoplong := up1 == 1 and needstops ? close - (close / 100 * stoppercent) : stoplong[1] stopshort = 0.0 stopshort := dn1 == 1 and needstops ? close + (close / 100 * stoppercent) : stopshort[1] time_cond = time > ta.timestamp(fromyear, frommonth, fromday, 00, 00) and time < ta.timestamp(toyear, tomonth, today, 23, 59) if (up1 or up2 or up3) and time_cond and needlong strategy.entry("Long", strategy.long) if needstops strategy.exit("Stop Long", "Long", stop = stoplong) if dn1 and time_cond and needshort strategy.entry("Short", strategy.short) if needstops strategy.exit("Stop Short", "Short", stop = stopshort) if time > ta.timestamp(toyear, tomonth, today, 23, 59) strategy.close_all()
核心升级改动说明
- 版本与策略声明:v5要求首行明确标注
//@version=5,必须添加strategy()声明配置策略的初始资金、手续费、下单默认参数等基础属性 - 输入函数标准化:v2通用的
input()拆分为细分类型函数,布尔值用input.bool()、整数用input.int(),避免类型歧义 - 命名空间规范:所有技术指标函数归属
ta.命名空间,数学计算函数归属math.命名空间,例如highest()改为ta.highest()、abs()改为math.abs() - 自引用变量处理:v5不允许未初始化直接引用变量历史值,需先定义初始值,再用
:=运算符更新数值 - 可视化参数调整:原透明度参数
transp整合到color.new()函数的第二个参数,取值逻辑保持0-100范围不变 - 内置常量规范:颜色常量统一添加
color.前缀,例如red改为color.red - 变量名冲突修复:原代码中
sma、max、min变量名与内置函数重名,重命名为len_sma、bar_max、bar_min避免运行报错 - 逻辑优化:将时间条件、多空开关提取到if判断条件中,逻辑更清晰,避免无效订单生成
内容的提问来源于stack exchange,提问作者Serdar Turan
相关产品推荐
相关产品推荐

