-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
267 lines (207 loc) · 6.61 KB
/
Copy pathcli.py
File metadata and controls
267 lines (207 loc) · 6.61 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""CLI entrypoint for the Binance Futures Testnet trading bot."""
import argparse
import sys
from typing import Any
from binance.exceptions import BinanceAPIException, BinanceRequestException
from colorama import Fore, Style, init
from requests.exceptions import ConnectionError as RequestsConnectionError
from requests.exceptions import RequestException, Timeout
from bot.client import CredentialError, create_futures_client
from bot.logging_config import log_error, setup_logging
from bot.orders import place_limit_order, place_market_order
from bot.validators import OrderArgs, ValidationError, validate_order_args
logger = setup_logging()
init(autoreset=True)
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
def build_parser() -> argparse.ArgumentParser:
"""Build the CLI argument parser."""
parser = argparse.ArgumentParser(
description="Place MARKET or LIMIT orders on Binance Futures Testnet.",
)
parser.add_argument(
"--symbol",
required=True,
help="Trading pair, e.g. BTCUSDT",
)
parser.add_argument(
"--side",
required=True,
help="Order side: BUY or SELL",
)
parser.add_argument(
"--type",
required=True,
dest="order_type",
help="Order type: MARKET or LIMIT",
)
parser.add_argument(
"--quantity",
required=True,
help="Order quantity (must be > 0)",
)
parser.add_argument(
"--price",
help="Limit price (required for LIMIT orders, must be > 0)",
)
return parser
def print_request_summary(order_args: OrderArgs) -> None:
"""Print a professional request summary."""
print("\n" + "=" * 60)
print(
Fore.CYAN
+ Style.BRIGHT
+ " BINANCE FUTURES TESTNET TRADING BOT"
)
print("=" * 60)
print(Fore.YELLOW + Style.BRIGHT + "\nOrder Summary")
print("-" * 60)
print(f"Symbol : {order_args['symbol']}")
print(f"Side : {order_args['side']}")
print(f"Order Type : {order_args['type']}")
print(f"Quantity : {order_args['quantity']}")
if order_args["type"] == "LIMIT":
print(f"Price : {order_args['price']}")
print("-" * 60)
print(Fore.BLUE + "Connecting to Binance Futures Testnet...\n")
def _has_avg_price(avg_price: Any) -> bool:
"""Return True when avgPrice exists."""
return avg_price is not None and str(avg_price) not in (
"",
"0",
"0.0",
"0.00",
)
def print_order_response(response: dict[str, Any]) -> None:
"""Print formatted order response."""
print(Fore.GREEN + Style.BRIGHT + "✓ Order Submitted Successfully")
print("-" * 60)
print(f"Order ID : {response.get('orderId', 'N/A')}")
status = response.get("status", "N/A")
if status == "NEW":
print(f"Status : {status} (Accepted by Binance)")
else:
print(f"Status : {status}")
print(f"Executed Qty : {response.get('executedQty', 'N/A')}")
avg_price = response.get("avgPrice")
if _has_avg_price(avg_price):
print(f"Average Price : {avg_price}")
else:
print("Average Price : N/A")
print("-" * 60)
def place_order(order_args: OrderArgs) -> dict[str, Any]:
"""Create client and place order."""
client = create_futures_client()
if order_args["type"] == "MARKET":
return place_market_order(
client=client,
symbol=order_args["symbol"],
side=order_args["side"],
quantity=order_args["quantity"],
)
return place_limit_order(
client=client,
symbol=order_args["symbol"],
side=order_args["side"],
quantity=order_args["quantity"],
price=order_args["price"],
)
def _exit_code_from_system_exit(exc: SystemExit) -> int:
"""Convert argparse exit into integer."""
if isinstance(exc.code, int):
return exc.code
return EXIT_FAILURE
def run(argv: list[str] | None = None) -> int:
"""Run the CLI."""
parser = build_parser()
try:
args = parser.parse_args(argv)
except SystemExit as exc:
return _exit_code_from_system_exit(exc)
try:
order_args = validate_order_args(
symbol=args.symbol,
side=args.side,
order_type=args.order_type,
quantity=args.quantity,
price=args.price,
)
except ValidationError as exc:
log_error(logger, "Validation failed", exc)
print(
Fore.RED
+ Style.BRIGHT
+ f"\n✗ Validation Error: {exc}"
)
return EXIT_FAILURE
print_request_summary(order_args)
try:
response = place_order(order_args)
except CredentialError as exc:
log_error(logger, "Credential error", exc)
print(
Fore.RED
+ Style.BRIGHT
+ f"\n✗ Credential Error: {exc}"
)
return EXIT_FAILURE
except BinanceAPIException as exc:
log_error(logger, "Binance API error", exc)
print(
Fore.RED
+ Style.BRIGHT
+ f"\n✗ Binance API Error ({exc.code}): {exc.message}"
)
return EXIT_FAILURE
except BinanceRequestException as exc:
log_error(logger, "Binance request error", exc)
print(
Fore.RED
+ Style.BRIGHT
+ f"\n✗ Binance Request Error: {exc}"
)
return EXIT_FAILURE
except Timeout as exc:
log_error(logger, "Timeout", exc)
print(
Fore.RED
+ Style.BRIGHT
+ "\n✗ Request timed out. Please try again."
)
return EXIT_FAILURE
except RequestsConnectionError as exc:
log_error(logger, "Connection error", exc)
print(
Fore.RED
+ Style.BRIGHT
+ "\n✗ Network connection error."
)
return EXIT_FAILURE
except RequestException as exc:
log_error(logger, "Request error", exc)
print(
Fore.RED
+ Style.BRIGHT
+ f"\n✗ Network Error: {exc}"
)
return EXIT_FAILURE
except Exception as exc:
log_error(logger, "Unexpected error", exc)
print(
Fore.RED
+ Style.BRIGHT
+ f"\n✗ Unexpected Error: {exc}"
)
return EXIT_FAILURE
print_order_response(response)
print(
Fore.GREEN
+ Style.BRIGHT
+ "\n✓ SUCCESS: Order placed successfully on Binance Futures Testnet."
)
return EXIT_SUCCESS
def main() -> None:
"""Application entry point."""
sys.exit(run())
if __name__ == "__main__":
main()