chore(calendar): enhance record format
This commit is contained in:
@@ -37,12 +37,11 @@ def load_config(config_path="config/trading_config.json"):
|
||||
raise
|
||||
|
||||
|
||||
def create_event(symbol, comment, date_obj, description):
|
||||
def create_event(summary: str, date_obj, description):
|
||||
"""创建单个日历事件。
|
||||
|
||||
Args:
|
||||
symbol (str): 交易对符号
|
||||
comment (str): 交易备注
|
||||
summary (str): 事件标题
|
||||
date_obj (date): 交易日期
|
||||
description (str): 事件描述
|
||||
|
||||
@@ -50,10 +49,13 @@ def create_event(symbol, comment, date_obj, description):
|
||||
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("dtstart", date_obj)
|
||||
event.add("summary", f"{symbol} 交易 ({comment})")
|
||||
event.add("summary", summary)
|
||||
event.add("description", description)
|
||||
return event
|
||||
|
||||
@@ -71,10 +73,8 @@ def generate_ics(config):
|
||||
cal.add("prodid", "-//Trading Calendar//EN")
|
||||
cal.add("version", "2.0")
|
||||
|
||||
symbol_mapping = config.get("symbol_mapping", {})
|
||||
|
||||
for trade in config.get("trades", []):
|
||||
symbol = symbol_mapping.get(trade["symbol"], trade["symbol"])
|
||||
symbol = trade["symbol"]
|
||||
order_type = trade["order_type"]
|
||||
side = trade["side"]
|
||||
comment = trade["comment"]
|
||||
@@ -82,27 +82,29 @@ def generate_ics(config):
|
||||
# 构建描述
|
||||
amount = trade["params"].get("quoteOrderQty")
|
||||
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"] == ["*"]:
|
||||
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"})
|
||||
cal.add_component(event)
|
||||
logger.info("已创建项目:%s 交易 (%s) 每日", symbol, comment)
|
||||
logger.info("已创建项目:每日 %s", summary)
|
||||
else:
|
||||
for date_str in trade["execute_dates"]:
|
||||
try:
|
||||
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)
|
||||
logger.info(
|
||||
"已创建项目:%s 交易 (%s) %s", symbol, comment, date_str
|
||||
)
|
||||
logger.info("已创建项目:%s %s", date_str, summary)
|
||||
except ValueError as ex:
|
||||
logger.warning("跳过无效日期 %s: %s", date_str, ex)
|
||||
|
||||
|
Reference in New Issue
Block a user