feat: bidirectional daily auto-transfer with position skip

Rebalance swap to AUTO_TRANSFER_AMOUNT at Beijing hour: top up from funding or sweep excess back. Skip and WeChat notify when active positions exist.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-04 09:57:25 +08:00
parent 0456d5fa2c
commit 29b0634c6d
16 changed files with 241 additions and 270 deletions
+17 -65
View File
@@ -97,6 +97,7 @@ from key_monitor_full_margin_lib import (
monitor_type_disallowed_in_full_margin,
purge_disallowed_key_monitors,
)
from auto_transfer_daily_lib import run_auto_transfer_once_per_day
from key_monitor_lib import (
KEY_DIRECTION_WATCH,
KEY_MONITOR_ALERT_ONLY_TYPES,
@@ -2670,72 +2671,23 @@ def get_account_usdt_total(account_type):
def auto_transfer_once_per_day():
if not AUTO_TRANSFER_ENABLED:
return
utc_dt = utc_now_dt()
bj = utc_dt.astimezone(APP_TZ)
if bj.hour != AUTO_TRANSFER_BJ_HOUR:
return
transfer_day = utc_calendar_date_str()
conn = get_db()
exists = conn.execute(
"SELECT id FROM transfer_logs WHERE transfer_type=? AND transfer_day=?",
("auto_daily", transfer_day)
).fetchone()
if exists:
conn.close()
return
target_amount = AUTO_TRANSFER_AMOUNT
to_balance = get_account_usdt_total(AUTO_TRANSFER_TO)
from_balance = get_account_usdt_total(AUTO_TRANSFER_FROM)
if to_balance is None:
conn.execute(
"INSERT INTO transfer_logs (transfer_type, transfer_day, amount, from_account, to_account, status, message) VALUES (?,?,?,?,?,?,?)",
("auto_daily", transfer_day, 0, AUTO_TRANSFER_FROM, AUTO_TRANSFER_TO, "failed", f"读取{AUTO_TRANSFER_TO}账户USDT失败")
)
conn.commit()
conn.close()
return
needed = round(max(target_amount - float(to_balance), 0), 4)
if needed <= 0:
conn.execute(
"INSERT INTO transfer_logs (transfer_type, transfer_day, amount, from_account, to_account, status, message) VALUES (?,?,?,?,?,?,?)",
("auto_daily", transfer_day, 0, AUTO_TRANSFER_FROM, AUTO_TRANSFER_TO, "skipped", f"{AUTO_TRANSFER_TO}账户已达到目标{round(float(target_amount), 2)}U")
)
conn.commit()
conn.close()
return
if from_balance is not None and from_balance < needed:
conn.execute(
"INSERT INTO transfer_logs (transfer_type, transfer_day, amount, from_account, to_account, status, message) VALUES (?,?,?,?,?,?,?)",
("auto_daily", transfer_day, needed, AUTO_TRANSFER_FROM, AUTO_TRANSFER_TO, "failed", f"{AUTO_TRANSFER_FROM}账户USDT不足,需{round(needed, 2)}U,当前{round(from_balance, 2)}U")
)
conn.commit()
conn.close()
send_wechat_msg(
f"自动划转失败:{AUTO_TRANSFER_FROM}余额不足,需{round(needed, 2)}U,当前{round(from_balance, 2)}U\n"
f"账簿日(UTC){transfer_day}|触发时刻(北京){app_now_str()}"
)
return
ok, msg, _ = execute_transfer_usdt(needed, AUTO_TRANSFER_FROM, AUTO_TRANSFER_TO)
conn.execute(
"INSERT INTO transfer_logs (transfer_type, transfer_day, amount, from_account, to_account, status, message) VALUES (?,?,?,?,?,?,?)",
("auto_daily", transfer_day, needed, AUTO_TRANSFER_FROM, AUTO_TRANSFER_TO, "success" if ok else "failed", msg[:500])
run_auto_transfer_once_per_day(
enabled=AUTO_TRANSFER_ENABLED,
bj_hour=AUTO_TRANSFER_BJ_HOUR,
target_amount=AUTO_TRANSFER_AMOUNT,
from_account=AUTO_TRANSFER_FROM,
to_account=AUTO_TRANSFER_TO,
funds_decimals=2,
get_db=get_db,
get_active_position_count=get_active_position_count,
get_account_usdt_total=get_account_usdt_total,
execute_transfer_usdt=execute_transfer_usdt,
send_wechat_msg=send_wechat_msg,
utc_now_dt=utc_now_dt,
app_tz=APP_TZ,
utc_calendar_date_str=utc_calendar_date_str,
app_now_str=app_now_str,
)
conn.commit()
conn.close()
if ok:
send_wechat_msg(
f"自动划转成功:补足到{round(float(target_amount), 2)}U,实际划转{round(needed, 2)}U "
f"{AUTO_TRANSFER_FROM}->{AUTO_TRANSFER_TO}\n"
f"账簿日(UTC){transfer_day}|触发时刻(北京){app_now_str()}"
)
else:
send_wechat_msg(
f"自动划转失败:计划补足到{round(float(target_amount), 2)}U,需划转{round(needed, 2)}U\n原因:{msg}\n"
f"账簿日(UTC){transfer_day}|触发时刻(北京){app_now_str()}"
)
def trading_day_reset_allows_new_open(now):