增加资金费率
This commit is contained in:
@@ -68,6 +68,34 @@ def init_db() -> None:
|
||||
last_fetch_at TEXT NOT NULL,
|
||||
bar_count INTEGER NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS funding_history (
|
||||
symbol TEXT NOT NULL,
|
||||
funding_time INTEGER NOT NULL,
|
||||
funding_rate REAL NOT NULL,
|
||||
mark_price REAL NOT NULL DEFAULT 0,
|
||||
updated_at TEXT NOT NULL,
|
||||
PRIMARY KEY (symbol, funding_time)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_funding_history_symbol
|
||||
ON funding_history(symbol, funding_time);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS funding_meta (
|
||||
symbol TEXT PRIMARY KEY,
|
||||
last_fetch_at TEXT NOT NULL,
|
||||
bar_count INTEGER NOT NULL,
|
||||
last_funding_rate REAL NOT NULL DEFAULT 0,
|
||||
next_funding_time INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS funding_current (
|
||||
symbol TEXT PRIMARY KEY,
|
||||
last_funding_rate REAL NOT NULL,
|
||||
next_funding_time INTEGER NOT NULL,
|
||||
mark_price REAL NOT NULL DEFAULT 0,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
@@ -216,6 +244,115 @@ def get_kline_meta(symbol: str) -> dict[str, Any] | None:
|
||||
}
|
||||
|
||||
|
||||
def save_funding_history(
|
||||
symbol: str,
|
||||
rows: list[dict[str, Any]],
|
||||
next_funding_time: int = 0,
|
||||
) -> None:
|
||||
sym = symbol.upper()
|
||||
now = datetime.now().isoformat()
|
||||
last_rate = 0.0
|
||||
next_time = next_funding_time
|
||||
with get_conn() as conn:
|
||||
for r in rows:
|
||||
ft = int(r["time"])
|
||||
rate = float(r["rate"])
|
||||
mp = float(r.get("mark_price", 0))
|
||||
last_rate = rate
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO funding_history (symbol, funding_time, funding_rate, mark_price, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(symbol, funding_time) DO UPDATE SET
|
||||
funding_rate = excluded.funding_rate,
|
||||
mark_price = excluded.mark_price,
|
||||
updated_at = excluded.updated_at
|
||||
""",
|
||||
(sym, ft, rate, mp, now),
|
||||
)
|
||||
if rows:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO funding_meta (symbol, last_fetch_at, bar_count, last_funding_rate, next_funding_time)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(symbol) DO UPDATE SET
|
||||
last_fetch_at = excluded.last_fetch_at,
|
||||
bar_count = excluded.bar_count,
|
||||
last_funding_rate = excluded.last_funding_rate
|
||||
""",
|
||||
(sym, now, len(rows), last_rate, next_time),
|
||||
)
|
||||
|
||||
|
||||
def get_funding_history_from_db(symbol: str, limit: int) -> list[dict[str, Any]]:
|
||||
sym = symbol.upper()
|
||||
with get_conn() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT funding_time, funding_rate, mark_price
|
||||
FROM funding_history
|
||||
WHERE symbol = ?
|
||||
ORDER BY funding_time DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
(sym, limit),
|
||||
).fetchall()
|
||||
rows = list(reversed(rows))
|
||||
return [
|
||||
{
|
||||
"time": int(r["funding_time"]),
|
||||
"rate": float(r["funding_rate"]),
|
||||
"rate_pct": float(r["funding_rate"]) * 100,
|
||||
"mark_price": float(r["mark_price"]),
|
||||
}
|
||||
for r in rows
|
||||
]
|
||||
|
||||
|
||||
def get_funding_meta(symbol: str) -> dict[str, Any] | None:
|
||||
sym = symbol.upper()
|
||||
with get_conn() as conn:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT last_fetch_at, bar_count, last_funding_rate, next_funding_time
|
||||
FROM funding_meta WHERE symbol = ?
|
||||
""",
|
||||
(sym,),
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
return {
|
||||
"last_fetch_at": row["last_fetch_at"],
|
||||
"bar_count": row["bar_count"],
|
||||
"last_funding_rate": row["last_funding_rate"],
|
||||
"next_funding_time": row["next_funding_time"],
|
||||
}
|
||||
|
||||
|
||||
def save_funding_current_bulk(data: dict[str, dict[str, Any]]) -> None:
|
||||
now = datetime.now().isoformat()
|
||||
with get_conn() as conn:
|
||||
for sym, info in data.items():
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO funding_current (symbol, last_funding_rate, next_funding_time, mark_price, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(symbol) DO UPDATE SET
|
||||
last_funding_rate = excluded.last_funding_rate,
|
||||
next_funding_time = excluded.next_funding_time,
|
||||
mark_price = excluded.mark_price,
|
||||
updated_at = excluded.updated_at
|
||||
""",
|
||||
(
|
||||
sym.upper(),
|
||||
float(info.get("lastFundingRate", 0) or 0),
|
||||
int(info.get("nextFundingTime", 0) or 0),
|
||||
float(info.get("markPrice", 0) or 0),
|
||||
now,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def was_pushed_today(period_start: str, period_end: str) -> bool:
|
||||
with get_conn() as conn:
|
||||
row = conn.execute(
|
||||
|
||||
Reference in New Issue
Block a user