fe3011cfc2
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
88 lines
2.4 KiB
Python
88 lines
2.4 KiB
Python
"""
|
|
宏观数据管理:记录和管理月频地产景气度数据。
|
|
"""
|
|
import sqlite3
|
|
import sys
|
|
from datetime import datetime
|
|
|
|
DB_PATH = "/Users/vipg/Documents/futures-data-warehouse/db/futures.db"
|
|
|
|
TABLE_SQL = """
|
|
CREATE TABLE IF NOT EXISTS macro (
|
|
month TEXT PRIMARY KEY,
|
|
housing_start_yoy REAL,
|
|
note TEXT
|
|
)
|
|
"""
|
|
|
|
|
|
def init():
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn.execute(TABLE_SQL)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
|
|
def add(month, yoy, note=""):
|
|
"""添加/更新一个月的地产数据"""
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn.execute(
|
|
"INSERT OR REPLACE INTO macro (month, housing_start_yoy, note) VALUES (?, ?, ?)",
|
|
(month, yoy, note),
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
print(f"macro: {month} 新房开工同比 {yoy:+.1f}% {note}")
|
|
|
|
|
|
def list_all():
|
|
conn = sqlite3.connect(DB_PATH)
|
|
rows = conn.execute(
|
|
"SELECT month, housing_start_yoy, note FROM macro ORDER BY month DESC"
|
|
).fetchall()
|
|
conn.close()
|
|
if not rows:
|
|
print("暂无宏观数据")
|
|
return
|
|
print(f"{'月份':>8} {'新房开工同比':>10} {'备注'}")
|
|
print("-" * 40)
|
|
for r in rows:
|
|
print(f"{r[0]:>8} {r[1]:>+9.1f}% {r[2] or ''}")
|
|
|
|
|
|
def direction():
|
|
"""根据最新宏观数据返回当月方向偏好"""
|
|
conn = sqlite3.connect(DB_PATH)
|
|
row = conn.execute(
|
|
"SELECT month, housing_start_yoy FROM macro ORDER BY month DESC LIMIT 1"
|
|
).fetchone()
|
|
conn.close()
|
|
if not row:
|
|
return "观望", "无宏观数据"
|
|
|
|
month, yoy = row
|
|
if yoy > 5:
|
|
return "做多", f"{month} 开工同比 {yoy:+.1f}%,景气回暖"
|
|
elif yoy > -5:
|
|
return "观望", f"{month} 开工同比 {yoy:+.1f}%,方向不明"
|
|
else:
|
|
return "做空", f"{month} 开工同比 {yoy:+.1f}%,持续低迷"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
init()
|
|
|
|
if len(sys.argv) >= 3 and sys.argv[1] == "add":
|
|
add(sys.argv[2], float(sys.argv[3]), " ".join(sys.argv[4:]))
|
|
elif len(sys.argv) >= 2 and sys.argv[1] == "list":
|
|
list_all()
|
|
elif len(sys.argv) >= 2 and sys.argv[1] == "direction":
|
|
d, reason = direction()
|
|
print(f"当前方向: {d}")
|
|
print(f"依据: {reason}")
|
|
else:
|
|
print("用法:")
|
|
print(" python3 macro.py add 202606 -26.0 6月数据")
|
|
print(" python3 macro.py list 查看所有")
|
|
print(" python3 macro.py direction 查看当月方向")
|