-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarket_snapshot_support.py
More file actions
82 lines (73 loc) · 3.13 KB
/
market_snapshot_support.py
File metadata and controls
82 lines (73 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import annotations
from typing import Any, Callable, Mapping
from notify_i18n_support import translate as t
def capture_market_snapshot(
runtime,
report: dict[str, Any],
runtime_trend_universe: Mapping[str, Mapping[str, Any]],
log_buffer,
min_bnb_value: float,
buy_bnb_amount: float,
*,
get_total_balance_fn: Callable[..., float],
ensure_asset_available_fn: Callable[..., bool],
runtime_call_client_fn: Callable[..., Any],
runtime_notify_fn: Callable[..., Any],
append_log_fn: Callable[..., Any],
resolve_btc_snapshot_fn: Callable[..., Any],
resolve_trend_indicators_fn: Callable[..., Any],
bnb_fuel_symbol: str = "BNBUSDT",
bnb_fuel_asset: str = "BNB",
) -> dict[str, Any]:
u_total = get_total_balance_fn(runtime.client, "USDT", log_buffer=log_buffer)
bnb_total = get_total_balance_fn(runtime.client, bnb_fuel_asset, log_buffer=log_buffer)
bnb_price = float(runtime.client.get_avg_price(symbol=bnb_fuel_symbol)["price"])
dynamic_usdt_buffer = max(50.0, min(u_total * 0.05, 300.0))
if bnb_total * bnb_price < min_bnb_value and u_total >= buy_bnb_amount:
report["buy_sell_intents"].append(
{
"category": "fuel",
"action": "buy",
"symbol": bnb_fuel_symbol,
"quote_order_qty": buy_bnb_amount,
}
)
try:
if not ensure_asset_available_fn(runtime, report, "USDT", buy_bnb_amount, log_buffer):
raise RuntimeError(t("usdt_spot_buffer_unavailable_for_bnb_top_up"))
runtime_call_client_fn(
runtime,
report,
method_name="order_market_buy",
payload={"symbol": bnb_fuel_symbol, "quoteOrderQty": buy_bnb_amount},
effect_type="order_buy",
)
u_total -= buy_bnb_amount
bnb_total += (buy_bnb_amount * 0.995) / bnb_price
append_log_fn(log_buffer, t("bnb_top_up_completed"))
except Exception as exc:
runtime_notify_fn(
runtime,
report,
f"{t('bnb_top_up_failed')}\n{t('error_label')}: {exc}",
)
prices = {}
balances = {}
for symbol, config in runtime_trend_universe.items():
prices[symbol] = float(runtime.client.get_avg_price(symbol=symbol)["price"])
balances[symbol] = get_total_balance_fn(runtime.client, config["base_asset"], log_buffer=log_buffer)
btc_price = float(runtime.client.get_avg_price(symbol="BTCUSDT")["price"])
balances["BTCUSDT"] = get_total_balance_fn(runtime.client, "BTC", log_buffer=log_buffer)
prices["BTCUSDT"] = btc_price
btc_snapshot = resolve_btc_snapshot_fn(runtime, btc_price, log_buffer)
if btc_snapshot is None:
raise RuntimeError(t("btc_indicators_insufficient"))
return {
"u_total": u_total,
"fuel_val": bnb_total * bnb_price,
"dynamic_usdt_buffer": dynamic_usdt_buffer,
"prices": prices,
"balances": balances,
"btc_snapshot": btc_snapshot,
"trend_indicators": resolve_trend_indicators_fn(runtime),
}