-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
238 lines (201 loc) · 8.53 KB
/
cli.py
File metadata and controls
238 lines (201 loc) · 8.53 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
"""
Binance Futures Testnet Trading Bot — CLI Entry Point
"""
import os
import sys
from typing import Optional
import typer
from dotenv import load_dotenv
from rich.console import Console
from rich.table import Table
from rich import box
from bot import __version__
from bot.client import BinanceFuturesClient, BinanceAPIError
from bot.orders import place_market_order, place_limit_order, place_stop_limit_order
from bot.validators import validate_order_params, ValidationError
from bot.logging_config import setup_logger
load_dotenv()
logger = setup_logger("trading_bot.cli")
app = typer.Typer(
name="trading-bot",
help="Binance Futures Testnet Trading Bot — place MARKET, LIMIT, and STOP-LIMIT orders.",
add_completion=False,
)
console = Console()
def version_callback(value: bool):
if value:
console.print(f"Trading Bot version: [bold cyan]{__version__}[/bold cyan]")
raise typer.Exit()
def _get_client() -> BinanceFuturesClient:
api_key = os.getenv("BINANCE_API_KEY", "").strip()
secret_key = os.getenv("BINANCE_SECRET_KEY", "").strip()
if not api_key or not secret_key:
console.print(
"[bold red]ERROR:[/bold red] BINANCE_API_KEY and BINANCE_SECRET_KEY "
"must be set in your .env file."
)
raise typer.Exit(code=1)
return BinanceFuturesClient(api_key, secret_key)
def _print_request_table(params: dict):
table = Table(
title="Order Request",
box=box.ROUNDED,
border_style="cyan",
show_header=False,
padding=(0, 2),
)
table.add_column("Field", style="bold dim", width=16)
table.add_column("Value", style="bold white")
for key, value in params.items():
if value is not None:
table.add_row(key.replace("_", " ").title(), str(value))
console.print(table)
def _print_response_table(result: dict, dry_run: bool = False):
title = "[DRY RUN] Order Response" if dry_run else "Order Response"
table = Table(
title=title,
box=box.ROUNDED,
border_style="green",
show_header=False,
padding=(0, 2),
)
table.add_column("Field", style="bold dim", width=16)
table.add_column("Value", style="bold white")
display_fields = [
("Order ID", "orderId"),
("Symbol", "symbol"),
("Side", "side"),
("Type", "type"),
("Status", "status"),
("Orig Qty", "origQty"),
("Executed Qty", "executedQty"),
("Avg Price", "avgPrice"),
("Price", "price"),
("Stop Price", "stopPrice"),
("Time In Force", "timeInForce"),
]
for label, key in display_fields:
val = result.get(key)
if val is not None and val != "" and val != "0":
table.add_row(label, str(val))
console.print(table)
@app.command()
def market(
symbol: str = typer.Argument(..., help="Trading pair, e.g. BTCUSDT"),
side: str = typer.Argument(..., help="BUY or SELL"),
quantity: float = typer.Argument(..., help="Order quantity"),
dry_run: bool = typer.Option(False, "--dry-run", help="Preview order without placing it"),
version: Optional[bool] = typer.Option(None, "--version", callback=version_callback, is_eager=True),
):
"""Place a MARKET order on Binance Futures Testnet."""
try:
params = validate_order_params(symbol, side, "MARKET", quantity)
except ValidationError as e:
console.print(f"[bold red]Validation Error:[/bold red] {e}")
logger.warning("VALIDATION_ERROR | %s", e)
raise typer.Exit(code=1)
console.print()
_print_request_table({"Symbol": params["symbol"], "Side": params["side"],
"Type": "MARKET", "Quantity": params["quantity"]})
if dry_run:
console.print("\n[bold yellow]DRY RUN — order not placed.[/bold yellow]\n")
return
console.print("\n[dim]Placing order...[/dim]")
try:
with _get_client() as client:
result = place_market_order(client, params["symbol"], params["side"], params["quantity"])
console.print()
_print_response_table(result)
console.print("\n[bold green]✅ Order placed successfully![/bold green]\n")
except BinanceAPIError as e:
console.print(f"\n[bold red]❌ Binance API Error {e.code}:[/bold red] {e.message}\n")
raise typer.Exit(code=1)
except (ConnectionError, Exception) as e:
console.print(f"\n[bold red]❌ Error:[/bold red] {e}\n")
raise typer.Exit(code=1)
@app.command()
def limit(
symbol: str = typer.Argument(..., help="Trading pair, e.g. BTCUSDT"),
side: str = typer.Argument(..., help="BUY or SELL"),
quantity: float = typer.Argument(..., help="Order quantity"),
price: float = typer.Option(..., "--price", "-p", help="Limit price (required)"),
time_in_force: str = typer.Option("GTC", "--tif", help="Time in force: GTC, IOC, FOK"),
dry_run: bool = typer.Option(False, "--dry-run", help="Preview order without placing it"),
):
"""Place a LIMIT order on Binance Futures Testnet."""
try:
params = validate_order_params(symbol, side, "LIMIT", quantity, price=price)
except ValidationError as e:
console.print(f"[bold red]Validation Error:[/bold red] {e}")
logger.warning("VALIDATION_ERROR | %s", e)
raise typer.Exit(code=1)
console.print()
_print_request_table({"Symbol": params["symbol"], "Side": params["side"],
"Type": "LIMIT", "Quantity": params["quantity"],
"Price": params["price"], "Time In Force": time_in_force})
if dry_run:
console.print("\n[bold yellow]DRY RUN — order not placed.[/bold yellow]\n")
return
console.print("\n[dim]Placing order...[/dim]")
try:
with _get_client() as client:
result = place_limit_order(
client, params["symbol"], params["side"],
params["quantity"], params["price"], time_in_force
)
console.print()
_print_response_table(result)
console.print("\n[bold green]✅ Order placed successfully![/bold green]\n")
except BinanceAPIError as e:
console.print(f"\n[bold red]❌ Binance API Error {e.code}:[/bold red] {e.message}\n")
raise typer.Exit(code=1)
except (ConnectionError, Exception) as e:
console.print(f"\n[bold red]❌ Error:[/bold red] {e}\n")
raise typer.Exit(code=1)
@app.command(name="stop-limit")
def stop_limit(
symbol: str = typer.Argument(..., help="Trading pair, e.g. BTCUSDT"),
side: str = typer.Argument(..., help="BUY or SELL"),
quantity: float = typer.Argument(..., help="Order quantity"),
price: float = typer.Option(..., "--price", "-p", help="Limit price (execution price)"),
stop_price: float = typer.Option(..., "--stop-price", "-s", help="Stop trigger price"),
time_in_force: str = typer.Option("GTC", "--tif", help="Time in force: GTC, IOC, FOK"),
dry_run: bool = typer.Option(False, "--dry-run", help="Preview order without placing it"),
):
"""Place a STOP-LIMIT order on Binance Futures Testnet (bonus order type)."""
try:
params = validate_order_params(
symbol, side, "STOP_LIMIT", quantity, price=price, stop_price=stop_price
)
except ValidationError as e:
console.print(f"[bold red]Validation Error:[/bold red] {e}")
logger.warning("VALIDATION_ERROR | %s", e)
raise typer.Exit(code=1)
console.print()
_print_request_table({
"Symbol": params["symbol"], "Side": params["side"],
"Type": "STOP_LIMIT", "Quantity": params["quantity"],
"Price": params["price"], "Stop Price": params["stop_price"],
"Time In Force": time_in_force,
})
if dry_run:
console.print("\n[bold yellow]DRY RUN — order not placed.[/bold yellow]\n")
return
console.print("\n[dim]Placing order...[/dim]")
try:
with _get_client() as client:
result = place_stop_limit_order(
client, params["symbol"], params["side"],
params["quantity"], params["price"], params["stop_price"], time_in_force
)
console.print()
_print_response_table(result)
console.print("\n[bold green]✅ Order placed successfully![/bold green]\n")
except BinanceAPIError as e:
console.print(f"\n[bold red]❌ Binance API Error {e.code}:[/bold red] {e.message}\n")
raise typer.Exit(code=1)
except (ConnectionError, Exception) as e:
console.print(f"\n[bold red]❌ Error:[/bold red] {e}\n")
raise typer.Exit(code=1)
if __name__ == "__main__":
app()