I'm here to help! I'll guide you through the process of updating the provided TradingView Pine Script to version 4. Here's a step-by-step breakdown of the necessary changes:
- Change the script version at the beginning of the code:
Change //@version=2 to //@version=4.
- Replace obsolete functions and built-in variables:
Replace study() with indicator().
Replace input(title, defval, minval, maxval) with input.int(defval, title, minval, maxval) or input.float(defval, title, minval, maxval).
Replace nz() with the conditional operator ?:.
Replace na() with is.na().
Replace sum() with volume.sum().
- Adjust variable declarations and calculations:
Use var keyword for variables that need to keep their value between bars.
Change sma() to ta.sma().
Change stdev() to ta.stdev().
Change rsi() to ta.rsi().
- Update plot and style definitions:
Use the new plot style syntax, e.g., plot(series, title, color, style, linewidth).
Here's the updated code:
//@version=4
study("Blai5 Koncorde", shorttitle="KONCORDE")
lengthEMA = input.int(255, minval=1, title="EMA Length")
m = input.int(15, title="MFI Length")
mult = input.float(2.0, title="MFI Multiplier")
calc_pvi() =>
sval = volume
pvi = (volume > volume[1]) ? nz(pvi[1]) + ((close - close[1])/close[1]) * (is.na(pvi[1]) ? sval : sval) : nz(pvi[1])
calc_nvi() =>
sval = volume
nvi = (volume < volume[1]) ? nz(nvi[1]) + ((close - close[1])/close[1]) * (is.na(nvi[1]) ? sval : sval) : nz(nvi[1])
calc_mfi(length) =>
src = hlc3
upper = volume.sum(volume * (change(src) <= 0 ? 0 : src), length)
lower = volume.sum(volume * (change(src) >= 0 ? 0 : src), length)
ta.rsi(upper, lower)
tprice = ohlc4
lengthMA = input.int(21, minval=1, title="Stochastic Length")
smoothFastD = input.int(3, minval=1, title="Stochastic Smoothing")
pvi = calc_pvi()
pvim = ta.sma(pvi, m)
pvimax = ta.highest(pvim, 90)
pvimin = ta.lowest(pvim, 90)
oscp = (pvi - pvim) * 100 / (pvimax - pvimin)
nvi = calc_nvi()
nvim = ta.sma(nvi, m)
nvimax = ta.highest(nvim, 90)
nvimin = ta.lowest(nvim, 90)
azul = (nvi - nvim) * 100 / (nvimax - nvimin)
xmf = calc_mfi(14)
basis = ta.sma(tprice, 25)
dev = mult * ta.stdev(tprice, 25)
upper = basis + dev
lower = basis - dev
OB1 = (upper + lower) / 2.0
OB2 = upper - lower
BollOsc = ((tprice - OB1) / OB2) * 100
xrsi = ta.rsi(tprice, 14)
calc_stoch(src, length, smoothFastD) =>
ll = ta.lowest(low, length)
hh = ta.highest(high, length)
k = 100 * (src - ll) / (hh - ll)
ta.sma(k, smoothFastD)
stoc = calc_stoch(tprice, 21, 3)
marron = (xrsi + xmf + BollOsc + (stoc / 3)) / 2
verde = marron + oscp
media = ta.sma(marron, m)
plot(verde, color=color.rgb(102, 255, 102), title="verde", style=plot.style_area, linewidth=1)
plot(marron, color=color.rgb(255, 204, 153), title="marron", style=plot.style_area, linewidth=1, transp=0)
plot(azul, color=color.rgb(0, 255, 255), title="azul", style=plot.style_area, linewidth=1)
plot(marron, color=color.maroon, title="Marron", style=plot.style_line, linewidth=2)
plot(verde, color=color.rgb(0, 102, 0), title="Verde", style=plot.style_line, linewidth=2)
plot(azul, color=color.rgb(0, 0, 102), title="Azul", style=plot.style_line, linewidth=2)
plot(media, color=color.red, title="Media", style=plot.style_line, linewidth=2)
hline(0, color=color.black, linestyle=hline.style_solid)