1
This commit is contained in:
1 parent
3ae4e18211
commit
730adb3ff2
9 files changed
+206
-33
No files matched your search
+44
-8
@@ -315,6 +315,12 @@ class Position:
|
||||
tp_price: float = 0.0
|
||||
exact: bool = True # all prices and fees are the exchange's own figures
|
||||
innovation: bool = False # an Innovation Zone perp: its own fee rate
|
||||
decision_open_time: float = 0.0
|
||||
decision_spot_price: float = 0.0
|
||||
decision_futures_price: float = 0.0
|
||||
decision_close_time: float = 0.0
|
||||
decision_close_spot_price: float = 0.0
|
||||
decision_close_futures_price: float = 0.0
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -593,6 +599,10 @@ class BasisEngine:
|
||||
reason = reason or "продажа спота после выкупа шорта"
|
||||
if reason:
|
||||
if pos.real:
|
||||
if not pos.decision_close_time:
|
||||
pos.decision_close_time = now
|
||||
pos.decision_close_spot_price = p.spot_bid
|
||||
pos.decision_close_futures_price = p.fut_ask
|
||||
if pos.uid not in self.busy and now >= pos.retry_at:
|
||||
self.busy.add(pos.uid)
|
||||
self.jobs.put(("close", pos.uid, reason))
|
||||
@@ -874,6 +884,8 @@ class BasisEngine:
|
||||
|
||||
def _real_open(self, p: Pair, amount: float) -> None:
|
||||
import live
|
||||
decision_open_time = time.time()
|
||||
decision_spot_price, decision_futures_price = p.spot_ask, p.fut_bid
|
||||
notes = []
|
||||
# BEFORE buying anything: can the perp be shorted for this amount at all (its minimum lot)?
|
||||
try:
|
||||
@@ -897,7 +909,10 @@ class BasisEngine:
|
||||
or len(held) >= max(1, self.settings.max_real_positions)):
|
||||
self.opening.discard(p.base)
|
||||
return
|
||||
self.pending[p.base] = {"time": time.time(), "amount": amount} # a stop now: found again at start
|
||||
self.pending[p.base] = {"time": time.time(), "amount": amount,
|
||||
"decision_open_time": decision_open_time,
|
||||
"decision_spot_price": decision_spot_price,
|
||||
"decision_futures_price": decision_futures_price} # a stop now: found again at start
|
||||
self.save()
|
||||
# money where it is needed: the amount on spot for the buy, the same on futures for the short's margin
|
||||
# (leverage 1 assumed — the safe side); moved from the other account when one of them is short
|
||||
@@ -915,7 +930,7 @@ class BasisEngine:
|
||||
return
|
||||
self._journal(p.base, "transfer", False, f"перевод не удался: {exc} — пробую без него")
|
||||
if self.settings.entry_limit:
|
||||
self._open_limit_first(c, p, amount)
|
||||
self._open_limit_first(c, p, amount, decision_open_time, decision_spot_price, decision_futures_price)
|
||||
return
|
||||
try:
|
||||
buy = c.spot_buy(p.base, amount)
|
||||
@@ -936,6 +951,7 @@ class BasisEngine:
|
||||
return
|
||||
buy = live.Fill(bought, p.spot_ask, amount * self.settings.spot_fee / 100, exc.order_id)
|
||||
notes.append("покупка не подтверждена биржей — цена оценочная")
|
||||
actual_spot_time = time.time()
|
||||
try:
|
||||
free = c.spot_free(p.base)
|
||||
except live.LiveError:
|
||||
@@ -953,6 +969,7 @@ class BasisEngine:
|
||||
# the short may be open: keep the position (estimated price) — closing it is reduce-only
|
||||
fut = live.Fill(coins, p.fut_bid, coins * p.fut_bid * self._fut_rate(p) / 100, exc.order_id)
|
||||
notes.append("шорт не подтверждён биржей — проверьте позицию, цена оценочная")
|
||||
actual_futures_time = time.time() if fut is not None else 0.0
|
||||
if fut is None:
|
||||
# no hedge → sell the spot back at once
|
||||
try:
|
||||
@@ -971,16 +988,23 @@ class BasisEngine:
|
||||
pos = Position(self.next_uid, p.base, time.time(), fut.coins, buy.price, fut.price,
|
||||
(fut.price / buy.price - 1) * 100, buy.fee_usd + fut.fee_usd, p.next_funding,
|
||||
real=True, spot_qty=held, fut_symbol=p.fut_symbol, error="; ".join(notes),
|
||||
exact=buy.exact and fut.exact and not notes, innovation=self._innovation(p))
|
||||
exact=buy.exact and fut.exact and not notes, innovation=self._innovation(p),
|
||||
decision_open_time=decision_open_time, decision_spot_price=decision_spot_price,
|
||||
decision_futures_price=decision_futures_price)
|
||||
self.next_uid += 1
|
||||
self.positions.append(pos)
|
||||
self.save()
|
||||
self._journal(p.base, "open", True, f"куплено {held:g} по {buy.price:.10g}, шорт {fut.coins:g} по "
|
||||
f"{fut.price:.10g}, разница {pos.entry_gap:+.3f}%" + (f" ({'; '.join(notes)})" if notes else ""),
|
||||
amount=amount)
|
||||
amount=amount, decision_open_time=decision_open_time,
|
||||
decision_spot_price=decision_spot_price, decision_futures_price=decision_futures_price,
|
||||
actual_open_time=max(actual_spot_time, actual_futures_time),
|
||||
actual_spot_time=actual_spot_time, actual_futures_time=actual_futures_time,
|
||||
actual_spot_price=buy.price, actual_futures_price=fut.price)
|
||||
self._place_tp(c, pos)
|
||||
|
||||
def _open_limit_first(self, c, p: Pair, amount: float) -> None:
|
||||
def _open_limit_first(self, c, p: Pair, amount: float, decision_open_time: float,
|
||||
decision_spot_price: float, decision_futures_price: float) -> None:
|
||||
"""Entry with a LIMIT short at the signal's perp bid, kept up to `limit_hold_s` seconds; whatever part of it
|
||||
fills is hedged at once by buying the same coins on spot. Nothing filled → the order is removed, no spot."""
|
||||
import live
|
||||
@@ -1028,6 +1052,7 @@ class BasisEngine:
|
||||
self.last_close[p.base] = time.time()
|
||||
return
|
||||
fut_price = st["price"] or p.fut_bid
|
||||
actual_futures_time = time.time()
|
||||
# hedge: the same coins on spot (the buy fee is taken in coins: a little more USDT)
|
||||
try:
|
||||
buy = c.spot_buy(p.base, shorted * p.spot_ask * (1 + s.spot_fee / 100) * 1.003)
|
||||
@@ -1043,6 +1068,7 @@ class BasisEngine:
|
||||
self.last_close[p.base] = time.time()
|
||||
self._pause_if_balance(str(exc))
|
||||
return
|
||||
actual_spot_time = time.time()
|
||||
try:
|
||||
free = c.spot_free(p.base)
|
||||
except live.LiveError:
|
||||
@@ -1053,12 +1079,18 @@ class BasisEngine:
|
||||
pos = Position(self.next_uid, p.base, time.time(), shorted, buy.price, fut_price,
|
||||
(fut_price / buy.price - 1) * 100, buy.fee_usd + st["fee"], p.next_funding,
|
||||
real=True, spot_qty=held, fut_symbol=p.fut_symbol, exact=buy.exact,
|
||||
innovation=self._innovation(p))
|
||||
innovation=self._innovation(p), decision_open_time=decision_open_time,
|
||||
decision_spot_price=decision_spot_price, decision_futures_price=decision_futures_price)
|
||||
self.next_uid += 1
|
||||
self.positions.append(pos)
|
||||
self.save()
|
||||
self._journal(p.base, "open", True, f"лимитный шорт {shorted:g} по {fut_price:.10g} (заявка {p.fut_bid:.10g}), "
|
||||
f"спот куплен {held:g} по {buy.price:.10g}, разница {pos.entry_gap:+.3f}%", amount=amount)
|
||||
f"спот куплен {held:g} по {buy.price:.10g}, разница {pos.entry_gap:+.3f}%", amount=amount,
|
||||
decision_open_time=decision_open_time, decision_spot_price=decision_spot_price,
|
||||
decision_futures_price=decision_futures_price,
|
||||
actual_open_time=max(actual_futures_time, actual_spot_time),
|
||||
actual_futures_time=actual_futures_time, actual_spot_time=actual_spot_time,
|
||||
actual_futures_price=fut_price, actual_spot_price=buy.price)
|
||||
self._place_tp(c, pos)
|
||||
|
||||
def _place_tp(self, c, pos: Position) -> None:
|
||||
@@ -1252,7 +1284,11 @@ class BasisEngine:
|
||||
self.last_close[pos.base] = now
|
||||
self.save()
|
||||
self._journal(pos.base, "close", True, f"шорт выкуплен по {pos.fut_exit:.10g} (биржа: позиции нет), спот "
|
||||
f"продан по {sell_price:.10g}, итог {net:+.4f} $, комиссии {fees:.4f} $ ({reason})", net=net)
|
||||
f"продан по {sell_price:.10g}, итог {net:+.4f} $, комиссии {fees:.4f} $ ({reason})", net=net,
|
||||
decision_close_time=pos.decision_close_time or None,
|
||||
decision_spot_price=pos.decision_close_spot_price or None,
|
||||
decision_futures_price=pos.decision_close_futures_price or None,
|
||||
actual_close_time=now, actual_spot_price=sell_price, actual_futures_price=pos.fut_exit)
|
||||
|
||||
def reset(self) -> str:
|
||||
with self.lock:
|
||||
|
||||
Reference in new issue
Block a user