1
This commit is contained in:
1 parent
3ae4e18211
commit
730adb3ff2
9 files changed
+206
-33
No files matched your search
+25
-8
@@ -1638,8 +1638,9 @@ class CrossEngine:
|
||||
l = self._still_good(l, usual, amount)
|
||||
if l is None:
|
||||
return
|
||||
entry_decision = [time.time(), l.spot_ask, l.fut_bid, l.entry_gap]
|
||||
if l.ss:
|
||||
self._real_open_ss(l, usual, amount)
|
||||
self._real_open_ss(l, usual, amount, entry_decision)
|
||||
return
|
||||
s = self.settings
|
||||
a_fee = spot_fee(l.spot_ex, self.fees)
|
||||
@@ -1667,7 +1668,8 @@ class CrossEngine:
|
||||
or any(p.real and p.closing for p in self.positions)):
|
||||
return
|
||||
self.pending[l.base] = {"time": time.time(), "key": l.key, "coins": coins, "usual": usual,
|
||||
"amount": amount, "link": asdict(l), "phase": "prepared"}
|
||||
"amount": amount, "link": asdict(l), "phase": "prepared",
|
||||
"entry_decision": entry_decision}
|
||||
self.save()
|
||||
notional = coins * l.fut_bid
|
||||
fut_need = max(notional * self.FUT_NEED, equity_for(s.min_liq_pct, notional))
|
||||
@@ -1769,6 +1771,7 @@ class CrossEngine:
|
||||
return
|
||||
shorted, fut_price = held, l.fut_bid
|
||||
fut_fee_usd, fut_exact = held * l.fut_bid * fut_fee(l.fut_ex, self.fees, l.innovation) / 100, False
|
||||
fut_fill_time = time.time()
|
||||
self._pending_update(l.base, phase="hedge_submitted", shorted=shorted)
|
||||
# 2) the hedge: the same coins on spot on A (the buy fee is taken in coins: a little more USDT)
|
||||
try:
|
||||
@@ -1791,6 +1794,7 @@ class CrossEngine:
|
||||
self._pause_if_balance(str(exc), l.spot_ex)
|
||||
return
|
||||
buy = live.Fill(bought, l.spot_ask, bought * l.spot_ask * a_fee / 100, exc.order_id, exact=False)
|
||||
spot_fill_time = time.time()
|
||||
self._pending_update(l.base, phase="hedge_confirmed", buy=asdict(buy), shorted=shorted,
|
||||
fut_price=fut_price, fut_fee=fut_fee_usd)
|
||||
try:
|
||||
@@ -1808,7 +1812,7 @@ class CrossEngine:
|
||||
exact=buy.exact and fut_exact, innovation=l.innovation,
|
||||
liq_price=fut_price * (1 + to_liq / 100), liq_calc=fut_price * (1 + to_liq / 100),
|
||||
liq_src="расчёт", liq_checked=now,
|
||||
entry_signal=[now, l.spot_ask, l.fut_bid, l.entry_gap],
|
||||
entry_signal=entry_decision,
|
||||
fills=[[l.spot_ex, "spot", buy.fee_usd, buy.coins * buy.price, buy.exact],
|
||||
[l.fut_ex, l.fut_leg, fut_fee_usd, shorted * fut_price, fut_exact]])
|
||||
if mismatch:
|
||||
@@ -1825,7 +1829,11 @@ class CrossEngine:
|
||||
return
|
||||
self._journal(l.base, "open", True, f"шорт {shorted:g} на {l.fut_ex} по {fut_price:.10g}, спот {held:g} на "
|
||||
f"{l.spot_ex} по {buy.price:.10g}, разница {pos.entry_gap:+.3f}% (обычная {usual:+.3f}%), "
|
||||
f"ликвидация шорта по расчёту {pos.liq_calc:.10g} (+{to_liq:.0f}%)", amount=amount)
|
||||
f"ликвидация шорта по расчёту {pos.liq_calc:.10g} (+{to_liq:.0f}%)", amount=amount,
|
||||
decision_open_time=entry_decision[0], decision_spot_price=entry_decision[1],
|
||||
decision_futures_price=entry_decision[2], actual_open_time=max(fut_fill_time, spot_fill_time),
|
||||
actual_spot_price=buy.price, actual_futures_price=fut_price,
|
||||
actual_spot_time=spot_fill_time, actual_futures_time=fut_fill_time)
|
||||
self._refresh_liq(pos.uid, first=True)
|
||||
|
||||
CONFIRM_CHECKS = 2
|
||||
@@ -1875,7 +1883,7 @@ class CrossEngine:
|
||||
self.fast_seen[l.key] = time.time()
|
||||
return fresh
|
||||
|
||||
def _real_open_ss(self, l: Link, usual: float, amount: float) -> None:
|
||||
def _real_open_ss(self, l: Link, usual: float, amount: float, entry_decision: list | None = None) -> None:
|
||||
"""Spot–spot: sell coins from the stock on B's spot first (they are there: nothing can fail half-way on a
|
||||
missing coin), then buy the same coins on A's spot. A failed buy on A buys the stock back on B at once."""
|
||||
import basis
|
||||
@@ -1897,6 +1905,7 @@ class CrossEngine:
|
||||
self._not_tradable(l.base, f"на споте {l.fut_ex} {have:g} {l.base}, а для продажи нужно {coins:.6g} — "
|
||||
f"положите монету на {l.fut_ex}", 300)
|
||||
return
|
||||
entry_decision = entry_decision or [time.time(), l.spot_ask, l.fut_bid, l.entry_gap]
|
||||
with self.lock:
|
||||
held = [x.base for x in self.positions if x.real]
|
||||
if (l.base in held or self.opening - {l.base} or self.elsewhere(l.base) or self.full(l)
|
||||
@@ -1960,7 +1969,7 @@ class CrossEngine:
|
||||
pos = CrossPosition(self.next_uid, l.base, l.spot_ex, l.fut_ex, l.spot_symbol, l.fut_symbol, now,
|
||||
sold.coins, buy.price, sold.price, (sold.price / buy.price - 1) * 100, usual,
|
||||
buy.fee_usd + sold.fee_usd, 0.0, real=True, spot_qty=got, ss=True,
|
||||
exact=buy.exact and sold.exact, entry_signal=[now, l.spot_ask, l.fut_bid, l.entry_gap],
|
||||
exact=buy.exact and sold.exact, entry_signal=entry_decision,
|
||||
fills=[[l.spot_ex, "spot", buy.fee_usd, buy.coins * buy.price, buy.exact],
|
||||
[l.fut_ex, "spot", sold.fee_usd, sold.coins * sold.price, sold.exact]])
|
||||
self.next_uid += 1
|
||||
@@ -1969,7 +1978,10 @@ class CrossEngine:
|
||||
self.save()
|
||||
self._journal(l.base, "open", True, f"спот-спот: продано {sold.coins:g} на {l.fut_ex} по {sold.price:.10g}, "
|
||||
f"куплено {got:g} на {l.spot_ex} по {buy.price:.10g}, разница {pos.entry_gap:+.3f}% "
|
||||
f"(обычная {usual:+.3f}%)", amount=amount)
|
||||
f"(обычная {usual:+.3f}%)", amount=amount,
|
||||
decision_open_time=entry_decision[0], decision_spot_price=entry_decision[1],
|
||||
decision_futures_price=entry_decision[2], actual_open_time=now,
|
||||
actual_spot_price=buy.price, actual_futures_price=sold.price)
|
||||
|
||||
def _refresh_liq(self, uid: int, first: bool = False) -> None:
|
||||
"""The exchange's own liquidation price of the short replaces the estimate (cross margin: it moves with
|
||||
@@ -2174,8 +2186,13 @@ class CrossEngine:
|
||||
self.save()
|
||||
first = (f"монеты на споте {pos.fut_ex} выкуплены по {pos.fut_exit:.10g}" if pos.ss else
|
||||
f"шорт на {pos.fut_ex} выкуплен по {pos.fut_exit:.10g} (биржа: позиции нет)")
|
||||
decision = pos.exit_signal or []
|
||||
self._journal(pos.base, "close", True, f"{first}, спот на {pos.spot_ex} продан по {sell_price:.10g}, итог "
|
||||
f"{net:+.4f} $, комиссии {fees:.4f} $ ({reason}){self._slippage_text(t)}", net=net)
|
||||
f"{net:+.4f} $, комиссии {fees:.4f} $ ({reason}){self._slippage_text(t)}", net=net,
|
||||
decision_close_time=decision[0] if len(decision) > 0 else None,
|
||||
decision_spot_price=decision[1] if len(decision) > 1 else None,
|
||||
decision_futures_price=decision[2] if len(decision) > 2 else None,
|
||||
actual_close_time=now, actual_spot_price=sell_price, actual_futures_price=pos.fut_exit)
|
||||
|
||||
@staticmethod
|
||||
def _slippage_text(t: CrossTrade) -> str:
|
||||
|
||||
Reference in new issue
Block a user