如何将PineScript版本2转换为版本4/5?请求将指定ADX/DI Histogram脚本转换为版本5
Converting PineScript Version 2 to Version 4/5 + ADX/DI Histogram Port to v5
Great question! Let's break this down into two parts: first, a general guide for converting PineScript v2 code to v4 or v5, then the ported ADX/DI Histogram script tailored for v5.
General Guide to Convert PineScript v2 to v4/v5
Here are key steps to adapt v2 code to newer versions:
- Replace
study()withindicator(): In v4 and v5,study()is deprecated. Useindicator()instead—it supports all the same core parameters liketitle,overlay, andscale. - Simplify input declarations: v2's
input()withtypeparameters can be replaced with v5's dedicated input functions, such asinput.int()for integers,input.float()for decimals, etc. This makes code more readable and explicit. - Leverage built-in TA functions: v5 includes pre-built functions for most common technical calculations. For example,
ta.tr()computes True Range automatically, andta.dmi()can handle DI+/DI- and ADX calculations. That said, you can still replicate custom logic if needed. - Update color handling: v2's
color()function is replaced withcolor.new()in v4/v5 for transparency control. Alternatively, plot functions now accept atranspparameter directly, which simplifies setting opacity. - Namespace-qualify functions: Many v2 functions (like
sma()) are now under thetanamespace in v5 (e.g.,ta.sma()) to avoid naming conflicts. - Handle variable initialization: v2 allowed implicit initialization using
nz()on the first bar. In v5, ensure variables are properly initialized, either with explicitta.nz()calls or by using functions that handle null values automatically. - Check for breaking changes: Review the PineScript release notes for v4 and v5 to catch any other deprecated features or behavior changes (e.g., loop syntax, variable scoping).
Ported ADX/DI Histogram Script (PineScript v5)
Here's your original ADX/DI Histogram script converted to PineScript v5, with all original logic preserved and adapted to v5 syntax:
indicator("ADX/DI Histogram", overlay=false, scale=scale.left) len = input.int(title="Length", defval=14) th = input.int(title="threshold", defval=25) TrueRange = ta.tr DirectionalMovementPlus = high - ta.nz(high[1]) > ta.nz(low[1]) - low ? math.max(high - ta.nz(high[1]), 0) : 0 DirectionalMovementMinus = ta.nz(low[1]) - low > high - ta.nz(high[1]) ? math.max(ta.nz(low[1]) - low, 0) : 0 SmoothedTrueRange = ta.nz(SmoothedTrueRange[1]) - (ta.nz(SmoothedTrueRange[1]) / len) + TrueRange SmoothedDirectionalMovementPlus = ta.nz(SmoothedDirectionalMovementPlus[1]) - (ta.nz(SmoothedDirectionalMovementPlus[1]) / len) + DirectionalMovementPlus SmoothedDirectionalMovementMinus = ta.nz(SmoothedDirectionalMovementMinus[1]) - (ta.nz(SmoothedDirectionalMovementMinus[1]) / len) + DirectionalMovementMinus DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100 DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100 DX = math.abs(DIPlus - DIMinus) / (DIPlus + DIMinus) * 100 ADX = ta.sma(DX, len) ve = DIPlus > (DIMinus + (DIPlus * 10) / 100) ro = DIMinus > (DIPlus + (DIMinus * 10) / 100) and ADX > 11 and ADX <= 25 xve = DIPlus > (DIMinus + (DIPlus * 10) / 100) and ADX > 22 xro = DIMinus > (DIPlus + (DIMinus * 10) / 100) and ADX > 22 fl = DIPlus - DIMinus < math.abs(10) and ADX < 15 di = 25 + (DIPlus - DIMinus) cr = ADX <= 22 ? color.new(color.green, 80) : ADX <= 34 ? color.new(color.green, 60) : ADX <= 46 ? color.new(color.green, 35) : ADX <= 58 ? color.new(color.green, 5) : ADX <= 70 ? color.new(color.green, 0) : color.new(color.green, 0) cg = ADX <= 22 ? color.new(color.maroon, 80) : ADX <= 34 ? color.new(color.maroon, 60) : ADX <= 46 ? color.new(color.maroon, 35) : ADX <= 58 ? color.new(color.maroon, 5) : ADX <= 70 ? color.new(color.maroon, 0) : color.new(color.maroon, 0) plot(di, style=plot.style_columns, linewidth=4, color=di >= 25 ? cr : cg, transp=50, histbase=th, title="DI") plot(di, style=plot.style_line, linewidth=2, color=di >= 25 ? cr : cg, transp=50, histbase=th, title="DI Line") plot(ADX, color=color.black, linewidth=1, transp=0, title="ADX")
Key changes made in the port:
- Replaced
study()withindicator() - Switched
input()withinput.int()for integer inputs - Used
ta.trfor True Range (built-in function equivalent to your manual calculation) - Replaced
max()withmath.max()andabs()withmath.abs()(v5 uses namespace-qualified math functions) - Updated color handling to use
color.new()for transparency instead of the oldcolor()function - Used
plot.style_columnsandplot.style_linefor explicit plot styles (v5 uses enum-style constants) - Namespaced
nz()tota.nz()andsma()tota.sma()for clarity
内容的提问来源于stack exchange,提问作者Dante
相关产品推荐
相关产品推荐

