簡體   English   中英

在 Pine Script 中將數據限制在最后 2 個時間范圍內

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

我有一個計算樞軸的腳本,但我只想顯示最近 2 個時間段的樞軸。 因此,如果我選擇 Day 作為范圍,則僅顯示過去 2 天的樞軸。 在每周,顯示最近 2 周。 內置 Pivot 點標准有一個輸入變量來限制樞軸的數量。 我只是無法弄清楚他們是如何做到這一點的。 我的圖表變得丑陋,顯示了這么多先前的樞軸: 1

我的代碼:

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

我最終做的是添加代碼來確定當前柱是否在當前或上一個時間范圍內(意思是如果計算每日的樞軸,那么只顯示今天和昨天的樞軸)。 不確定這是最優雅的編碼方式,但它正在工作:

    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