Reputation: 9
I have an old script that is Version 2 I believe, and I need assistance with it to convert it to Version 5. Its a short script nothing long, as my knowledge is pretty basic on Pinescript. Any help would be apprectiated. Thanks in Advance
//@version=2
study("ADX")
len = input(title="Length", type=integer, defval=14)
TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0
SmoothedTrueRange = nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus = nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus
DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, len)
ColorBuy=color(green,0)
ColorSell=color(red,0)
ColorDoNothing=color(white,100)
plot(DIPlus, color=green, title="DI+",linewidth=3,style=line)
plot(DIMinus, color=red, title="DI-",linewidth=3,style=line)
plot(ADX, color=(ADX>DIMinus and DIPlus>DIMinus and ADX > ADX[1]? ColorBuy : (ADX>DIPlus and DIMinus>DIPlus and ADX > ADX[1] ? ColorSell:ColorDoNothing)),title="ADX",style=columns)
I tried to do the conversion by adding the version 3 but looks like some of the values have been replaced now.
Upvotes: 0
Views: 230
Reputation: 24
//@version=6
indicator("ADX")
len = input.int(title="Length", defval=14)
TrueRange = math.max(math.max(high-low, math.abs(high-nz(close[1]))), math.abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? math.max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? math.max(nz(low[1])-low, 0): 0
var float SmoothedTrueRange=na
var float SmoothedDirectionalMovementPlus=na
var float SmoothedDirectionalMovementMinus=na
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (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)
ColorBuy=color.new(color.green,0)
ColorSell=color.new(color.red,0)
ColorDoNothing=color.new(color.white,100)
plot(DIPlus, color=color.green, title="DI+",linewidth=3,style=plot.style_line)
plot(DIMinus, color=color.red, title="DI-",linewidth=3,style=plot.style_line)
plot(ADX, color=(ADX>DIMinus and DIPlus>DIMinus and ADX > ADX[1]? ColorBuy : (ADX>DIPlus and DIMinus>DIPlus and ADX > ADX[1] ? ColorSell:ColorDoNothing)),title="ADX",style=plot.style_columns)
Upvotes: 0
Reputation: 123
The main problem of pinescript TrederView to pass to python is the := symbol, it means that it is reassigned during execution (the close[1] changes during execution, while with = the value is kept during execution).
For example: This code with = :
pvi = iff(volume > volume[1], nz(pvi[1]) + (close - close[1]) / close[1], nz(pvi[1]))
In python it translates:
df['pvi'] = np.where(df['Volume'] > df['Volume'].shift(-1), df['pvi'] + df['Volume'] - df['Volume'].shift(-1), df['pvi'].shift(-1))
But if the := is used (there must be constant reassignment), the translation is:
def positive_volume_index(df, close_col='Close', vol_col='Volume'):
df['pvi'] = 0.
df.reset_index(drop=True, inplace=True)
for index, row in df.iterrows():
if index > 0:
prev_pvi = df.at[index - 1, 'pvi']
prev_close = df.at[index - 1, close_col]
if row[vol_col] > df.at[index - 1, vol_col]:
pvi = prev_pvi + ((row[close_col] - prev_close) / prev_close )# * prev_pvi)
else:
pvi = prev_pvi
else:
pvi = 0
df.at[index, 'pvi'] = pvi
If you want a more generic solution, there are custom chatGPT chats for such transformations, (highly recommended to check the result). https://chatgpt.com/g/g-KltZfpAIp-pine-to-python-converter?utm_source=gptshunter.com
Upvotes: 0
Reputation: 21342
You can use this guideline for the migration.
In this specific example, one change you need to know is; Self-referenced variables are removed.
Your script has a few self referencing variables like below:
SmoothedTrueRange = nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
You should first define the variable and then assign it a value with the :=
operator.
SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
Do this for the rest of the variables and you should be good to go.
Once it is converted to v3, you can use the auto converter tool to convert it to v4 and then v5.
Upvotes: 0