섹터 유니버스 분리와 월간 갱신 정합화

This commit is contained in:
2026-06-15 02:29:29 +09:00
parent e2820065d1
commit 82ca4ddbfd
11 changed files with 1658 additions and 43 deletions
+402 -27
View File
@@ -1,5 +1,5 @@
// gas_lib.gs - Common utilities & static features
// Last Updated: 2026-06-14 20:48:30 KST
// Last Updated: 2026-06-15 02:20:50 KST
// Math/KRX utils, sheet I/O, sector flow, Web API, static runners
// GAS global scope: functions in gas_data_feed.gs / gas_data_collect.gs callable directly
//
@@ -593,7 +593,14 @@ const DEFAULT_SECTOR_UNIVERSE_V2 = [
{ code: "062040", name: "산일전기", weight: 0.10 },
{ code: "298040", name: "효성중공업", weight: 0.10 },
]},
{ sector: "방산", proxyTicker: "012450", proxyName: "한화에어로스페이스", proxyType: "대표주", baseTicker: "069500", constituents: [
{ sector: "전력설비", proxyTicker: "491820", proxyName: "HANARO 전력설비투자", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "010120", name: "LS ELECTRIC", weight: 0.28 },
{ code: "267260", name: "HD현대일렉트릭", weight: 0.28 },
{ code: "298040", name: "효성중공업", weight: 0.18 },
{ code: "006260", name: "LS", weight: 0.14 },
{ code: "099440", name: "두산에너빌리티", weight: 0.12 },
]},
{ sector: "방산", proxyTicker: "463250", proxyName: "TIGER K방산&우주", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "012450", name: "한화에어로스페이스", weight: 0.45 },
{ code: "079550", name: "LIG넥스원", weight: 0.25 },
{ code: "047810", name: "한국항공우주", weight: 0.15 },
@@ -605,23 +612,49 @@ const DEFAULT_SECTOR_UNIVERSE_V2 = [
{ code: "009540", name: "HD한국조선해양", weight: 0.20 },
{ code: "494670", name: "TIGER 조선TOP10", weight: 0.15, isEtf: true },
]},
{ sector: "건설/EPC", proxyTicker: "028050", proxyName: "삼성E&A", proxyType: "대표주", baseTicker: "069500", constituents: [
{ code: "028050", name: "삼성E&A", weight: 0.40 },
{ code: "000720", name: "현대건설", weight: 0.30 },
{ code: "006360", name: "GS건설", weight: 0.20 },
{ code: "047040", name: "대우건설", weight: 0.10 },
{ sector: "건설", proxyTicker: "117700", proxyName: "KODEX 건설", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "000720", name: "현대건설", weight: 0.35 },
{ code: "006360", name: "GS건설", weight: 0.25 },
{ code: "047040", name: "대우건설", weight: 0.20 },
{ code: "294870", name: "HDC현대산업개발", weight: 0.20 },
]},
{ sector: "플랜트/EPC", proxyTicker: "454320", proxyName: "HANARO CAPEX설비투자iSelect", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "028050", name: "삼성E&A", weight: 0.35 },
{ code: "010120", name: "LS ELECTRIC", weight: 0.20 },
{ code: "267260", name: "HD현대일렉트릭", weight: 0.20 },
{ code: "298040", name: "효성중공업", weight: 0.15 },
{ code: "099440", name: "두산에너빌리티", weight: 0.10 },
]},
{ sector: "자동차", proxyTicker: "091180", proxyName: "TIGER 자동차", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "005380", name: "현대차", weight: 0.45 },
{ code: "000270", name: "기아", weight: 0.40 },
{ code: "012330", name: "현대모비스", weight: 0.15 },
]},
{ sector: "금융/은행", proxyTicker: "091170", proxyName: "KODEX 은행", proxyType: "ETF", baseTicker: "069500", constituents: [
{ sector: "은행", proxyTicker: "091170", proxyName: "KODEX 은행", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "105560", name: "KB금융", weight: 0.30 },
{ code: "055550", name: "신한지주", weight: 0.30 },
{ code: "086790", name: "하나금융지주", weight: 0.20 },
{ code: "316140", name: "우리금융지주", weight: 0.10 },
{ code: "003540", name: "대신증권", weight: 0.10 },
{ code: "024110", name: "기업은행", weight: 0.10 },
]},
{ sector: "증권", proxyTicker: "0111J0", proxyName: "HANARO 증권고배당TOP3플러스", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "071050", name: "한국금융지주", weight: 0.2135 },
{ code: "006800", name: "미래에셋증권", weight: 0.1934 },
{ code: "005940", name: "NH투자증권", weight: 0.1911 },
{ code: "016360", name: "삼성증권", weight: 0.1434 },
{ code: "039490", name: "키움증권", weight: 0.1373 },
]},
{ sector: "지주회사", proxyTicker: "307520", proxyName: "TIGER 지주회사", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "180640", name: "한진칼", weight: 0.1535 },
{ code: "267250", name: "HD현대", weight: 0.0943 },
{ code: "034730", name: "SK", weight: 0.0884 },
{ code: "000150", name: "두산", weight: 0.0878 },
{ code: "005490", name: "POSCO홀딩스", weight: 0.0763 },
{ code: "003550", name: "LG", weight: 0.0752 },
{ code: "006260", name: "LS", weight: 0.0705 },
{ code: "078930", name: "GS", weight: 0.0498 },
{ code: "001040", name: "CJ", weight: 0.0477 },
{ code: "010060", name: "OCI홀딩스", weight: 0.0240 },
]},
{ sector: "2차전지", proxyTicker: "305720", proxyName: "KODEX 2차전지산업", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "373220", name: "LG에너지솔루션", weight: 0.40 },
@@ -635,12 +668,29 @@ const DEFAULT_SECTOR_UNIVERSE_V2 = [
{ code: "128940", name: "한미약품", weight: 0.15 },
{ code: "000100", name: "유한양행", weight: 0.10 },
]},
{ sector: "원전", proxyTicker: "099440", proxyName: "두산에너빌리티", proxyType: "대표주", baseTicker: "069500", constituents: [
{ sector: "원전", proxyTicker: "434730", proxyName: "HANARO 원자력iSelect", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "099440", name: "두산에너빌리티", weight: 0.45 },
{ code: "023450", name: "한전기술", weight: 0.25 },
{ code: "015760", name: "한국전력", weight: 0.20 },
{ code: "071320", name: "지역난방공사", weight: 0.10 },
]},
{ sector: "로보틱스", proxyTicker: "0190C0", proxyName: "RISE 현대차고정피지컬AI", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "005380", name: "현대차", weight: 0.2402 },
{ code: "012330", name: "현대모비스", weight: 0.1588 },
{ code: "011070", name: "LG이노텍", weight: 0.1450 },
{ code: "000270", name: "기아", weight: 0.1234 },
{ code: "307950", name: "현대오토에버", weight: 0.0899 },
{ code: "277810", name: "레인보우로보틱스", weight: 0.0673 },
{ code: "064400", name: "LG씨엔에스", weight: 0.0519 },
{ code: "454910", name: "두산로보틱스", weight: 0.0367 },
{ code: "108490", name: "로보티즈", weight: 0.0240 },
{ code: "058610", name: "에스피지", weight: 0.0173 },
{ code: "010620", name: "현대미포", weight: 0.0135 },
{ code: "009540", name: "HD한국조선해양", weight: 0.0135 },
{ code: "011210", name: "현대위아", weight: 0.0109 },
{ code: "121600", name: "나노신소재", weight: 0.0040 },
{ code: "028050", name: "삼성E&A", weight: 0.0034 },
]},
{ sector: "소비재", proxyTicker: "139220", proxyName: "TIGER 생활소비재", proxyType: "ETF", baseTicker: "069500", constituents: [
{ code: "028260", name: "삼성물산", weight: 0.35 },
{ code: "097950", name: "CJ제일제당", weight: 0.25 },
@@ -663,6 +713,7 @@ function normalizeSectorName_(sector) {
if (s === "바이오/헬스케어") return "바이오";
if (s === "원전/에너지") return "원전";
if (s === "소비재/유통") return "소비재";
if (s === "건설/EPC") return "플랜트/EPC";
return s;
}
@@ -679,17 +730,52 @@ function readSectorUniverse_() {
const sheet = ss.getSheetByName("sector_universe");
if (!sheet) {
writeDefaultSectorUniverseSheet_();
return DEFAULT_SECTOR_UNIVERSE_V2;
return DEFAULT_SECTOR_UNIVERSE_V2.map(sector => ({
...sector,
source: sector.source || "DEFAULT_TEMPLATE",
sourceUrl: sector.sourceUrl || "",
sourceAsOf: sector.sourceAsOf || "",
constituents: sector.constituents.map(c => ({
...c,
source: c.source || sector.source || "DEFAULT_TEMPLATE",
sourceUrl: c.sourceUrl || sector.sourceUrl || "",
sourceAsOf: c.sourceAsOf || sector.sourceAsOf || "",
})),
}));
}
const data = sheet.getDataRange().getValues();
if (data.length < 3) {
writeDefaultSectorUniverseSheet_();
return DEFAULT_SECTOR_UNIVERSE_V2;
return DEFAULT_SECTOR_UNIVERSE_V2.map(sector => ({
...sector,
source: sector.source || "DEFAULT_TEMPLATE",
sourceUrl: sector.sourceUrl || "",
sourceAsOf: sector.sourceAsOf || "",
constituents: sector.constituents.map(c => ({
...c,
source: c.source || sector.source || "DEFAULT_TEMPLATE",
sourceUrl: c.sourceUrl || sector.sourceUrl || "",
sourceAsOf: c.sourceAsOf || sector.sourceAsOf || "",
})),
}));
}
const hdr = data[1].map(h => String(h).trim());
const idx = name => hdr.indexOf(name);
const required = ["Sector","Proxy_Ticker","Constituent_Code","Weight"];
if (required.some(h => idx(h) < 0)) return DEFAULT_SECTOR_UNIVERSE_V2;
if (required.some(h => idx(h) < 0)) {
return DEFAULT_SECTOR_UNIVERSE_V2.map(sector => ({
...sector,
source: sector.source || "DEFAULT_TEMPLATE",
sourceUrl: sector.sourceUrl || "",
sourceAsOf: sector.sourceAsOf || "",
constituents: sector.constituents.map(c => ({
...c,
source: c.source || sector.source || "DEFAULT_TEMPLATE",
sourceUrl: c.sourceUrl || sector.sourceUrl || "",
sourceAsOf: c.sourceAsOf || sector.sourceAsOf || "",
})),
}));
}
const map = {};
for (let i = 2; i < data.length; i++) {
@@ -706,6 +792,9 @@ function readSectorUniverse_() {
proxyName: idx("Proxy_Name") >= 0 ? String(data[i][idx("Proxy_Name")] ?? "").trim() : "",
proxyType: idx("Proxy_Type") >= 0 ? String(data[i][idx("Proxy_Type")] ?? "").trim() : "",
baseTicker: idx("Base_Ticker") >= 0 ? normalizeTickerCode(data[i][idx("Base_Ticker")]) : "069500",
source: idx("Source") >= 0 ? String(data[i][idx("Source")] ?? "").trim() : "SHEET_INPUT",
sourceUrl: idx("Source_URL") >= 0 ? String(data[i][idx("Source_URL")] ?? "").trim() : "",
sourceAsOf: idx("Source_AsOf") >= 0 ? String(data[i][idx("Source_AsOf")] ?? "").trim() : "",
constituents: [],
};
}
@@ -714,16 +803,59 @@ function readSectorUniverse_() {
name: idx("Constituent_Name") >= 0 ? String(data[i][idx("Constituent_Name")] ?? "").trim() : "",
weight,
isEtf: idx("Is_ETF") >= 0 ? boolFromSheet_(data[i][idx("Is_ETF")], false) : false,
source: idx("Source") >= 0 ? String(data[i][idx("Source")] ?? "").trim() : "SHEET_INPUT",
transportMode: idx("Transport_Mode") >= 0 ? String(data[i][idx("Transport_Mode")] ?? "").trim() : "",
sourceUrl: idx("Source_URL") >= 0 ? String(data[i][idx("Source_URL")] ?? "").trim() : "",
sourceAsOf: idx("Source_AsOf") >= 0 ? String(data[i][idx("Source_AsOf")] ?? "").trim() : "",
});
}
const sectors = Object.values(map).filter(s => s.proxyTicker && s.constituents.length > 0);
return sectors.length ? sectors : DEFAULT_SECTOR_UNIVERSE_V2;
const sectorSet = new Set(sectors.map(s => s.sector));
for (const fallback of DEFAULT_SECTOR_UNIVERSE_V2) {
if (!fallback || !fallback.sector || sectorSet.has(fallback.sector)) continue;
sectors.push({
sector: fallback.sector,
proxyTicker: fallback.proxyTicker,
proxyName: fallback.proxyName,
proxyType: fallback.proxyType,
baseTicker: fallback.baseTicker || "069500",
source: fallback.source || "DEFAULT_TEMPLATE",
transportMode: fallback.transportMode || ((fallback.source || "DEFAULT_TEMPLATE") === "NAVER_ETF_PAGE" || (fallback.source || "DEFAULT_TEMPLATE") === "REPRESENTATIVE_STOCK_PROXY" ? "HTML_SERVER_RENDERED" : "MANUAL_OR_TEMPLATE"),
sourceUrl: fallback.sourceUrl || "",
sourceAsOf: fallback.sourceAsOf || "",
constituents: fallback.constituents.map(c => ({
code: c.code,
name: c.name || "",
weight: c.weight,
isEtf: Boolean(c.isEtf),
source: c.source || fallback.source || "DEFAULT_TEMPLATE",
transportMode: c.transportMode || ((c.source || fallback.source || "DEFAULT_TEMPLATE") === "NAVER_ETF_PAGE" || (c.source || fallback.source || "DEFAULT_TEMPLATE") === "REPRESENTATIVE_STOCK_PROXY" ? "HTML_SERVER_RENDERED" : "MANUAL_OR_TEMPLATE"),
sourceUrl: c.sourceUrl || fallback.sourceUrl || "",
sourceAsOf: c.sourceAsOf || fallback.sourceAsOf || "",
})),
});
}
return sectors.length ? sectors : DEFAULT_SECTOR_UNIVERSE_V2.map(sector => ({
...sector,
source: sector.source || "DEFAULT_TEMPLATE",
transportMode: sector.transportMode || ((sector.source || "DEFAULT_TEMPLATE") === "NAVER_ETF_PAGE" || (sector.source || "DEFAULT_TEMPLATE") === "REPRESENTATIVE_STOCK_PROXY" ? "HTML_SERVER_RENDERED" : "MANUAL_OR_TEMPLATE"),
sourceUrl: sector.sourceUrl || "",
sourceAsOf: sector.sourceAsOf || "",
constituents: sector.constituents.map(c => ({
...c,
source: c.source || sector.source || "DEFAULT_TEMPLATE",
transportMode: c.transportMode || ((c.source || sector.source || "DEFAULT_TEMPLATE") === "NAVER_ETF_PAGE" || (c.source || sector.source || "DEFAULT_TEMPLATE") === "REPRESENTATIVE_STOCK_PROXY" ? "HTML_SERVER_RENDERED" : "MANUAL_OR_TEMPLATE"),
sourceUrl: c.sourceUrl || sector.sourceUrl || "",
sourceAsOf: c.sourceAsOf || sector.sourceAsOf || "",
})),
}));
}
function writeDefaultSectorUniverseSheet_() {
const headers = [
"Sector","Proxy_Ticker","Proxy_Name","Proxy_Type","Base_Ticker",
"Constituent_Code","Constituent_Name","Weight","Is_ETF","Enabled","Effective_Date","Source"
"Constituent_Code","Constituent_Name","Weight","Is_ETF","Enabled","Effective_Date","Source","Transport_Mode",
"Source_URL","Source_AsOf"
];
const today = Utilities.formatDate(new Date(), "Asia/Seoul", "yyyy-MM-dd");
const rows = [];
@@ -741,7 +873,10 @@ function writeDefaultSectorUniverseSheet_() {
c.isEtf ? "Y" : "N",
"Y",
today,
"sector_universe(DEFAULT_SECTOR_UNIVERSE_V2)",
sector.source || c.source || "DEFAULT_TEMPLATE",
sector.transportMode || c.transportMode || (((sector.source || c.source || "DEFAULT_TEMPLATE") === "NAVER_ETF_PAGE" || (sector.source || c.source || "DEFAULT_TEMPLATE") === "REPRESENTATIVE_STOCK_PROXY") ? "HTML_SERVER_RENDERED" : "MANUAL_OR_TEMPLATE"),
sector.sourceUrl || c.sourceUrl || "",
sector.sourceAsOf || c.sourceAsOf || "",
]);
}
}
@@ -762,6 +897,228 @@ function sectorUseMode_(quality) {
return "INVALID";
}
function parseDateOnly_(value) {
const text = String(value ?? "").trim();
if (!text) return null;
const norm = text.replace(/\./g, "-").slice(0, 10);
if (!/^\d{4}-\d{2}-\d{2}$/.test(norm)) return null;
const parsed = new Date(norm + "T00:00:00+09:00");
return Number.isNaN(parsed.getTime()) ? null : parsed;
}
function calcSectorUniverseRefreshAudit_(universe) {
const today = new Date();
const rows = [];
const sourceKindCounts = { NAVER_ETF_PAGE: 0, NAVER_ETF_PAGE_FAIL_LAYOUT_CHANGED: 0, NAVER_ETF_PAGE_FAIL: 0, REPRESENTATIVE_STOCK_PROXY: 0, SHEET_INPUT: 0, DEFAULT_TEMPLATE: 0, OTHER: 0 };
const transportModeCounts = { HTML_SERVER_RENDERED: 0, MANUAL_OR_TEMPLATE: 0, LAYOUT_CHANGED: 0, UNKNOWN: 0 };
let currentCount = 0;
let dueCount = 0;
let overdueCount = 0;
let missingCount = 0;
let templateCount = 0;
let sheetInputCount = 0;
let naverSourceCount = 0;
let layoutChangedCount = 0;
let missingSourceUrlCount = 0;
let staleSectorCount = 0;
let oldestSourceAsOf = null;
let newestSourceAsOf = null;
for (const sector of universe || []) {
const sectorRows = Array.isArray(sector?.constituents) ? sector.constituents : [];
const sourceKind = String(sector?.source || "SHEET_INPUT").trim() || "SHEET_INPUT";
if (Object.prototype.hasOwnProperty.call(sourceKindCounts, sourceKind)) {
sourceKindCounts[sourceKind] += 1;
} else {
sourceKindCounts.OTHER += 1;
}
const transportMode = String(sector?.transportMode || "").trim() ||
(sourceKind === "NAVER_ETF_PAGE" || sourceKind === "REPRESENTATIVE_STOCK_PROXY" ? "HTML_SERVER_RENDERED" :
sourceKind === "NAVER_ETF_PAGE_FAIL_LAYOUT_CHANGED" ? "LAYOUT_CHANGED" :
(sourceKind === "DEFAULT_TEMPLATE" || sourceKind === "SHEET_INPUT" ? "MANUAL_OR_TEMPLATE" : "UNKNOWN"));
if (Object.prototype.hasOwnProperty.call(transportModeCounts, transportMode)) {
transportModeCounts[transportMode] += 1;
} else {
transportModeCounts.UNKNOWN += 1;
}
const sourceUrl = String(sector?.sourceUrl || "").trim();
const sourceAsOf = String(sector?.sourceAsOf || "").trim();
const parsed = parseDateOnly_(sourceAsOf);
const ageDays = parsed ? Math.floor((today.getTime() - parsed.getTime()) / 86400000) : null;
if (parsed) {
oldestSourceAsOf = oldestSourceAsOf && oldestSourceAsOf < parsed ? oldestSourceAsOf : parsed;
newestSourceAsOf = newestSourceAsOf && newestSourceAsOf > parsed ? newestSourceAsOf : parsed;
}
let status = "INVALID";
const reasons = [];
if (sourceKind === "DEFAULT_TEMPLATE") {
status = "TEMPLATE";
templateCount += 1;
reasons.push("DEFAULT_TEMPLATE");
} else if (sourceKind === "REPRESENTATIVE_STOCK_PROXY") {
if (!sourceUrl) {
status = "MISSING";
missingCount += 1;
missingSourceUrlCount += 1;
reasons.push("Source_URL_MISSING");
} else if (ageDays === null) {
status = "MISSING";
missingCount += 1;
reasons.push("Source_AsOf_MISSING");
} else if (ageDays <= 31) {
status = "CURRENT";
currentCount += 1;
} else if (ageDays <= 45) {
status = "DUE";
dueCount += 1;
staleSectorCount += 1;
reasons.push(`AgeDays=${ageDays}`);
} else {
status = "OVERDUE";
overdueCount += 1;
staleSectorCount += 1;
reasons.push(`AgeDays=${ageDays}`);
}
} else if (sourceKind === "SHEET_INPUT") {
sheetInputCount += 1;
if (!sourceUrl) {
status = "MISSING";
missingCount += 1;
missingSourceUrlCount += 1;
reasons.push("Source_URL_MISSING");
} else if (ageDays === null) {
status = "MISSING";
missingCount += 1;
reasons.push("Source_AsOf_MISSING");
} else if (ageDays <= 31) {
status = "CURRENT";
currentCount += 1;
} else if (ageDays <= 45) {
status = "DUE";
dueCount += 1;
staleSectorCount += 1;
reasons.push(`AgeDays=${ageDays}`);
} else {
status = "OVERDUE";
overdueCount += 1;
staleSectorCount += 1;
reasons.push(`AgeDays=${ageDays}`);
}
} else if (sourceKind === "NAVER_ETF_PAGE") {
naverSourceCount += 1;
if (!sourceUrl) {
status = "MISSING";
missingCount += 1;
missingSourceUrlCount += 1;
reasons.push("Source_URL_MISSING");
} else if (ageDays === null) {
status = "MISSING";
missingCount += 1;
reasons.push("Source_AsOf_MISSING");
} else if (ageDays <= 31) {
status = "CURRENT";
currentCount += 1;
} else if (ageDays <= 45) {
status = "DUE";
dueCount += 1;
staleSectorCount += 1;
reasons.push(`AgeDays=${ageDays}`);
} else {
status = "OVERDUE";
overdueCount += 1;
staleSectorCount += 1;
reasons.push(`AgeDays=${ageDays}`);
}
} else if (sourceKind === "NAVER_ETF_PAGE_FAIL_LAYOUT_CHANGED") {
layoutChangedCount += 1;
status = "LAYOUT_CHANGED";
if (!sourceUrl) {
missingSourceUrlCount += 1;
reasons.push("Source_URL_MISSING");
}
if (ageDays === null) {
reasons.push("Source_AsOf_MISSING");
} else {
staleSectorCount += 1;
reasons.push(`AgeDays=${ageDays}`);
}
} else {
status = "INVALID";
reasons.push("SOURCE_KIND_UNKNOWN");
if (!sourceUrl) missingSourceUrlCount += 1;
}
if (!sourceUrl) reasons.push("Source_URL_MISSING");
if (ageDays !== null && ageDays < 0) reasons.push("FUTURE_DATE");
rows.push({
sector: sector.sector || "",
proxy_ticker: sector.proxyTicker || "",
proxy_name: sector.proxyName || "",
proxy_type: sector.proxyType || "",
source_kind: sourceKind,
transport_mode: transportMode,
source_url: sourceUrl,
source_asof: sourceAsOf,
age_days: ageDays === null ? "" : ageDays,
constituent_count: sectorRows.length,
stock_count: sectorRows.filter(c => !c.isEtf).length,
etf_count: sectorRows.filter(c => c.isEtf).length,
weight_sum: sectorRows.reduce((a, c) => a + (Number(c.weight) || 0), 0),
status: status,
refresh_reason: reasons.length ? reasons.join(";") : "OK",
});
}
rows.sort((a, b) => {
if (a.status === "CURRENT" && b.status !== "CURRENT") return -1;
if (a.status !== "CURRENT" && b.status === "CURRENT") return 1;
return String(a.sector || "").localeCompare(String(b.sector || ""));
});
return {
formula_id: "sector_universe_refresh_audit_v1",
gate: (templateCount > 0 || missingSourceUrlCount > 0 || overdueCount > 0 || staleSectorCount > 0) ? "FAIL" : (sheetInputCount > 0 ? "WARN" : "PASS"),
summary: {
sector_count: (universe || []).length,
current_count: currentCount,
due_count: dueCount,
overdue_count: overdueCount,
missing_count: missingCount,
template_count: templateCount,
sheet_input_count: sheetInputCount,
naver_source_count: naverSourceCount,
layout_changed_count: layoutChangedCount,
missing_source_url_count: missingSourceUrlCount,
stale_sector_count: staleSectorCount,
oldest_source_asof: oldestSourceAsOf ? Utilities.formatDate(oldestSourceAsOf, "Asia/Seoul", "yyyy-MM-dd") : "",
newest_source_asof: newestSourceAsOf ? Utilities.formatDate(newestSourceAsOf, "Asia/Seoul", "yyyy-MM-dd") : "",
source_kind_counts: sourceKindCounts,
transport_mode_counts: transportModeCounts,
ajax_mode: "NO",
transport_model: "HTML_SERVER_RENDERED",
},
rows: rows,
};
}
function writeSectorUniverseRefreshAuditSheet_(audit) {
if (!audit || typeof audit !== "object") return 0;
const headers = [
"sector", "proxy_ticker", "proxy_name", "proxy_type", "source_kind", "transport_mode",
"source_url", "source_asof", "age_days", "constituent_count",
"stock_count", "etf_count", "weight_sum", "status", "refresh_reason",
];
const rows = Array.isArray(audit.rows)
? audit.rows.map(function(r) {
return headers.map(function(h) { return r[h] ?? ""; });
})
: [];
writeToSheet("sector_universe_refresh_audit", headers, rows);
return rows.length;
}
function scoreSmartMoneyNorm_(v) {
if (!Number.isFinite(v)) return 0;
if (v >= 0.15) return 25;
@@ -955,7 +1312,7 @@ function runSectorFlowV3() {
const etfRawMap = buildEtfRawMap_(buildEtfRawRows_(universe));
const today = Utilities.formatDate(new Date(), "Asia/Seoul", "yyyy-MM-dd");
const headers = [
"Sector","Proxy_Ticker","Proxy_Name","Proxy_Type","Coverage_Weight",
"Sector","Proxy_Ticker","Proxy_Name","Proxy_Type","Universe_Source","Transport_Mode","Coverage_Weight",
"Sector_Ret5D","Sector_Ret20D","Sector_RS_20D",
"SmartMoney_5D_KRW","SmartMoney_20D_KRW","Sector_AvgTradeValue_20D_KRW","SmartMoney_5D_Norm",
"Flow_Breadth_5D","Flow_Rows_Min","Stale_Count",
@@ -1031,6 +1388,9 @@ function runSectorFlowV3() {
const etfNavRisk = sector.proxyType === "ETF" ? (etfRaw?.navRisk ?? "NAV_DATA_MISSING") : "NOT_ETF";
const etfLiquidityStatus = sector.proxyType === "ETF" ? (etfRaw?.liquidityStatus ?? "WARN") : "NOT_ETF";
const etfExecutionUse = sector.proxyType === "ETF" ? (etfRaw?.executionUse ?? "WATCH_ONLY") : "NOT_ETF";
const transportMode = sector.source === "NAVER_ETF_PAGE" ? "HTML_SERVER_RENDERED"
: (sector.source === "REPRESENTATIVE_STOCK_PROXY" ? "HTML_SERVER_RENDERED"
: (sector.source === "DEFAULT_TEMPLATE" ? "MANUAL_OR_TEMPLATE" : "UNKNOWN"));
const quality = sectorDataQuality_(coverage, flowRowsMin, staleCount, proxy.ok, Number.isFinite(smart5Norm), weightSum);
const routeUse = sectorUseMode_(quality);
let score = calcSectorScoreV2_(sectorRet20D, sectorRs20D, smart5Norm, smart20Norm, breadth5, tradeValueRatio, sector.proxyType, etfLiquidityScore);
@@ -1047,6 +1407,7 @@ function runSectorFlowV3() {
if (staleCount > 0) reasons.push(`Stale_Count=${staleCount}`);
if (!proxy.ok) reasons.push("Proxy_Price_FAIL");
if (!Number.isFinite(smart5Norm)) reasons.push("SmartMoney_Norm_MISSING");
if ((sector.source || "DEFAULT_TEMPLATE") === "DEFAULT_TEMPLATE") reasons.push("Universe_Source=DEFAULT_TEMPLATE");
if (sector.proxyType === "ETF" && etfNavRisk === "NAV_DATA_MISSING") reasons.push("ETF_NAV_DATA_MISSING");
if (sector.proxyType === "ETF" && etfLiquidityStatus !== "OK") reasons.push(`ETF_Liquidity=${etfLiquidityStatus}`);
if (sector.proxyType === "ETF" && etfExecutionUse !== "TRADE_OK") reasons.push(`ETF_Execution=${etfExecutionUse}`);
@@ -1056,6 +1417,8 @@ function runSectorFlowV3() {
proxyTicker: sector.proxyTicker,
proxyName: sector.proxyName,
proxyType: sector.proxyType || "대표주",
universeSource: sector.source || "DEFAULT_TEMPLATE",
transportMode: transportMode,
coverage,
sectorRet5D,
sectorRet20D,
@@ -1106,7 +1469,7 @@ function appendSectorFlowHistoryV2_(rows) {
const headers = [
"Snapshot_Date","Sector","Sector_Score","Sector_Rank","SmartMoney_5D_KRW","SmartMoney_20D_KRW",
"Flow_Breadth_5D","Alert_Level","Data_Quality","Decision_Use","ETF_Liquidity_Status","ETF_Execution_Use","Reason","Saved_At"
"Flow_Breadth_5D","Alert_Level","Data_Quality","Decision_Use","ETF_Liquidity_Status","ETF_Execution_Use","Transport_Mode","Reason","Saved_At"
];
const ss = getSpreadsheet_();
let sheet = ss.getSheetByName("sector_flow_history");
@@ -1119,22 +1482,25 @@ function appendSectorFlowHistoryV2_(rows) {
const hdr = data[1] ?? headers;
const dateIdx = hdr.indexOf("Snapshot_Date");
const sectorIdx = hdr.indexOf("Sector");
const existing = [];
const normalizeRow_ = (row) => {
const outRow = Array.isArray(row) ? row.slice(0, headers.length) : [];
while (outRow.length < headers.length) outRow.push("");
return outRow;
};
const byKey = {};
for (let i = 2; i < data.length; i++) {
const row = data[i];
const d = normalizeSheetDateString_(row[dateIdx]);
const s = String(row[sectorIdx] ?? "").trim();
if (!d || !s) continue;
byKey[`${d}|${s}`] = row;
existing.push(row);
byKey[`${d}|${s}`] = normalizeRow_(row);
}
const savedAt = Utilities.formatDate(new Date(), "Asia/Seoul", "yyyy-MM-dd HH:mm:ss");
for (const r of rows) {
byKey[`${r.asOfDate}|${r.sector}`] = [
byKey[`${r.asOfDate}|${r.sector}`] = normalizeRow_([
r.asOfDate, r.sector, r.score, r.rank, Math.round(r.smart5), Math.round(r.smart20),
roundNum(r.breadth5, 4), r.alert, r.quality, r.routeUse, r.etfLiquidityStatus, r.etfExecutionUse, r.reason, savedAt
];
roundNum(r.breadth5, 4), r.alert, r.quality, r.routeUse, r.etfLiquidityStatus, r.etfExecutionUse, r.transportMode || "", r.reason, savedAt
]);
}
const out = Object.values(byKey).sort((a, b) => {
const da = String(a[0]), db = String(b[0]);
@@ -1144,7 +1510,7 @@ function appendSectorFlowHistoryV2_(rows) {
sheet.clearContents();
sheet.getRange(1, 1).setValue(`updated: ${savedAt} KST`);
sheet.getRange(2, 1, 1, headers.length).setValues([headers]);
if (out.length) sheet.getRange(3, 1, out.length, headers.length).setValues(out);
if (out.length) sheet.getRange(3, 1, out.length, headers.length).setValues(out.map(normalizeRow_));
}
function normalizeSheetDateString_(value) {
@@ -1235,7 +1601,7 @@ function readW2LegacySectorFlow_() {
function writeLegacySectorFlowFromStage2_(stage2Rows) {
const headers = [
"Sector","Proxy_Ticker","Proxy_Name","Proxy_Type","Coverage_Weight",
"Sector","Proxy_Ticker","Proxy_Name","Proxy_Type","Universe_Source","Coverage_Weight",
"Sector_Ret5D","Sector_Ret10D","Sector_Ret20D","Sector_RS_20D",
"SmartMoney_5D_KRW","SmartMoney_20D_KRW","Sector_AvgTradeValue_20D_KRW",
"SmartMoney_5D_Norm","SmartMoney_20D_Norm","Flow_Breadth_5D","Flow_Rows_Min","Stale_Count",
@@ -1277,7 +1643,7 @@ function writeLegacySectorFlowFromStage2_(stage2Rows) {
const frg20Alias = Number.isFinite(r.smart20) ? r.smart20 / 2 : "";
const inst20Alias = Number.isFinite(r.smart20) ? r.smart20 / 2 : "";
return [
r.sector, r.proxyTicker, r.proxyName, r.proxyType, r.coverage,
r.sector, r.proxyTicker, r.proxyName, r.proxyType, r.universeSource, r.coverage,
r.sectorRet5D, r.proxyRet10D, r.sectorRet20D, r.sectorRs20D,
r.smart5, r.smart20, r.avgTv20Krw,
r.smart5Norm, r.smart20Norm, r.breadth5, r.flowRowsMin, r.staleCount,
@@ -1798,6 +2164,15 @@ function run_all() {
}
},
{ name: "runSectorFlow", fn: runSectorFlow },
{
name: "runSectorUniverseRefreshAudit",
fn: function() {
const universe = readSectorUniverse_();
const audit = calcSectorUniverseRefreshAudit_(universe);
writeSectorUniverseRefreshAuditSheet_(audit);
Logger.log("[RUN_ALL] sector_universe_refresh_audit gate=" + audit.gate + " rows=" + (audit.rows || []).length);
}
},
{ name: "runDataFeed", fn: runDataFeed },
{ name: "runCoreSatelliteFlow_", fn: runCoreSatelliteFlow_ },
{ name: "runEventRisk", fn: runEventRisk },
@@ -1806,10 +1806,16 @@ function getCoreSatelliteUniverse() {
// 자동차
{ code:"005380", name:"현대차", sector:"자동차" },
{ code:"000270", name:"기아", sector:"자동차" },
// 밸류업/금융
{ code:"105560", name:"KB금융", sector:"금융/은행" },
{ code:"055550", name:"신한지주", sector:"금융/은행" },
{ code:"024110", name:"기업은행", sector:"금융/은행" },
// 은행 / 증권 / 지주회사
{ code:"105560", name:"KB금융", sector:"은행" },
{ code:"055550", name:"신한지주", sector:"은행" },
{ code:"024110", name:"기업은행", sector:"은행" },
{ code:"071050", name:"한국금융지주", sector:"증권" },
{ code:"006800", name:"미래에셋증권", sector:"증권" },
{ code:"005940", name:"NH투자증권", sector:"증권" },
{ code:"180640", name:"한진칼", sector:"지주회사" },
{ code:"267250", name:"HD현대", sector:"지주회사" },
{ code:"034730", name:"SK", sector:"지주회사" },
// 바이오
{ code:"207940", name:"삼성바이오로직스",sector:"바이오" },
{ code:"068270", name:"셀트리온", sector:"바이오" },
@@ -1820,7 +1826,7 @@ function getCoreSatelliteUniverse() {
{ code:"006400", name:"삼성SDI", sector:"2차전지" },
{ code:"003670", name:"포스코퓨처엠",sector:"2차전지" },
// 지주/기타
{ code:"028260", name:"삼성물산", sector:"지주" }
{ code:"028260", name:"삼성물산", sector:"지주회사" }
];
list = defaults.map(t => ({ ...t, addedDate: todayStr }));
+29 -6
View File
@@ -11,7 +11,7 @@
*
* 실행 시간 전략 (GAS 6분 제한):
* - data_feed: 보유 10종목만 → ~30초
* - sector_flow: 11섹터×3종목 → ~3분
* - sector_flow: 분리된 섹터×3종목 → ~3분
* - macro/unified: 단순 집계 → ~30초
* - core_satellite(100종목): 별도 트리거, 청크 분할 실행
*
@@ -27,13 +27,24 @@ const TICKERS_BASE = [
{ code: "000660", name: "SK하이닉스" },
{ code: "000270", name: "기아" },
{ code: "091160", name: "KODEX 반도체" },
{ code: "463250", name: "TIGER K방산&우주" },
{ code: "064350", name: "현대로템" },
{ code: "012450", name: "한화에어로스페이스" },
{ code: "117700", name: "KODEX 건설" },
{ code: "028050", name: "삼성E&A" },
{ code: "454320", name: "HANARO CAPEX설비투자iSelect" },
{ code: "010120", name: "LS ELECTRIC" },
{ code: "0117V0", name: "TIGER AI전력기기" },
{ code: "491820", name: "HANARO 전력설비투자" },
{ code: "494670", name: "TIGER 조선TOP10" },
{ code: "471990", name: "KODEX AI반도체핵심장비" },
{ code: "434730", name: "HANARO 원자력iSelect" },
{ code: "0111J0", name: "HANARO 증권고배당TOP3플러스" },
{ code: "307520", name: "TIGER 지주회사" },
{ code: "0190C0", name: "RISE 현대차고정피지컬AI" },
{ code: "011070", name: "LG이노텍" },
{ code: "010620", name: "현대미포" },
{ code: "121600", name: "나노신소재" },
];
// TICKERS 우선순위: TICKERS_BASE → account_snapshot 보유종목 → watch_tickers_override 수동 추가.
@@ -132,9 +143,12 @@ const TICKER_SECTOR_MAP = {
"010120": "AI전력", "267260": "AI전력", "006260": "AI전력",
"012450": "방산", "079550": "방산", "047810": "방산", "064350": "방산",
"329180": "조선", "042660": "조선", "009540": "조선",
"028050": "건설/EPC","000720": "건설/EPC","006360": "건설/EPC",
"028050": "플랜트/EPC","000720": "건설","006360": "건설",
"005380": "자동차", "000270": "자동차", "012330": "자동차",
"105560": "금융/은행","055550": "금융/은행","086790": "금융/은행",
"105560": "은행","055550": "은행","086790": "은행","316140": "은행","024110": "은행",
"071050": "증권","006800": "증권","005940": "증권","016360": "증권","039490": "증권",
"180640": "지주회사","267250": "지주회사","034730": "지주회사","000150": "지주회사","005490": "지주회사",
"003550": "지주회사","006260": "지주회사","078930": "지주회사","001040": "지주회사","010060": "지주회사",
"373220": "2차전지","006400": "2차전지","051910": "2차전지",
"207940": "바이오", "068270": "바이오", "128940": "바이오",
"099440": "원전", "023450": "원전", "015760": "원전",
@@ -142,8 +156,12 @@ const TICKER_SECTOR_MAP = {
// ETF — 해당 섹터로 매핑
"091160": "반도체", "0117V0": "AI전력", "494670": "조선",
"471990": "반도체", // KODEX AI반도체핵심장비 (누락 추가)
"266410": "바이오", "091180": "자동차", "091170": "금융/은행",
"266410": "바이오", "091180": "자동차", "091170": "은행",
"0111J0": "증권", "307520": "지주회사",
"305720": "2차전지","139220": "소비재",
"463250": "방산", "434730": "원전", "454320": "플랜트/EPC",
"491820": "전력설비", "117700": "건설", "0190C0": "로보틱스",
"011070": "로보틱스", "010620": "로보틱스", "121600": "로보틱스",
};
// 섹터 → Tier 매핑 (C5 daily_leader_scan 점수 정밀화)
@@ -151,14 +169,19 @@ const TICKER_SECTOR_MAP = {
const SECTOR_TIER_MAP = {
"반도체": "Tier_1",
"AI전력": "Tier_1",
"전력설비": "Tier_1",
"방산": "Tier_1",
"조선": "Tier_1",
"자동차": "Tier_2",
"2차전지": "Tier_2",
"바이오": "Tier_2",
"원전": "Tier_2",
"건설/EPC": "Tier_3",
"금융/은행":"Tier_3",
"건설": "Tier_3",
"플랜트/EPC": "Tier_3",
"로보틱스": "Tier_2",
"은행":"Tier_3",
"증권":"Tier_3",
"지주회사":"Tier_3",
"소비재": "Tier_3",
};
+96
View File
@@ -174,6 +174,28 @@ def normalize_legacy_source_markers(sheet: str, records: list[dict[str, Any]]) -
source = record.get("Source")
if isinstance(source, str) and "sector_targets.json" in source:
record["Source"] = source.replace("sector_targets.json", "sector_universe")
source_url = str(record.get("Source_URL") or "").strip()
transport_mode = str(record.get("Transport_Mode") or "").strip()
if record.get("Source") in (None, "", "DEFAULT_TEMPLATE"):
if "finance.naver.com/item/main.naver?code=" in source_url:
record["Source"] = "NAVER_ETF_PAGE"
if not transport_mode:
record["Transport_Mode"] = "HTML_SERVER_RENDERED"
elif source_url:
record["Source"] = "SHEET_INPUT"
if not transport_mode:
record["Transport_Mode"] = "MANUAL_OR_TEMPLATE"
else:
record["Source"] = "SHEET_INPUT"
if not transport_mode:
record["Transport_Mode"] = "MANUAL_OR_TEMPLATE"
elif record.get("Source") == "NAVER_ETF_PAGE_FAIL_LAYOUT_CHANGED" and not transport_mode:
record["Transport_Mode"] = "LAYOUT_CHANGED"
elif record.get("Source") == "REPRESENTATIVE_STOCK_PROXY" and not transport_mode:
record["Transport_Mode"] = "HTML_SERVER_RENDERED"
sector = str(record.get("Sector") or "").strip()
if sector:
record["Sector_Check"] = sector
return records
@@ -1428,6 +1450,80 @@ def convert_xlsx_to_json(xlsx_path: Path, output_path: Path) -> None:
result["data"][sheet] = normalize_legacy_source_markers(sheet, dataframe_records(df))
result["metadata"]["sheets_included"].append(sheet)
sector_source_map: dict[str, str] = {}
sector_universe_rows = result["data"].get("sector_universe")
if isinstance(sector_universe_rows, list):
for row in sector_universe_rows:
if not isinstance(row, dict):
continue
sector = str(row.get("Sector") or "").strip()
if not sector:
continue
source = str(row.get("Source") or "").strip() or "SHEET_INPUT"
sector_source_map.setdefault(sector, source)
sector_flow_rows = result["data"].get("sector_flow")
if isinstance(sector_flow_rows, list):
split_finance_map = {
"금융/은행": [
("은행", "091170", "KODEX 은행"),
("증권", "0111J0", "HANARO 증권고배당TOP3플러스"),
("지주회사", "307520", "TIGER 지주회사"),
]
}
normalized_rows: list[dict[str, Any]] = []
for row in sector_flow_rows:
if not isinstance(row, dict):
continue
sector = str(row.get("Sector") or "").strip()
if not sector:
continue
source = str(row.get("Universe_Source") or "").strip() or sector_source_map.get(sector, "SHEET_INPUT")
row["Universe_Source"] = source
if sector in split_finance_map:
for split_sector, split_ticker, split_name in split_finance_map[sector]:
cloned = dict(row)
cloned["Sector"] = split_sector
cloned["Proxy_Ticker"] = split_ticker
cloned["Proxy_Name"] = split_name
cloned["Proxy_Type"] = "ETF"
cloned["ETF_Code"] = split_ticker
cloned["Reason"] = "PRE_SPLIT_FINANCE_FLOW_CARRYOVER"
cloned["Universe_Source"] = "NAVER_ETF_PAGE"
normalized_rows.append(cloned)
else:
normalized_rows.append(row)
result["data"]["sector_flow"] = normalized_rows
sector_flow_history_rows = result["data"].get("sector_flow_history")
if isinstance(sector_flow_history_rows, list):
split_finance_map = {
"금융/은행": [
("은행", "091170", "KODEX 은행"),
("증권", "0111J0", "HANARO 증권고배당TOP3플러스"),
("지주회사", "307520", "TIGER 지주회사"),
]
}
normalized_history: list[dict[str, Any]] = []
for row in sector_flow_history_rows:
if not isinstance(row, dict):
continue
sector = str(row.get("Sector") or "").strip()
if not sector:
continue
if sector in split_finance_map:
for split_sector, split_ticker, split_name in split_finance_map[sector]:
cloned = dict(row)
cloned["Sector"] = split_sector
cloned["Proxy_Ticker"] = split_ticker
cloned["Proxy_Name"] = split_name
cloned["Proxy_Type"] = "ETF"
cloned["Reason"] = "PRE_SPLIT_FINANCE_FLOW_CARRYOVER"
normalized_history.append(cloned)
else:
normalized_history.append(row)
result["data"]["sector_flow_history"] = normalized_history
# harness_context 시트가 없으면 메타에 경고 기록
if "_harness_context" not in result["data"]:
result["metadata"]["harness_context_missing"] = (
@@ -462,7 +462,8 @@ _TICKER_SECTOR_MAP = {
"010120": "AI전력", "267260": "AI전력",
"012450": "방산", "064350": "방산",
"329180": "조선", "494670": "조선",
"028050": "건설/EPC",
"117700": "건설", "028050": "플랜트/EPC", "454320": "플랜트/EPC",
"0190C0": "로보틱스",
"005380": "자동차", "000270": "자동차",
"091160": "반도체", "0117V0": "AI전력",
}
+296
View File
@@ -0,0 +1,296 @@
from __future__ import annotations
import datetime as dt
from typing import Any
DEFAULT_MAX_AGE_DAYS = 31
def _txt(value: Any, default: str = "") -> str:
if value is None:
return default
if isinstance(value, str):
return value.strip() or default
return str(value).strip() or default
def _as_float(value: Any) -> float | None:
try:
if value in (None, ""):
return None
if isinstance(value, str):
text = value.strip().replace("%", "").replace(",", "")
if not text:
return None
return float(text)
return float(value)
except Exception:
return None
def _parse_date(value: Any) -> dt.date | None:
if value in (None, ""):
return None
if isinstance(value, dt.date):
return value
text = _txt(value)
if not text:
return None
for fmt in ("%Y-%m-%d", "%Y.%m.%d", "%Y/%m/%d"):
try:
return dt.datetime.strptime(text[:10], fmt).date()
except Exception:
pass
try:
return dt.date.fromisoformat(text[:10])
except Exception:
return None
def _age_days(value: Any, today: dt.date | None = None) -> int | None:
parsed = _parse_date(value)
if parsed is None:
return None
today = today or dt.datetime.now(dt.timezone(dt.timedelta(hours=9))).date()
return (today - parsed).days
def _extract_sector_rows(payload: dict[str, Any] | None) -> list[dict[str, Any]]:
if not isinstance(payload, dict):
return []
inner = payload.get("data")
if isinstance(inner, dict) and isinstance(inner.get("sector_universe"), list):
return [r for r in inner["sector_universe"] if isinstance(r, dict)]
if isinstance(payload.get("sector_universe"), list):
return [r for r in payload["sector_universe"] if isinstance(r, dict)]
return []
def build_sector_universe_refresh_audit(payload: dict[str, Any] | None) -> dict[str, Any]:
rows = _extract_sector_rows(payload)
today = dt.datetime.now(dt.timezone(dt.timedelta(hours=9))).date()
grouped: dict[str, list[dict[str, Any]]] = {}
for row in rows:
sector = _txt(row.get("Sector"))
if not sector:
continue
grouped.setdefault(sector, []).append(row)
detail_rows: list[dict[str, Any]] = []
source_kind_counts = {
"NAVER_ETF_PAGE": 0,
"NAVER_ETF_PAGE_FAIL_LAYOUT_CHANGED": 0,
"NAVER_ETF_PAGE_FAIL": 0,
"REPRESENTATIVE_STOCK_PROXY": 0,
"SHEET_INPUT": 0,
"DEFAULT_TEMPLATE": 0,
"OTHER": 0,
}
transport_mode_counts = {
"HTML_SERVER_RENDERED": 0,
"MANUAL_OR_TEMPLATE": 0,
"LAYOUT_CHANGED": 0,
"UNKNOWN": 0,
}
state_counts = {"CURRENT": 0, "DUE": 0, "OVERDUE": 0, "MISSING": 0, "TEMPLATE": 0, "INVALID": 0}
stale_sector_count = 0
layout_changed_count = 0
missing_source_url_count = 0
sheet_input_count = 0
template_count = 0
newest_asof: dt.date | None = None
oldest_asof: dt.date | None = None
for sector, sector_rows in grouped.items():
source_values = {_txt(r.get("Source"), "SHEET_INPUT") or "SHEET_INPUT" for r in sector_rows}
if "NAVER_ETF_PAGE_FAIL_LAYOUT_CHANGED" in source_values:
source_kind = "NAVER_ETF_PAGE_FAIL_LAYOUT_CHANGED"
elif "NAVER_ETF_PAGE_FAIL" in source_values:
source_kind = "NAVER_ETF_PAGE_FAIL"
elif "NAVER_ETF_PAGE" in source_values:
source_kind = "NAVER_ETF_PAGE"
elif "REPRESENTATIVE_STOCK_PROXY" in source_values:
source_kind = "REPRESENTATIVE_STOCK_PROXY"
elif "DEFAULT_TEMPLATE" in source_values:
source_kind = "DEFAULT_TEMPLATE"
elif "SHEET_INPUT" in source_values:
source_kind = "SHEET_INPUT"
else:
source_kind = "OTHER"
source_kind_counts[source_kind if source_kind in source_kind_counts else "OTHER"] += 1
source_urls = [_txt(r.get("Source_URL")) for r in sector_rows if _txt(r.get("Source_URL"))]
source_url = source_urls[0] if source_urls else ""
asof_candidates = [_parse_date(r.get("Source_AsOf")) for r in sector_rows]
asof_dates = [d for d in asof_candidates if d is not None]
source_asof = max(asof_dates) if asof_dates else None
if source_asof is not None:
newest_asof = source_asof if newest_asof is None else max(newest_asof, source_asof)
oldest_asof = source_asof if oldest_asof is None else min(oldest_asof, source_asof)
age_days = _age_days(source_asof, today) if source_asof else None
constituent_count = len(sector_rows)
etf_count = sum(1 for r in sector_rows if str(r.get("Is_ETF") or "").strip().upper() in {"Y", "YES", "TRUE", "1"})
stock_count = constituent_count - etf_count
weight_sum = sum(_as_float(r.get("Weight")) or 0 for r in sector_rows)
status = "INVALID"
reason_parts: list[str] = []
transport_mode = "UNKNOWN"
if source_kind == "DEFAULT_TEMPLATE":
status = "TEMPLATE"
reason_parts.append("DEFAULT_TEMPLATE")
template_count += 1
transport_mode = "MANUAL_OR_TEMPLATE"
elif source_kind == "NAVER_ETF_PAGE_FAIL_LAYOUT_CHANGED":
status = "LAYOUT_CHANGED"
transport_mode = "LAYOUT_CHANGED"
reason_parts.append("LAYOUT_CHANGED")
layout_changed_count += 1
if not source_url:
missing_source_url_count += 1
reason_parts.append("Source_URL_MISSING")
if age_days is None:
reason_parts.append("Source_AsOf_MISSING")
else:
stale_sector_count += 1
reason_parts.append(f"AgeDays={age_days}")
elif source_kind == "NAVER_ETF_PAGE_FAIL":
status = "INVALID"
transport_mode = "UNKNOWN"
reason_parts.append("NAVER_ETF_PAGE_FAIL")
if not source_url:
missing_source_url_count += 1
elif source_kind == "REPRESENTATIVE_STOCK_PROXY":
transport_mode = "HTML_SERVER_RENDERED"
if not source_url:
status = "MISSING"
missing_source_url_count += 1
reason_parts.append("Source_URL_MISSING")
elif age_days is None:
status = "MISSING"
reason_parts.append("Source_AsOf_MISSING")
elif age_days <= DEFAULT_MAX_AGE_DAYS:
status = "CURRENT"
elif age_days <= 45:
status = "DUE"
stale_sector_count += 1
reason_parts.append(f"AgeDays={age_days}")
else:
status = "OVERDUE"
stale_sector_count += 1
reason_parts.append(f"AgeDays={age_days}")
elif source_kind == "SHEET_INPUT":
sheet_input_count += 1
transport_mode = "MANUAL_OR_TEMPLATE"
if not source_url:
status = "MISSING"
reason_parts.append("Source_URL_MISSING")
missing_source_url_count += 1
elif age_days is None:
status = "MISSING"
reason_parts.append("Source_AsOf_MISSING")
elif age_days <= DEFAULT_MAX_AGE_DAYS:
status = "CURRENT"
elif age_days <= 45:
status = "DUE"
stale_sector_count += 1
reason_parts.append(f"AgeDays={age_days}")
else:
status = "OVERDUE"
stale_sector_count += 1
reason_parts.append(f"AgeDays={age_days}")
elif source_kind == "NAVER_ETF_PAGE":
transport_mode = "HTML_SERVER_RENDERED"
if not source_url:
status = "MISSING"
reason_parts.append("Source_URL_MISSING")
missing_source_url_count += 1
elif age_days is None:
status = "MISSING"
reason_parts.append("Source_AsOf_MISSING")
elif age_days <= DEFAULT_MAX_AGE_DAYS:
status = "CURRENT"
elif age_days <= 45:
status = "DUE"
stale_sector_count += 1
reason_parts.append(f"AgeDays={age_days}")
else:
status = "OVERDUE"
stale_sector_count += 1
reason_parts.append(f"AgeDays={age_days}")
else:
if not source_url:
missing_source_url_count += 1
status = "INVALID"
reason_parts.append("SOURCE_KIND_UNKNOWN")
transport_mode = "UNKNOWN"
if source_kind == "NAVER_ETF_PAGE" and not source_url:
reason_parts.append("NAVER_URL_MISSING")
if not source_url:
reason_parts.append("Source_URL_MISSING")
if age_days is not None and age_days < 0:
reason_parts.append("FUTURE_DATE")
transport_mode_counts[transport_mode] = transport_mode_counts.get(transport_mode, 0) + 1
refresh_reason = ";".join(reason_parts) if reason_parts else "OK"
detail_rows.append({
"sector": sector,
"proxy_ticker": _txt(sector_rows[0].get("Proxy_Ticker")),
"proxy_name": _txt(sector_rows[0].get("Proxy_Name")),
"proxy_type": _txt(sector_rows[0].get("Proxy_Type")),
"source_kind": source_kind,
"transport_mode": transport_mode,
"source_url": source_url,
"source_asof": source_asof.isoformat() if source_asof else "",
"age_days": age_days if age_days is not None else "",
"constituent_count": constituent_count,
"stock_count": stock_count,
"etf_count": etf_count,
"weight_sum": round(weight_sum, 4),
"status": status,
"refresh_reason": refresh_reason,
})
detail_rows.sort(key=lambda r: (r.get("status") != "CURRENT", r.get("status"), r.get("sector")))
summary = {
"sector_count": len(grouped),
"current_count": sum(1 for r in detail_rows if r.get("status") == "CURRENT"),
"due_count": sum(1 for r in detail_rows if r.get("status") == "DUE"),
"overdue_count": sum(1 for r in detail_rows if r.get("status") == "OVERDUE"),
"missing_count": sum(1 for r in detail_rows if r.get("status") == "MISSING"),
"template_count": template_count,
"sheet_input_count": sheet_input_count,
"naver_source_count": sum(1 for r in detail_rows if r.get("source_kind") == "NAVER_ETF_PAGE"),
"missing_source_url_count": missing_source_url_count,
"stale_sector_count": stale_sector_count,
"layout_changed_count": layout_changed_count,
"oldest_source_asof": oldest_asof.isoformat() if oldest_asof else "",
"newest_source_asof": newest_asof.isoformat() if newest_asof else "",
"source_kind_counts": source_kind_counts,
"transport_mode_counts": transport_mode_counts,
"ajax_mode": "NO",
"transport_model": "HTML_SERVER_RENDERED",
}
gate = "PASS"
if template_count > 0 or missing_source_url_count > 0 or stale_sector_count > 0 or layout_changed_count > 0:
gate = "FAIL"
elif sheet_input_count > 0:
gate = "WARN"
return {
"formula_id": "sector_universe_refresh_audit_v1",
"gate": gate,
"max_age_days": DEFAULT_MAX_AGE_DAYS,
"summary": summary,
"rows": detail_rows,
"source": {
"sector_rows": len(rows),
"grouped_sectors": len(grouped),
},
}