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:
@@ -98,6 +98,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,
|
||||
@@ -2977,72 +2978,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), FUNDS_DECIMALS)
|
||||
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}账户已达到目标{target_amount}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不足,需{needed}U,当前{round(from_balance, FUNDS_DECIMALS)}U")
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
send_wechat_msg(
|
||||
f"自动划转失败:{AUTO_TRANSFER_FROM}余额不足,需{needed}U,当前{round(from_balance, FUNDS_DECIMALS)}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=FUNDS_DECIMALS,
|
||||
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"自动划转成功:补足到{target_amount}U,实际划转{needed}U "
|
||||
f"{AUTO_TRANSFER_FROM}->{AUTO_TRANSFER_TO}\n"
|
||||
f"账簿日(UTC):{transfer_day}|触发时刻(北京):{app_now_str()}"
|
||||
)
|
||||
else:
|
||||
send_wechat_msg(
|
||||
f"自动划转失败:计划补足到{target_amount}U,需划转{needed}U\n原因:{msg}\n"
|
||||
f"账簿日(UTC):{transfer_day}|触发时刻(北京):{app_now_str()}"
|
||||
)
|
||||
|
||||
|
||||
def trading_day_reset_allows_new_open(now):
|
||||
|
||||
Reference in New Issue
Block a user