本页源码来自 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/
// © Quants_HK
//@version=6
indicator("Mag 7 Earnings Metrics", shorttitle="Mag 7 Earnings Metrics 1.0", overlay=false, precision=2, max_labels_count=500)
groupCore = "01 - Financial Settings"
minVotes = input.int(2, "Minimum Confirming Companies", minval=2, maxval=7, group=groupCore)
threshold = input.float(0.20, "Company Financial Score Threshold", minval=0.01, maxval=1.0, step=0.05, group=groupCore)
floorUSD = input.float(1.0, "Normalization Floor (USD Billions)", minval=0.01, group=groupCore) * 1000000000.0
showEvents = input.bool(true, "Show Company Earnings Labels", group="02 - Display")
showBG = input.bool(false, "Show Risk Background", group="02 - Display")
reactionThreshold = input.float(0.0, "First Full-Day Price Reaction Threshold (%)", minval=0, maxval=20, step=0.25, group=groupCore)
f_clip(x) =>
math.max(-1.0, math.min(1.0, x))
f_company(symbol) =>
nd = request.financial(symbol, "NET_DEBT", "FQ", gaps=barmerge.gaps_off, ignore_invalid_symbol=true, currency=currency.USD)
fc = request.financial(symbol, "FREE_CASH_FLOW", "FQ", gaps=barmerge.gaps_off, ignore_invalid_symbol=true, currency=currency.USD)
actual = request.earnings(symbol, earnings.actual, gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
estimate = request.earnings(symbol, earnings.estimate, gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
dailyClose = request.security(symbol, "D", close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
var float prevND = na
var float prevFC = na
var float lastScore = 0.0
var int pendingVote = 0
var bool pending = false
var float eventClose = na
var float lastEPS = na
var float lastReaction = na
var float lastNdDiff = na
var float lastFcDiff = na
bool event = not na(actual) and barstate.isconfirmed
bool confirmed = false
int newVote = 0
if pending and barstate.isconfirmed
float reaction = not na(eventClose) and eventClose != 0 and not na(dailyClose) ? 100.0 * (dailyClose - eventClose) / eventClose : na
lastReaction := reaction
bool priceAgree = not na(reaction) and (pendingVote == 1 ? reaction > reactionThreshold : pendingVote == -1 ? reaction < -reactionThreshold : false)
newVote := priceAgree ? pendingVote : 0
confirmed := true
pending := false
if event
pending := false
pendingVote := 0
lastEPS := na
lastReaction := na
lastNdDiff := na
lastFcDiff := na
bool validFinance = not na(nd) and not na(fc)
if validFinance
bool havePrevious = not na(prevND) and not na(prevFC)
if havePrevious
bool changed = nd != prevND or fc != prevFC
if changed
float ndRisk = f_clip((nd - prevND) / math.max(math.abs(prevND), floorUSD))
float fcRisk = f_clip((prevFC - fc) / math.max(math.abs(prevFC), floorUSD))
lastScore := 0.50 * ndRisk + 0.50 * fcRisk
lastNdDiff := (nd - prevND) / 1000000000.0
lastFcDiff := (fc - prevFC) / 1000000000.0
int financialVote = lastScore <= -threshold ? 1 : lastScore >= threshold ? -1 : 0
lastEPS := not na(estimate) ? actual - estimate : na
bool epsAgree = not na(lastEPS) and (financialVote == 1 ? lastEPS > 0 : financialVote == -1 ? lastEPS < 0 : false)
financialVote := epsAgree ? financialVote : 0
pendingVote := financialVote
if financialVote != 0
eventClose := dailyClose
pending := true
prevND := nd
prevFC := fc
[event, confirmed, newVote, lastScore, lastEPS, lastReaction, lastNdDiff, lastFcDiff]
[aER, aUp, aNew, aRisk, aEPS, aReaction, aND, aFC] = f_company("NASDAQ:AAPL")
[mER, mUp, mNew, mRisk, mEPS, mReaction, mND, mFC] = f_company("NASDAQ:MSFT")
[gER, gUp, gNew, gRisk, gEPS, gReaction, gND, gFC] = f_company("NASDAQ:GOOGL")
[zER, zUp, zNew, zRisk, zEPS, zReaction, zND, zFC] = f_company("NASDAQ:AMZN")
[nER, nUp, nNew, nRisk, nEPS, nReaction, nND, nFC] = f_company("NASDAQ:NVDA")
[fER, fUp, fNew, fRisk, fEPS, fReaction, fND, fFC] = f_company("NASDAQ:META")
[tER, tUp, tNew, tRisk, tEPS, tReaction, tND, tFC] = f_company("NASDAQ:TSLA")
int season = year * 4 + int(math.floor((month - 1) / 3.0))
bool newSeason = not na(season[1]) and season != season[1]
var int aVote = 0
var int mVote = 0
var int gVote = 0
var int zVote = 0
var int nVote = 0
var int fVote = 0
var int tVote = 0
if newSeason
aVote := 0
mVote := 0
gVote := 0
zVote := 0
nVote := 0
fVote := 0
tVote := 0
if aUp
aVote := aNew
if mUp
mVote := mNew
if gUp
gVote := gNew
if zUp
zVote := zNew
if nUp
nVote := nNew
if fUp
fVote := fNew
if tUp
tVote := tNew
int longVotes = (aVote == 1 ? 1 : 0) + (mVote == 1 ? 1 : 0) + (gVote == 1 ? 1 : 0) + (zVote == 1 ? 1 : 0) + (nVote == 1 ? 1 : 0) + (fVote == 1 ? 1 : 0) + (tVote == 1 ? 1 : 0)
int shortVotes = (aVote == -1 ? 1 : 0) + (mVote == -1 ? 1 : 0) + (gVote == -1 ? 1 : 0) + (zVote == -1 ? 1 : 0) + (nVote == -1 ? 1 : 0) + (fVote == -1 ? 1 : 0) + (tVote == -1 ? 1 : 0)
bool anyEvent = aER or mER or gER or zER or nER or fER or tER
bool anyUpdate = aUp or mUp or gUp or zUp or nUp or fUp or tUp
float riskIndex = math.max(0.0, math.min(100.0, 50.0 + 50.0 / 7.0 * (aRisk + mRisk + gRisk + zRisk + nRisk + fRisk + tRisk)))
color riskColor = riskIndex >= 55 ? color.red : riskIndex <= 45 ? color.lime : color.orange
plot(riskIndex, "M7 Historical Risk", color=riskColor, linewidth=3, style=plot.style_stepline)
hline(50, "Neutral", color=color.gray, linestyle=hline.style_dashed)
hline(55, "High Risk", color=color.new(color.red, 65))
hline(45, "Low Risk", color=color.new(color.green, 65))
bgcolor(showBG ? riskIndex >= 55 ? color.new(color.red, 92) : riskIndex <= 45 ? color.new(color.green, 92) : na : na)
plot(longVotes, "LONG Votes", display=display.data_window)
plot(shortVotes, "SHORT Votes", display=display.data_window)
plot(aVote, "AAPL Vote", display=display.data_window)
plot(aRisk, "AAPL Financial Score", display=display.data_window)
plot(mVote, "MSFT Vote", display=display.data_window)
plot(mRisk, "MSFT Financial Score", display=display.data_window)
plot(gVote, "GOOGL Vote", display=display.data_window)
plot(gRisk, "GOOGL Financial Score", display=display.data_window)
plot(zVote, "AMZN Vote", display=display.data_window)
plot(zRisk, "AMZN Financial Score", display=display.data_window)
plot(nVote, "NVDA Vote", display=display.data_window)
plot(nRisk, "NVDA Financial Score", display=display.data_window)
plot(fVote, "META Vote", display=display.data_window)
plot(fRisk, "META Financial Score", display=display.data_window)
plot(tVote, "TSLA Vote", display=display.data_window)
plot(tRisk, "TSLA Financial Score", display=display.data_window)
string eventNames = (aER ? " AAPL" : "") + (mER ? " MSFT" : "") + (gER ? " GOOGL" : "") + (zER ? " AMZN" : "") + (nER ? " NVDA" : "") + (fER ? " META" : "") + (tER ? " TSLA" : "")
string longNames = (aVote == 1 ? " AAPL" : "") + (mVote == 1 ? " MSFT" : "") + (gVote == 1 ? " GOOGL" : "") + (zVote == 1 ? " AMZN" : "") + (nVote == 1 ? " NVDA" : "") + (fVote == 1 ? " META" : "") + (tVote == 1 ? " TSLA" : "")
string shortNames = (aVote == -1 ? " AAPL" : "") + (mVote == -1 ? " MSFT" : "") + (gVote == -1 ? " GOOGL" : "") + (zVote == -1 ? " AMZN" : "") + (nVote == -1 ? " NVDA" : "") + (fVote == -1 ? " META" : "") + (tVote == -1 ? " TSLA" : "")
f_detail(name, event, updated, vote, score, eps, reaction, ndDiff, fcDiff) =>
string result = ""
if event or updated
result := "\n" + name + (updated ? " CONFIRMED" : " EARNINGS")
result += " | VOTE " + str.tostring(vote)
result += " | RISK " + str.tostring(score, "#.###")
result += " | ND dB " + (na(ndDiff) ? "N/A" : str.tostring(ndDiff, "#.##"))
result += " | FCF dB " + (na(fcDiff) ? "N/A" : str.tostring(fcDiff, "#.##"))
result += " | EPS surprise " + (na(eps) ? "N/A" : str.tostring(eps, "#.###"))
result += " | Price response % " + (na(reaction) ? "PENDING" : str.tostring(reaction, "#.##"))
result
if showEvents and anyEvent
string tip = "M7 VERSION C | " + str.format_time(time, "yyyy-MM-dd", syminfo.timezone)
tip += "\nM7 RISK: " + str.tostring(riskIndex, "#.##")
tip += "\nLONG: " + str.tostring(longVotes) + ":" + longNames
tip += "\nSHORT: " + str.tostring(shortVotes) + ":" + shortNames
tip += f_detail("AAPL", aER, aUp, aVote, aRisk, aEPS, aReaction, aND, aFC)
tip += f_detail("MSFT", mER, mUp, mVote, mRisk, mEPS, mReaction, mND, mFC)
tip += f_detail("GOOGL", gER, gUp, gVote, gRisk, gEPS, gReaction, gND, gFC)
tip += f_detail("AMZN", zER, zUp, zVote, zRisk, zEPS, zReaction, zND, zFC)
tip += f_detail("NVDA", nER, nUp, nVote, nRisk, nEPS, nReaction, nND, nFC)
tip += f_detail("META", fER, fUp, fVote, fRisk, fEPS, fReaction, fND, fFC)
tip += f_detail("TSLA", tER, tUp, tVote, tRisk, tEPS, tReaction, tND, tFC)
tip += "\nRESEARCH ONLY: financial release timing not verified"
string labelText = str.trim(eventNames)
if labelText == ""
labelText := "CONFIRM"
label.new(bar_index, riskIndex, labelText, style=label.style_label_down, color=color.new(color.blue, 25), textcolor=color.white, size=size.tiny, tooltip=tip)