Convert this pine script to a mql4 script. Anticipate potential errors and solve them…
- User
Convert this pine script to a mql4 script. Anticipate potential errors and solve them before running them. This will be run over a ton off forex data:
I gave this too you once before and it just crashed when I would try to run it so take that into account. Take out the arrows and color changing so it is a bit simpler.
Code: //@version=5
indicator("Variable Moving Average 2", shorttitle="VMA_2", overlay=true)
// Input parameters
vma1_length = input.int(6, title="VMA1 Length", minval=1)
vma2_length = input.int(20, title="VMA2 Length", minval=1)
show_trend1 = input.bool(true, title="Show VMA1 Trend Direction")
show_trend2 = input.bool(true, title="Show VMA2 Trend Direction")
// Function to calculate VMA
vma_calc(src, length) =>
var float pdmS = 0.0
var float mdmS = 0.0
var float pdiS = 0.0
var float mdiS = 0.0
var float iS = 0.0
var float vma = 0.0
k = 1.0 / length
pdm = math.max(src - src[1], 0)
mdm = math.max(src[1] - src, 0)
pdmS := (1 - k) * nz(pdmS[1]) + k * pdm
mdmS := (1 - k) * nz(mdmS[1]) + k * mdm
s = pdmS + mdmS
pdi = pdmS / s
mdi = mdmS / s
pdiS := (1 - k) * nz(pdiS[1]) + k * pdi
mdiS := (1 - k) * nz(mdiS[1]) + k * mdi
d = math.abs(pdiS - mdiS)
s1 = pdiS + mdiS
iS := (1 - k) * nz(iS[1]) + k * d / s1
hhv = ta.highest(iS, length)
llv = ta.lowest(iS, length)
d1 = hhv - llv
vI = (iS - llv) / d1
vma := (1 - k * vI) * nz(vma[1]) + k * vI * src
vma
// Calculate VMAs
vma1 = vma_calc(close, vma1_length)
vma2 = vma_calc(close, vma2_length)
// Determine trend direction changes
vma1_up = vma1 > vma1[1]
vma1_down = vma1 < vma1[1]
vma2_up = vma2 > vma2[1]
vma2_down = vma2 < vma2[1]
// Plot VMAs
plot(vma1, color=show_trend1 ? (vma1_up ? color.green : vma1_down ? color.red : color.blue) : color.black, linewidth=2, title="VMA1")
plot(vma2, color=show_trend2 ? (vma2_up ? color.green : vma2_down ? color.red : color.blue) : color.black, linewidth=2, title="VMA2")
// Plot arrows for VMA1
plotshape(show_trend1 and ta.change(vma1_up) and vma1_up, title="VMA1 Up Arrow", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(show_trend1 and ta.change(vma1_down) and vma1_down, title="VMA1 Down Arrow", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
- Assistant