如何在篩選器中計算績效?

篩選器績效使用以下公式計算:

Perf. = (currentClose – openDaysAgo) × 100 / abs(openDaysAgo)

在:

  • currentClose — 最新收盤價
  • openDaysAgo — 對應過去K線的開盤價,由所選週期決定(例如,1週、3個月、365天)

範例

今天是星期二,我們來計算一下Perf.W:

  1. 取今日收盤價
  2. 減去上週二日線圖的開盤價
  3. 將差值乘以100
  4. 將結果除以上週二日線圖開盤價的絕對值

以下是最常用週期的詳細公式,其中考慮了閏年天數等具體因素。

//@version=6
indicator("Screener Performance")

// first bar's timestamp in pine history
var first_bar_time = time / 1000

// Performance helper functions
rateOfreturn(ref) =>
	if ref < 0 and close > 0
		na
	else
		(close - ref) * 100 / math.abs(ref)
rr(bb, maxbarsback) =>
	nz(open[maxbarsback] * 0) + bb == 0 ? na : rateOfreturn(open[bb])
perfYTD() =>
	if year != year(timenow)
		na
	else
		var lastYearOpen = open
		if year > year[1]
			lastYearOpen := open
		rateOfreturn(lastYearOpen)

fastSearchTimeIndex(x, maxbarsback) =>
	mid = 0 * time[maxbarsback]
	right = math.min(bar_index, maxbarsback)
	left = 0
	if x/1000 <= first_bar_time
		bar_index
	else if time < x
		0
	else
		for i = 0 to 10
			mid := math.ceil((left + right) / 2)
			if left == right
				break
			else if time[mid] < x
				right := mid
				continue
			else if time[mid] > x
				left := mid
				continue
			else
				break
		mid

week1 = 7
week_ago = timenow - 1000 * 60 * 60 * 24 * week1
week_ago_this_bar = time - 1000 * 60 * 60 * 24 * week1
countOfBarsWeekAgo = fastSearchTimeIndex(week_ago, week1)

month1 = 30
month_ago = timenow - 1000 * 60 * 60 * 24 * month1
countOfBars1MonthAgo = fastSearchTimeIndex(month_ago, month1)

month3 = 90
months3_ago = timenow - 1000 * 60 * 60 * 24 * month3
countOfBars3MonthAgo = fastSearchTimeIndex(months3_ago, month3)

month6 = 180
months6_ago = timenow - 1000 * 60 * 60 * 24 * month6
countOfBars6MonthAgo = fastSearchTimeIndex(months6_ago, month6)

years1 = 365
oneYearAgo = timenow - 1000 * 60 * 60 * 24 * years1
barsCountOneYear = fastSearchTimeIndex(oneYearAgo, years1)

years3 = 365 * 3
years3_ago = timenow - 1000 * 60 * 60 * 24 * years3
countOfBars3YearAgo = fastSearchTimeIndex(years3_ago, years3)

years5 = 365 * 4 + 366
years5_ago = timenow - 1000 * 60 * 60 * 24 * years5
countOfBars5YearAgo = fastSearchTimeIndex(years5_ago, years5)

years10 = (365 * 4 + 366) * 2
years10_ago = timenow - 1000 * 60 * 60 * 24 * years10
countOfBars10YearAgo = fastSearchTimeIndex(years10_ago, years10)

// Perf.<W | 1M | 3M | 6M | Y | 5Y | 10Y | YTD>
fiveDays = 5
fiveDaysAgo = timenow - 1000 * 60 * 60 * 24 * fiveDays
countOfBarsFiveDaysAgo = fastSearchTimeIndex(fiveDaysAgo, fiveDays)
perfYTD = perfYTD()
plot(rr(countOfBarsFiveDaysAgo, fiveDays), title='Perf.5D')
plot(rr(countOfBarsWeekAgo, week1), title='Perf.W')
plot(rr(countOfBars1MonthAgo, month1), title='Perf.1M')
plot(rr(countOfBars3MonthAgo, month3), title='Perf.3M')
plot(rr(countOfBars6MonthAgo, month6), title='Perf.6M')
plot(rr(barsCountOneYear, years1), title='Perf.Y')
plot(rr(countOfBars3YearAgo, years3), title='Perf.3Y')
plot(rr(countOfBars5YearAgo, years5), title='Perf.5Y')
plot(rr(countOfBars10YearAgo, years10), title='Perf.10Y')
plot(perfYTD, title='Perf.YTD')

注意:由於timenow的影響,此腳本的歷史記錄值和即時值有所不同,請參閱此處

為了方便視覺化顯示,您可以使用Pine編輯器,使用圖表的日線時間週期將此腳本新增到您的圖表中。圖表上將出現一個指標,其圖表將顯示每種表現類型的值。

漲跌% vs 績效%:

假設今天是星期二。

每週漲跌 - 目前收盤價(星期二)與上週收盤價(上週五收盤價)之間的差值。

每週績效 - 目前收盤價(星期二)與上週前(上週二)開盤價之間的差值。