简体   繁体   中英

V1 to V5 script conversion

I'm trying to convert a Pine code from V1 to V5 but I can't seem to get it right. I've done a lot of refactoring to the best of my knowledge but I'm not sure how V1 worked with regards to self-referencing undefined variables inside functions. This is the script I'm trying to convert: https://www.tradingview.com/script/a0h7abzK-Volume-Pressure-Composite-Average-with-Bands-by-XeL-Arjona/

And here's the result so far. It does something but it fails to calculate the color of candles correctly when compared to original. I'd be grateful for any kind of help, as this is now beyond my capabilities to understand how it works.

//@version=5
indicator("VOLUME PRESSURE -COMPOSITE- WEIGHTED MOVING AVERAGE by @XeL_Arjona", shorttitle="VPMA_XeL", overlay=true)

GROUP_VOLUME_OPTIONS         = "Volume Pressure Options"

i_p = input.int(21, "Lookback Periods", group = GROUP_VOLUME_OPTIONS )
i_bands = input.bool(false, "Show Pressure Band", group = GROUP_VOLUME_OPTIONS)
i_colb = input.bool(true, "Color Trend on price bars", group = GROUP_VOLUME_OPTIONS)

vol = volume > 0 ? volume : 1
var VMema = array.new_float()
// Close Conditions for Pressure Algorithms
cl = close
op = open
hi = high
lo = low

vema(arr,periods,K) =>
    ret = 0.0
    VolW = vol*arr
    VolK = K / ( periods + 1 )
    VMsma = math.sum( ( VolW ), periods ) / math.sum( vol, periods )
    if array.size(VMema) < 2
        ret := ( VMsma * VolK )
    else if array.size(VMema) >= 2 and na( array.get(VMema, array.size( VMema ) - 2) )
        ret := ( VMsma * VolK )
    else
        ret := ( ( ( VolW - array.get(VMema, array.size( VMema ) - 2 ) ) * VolK) + array.get(VMema, array.size( VMema ) - 2) )
    ret

// Bull And Bear "Power-Balance" by Vadim Gimelfarb Algorithm's
BP =    (cl<op ? (cl[1]<op ? math.max(hi-cl[1], cl-lo) : math.max(hi-op, cl-lo)) : (cl>op ? (cl[1]>op ? hi-lo: math.max(op-cl[1], hi-lo)) : (hi-cl>cl-lo ? (cl[1]<op ? math.max(hi-cl[1],cl-lo) : hi-op) : (hi-cl<cl-lo ? (cl[1]>op ? hi-lo : math.max(op-cl[1], hi-lo)) : (cl[1]>op ? math.max(hi-op, cl-lo) : (cl[1]<op ? math.max(op-cl[1], hi-lo) : hi-lo))))))
SP =    (cl<op ? (cl[1]>op ? math.max(cl[1]-op, hi-lo) : hi-lo) : (cl>op ? (cl[1]>op ? math.max(cl[1]-lo, hi-cl) : math.max(op-lo, hi-cl)) : (hi-cl>cl-lo ? (cl[1]>op ? math.max(cl[1]-op, hi-lo) : hi-lo) : (hi-cl<cl-lo ? (cl[1]>op ? math.max(cl[1]-lo, hi-cl) : op-lo) : (cl[1]>op ? math.max(cl[1]-op, hi-lo) : (cl[1]<op ? math.max(op-lo, hi-cl) : hi-lo))))))
TP = BP+SP
// GENERAL CALCULATION VARIABLES FOR STUDIES
BPV = (BP/TP)*vol
SPV = (SP/TP)*vol
TPV = BPV+SPV
TH  = math.max(high, close[1])
TL  = math.min(low, close[1])
BPP = (TL+close)/2
SPP = (TH+close)/2
// Volume Pressures Weighted Averages
v1 = vema((BPV*BPP),i_p,3)
array.push(VMema, v1)
v2 = vema(TPV,i_p,3)*2
array.push(VMema, v2)
bpMavg = v1 / v2
v3 = vema((SPV*SPP),i_p,3)
array.push(VMema, v3)
v4 = vema(TPV,i_p,3)*2
array.push(VMema, v4)
spMavg = v3 / v4
VPMavg = (bpMavg+spMavg)/2
VPMAc = bpMavg > VPMavg ? color.green : color.red
// PLOT DIRECTIVES
//Cloud coloring method by @ChrisMoody
BPAbove = bpMavg >= spMavg ? 1 : na
SPBelow = bpMavg <= spMavg ? 1 : na
BPplotU = BPAbove ? bpMavg : na
SPplotU = BPAbove ? spMavg : na
BPplotD = SPBelow ? bpMavg : na
SPplotD = SPBelow ? spMavg : na
// Standard Line Coloring
CondCol = bpMavg > spMavg ? color.green : color.red
//Center Avg Line
plot(i_bands?na:VPMavg, color=CondCol, title='VPMA', style=plot.style_line, linewidth=2)
//Cloud Lines Plot Statements - ***linebr to create rules for change in Shading
p1 = plot(i_bands and BPplotU ? BPplotU  : na, title = 'BP/SP', style=plot.style_linebr, linewidth=1, color=CondCol)
p2 = plot(i_bands and SPplotU ? SPplotU  : na, title = 'SP/BP', style=plot.style_linebr, linewidth=1, color=CondCol)
p3 = plot(i_bands and BPplotD ? BPplotD  : na, title = 'BP/SP', style=plot.style_linebr, linewidth=1, color=CondCol)
p4 = plot(i_bands and SPplotD ? SPplotD  : na, title = 'SP/BP', style=plot.style_linebr, linewidth=1, color=CondCol)
//Fills that color cloud based on Trend.
fill(p1, p2, color=color.green, transp=90, title='Cloud')
fill(p3, p4, color=color.red, transp=90, title='Cloud')
plot(i_bands and bpMavg ? bpMavg : na, title = 'BPavg', style=plot.style_line, linewidth=1, color=color.green)
plot(i_bands and spMavg ? spMavg : na, title = 'SPavg', style=plot.style_line, linewidth=1, color=color.red)
barcolor(i_colb?CondCol:na)

I made the conversion and it seems to work.

//@version=5

// Bull And Bear "Power-Balance" by Vadim Gimelfarb Algorithm's

indicator('VOLUME PRESSURE -COMPOSITE- WEIGHTED MOVING AVERAGE by @XeL_Arjona', shorttitle='VPMA_XeL', overlay=true)
p = input(title='Lookback Periods:', defval=21)
bands = input(false, title='Show Pressure Bands:')
colb = input(true, title='Color Trend on price bars:')
// MAIN GENERAL VARIABLES/FUNCTIONS
vol = volume > 0 ? volume : 1
vema(array, periods, K) =>
    VolW = vol * array
    VolK = K / (periods + 1)
    VMsma = math.sum(VolW, periods) / math.sum(vol, periods)
    VMema = 0.
    VMema := na(VMema[1]) ? VMsma * VolK : (VolW - VMema[1]) * VolK + VMema[1]
    VMema

// Close Conditions for Pressure Algorithms
cl = close
op = open
hi = high
lo = low
// Bull And Bear "Power-Balance" by Vadim Gimelfarb Algorithm's
BP = cl < op ? cl[1] < op ? math.max(hi - cl[1], cl - lo) : math.max(hi - op, cl - lo) : cl > op ? cl[1] > op ? hi - lo : math.max(op - cl[1], hi - lo) : hi - cl > cl - lo ? cl[1] < op ? math.max(hi - cl[1], cl - lo) : hi - op : hi - cl < cl - lo ? cl[1] > op ? hi - lo : math.max(op - cl[1], hi - lo) : cl[1] > op ? math.max(hi - op, cl - lo) : cl[1] < op ? math.max(op - cl[1], hi - lo) : hi - lo
SP = cl < op ? cl[1] > op ? math.max(cl[1] - op, hi - lo) : hi - lo : cl > op ? cl[1] > op ? math.max(cl[1] - lo, hi - cl) : math.max(op - lo, hi - cl) : hi - cl > cl - lo ? cl[1] > op ? math.max(cl[1] - op, hi - lo) : hi - lo : hi - cl < cl - lo ? cl[1] > op ? math.max(cl[1] - lo, hi - cl) : op - lo : cl[1] > op ? math.max(cl[1] - op, hi - lo) : cl[1] < op ? math.max(op - lo, hi - cl) : hi - lo
TP = BP + SP


// GENERAL CALCULATION VARIABLES FOR STUDIES
BPV = BP / TP * vol
SPV = SP / TP * vol
TPV = BPV + SPV
TH = math.max(high, close[1])
TL = math.min(low, close[1])
BPP = (TL + close) / 2
SPP = (TH + close) / 2
// Volume Pressures Weighted Averages
bpMavg = vema(BPV * BPP, p, 3) / vema(TPV, p, 3) * 2
spMavg = vema(SPV * SPP, p, 3) / vema(TPV, p, 3) * 2
VPMavg = (bpMavg + spMavg) / 2
VPMAc = bpMavg > VPMavg ? color.green : color.red
// PLOT DIRECTIVES
//Cloud coloring method by @ChrisMoody
BPAbove = bpMavg >= spMavg ? 1 : na
SPBelow = bpMavg <= spMavg ? 1 : na
BPplotU = BPAbove ? bpMavg : na
SPplotU = BPAbove ? spMavg : na
BPplotD = SPBelow ? bpMavg : na
SPplotD = SPBelow ? spMavg : na
// Standard Line Coloring
CondCol = bpMavg > spMavg ? color.green : color.red
//Center Avg Line
plot(bands ? na : VPMavg, color=CondCol, title='VPMA', style=plot.style_line, linewidth=2)
//Cloud Lines Plot Statements - ***linebr to create rules for change in Shading
p1 = plot(bands and BPplotU ? BPplotU : na, title='BP/SP', style=plot.style_linebr, linewidth=1, color=CondCol)
p2 = plot(bands and SPplotU ? SPplotU : na, title='SP/BP', style=plot.style_linebr, linewidth=1, color=CondCol)
p3 = plot(bands and BPplotD ? BPplotD : na, title='BP/SP', style=plot.style_linebr, linewidth=1, color=CondCol)
p4 = plot(bands and SPplotD ? SPplotD : na, title='SP/BP', style=plot.style_linebr, linewidth=1, color=CondCol)
//Fills that color cloud based on Trend.
fill(p1, p2, color=color.new(color.green, 90), title='Cloud')
fill(p3, p4, color=color.new(color.red, 90), title='Cloud')
plot(bands and bpMavg ? bpMavg : na, title='BPavg', style=plot.style_line, linewidth=1, color=color.new(color.green, 0))
plot(bands and spMavg ? spMavg : na, title='SPavg', style=plot.style_line, linewidth=1, color=color.new(color.red, 0))
barcolor(colb ? CondCol : na)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM