本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gogaz
//@version=6
indicator("Seasonality Symbol Screener ", overlay = true, max_labels_count = 500)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Settings
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
groupLayout = "Dashboard Layout"
tablePosition = input.string("Top Right", "Table Position", options = ["Top Right", "Bottom Right", "Top Left", "Bottom Left", "Middle Right"], group = groupLayout)
textSize = input.string("Small", "Text Size", options = ["Tiny", "Small", "Normal", "Large"], group = groupLayout)
groupData = "Seasonality Settings"
yearsToCalculate = input.int(10, "Years to Calculate", minval = 3, maxval = 30, group = groupData)
groupTarget = "Target Month (screener columns + table highlight)"
targetMonthSel = input.string("Current", "Target Month", options = ["Current", "Next", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], group = groupTarget, tooltip = "Current = the month of the last bar. Next = the month after it. Or pick an exact month. Drives the Target Month screener columns.")
groupBias = "Bias Settings"
bullHit = input.float(60.0, "Bullish: min hit rate %", minval = 50.0, maxval = 100.0, group = groupBias, tooltip = "Current-month bias = +1 when avg return > 0 and hit rate is at or above this value.")
bearHit = input.float(40.0, "Bearish: max hit rate %", minval = 0.0, maxval = 50.0, group = groupBias, tooltip = "Current-month bias = -1 when avg return < 0 and hit rate is at or below this value.")
groupStyle = "Style"
headerBg = input.color(color.rgb(30, 34, 45), "Header Background", group = groupStyle)
cellBg = input.color(color.rgb(20, 24, 33), "Cell Background", group = groupStyle)
textColor = input.color(color.white, "Text Color", group = groupStyle)
posColor = input.color(color.rgb(38, 166, 154), "Positive Color", group = groupStyle)
negColor = input.color(color.rgb(239, 83, 80), "Negative Color", group = groupStyle)
curColor = input.color(color.rgb(255, 193, 7), "Target Month Highlight", group = groupStyle)
curFill = input.color(color.new(color.rgb(255, 193, 7), 75), "Current Month Fill (this-year row)", group = groupStyle)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Helper Functions
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_get_position(string pos) =>
switch pos
"Top Right" => position.top_right
"Bottom Right" => position.bottom_right
"Top Left" => position.top_left
"Bottom Left" => position.bottom_left
"Middle Right" => position.middle_right
=> position.top_right
f_get_size(string sz) =>
switch sz
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
=> size.small
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Data Collection
// Completed monthly returns: prior month-end close -> this month-end close
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var float[] retArr = array.new_float()
var int[] yrArr = array.new_int()
var int[] moArr = array.new_int()
var float prevMonthEnd = na
if bar_index > 0 and month(time) != month(time[1])
if not na(prevMonthEnd) and prevMonthEnd > 0 and not na(close[1])
array.push(retArr, (close[1] / prevMonthEnd - 1) * 100)
array.push(yrArr, year(time[1]))
array.push(moArr, month(time[1]))
prevMonthEnd := close[1]
string[] monthNames = array.from("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Seasonality Calculation (last bar only)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var float avgYearlyReturn = na
var float yearlyHitRate = na
var float[] monthlyReturns = array.new_float(12, 0.0)
var float[] monthlyHitRates = array.new_float(12, 0.0)
var float bestMonthAvg = na
var float worstMonthAvg = na
var string bestMonthName = ""
var int tgtIdx = 0
var float tgtAvg = na
var float tgtHit = na
var float tgtBias = na
var float tgtCount = na
var float bestMonthNum = na
var float worstMonthNum = na
var float curMonthRet = na
var float curVsAvg = na
var bool aheadOfAvg = false
var bool behindAvg = false
int nRet = array.size(retArr)
// This year's month-by-month returns, current month return so far (MTD), and YTD
var float[] thisYrRet = array.new_float(12, float(na))
var float ytdRet = na
if barstate.islast
curMonthRet := not na(prevMonthEnd) and prevMonthEnd > 0 ? (close / prevMonthEnd - 1) * 100 : na
array.fill(thisYrRet, float(na))
int cy = year(time)
float ytdProd = 1.0
bool anyYtd = false
if nRet > 0
for i = 0 to nRet - 1
if array.get(yrArr, i) == cy
float rr = array.get(retArr, i)
array.set(thisYrRet, array.get(moArr, i) - 1, rr)
ytdProd *= 1 + rr / 100
anyYtd := true
if not na(curMonthRet)
array.set(thisYrRet, month(time) - 1, curMonthRet)
ytdProd *= 1 + curMonthRet / 100
anyYtd := true
ytdRet := anyYtd ? (ytdProd - 1) * 100 : na
if barstate.islast and nRet > 0
int lastYear = year(time)
int n = array.size(retArr)
float[] sums = array.new_float(12, 0.0)
int[] cnts = array.new_int(12, 0)
int[] wins = array.new_int(12, 0)
// Monthly averages and hit rates over the window (completed months only)
for i = 0 to n - 1
int y = array.get(yrArr, i)
if y >= lastYear - yearsToCalculate and y < lastYear
int m = array.get(moArr, i) - 1
float r = array.get(retArr, i)
array.set(sums, m, array.get(sums, m) + r)
array.set(cnts, m, array.get(cnts, m) + 1)
if r > 0
array.set(wins, m, array.get(wins, m) + 1)
for m = 0 to 11
int c = array.get(cnts, m)
array.set(monthlyReturns, m, c > 0 ? array.get(sums, m) / c : 0.0)
array.set(monthlyHitRates, m, c > 0 ? array.get(wins, m) * 100.0 / c : 0.0)
// Average yearly return (compounded monthly returns) and yearly hit rate
float total = 0.0
int validYears = 0
int positiveYears = 0
for y = 1 to yearsToCalculate
float prod = 1.0
bool found = false
for i = 0 to n - 1
if array.get(yrArr, i) == lastYear - y
prod *= 1 + array.get(retArr, i) / 100
found := true
if found
float yr = (prod - 1) * 100
total += yr
validYears += 1
if yr > 0
positiveYears += 1
avgYearlyReturn := validYears > 0 ? total / validYears : 0.0
yearlyHitRate := validYears > 0 ? positiveYears * 100.0 / validYears : 0.0
bestMonthAvg := array.max(monthlyReturns)
worstMonthAvg := array.min(monthlyReturns)
int bIdx = array.indexof(monthlyReturns, bestMonthAvg)
int wIdx = array.indexof(monthlyReturns, worstMonthAvg)
bestMonthName := bIdx >= 0 ? array.get(monthNames, bIdx) : ""
// Current-month seasonal bias
tgtIdx := targetMonthSel == "Current" ? month(time) - 1 : targetMonthSel == "Next" ? month(time) % 12 : math.max(array.indexof(monthNames, targetMonthSel), 0)
tgtCount := array.get(cnts, tgtIdx)
tgtAvg := array.get(monthlyReturns, tgtIdx)
tgtHit := array.get(monthlyHitRates, tgtIdx)
tgtBias := tgtAvg > 0 and tgtHit >= bullHit ? 1.0 : tgtAvg < 0 and tgtHit <= bearHit ? -1.0 : 0.0
bestMonthNum := bIdx + 1
worstMonthNum := wIdx + 1
// Current month return so far vs the average of this calendar month (percentage points)
int curM = month(time) - 1
bool hasHist = array.get(cnts, curM) > 0
curVsAvg := na(curMonthRet) or not hasHist ? na : curMonthRet - array.get(monthlyReturns, curM)
aheadOfAvg := not na(curVsAvg) and curVsAvg > 0
behindAvg := not na(curVsAvg) and curVsAvg < 0
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Screener Plots (visible in Pine Screener only)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// First ten plots = default screener columns (the rest can be added manually)
plot(avgYearlyReturn, "Avg Yearly Return %", color = color.blue, display = display.pine_screener)
plot(yearlyHitRate, "Yearly Hit Rate %", color = color.blue, display = display.pine_screener)
plot(curMonthRet, "Current Month Return % (MTD)", color = color.white, display = display.pine_screener)
plot(curVsAvg, "MTD vs Month Avg (pp)", color = color.white, display = display.pine_screener)
plot(tgtAvg, "Target Month Avg %", color = color.orange, display = display.pine_screener)
plot(tgtHit, "Target Month Hit %", color = color.orange, display = display.pine_screener)
plot(tgtBias, "Target Month Bias (+1/0/-1)", color = color.orange, display = display.pine_screener)
plot(tgtCount, "Target Month Sample (yrs)", color = color.orange, display = display.pine_screener)
plot(bestMonthAvg, "Best Month Avg %", color = color.green, display = display.pine_screener)
plot(bestMonthNum, "Best Month (1-12)", color = color.green, display = display.pine_screener)
plot(worstMonthAvg, "Worst Month Avg %", color = color.red, display = display.pine_screener)
plot(worstMonthNum, "Worst Month (1-12)", color = color.red, display = display.pine_screener)
plot(ytdRet, "YTD Return %", color = color.white, display = display.pine_screener)
// Screener filter conditions
alertcondition(aheadOfAvg, "Current month ahead of its average", "Current month return is above the historical average for this month")
alertcondition(behindAvg, "Current month behind its average", "Current month return is below the historical average for this month")
// Extra columns (add them in the screener if wanted): per-month avg and hit rate
plot(array.get(monthlyReturns, 0), "Jan Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 1), "Feb Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 2), "Mar Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 3), "Apr Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 4), "May Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 5), "Jun Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 6), "Jul Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 7), "Aug Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 8), "Sep Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 9), "Oct Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 10), "Nov Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyReturns, 11), "Dec Avg %", color = color.gray, display = display.pine_screener)
plot(array.get(monthlyHitRates, 0), "Jan Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 1), "Feb Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 2), "Mar Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 3), "Apr Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 4), "May Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 5), "Jun Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 6), "Jul Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 7), "Aug Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 8), "Sep Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 9), "Oct Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 10), "Nov Hit %", color = color.silver, display = display.pine_screener)
plot(array.get(monthlyHitRates, 11), "Dec Hit %", color = color.silver, display = display.pine_screener)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// Table Rendering (last bar)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var table dashTable = na
if barstate.islast
if not na(dashTable)
table.delete(dashTable)
dashTable := table.new(
position = f_get_position(tablePosition),
columns = 16,
rows = 4,
bgcolor = cellBg,
border_width = 1,
border_color = color.gray,
frame_color = color.gray,
frame_width = 1
)
string sz = f_get_size(textSize)
// HEADER ROW
table.cell(dashTable, 0, 0, "Ticker", bgcolor = headerBg, text_color = textColor, text_size = sz, text_halign = text.align_center)
table.cell(dashTable, 1, 0, "Yrs", bgcolor = headerBg, text_color = textColor, text_size = sz, text_halign = text.align_center)
table.cell(dashTable, 2, 0, "Year", bgcolor = headerBg, text_color = textColor, text_size = sz, text_halign = text.align_center)
for m = 0 to 11
table.cell(dashTable, m + 3, 0, array.get(monthNames, m), bgcolor = headerBg, text_color = m == tgtIdx ? curColor : textColor, text_size = sz, text_halign = text.align_center)
table.cell(dashTable, 15, 0, "Best Month", bgcolor = headerBg, text_color = textColor, text_size = sz, text_halign = text.align_center)
// AVERAGE RETURN ROW
color cAvg = avgYearlyReturn >= 0 ? posColor : negColor
string tAvg = (avgYearlyReturn >= 0 ? "+" : "") + str.tostring(avgYearlyReturn, "#.##") + "%"
table.cell(dashTable, 0, 1, syminfo.ticker, text_color = textColor, text_size = sz, bgcolor = cellBg)
table.cell(dashTable, 1, 1, str.tostring(yearsToCalculate) + "Y", text_color = textColor, text_size = sz, bgcolor = cellBg)
table.cell(dashTable, 2, 1, tAvg, text_color = cAvg, text_size = sz, bgcolor = cellBg)
for m = 0 to 11
float ret = array.get(monthlyReturns, m)
table.cell(dashTable, m + 3, 1, (ret >= 0 ? "+" : "") + str.tostring(ret, "#.##") + "%", text_color = ret >= 0 ? posColor : negColor, text_size = sz, bgcolor = cellBg)
string bestMonthStr = bestMonthName + " (" + (bestMonthAvg >= 0 ? "+" : "") + str.tostring(bestMonthAvg, "#.##") + "%)"
table.cell(dashTable, 15, 1, bestMonthStr, text_color = bestMonthAvg >= 0 ? posColor : negColor, text_size = sz, bgcolor = cellBg)
// HIT RATE ROW
table.cell(dashTable, 0, 2, "Hit Rate", text_color = textColor, text_size = sz, bgcolor = cellBg)
table.cell(dashTable, 1, 2, "", text_color = textColor, text_size = sz, bgcolor = cellBg)
table.cell(dashTable, 2, 2, str.tostring(yearlyHitRate, "#") + "%", text_color = yearlyHitRate >= 50 ? posColor : negColor, text_size = sz, bgcolor = cellBg)
for m = 0 to 11
float hr = array.get(monthlyHitRates, m)
table.cell(dashTable, m + 3, 2, str.tostring(hr, "#") + "%", text_color = hr >= 50 ? posColor : negColor, text_size = sz, bgcolor = cellBg)
string biasStr = tgtBias > 0 ? "Bullish" : tgtBias < 0 ? "Bearish" : "Neutral"
table.cell(dashTable, 15, 2, array.get(monthNames, tgtIdx) + ": " + biasStr, text_color = tgtBias > 0 ? posColor : tgtBias < 0 ? negColor : curColor, text_size = sz, bgcolor = cellBg)
// THIS YEAR ROW: actual return of each month this year (current month = MTD), YTD in the Year column
string curMonthStr = na(curMonthRet) ? "n/a" : (curMonthRet >= 0 ? "+" : "") + str.tostring(curMonthRet, "#.##") + "%"
string ytdStr = na(ytdRet) ? "n/a" : (ytdRet >= 0 ? "+" : "") + str.tostring(ytdRet, "#.##") + "%"
int curIdx = month(time) - 1
table.cell(dashTable, 0, 3, "This Year", text_color = textColor, text_size = sz, bgcolor = cellBg)
table.cell(dashTable, 1, 3, "YTD", text_color = textColor, text_size = sz, bgcolor = cellBg)
table.cell(dashTable, 2, 3, ytdStr, text_color = na(ytdRet) ? textColor : ytdRet >= 0 ? posColor : negColor, text_size = sz, bgcolor = cellBg)
for m = 0 to 11
float tv = array.get(thisYrRet, m)
string ts = na(tv) ? "-" : (tv >= 0 ? "+" : "") + str.tostring(tv, "#.##") + "%"
color tc = na(tv) ? color.gray : tv >= 0 ? posColor : negColor
table.cell(dashTable, m + 3, 3, ts, text_color = tc, text_size = sz, bgcolor = m == curIdx ? curFill : cellBg)
string vsStr = na(curVsAvg) ? "" : " (" + (curVsAvg >= 0 ? "+" : "") + str.tostring(curVsAvg, "#.#") + " vs avg)"
table.cell(dashTable, 15, 3, "MTD " + curMonthStr + vsStr, text_color = na(curMonthRet) ? textColor : curMonthRet >= 0 ? posColor : negColor, text_size = sz, bgcolor = cellBg)