Pine Script Ploting填充枢轴线之间的背景

时间:2020-08-18 01:41:14

标签: pine-script

//@version=4
study("Pivot Points", "Pivot Points", true)
timeframe = input("D", type = input.resolution)

// Returns the average number of current chart bars in the given target HTF resolution (this reflects the dataset's history).
f_avgDilationOf(_res) =>
    // _res: resolution of any TF (in "timeframe.period" string format).

    b = barssince(change(time(_res)))
    cumTotal = cum(b == 0 ? b[1] + 2 : 0)
    cumCount = cum(b == 0 ? 1 : 0)
    cumTotal / cumCount

// Period change detection.
pChange(res) =>
    change(time(res == 'Y' ? 'D' : res))

// Get some previous value from last HTF period.
tfclose = security(syminfo.tickerid, timeframe, close[1], lookahead = true)
tfopen = security(syminfo.tickerid, timeframe, open[1], lookahead = true)
tfhigh = security(syminfo.tickerid, timeframe, high[1], lookahead = true)
tflow = security(syminfo.tickerid, timeframe, low[1], lookahead = true)

tdpp = (tfhigh + tflow + tfclose) / 3
tdr1 = tdpp + (tdpp - tflow)
tds1 = tdpp - (tfhigh - tdpp)
tdr2 = tdpp + (tfhigh - tflow)
tds2 = tdpp - (tfhigh - tflow)
tdr3 = tfhigh + (2 * (tdpp - tflow))
tds3 = tflow - (2 * (tfhigh - tdpp))

cdpp = (tfhigh + tflow + tfclose + tfopen) / 4
cdr1 = cdpp + (cdpp - tflow)
cds1 = cdpp - (tfhigh - cdpp)
cdr2 = cdpp + (tfhigh - tflow)
cds2 = cdpp - (tfhigh - tflow)
cdr3 = tfhigh + (2 * (cdpp - tflow)) 
cds3 = tflow - (2 * (tfhigh - cdpp))

// Verify if current charts bars are part of the last dilation of HTF.
lastBar = security(syminfo.tickerid, timeframe, barstate.islast, lookahead = barmerge.lookahead_on)
// Get avg no of chart bars in one dilation of HTF.
dilation = round(f_avgDilationOf(timeframe))
timeDelta = time - time[1]

var line tdr1Line = na
var line tdppLine = na
var line tds1Line = na
var line tdr2Line = na
var line tds2Line = na
var line tdr3Line = na
var line tds3Line = na

var line cdr1Line = na
var line cdppLine = na
var line cds1Line = na
var line cdr2Line = na
var line cds2Line = na
var line cdr3Line = na
var line cds3Line = na

// Holds bar index where a new line is created.
var Bar = 0
if pChange(timeframe)
    // Extend old line for the last bar before creating a new one.
    line.set_xy2(tdr1Line, time, tdr1[1])
    line.set_xy2(tdppLine, time, tdpp[1])
    line.set_xy2(tds1Line, time, tds1[1])
    line.set_xy2(tdr2Line, time, tdr2[1])
    line.set_xy2(tds2Line, time, tds2[1])
    line.set_xy2(tdr3Line, time, tdr3[1])
    line.set_xy2(tds3Line, time, tds3[1])
    
    line.set_xy2(cdr1Line, time, cdr1[1])
    line.set_xy2(cdppLine, time, cdpp[1])
    line.set_xy2(cds1Line, time, cds1[1])
    line.set_xy2(cdr2Line, time, cdr2[1])
    line.set_xy2(cds2Line, time, cds2[1])
    line.set_xy2(cdr3Line, time, cdr3[1])
    line.set_xy2(cds3Line, time, cds3[1])
    
    // Save bar index on transition.
    Bar := bar_index
    // Create new line.
    tdr1Line := line.new(time, tdr1, time + timeDelta, tdr1, xloc.bar_time, color = color.new(#2ebd85, 50), width = 1)
    tdppLine := line.new(time, tdpp, time + timeDelta, tdpp, xloc.bar_time, color = #2196f3, width = 1)
    tds1Line := line.new(time, tds1, time + timeDelta, tds1, xloc.bar_time, color = #e0294a, width = 1)
    tdr2Line := line.new(time, tdr2, time + timeDelta, tdr2, xloc.bar_time, color = color.new(#2ebd85, 50), width = 1)
    tds2Line := line.new(time, tds2, time + timeDelta, tds2, xloc.bar_time, color = #e0294a, width = 1)
    tdr3Line := line.new(time, tdr3, time + timeDelta, tdr3, xloc.bar_time, color = color.new(#2ebd85, 50), width = 1)
    tds3Line := line.new(time, tds3, time + timeDelta, tds3, xloc.bar_time, color = #e0294a, width = 1)
    
    cdr1Line := line.new(time, cdr1, time + timeDelta, cdr1, xloc.bar_time, color = color.new(#2ebd85, 50), width = 1)
    cdppLine := line.new(time, cdpp, time + timeDelta, cdpp, xloc.bar_time, color = #2196f3, width = 1)
    cds1Line := line.new(time, cds1, time + timeDelta, cds1, xloc.bar_time, color = #e0294a, width = 1)
    cdr2Line := line.new(time, cdr2, time + timeDelta, cdr2, xloc.bar_time, color = color.new(#2ebd85, 50), width = 1)
    cds2Line := line.new(time, cds2, time + timeDelta, cds2, xloc.bar_time, color = #e0294a, width = 1)
    cdr3Line := line.new(time, cdr3, time + timeDelta, cdr3, xloc.bar_time, color = color.new(#2ebd85, 50), width = 1)
    cds3Line := line.new(time, cds3, time + timeDelta, cds3, xloc.bar_time, color = #e0294a, width = 1)
    // Make type of the 2 `if` blocks the same.
    float(na)
else
    // We are not on a transition; prolong line until next transition.
    line.set_xy2(tdr1Line, time, tdr1)
    line.set_xy2(tdppLine, time, tdpp)
    line.set_xy2(tds1Line, time, tds1)
    line.set_xy2(tdr2Line, time, tdr2)
    line.set_xy2(tds2Line, time, tds2)
    line.set_xy2(tdr3Line, time, tdr3)
    line.set_xy2(tds3Line, time, tds3)
    
    line.set_xy2(cdr1Line, time, cdr1)
    line.set_xy2(cdppLine, time, cdpp)
    line.set_xy2(cds1Line, time, cds1)
    line.set_xy2(cdr2Line, time, cdr2)
    line.set_xy2(cds2Line, time, cds2)
    line.set_xy2(cdr3Line, time, cdr3)
    line.set_xy2(cds3Line, time, cds3)
    float(na)

// If we are in the last bars of the HTF resolution's dilation, project line into the future with remaining bars in average no of bars in dilation.
if lastBar
    line.set_xy2(tdr1Line, time + (timeDelta * (dilation - (bar_index - Bar))), tdr1)
    line.set_xy2(tdppLine, time + (timeDelta * (dilation - (bar_index - Bar))), tdpp)
    line.set_xy2(tds1Line, time + (timeDelta * (dilation - (bar_index - Bar))), tds1)
    line.set_xy2(tdr2Line, time + (timeDelta * (dilation - (bar_index - Bar))), tdr2)
    line.set_xy2(tds2Line, time + (timeDelta * (dilation - (bar_index - Bar))), tds2)
    line.set_xy2(tdr3Line, time + (timeDelta * (dilation - (bar_index - Bar))), tdr3)
    line.set_xy2(tds3Line, time + (timeDelta * (dilation - (bar_index - Bar))), tds3)
    
    line.set_xy2(cdr1Line, time + (timeDelta * (dilation - (bar_index - Bar))), cdr1)
    line.set_xy2(cdppLine, time + (timeDelta * (dilation - (bar_index - Bar))), cdpp)
    line.set_xy2(cds1Line, time + (timeDelta * (dilation - (bar_index - Bar))), cds1)
    line.set_xy2(cdr2Line, time + (timeDelta * (dilation - (bar_index - Bar))), cdr2)
    line.set_xy2(cds2Line, time + (timeDelta * (dilation - (bar_index - Bar))), cds2)
    line.set_xy2(cdr3Line, time + (timeDelta * (dilation - (bar_index - Bar))), cdr3)
    line.set_xy2(cds3Line, time + (timeDelta * (dilation - (bar_index - Bar))), cds3)

我试图在枢轴线(如图像)之间绘制背景填充,但是fill()函数不适用于line.new函数。有办法吗? 我试图在枢轴线(如图像)之间绘制背景填充,但是fill()函数不适用于line.new函数。有办法吗? 我试图在枢轴线(如图像)之间绘制背景填充,但是fill()函数不适用于line.new函数。有办法吗? 我试图在枢轴线(如图像)之间绘制背景填充,但是fill()函数不适用于line.new函数。有办法吗? 我试图在枢轴线(如图像)之间绘制背景填充,但fill()函数不适用于line.new函数。有办法吗? 我试图在枢轴线(如图像)之间绘制背景填充,但是fill()函数不适用于line.new函数。有办法吗? 我试图在枢轴线(如图像)之间绘制背景填充,但是fill()函数不适用于line.new函数。有办法吗?

0 个答案:

没有答案