簡體   English   中英

V1 到 V5 腳本轉換

[英]V1 to V5 script conversion

我正在嘗試將 Pine 代碼從 V1 轉換為 V5,但我似乎無法正確處理。 據我所知,我已經做了很多重構,但我不確定 V1 在函數內部自引用未定義變量方面是如何工作的。 這是我要轉換的腳本: https://www.tradingview.com/script/a0h7abzK-Volume-Pressure-Composite-Average-with-Bands-by-XeL-Arjona/

這是迄今為止的結果。 它做了一些事情,但與原始顏色相比,它無法正確計算蠟燭的顏色。 我將不勝感激任何形式的幫助,因為這超出了我理解其工作原理的能力。

//@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)

我進行了轉換,它似乎有效。

//@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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM