简体   繁体   English

在 Pine Script 中将数据限制在最后 2 个时间范围内

[英]Limit data to last 2 timeframes in Pine Script

I have a script that is calculating pivots, but I only want to show the pivots for the last 2 time periods.我有一个计算枢轴的脚本,但我只想显示最近 2 个时间段的枢轴。 So if I choose Day as the range, then show the pivots only for the last 2 days.因此,如果我选择 Day 作为范围,则仅显示过去 2 天的枢轴。 On Weekly, show the last 2 weeks.在每周,显示最近 2 周。 The built-in Pivot Point Standard has an input variable to limit the number of pivots.内置 Pivot 点标准有一个输入变量来限制枢轴的数量。 I just can't figure out how they are doing this.我只是无法弄清楚他们是如何做到这一点的。 My chart gets ugly with so many previous pivots displaying: 1我的图表变得丑陋,显示了这么多先前的枢轴: 1

My code:我的代码:

//@version=4
study(title="New Pivot", max_bars_back=100, overlay=true)

// Input settings
pivotRange = input(title="Pivot Range", options=["Day","Week","Month"], defval="Week")
pp_timeframe = pivotRange == "Day" ? "D" : pivotRange == "Week" ? "W" : "M" 
pivot_type = input(title="Pivot Type",type=input.string, defval="Fibonacci", options = ['Fibonacci', 'Traditional'])

// Function outputs 1 when it's the first bar of the D/W/M
is_newbar(res) =>
    change(time(res == 'Y' ? 'D' : res))
    
// Create a function to fetch data based on resolution
GetData(res, data) =>
    security(syminfo.tickerid, res, data[1], lookahead=barmerge.lookahead_on)

// Now pass pp_timeframe to get which kind of price data to use for the pivot calculation
theHigh = GetData(pp_timeframe, high)
theLow = GetData(pp_timeframe, low)
theClose = GetData(pp_timeframe, close)

// Calculate the pivot levels
r1 = 0.0
r2 = 0.0
r3 = 0.0
s1 = 0.0
s2 = 0.0
s3 = 0.0
pp = (theHigh + theLow + theClose) / 3
if (pivot_type == "Traditional")
    r1 := (pp * 2) - theLow
    s1 := (pp * 2) - theHigh
    r2 := pp + (theHigh - theLow)
    s2 := pp - (theHigh - theLow)
    r3 := theHigh + 2 * (pp - theLow)
    s3 := theLow - 2 * (theHigh - pp)
else
    // Fibonacci pivots
    r3 := pp + ((theHigh - theLow) * 1.000)
    r2 := pp + ((theHigh - theLow) * .618)
    r1 := pp + ((theHigh - theLow) * .382)
    s1 := pp - ((theHigh - theLow) * .382)
    s2 := pp - ((theHigh - theLow) * .618)
    s3 := pp - ((theHigh - theLow) * 1.000)
    
// Midpoint pivots
m2 = (pp + s1) / 2
m1 = (s1 + s2) / 2
m3 = (pp + r1) / 2
m4 = (r1 + r2) / 2

// Returns the average number of current chart bars in the given target HTF resolution (this reflects the dataset's history).
f_avgDilationOf(_res) =>
    b = barssince(change(time(_res)))
    cumTotal = cum(b == 0 ? b[1] + 1 : 0)
    cumCount = cum(b == 0 ? 1 : 0)
    cumTotal / cumCount
    
// Get some previous value from last HTF period.
pHi = security(syminfo.tickerid, pp_timeframe, high[1], lookahead = barmerge.lookahead_on)
// Verify if current charts bars are part of the last dilation of HTF.
lastPBar = security(syminfo.tickerid, pp_timeframe, barstate.islast, lookahead = barmerge.lookahead_on)
// Get avg no of chart bars in one dilation of HTF.
dilation = round(f_avgDilationOf(pp_timeframe))
timeDelta = time - time[1]
// Store bar index when a new timeframe starts 
var newBar = 0
bars_since = 0
if is_newbar(pp_timeframe)
    newBar := bar_index
else
    bars_since := bars_since[1] + 1
    
// Output pivot points
var line pp_line = na
var line r1_line = na
var line r2_line = na
var line r3_line = na
var line s1_line = na
var line s2_line = na
var line s3_line = na
var line m1_line = na
var line m2_line = na
var line m3_line = na
var line m4_line = na
var label pp_label = na
var label r1_label = na
var label r2_label = na
var label r3_label = na
var label s1_label = na
var label s2_label = na
var label s3_label = na

//label.new(bar_index[0], high, text=tostring(bars_since))
pp_line := line.new(bar_index[bars_since], pp, bar_index, pp, color=#000000, style=line.style_solid)
s1_line := line.new(bar_index[bars_since], s1, bar_index, s1, color=color.silver, style=line.style_solid)
s2_line := line.new(bar_index[bars_since], s2, bar_index, s2, color=color.green, style=line.style_solid)
s3_line := line.new(bar_index[bars_since], s3, bar_index, s3, color=color.silver, style=line.style_solid)
r1_line := line.new(bar_index[bars_since], r1, bar_index, r1, color=color.silver, style=line.style_solid)
r2_line := line.new(bar_index[bars_since], r2, bar_index, r2, color=color.red, style=line.style_solid)
r3_line := line.new(bar_index[bars_since], r3, bar_index, r3, color=color.silver, style=line.style_solid)
m4_line := line.new(bar_index[bars_since], m4, bar_index, m4, color=color.red, style=line.style_dashed)
m1_line := line.new(bar_index[bars_since], m1, bar_index, m1, color=color.green, style=line.style_dashed)

// If we are in the last bars of the HTF resolution's dilation, project line into the future with remaining bars in average# of bars in dilation
if lastPBar
    line.set_xloc(id=pp_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    line.set_xloc(id=r3_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    line.set_xloc(id=r2_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    line.set_xloc(id=r1_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    line.set_xloc(id=s1_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    line.set_xloc(id=s2_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    line.set_xloc(id=s3_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    line.set_xloc(id=m1_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    line.set_xloc(id=m4_line, x1=time[bars_since], x2=time + (timeDelta * (dilation - (bar_index - newBar))), xloc=xloc.bar_time)
    
// Set labels
r1_label := label.new(bar_index, r1, "R1", style=label.style_none)
r2_label := label.new(bar_index, r2, "R2", style=label.style_none)
r3_label := label.new(bar_index, r3, "R3", style=label.style_none) 
s1_label := label.new(bar_index, s1, "S1", style=label.style_none)
s2_label := label.new(bar_index, s2, "S2", style=label.style_none)
s3_label := label.new(bar_index, s3, "S3", style=label.style_none)

if not is_newbar(pp_timeframe)
    line.delete(pp_line[1])
    line.delete(r3_line[1])
    line.delete(r2_line[1])
    line.delete(r1_line[1])
    line.delete(s3_line[1])
    line.delete(s2_line[1])
    line.delete(s1_line[1])
    label.delete(pp_label[0])
    label.delete(r1_label[0])
    label.delete(r2_label[0])
    label.delete(r3_label[0])
    label.delete(s1_label[0])
    label.delete(s2_label[0])
    label.delete(s3_label[0])

What I ended up doing is adding code to figure out if the current bar is in the current or previous time range (meaning if calculating pivots for Daily, then only display the pivots for today and yesterday).我最终做的是添加代码来确定当前柱是否在当前或上一个时间范围内(意思是如果计算每日的枢轴,那么只显示今天和昨天的枢轴)。 Not sure this is the most elegant way to code this, but it's working:不确定这是最优雅的编码方式,但它正在工作:

    currMonth = 0
    isCurrOrPrevPeriod = false

// Get the time difference between current time and time of current bar
timeDiff = timenow - time

// Turn that time difference into days
diffDays = round(timeDiff / 86400000)

if pp_timeframe == "D"
    // Is the current bar on today or yesterday?
    if diffDays < 2
        isCurrOrPrevPeriod := true
else
    if pp_timeframe == "W"
        // Is current bar on this week or last week
        offSetDays = dayofweek(timenow) == dayofweek.sunday ? 7 : 
                     dayofweek(timenow) == dayofweek.monday ? 8 : 
                     dayofweek(timenow) == dayofweek.tuesday ? 9 : 
                     dayofweek(timenow) == dayofweek.wednesday ? 10 : 
                     dayofweek(timenow) == dayofweek.thursday ? 11 : 
                     dayofweek(timenow) == dayofweek.friday ? 12 : 13
        if diffDays <= offSetDays
            isCurrOrPrevPeriod := true
    else
        // Is current bar on this month or last month
        currMonth := month(timenow)
        if (year(timenow) == year(time) and currMonth == month(time)) or (currMonth == 1 and year(timenow)-1 == year(time) and month(time) == 12) or (year(timenow) == year(time) and currMonth-1 == month(time))
            isCurrOrPrevPeriod := true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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