chore(calendar): enhance record format

This commit is contained in:
2025-07-21 19:46:28 +08:00
parent 1013e9563f
commit c80e01a831

View File

@@ -37,12 +37,11 @@ def load_config(config_path="config/trading_config.json"):
raise raise
def create_event(symbol, comment, date_obj, description): def create_event(summary: str, date_obj, description):
"""创建单个日历事件。 """创建单个日历事件。
Args: Args:
symbol (str): 交易对符号 summary (str): 事件标题
comment (str): 交易备注
date_obj (date): 交易日期 date_obj (date): 交易日期
description (str): 事件描述 description (str): 事件描述
@@ -50,10 +49,13 @@ def create_event(symbol, comment, date_obj, description):
Event: 创建好的事件对象 Event: 创建好的事件对象
""" """
event = Event() event = Event()
event.add("uid", f"{symbol.lower()}-{date_obj.strftime('%Y%m%d')}@trade") event.add(
"uid",
f"{summary.lower().replace('/', '-')}-{date_obj.strftime('%Y%m%d')}@trade",
)
event.add("dtstamp", datetime.now()) event.add("dtstamp", datetime.now())
event.add("dtstart", date_obj) event.add("dtstart", date_obj)
event.add("summary", f"{symbol} 交易 ({comment})") event.add("summary", summary)
event.add("description", description) event.add("description", description)
return event return event
@@ -71,10 +73,8 @@ def generate_ics(config):
cal.add("prodid", "-//Trading Calendar//EN") cal.add("prodid", "-//Trading Calendar//EN")
cal.add("version", "2.0") cal.add("version", "2.0")
symbol_mapping = config.get("symbol_mapping", {})
for trade in config.get("trades", []): for trade in config.get("trades", []):
symbol = symbol_mapping.get(trade["symbol"], trade["symbol"]) symbol = trade["symbol"]
order_type = trade["order_type"] order_type = trade["order_type"]
side = trade["side"] side = trade["side"]
comment = trade["comment"] comment = trade["comment"]
@@ -82,27 +82,29 @@ def generate_ics(config):
# 构建描述 # 构建描述
amount = trade["params"].get("quoteOrderQty") amount = trade["params"].get("quoteOrderQty")
quantity = trade["params"].get("quantity") quantity = trade["params"].get("quantity")
description = f"交易类型: {order_type}\n方向: {side}"
if quantity:
description = f"{description}\n数量: {quantity}"
if amount:
description = f"{description}\n金额: {amount}"
description = f"{description}\n备注: {comment}"
if trade["execute_dates"] == ["*"]: if trade["execute_dates"] == ["*"]:
event = create_event(symbol, comment, datetime.now().date(), description) summary = f"{symbol}/{order_type}/{side}"
if amount:
summary = f"{summary}/{amount}{symbol[-4:]}"
if quantity:
summary = f"{summary}/{quantity}{symbol[:-4]}"
event = create_event(summary, datetime.now().date(), comment)
event.add("rrule", {"freq": "daily"}) event.add("rrule", {"freq": "daily"})
cal.add_component(event) cal.add_component(event)
logger.info("已创建项目:%s 交易 (%s) 每日", symbol, comment) logger.info("已创建项目:每日 %s", summary)
else: else:
for date_str in trade["execute_dates"]: for date_str in trade["execute_dates"]:
try: try:
date_obj = datetime.strptime(date_str, "%Y-%m-%d").date() date_obj = datetime.strptime(date_str, "%Y-%m-%d").date()
event = create_event(symbol, comment, date_obj, description) summary = f"{symbol}/{order_type}/{side}"
if amount:
summary = f"{summary}/{amount}{symbol[-4:]}"
if quantity:
summary = f"{summary}/{quantity}{symbol[:-4]}"
event = create_event(summary, date_obj, comment)
cal.add_component(event) cal.add_component(event)
logger.info( logger.info("已创建项目:%s %s", date_str, summary)
"已创建项目:%s 交易 (%s) %s", symbol, comment, date_str
)
except ValueError as ex: except ValueError as ex:
logger.warning("跳过无效日期 %s: %s", date_str, ex) logger.warning("跳过无效日期 %s: %s", date_str, ex)