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

如何将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() with indicator(): In v4 and v5, study() is deprecated. Use indicator() instead—it supports all the same core parameters like title, overlay, and scale.
  • Simplify input declarations: v2's input() with type parameters can be replaced with v5's dedicated input functions, such as input.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, and ta.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 with color.new() in v4/v5 for transparency control. Alternatively, plot functions now accept a transp parameter directly, which simplifies setting opacity.
  • Namespace-qualify functions: Many v2 functions (like sma()) are now under the ta namespace 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 explicit ta.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() with indicator()
  • Switched input() with input.int() for integer inputs
  • Used ta.tr for True Range (built-in function equivalent to your manual calculation)
  • Replaced max() with math.max() and abs() with math.abs() (v5 uses namespace-qualified math functions)
  • Updated color handling to use color.new() for transparency instead of the old color() function
  • Used plot.style_columns and plot.style_line for explicit plot styles (v5 uses enum-style constants)
  • Namespaced nz() to ta.nz() and sma() to ta.sma() for clarity

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 00:14:07