feat: переход на USDC + live mode
- spot.py: универсальная quote currency (USDT/USDC) вместо хардкода - config.yaml: mode=live, testnet=false, BTC/USDC, 1 актив, $114 капитал Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -59,14 +59,15 @@ class SpotExecutor:
|
||||
log.error(f"Price fetch {symbol} failed: {e}")
|
||||
return None
|
||||
|
||||
def buy(self, symbol: str, usdt_amount: float) -> dict | None:
|
||||
"""Market buy with specified USDT amount.
|
||||
def buy(self, symbol: str, quote_amount: float) -> dict | None:
|
||||
"""Market buy with specified quote currency amount.
|
||||
|
||||
Returns order dict or None on failure.
|
||||
Does NOT retry on failure (prevents double orders).
|
||||
"""
|
||||
self._ensure_markets()
|
||||
coin = symbol.split("/")[0]
|
||||
quote = symbol.split("/")[1] # USDT, USDC, etc.
|
||||
|
||||
# 1. Get price
|
||||
price = self.get_price(symbol)
|
||||
@@ -75,7 +76,7 @@ class SpotExecutor:
|
||||
|
||||
# 2. Calculate amount
|
||||
market = self.exchange.market(symbol)
|
||||
amount = usdt_amount / price
|
||||
amount = quote_amount / price
|
||||
amount = float(self.exchange.amount_to_precision(symbol, amount))
|
||||
|
||||
# 3. Validate minimums
|
||||
@@ -85,37 +86,37 @@ class SpotExecutor:
|
||||
if amount < min_amount:
|
||||
log.error(f"BUY {symbol}: amount {amount} < min {min_amount}")
|
||||
return None
|
||||
if usdt_amount < min_cost:
|
||||
log.error(f"BUY {symbol}: cost ${usdt_amount:.2f} < min ${min_cost}")
|
||||
if quote_amount < min_cost:
|
||||
log.error(f"BUY {symbol}: cost ${quote_amount:.2f} < min ${min_cost}")
|
||||
return None
|
||||
|
||||
# 4. Check balance (strict)
|
||||
usdt_free = self.get_balance("USDT")
|
||||
if usdt_free < usdt_amount:
|
||||
quote_free = self.get_balance(quote)
|
||||
if quote_free < quote_amount:
|
||||
# Try to buy what we can afford
|
||||
if usdt_free >= min_cost:
|
||||
log.warning(f"BUY {symbol}: USDT {usdt_free:.2f} < requested {usdt_amount:.2f}, "
|
||||
f"reducing to {usdt_free:.2f}")
|
||||
usdt_amount = usdt_free
|
||||
amount = usdt_amount / price
|
||||
if quote_free >= min_cost:
|
||||
log.warning(f"BUY {symbol}: {quote} {quote_free:.2f} < requested {quote_amount:.2f}, "
|
||||
f"reducing to {quote_free:.2f}")
|
||||
quote_amount = quote_free
|
||||
amount = quote_amount / price
|
||||
amount = float(self.exchange.amount_to_precision(symbol, amount))
|
||||
else:
|
||||
log.error(f"BUY {symbol}: USDT {usdt_free:.2f} < min ${min_cost}")
|
||||
log.error(f"BUY {symbol}: {quote} {quote_free:.2f} < min ${min_cost}")
|
||||
return None
|
||||
|
||||
# 5. Execute
|
||||
ts = datetime.now(timezone.utc).isoformat()
|
||||
if self.dry_run:
|
||||
log.info(f"[DRY RUN] BUY {amount} {coin} @ ~${price:,.2f} "
|
||||
f"(${usdt_amount:.2f}) at {ts}")
|
||||
f"(${quote_amount:.2f}) at {ts}")
|
||||
return {"dry_run": True, "side": "buy", "symbol": symbol,
|
||||
"amount": amount, "price": price, "cost": usdt_amount}
|
||||
"amount": amount, "price": price, "cost": quote_amount}
|
||||
|
||||
try:
|
||||
order = self.exchange.create_market_buy_order(symbol, amount)
|
||||
filled = float(order.get("filled", 0))
|
||||
avg_price = float(order.get("average", price))
|
||||
cost = float(order.get("cost", usdt_amount))
|
||||
cost = float(order.get("cost", quote_amount))
|
||||
log.info(f"BUY OK: {filled} {coin} @ ${avg_price:,.2f} "
|
||||
f"(${cost:.2f}) order={order['id']}")
|
||||
return order
|
||||
|
||||
Reference in New Issue
Block a user