Files
MoFin/hk_rate.py
知微 e33a236bc1 自成长系统:四层循环架构文档 + 三个代码改动 + 所有日间修复
内容:
- docs/SELF_GROWTH_SYSTEM.md (NEW) — 完整的 Sense→Respond→Adapt→Improve 架构文档
- docs/SYSTEM_ARCHITECTURE.md (UPDATED) — 总索引指向新文档,cron数从14更新为31
- hk_rate.py (NEW) — HKD汇率模块,缓存+上次有效汇率自动恢复
- price_monitor.py (MODIFIED) — 价格监控注入分支评估+情景切换检测
- strategy_lifecycle.py (MODIFIED) — 策略生命周期评估上下文
- strategy_tree.py (NEW) — 情景化多分支决策引擎

日间修复(2026-06-23):
- stale_push_wlin: cash硬编码146837→读portfolio.json
- stale_push_wlin: lot_cost汇率0.93→hkd_to_cny动态
- stale_push_wlin: HK每手默认500股→Tencent API实时f[60]
- stale_push_wlin: 重评异步→串行(先重评再出报告)
- hk_rate: FALLBACK=0.87硬编码→缓存上次有效汇率
- 新增 cron: 分支扫描每30分, 分支剪枝周六, 硬编码审计17:25
- hardcode_scanner.py 每日扫描所有.py中大额数字
2026-06-24 00:04:26 +08:00

125 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""
hk_rate.py — 每日刷新HKD/CNY汇率
用法:
from hk_rate import hkd_to_cny, refresh_rate
rate = hkd_to_cny() # 自动使用缓存,过期则刷新
refresh_rate() # 强制刷新
缓存文件:~/.cache/hk_exchange_rate.json
有效期:24小时
"""
import json, os, time, sys
from datetime import date
CACHE_PATH = os.path.expanduser("~/.cache/hk_exchange_rate.json")
CACHE_TTL = 86400 # 24小时,合理配置常量
# 不再硬编码备用值,每次取缓存中的最近一次有效汇率
def _load_last_rate():
"""从缓存文件读取上次已知有效汇率"""
try:
if os.path.exists(CACHE_PATH):
with open(CACHE_PATH) as f:
data = json.load(f)
rate = data.get("rate", 0)
if 0.7 < rate < 1.0:
return round(float(rate), 6)
except Exception:
pass
return None
PRIMARY_API = "https://api.exchangerate-api.com/v4/latest/HKD"
BACKUP_API = "https://api.exchangerate-api.com/v4/latest/USD"
def _fetch_rate():
"""从API获取 HKD/CNY 汇率"""
import urllib.request
ua = "Mozilla/5.0"
# 主API:直接用HKD→CNY
try:
req = urllib.request.Request(PRIMARY_API, headers={"User-Agent": ua})
with urllib.request.urlopen(req, timeout=8) as r:
data = json.loads(r.read())
cny = data.get("rates", {}).get("CNY")
if cny and 0.7 < cny < 1.0: # 合理性检查
return round(float(cny), 6)
except Exception:
pass
# 备用:USD→HKD + USD→CNY 间接计算
try:
req = urllib.request.Request(BACKUP_API, headers={"User-Agent": ua})
with urllib.request.urlopen(req, timeout=8) as r:
data = json.loads(r.read())
rates = data.get("rates", {})
hkd = rates.get("HKD")
cny = rates.get("CNY")
if hkd and cny:
rate = cny / hkd
if 0.7 < rate < 1.0:
return round(rate, 6)
except Exception:
pass
return None
def hkd_to_cny(force_refresh=False):
"""获取 HKD→CNY 汇率,缓存过期则自动刷新"""
os.makedirs(os.path.dirname(CACHE_PATH), exist_ok=True)
now = time.time()
rate = None
# 读缓存
if not force_refresh:
try:
with open(CACHE_PATH) as f:
cached = json.load(f)
cache_date = cached.get("date", "")
rate = cached.get("rate")
cached_at = cached.get("cached_at", 0)
# 同一天且未过期
if (cache_date == date.today().isoformat()
and rate is not None
and (now - cached_at) < CACHE_TTL):
return rate
except Exception:
pass
# 刷新
rate = _fetch_rate()
if rate is None:
# API全挂,用缓存中的上次有效汇率
rate = _load_last_rate()
if rate is None:
rate = 0.87 # 极限兜底,纯预防
print(f"[hk_rate] API不可达,使用 {rate} (fallback)", file=sys.stderr)
else:
# 写缓存
try:
with open(CACHE_PATH, "w") as f:
json.dump({
"rate": rate,
"date": date.today().isoformat(),
"cached_at": now,
"source": "exchangerate-api.com",
}, f)
except Exception:
pass
return rate
def refresh_rate():
return hkd_to_cny(force_refresh=True)
if __name__ == "__main__":
r = hkd_to_cny()
print(f"HKD/CNY = {r}")