如何在筛选器中计算表现?

筛选器表现数据使用以下公式计算:

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

在哪里:

  • 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     elsevar lastYearOpen = openif year > year[1]
            lastYearOpen := openrateOfreturn(lastYearOpen)fastSearchTimeIndex(x, maxbarsback) =>
    mid = 0 * time[maxbarsback]
    right = math.min(bar_index, maxbarsback)
    left = 0if x/1000 <= first_bar_time         bar_index     else if time < x         0elsefor i = 0 to 10
            mid := math.ceil((left + right) / 2)if left == right                 breakelse if time[mid] < x                 right := mid                 continueelse if time[mid] > x                 left := mid                 continueelsebreak
        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')
Java

注意:由于timenow的影响,此脚本的历史记录值和实时值有所不同,请参见此处

为了便于可视化显示,您可以使用Pine编辑器,使用图表的日线时间框架将此脚本添加到您的图表中。图表上将出现一个指标,其图表将显示每种表现类型的值。

涨跌%与表现%:

假设今天是星期二。

每周涨跌 - 当前收盘价(星期二)与上周收盘价(上周五收盘价)之间的差值。

每周表现 - 当前收盘价(星期二)与一周前(上周二)开盘价之间的差值。