-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
135 lines (114 loc) · 4.33 KB
/
Copy pathcli.py
File metadata and controls
135 lines (114 loc) · 4.33 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
import argparse
import os
import sys
import json
from dotenv import load_dotenv
from bot.client import BinanceFuturesClient
from bot.validators import validate_order_input
from bot.orders import execute_order
from bot.exceptions import BinanceAPIError, ValidationError
from bot.logging_config import logger
def parse_args():
parser = argparse.ArgumentParser(
description="Simplified Trading Bot for Binance Futures Testnet"
)
parser.add_argument(
"--symbol", type=str, required=True,
help="Trading pair symbol (e.g., BTCUSDT)"
)
parser.add_argument(
"--side", type=str, required=True, choices=["BUY", "SELL", "buy", "sell"],
help="Order side: BUY or SELL"
)
parser.add_argument(
"--type", type=str, required=True, choices=["MARKET", "LIMIT", "STOP_MARKET", "market", "limit", "stop_market"],
help="Order type: MARKET, LIMIT, or STOP_MARKET"
)
parser.add_argument(
"--quantity", type=float, required=True,
help="Quantity to trade"
)
parser.add_argument(
"--price", type=float, required=False,
help="Price (Required for LIMIT orders)"
)
parser.add_argument(
"--stop-price", type=float, required=False,
help="Stop price (Required for STOP_MARKET orders)"
)
parser.add_argument(
"--env", type=str, required=False, default=".env",
help="Path to .env file containing API_KEY and API_SECRET"
)
return parser.parse_args()
def print_summary(params: dict):
print("\n" + "="*40)
print("ORDER REQUEST SUMMARY")
print("="*40)
print(f"Symbol : {params['symbol']}")
print(f"Side : {params['side']}")
print(f"Type : {params['order_type']}")
print(f"Quantity : {params['quantity']}")
if params['price']:
print(f"Price : {params['price']}")
if params['stop_price']:
print(f"Stop Price : {params['stop_price']}")
print("="*40)
def print_response(success: bool, data: dict = None, error: str = None):
print("\n" + "="*40)
print("ORDER RESPONSE DETAILS")
print("="*40)
if success and data:
print("[SUCCESS] Order placed successfully!")
print(f"Order ID : {data.get('orderId')}")
print(f"Status : {data.get('status')}")
print(f"Executed Qty : {data.get('executedQty')}")
print(f"Avg Price : {data.get('avgPrice')}")
else:
print("[FAILURE] Order placement failed!")
print(f"Error Reason : {error}")
print("="*40 + "\n")
def main():
args = parse_args()
# Load credentials
load_dotenv(args.env)
api_key = os.getenv("BINANCE_TESTNET_API_KEY")
api_secret = os.getenv("BINANCE_TESTNET_API_SECRET")
if not api_key or not api_secret:
logger.error("API credentials not found. Please set BINANCE_TESTNET_API_KEY and BINANCE_TESTNET_API_SECRET.")
print("[ERROR] Missing API credentials in environment or .env file.")
sys.exit(1)
try:
# Validate Input
params = validate_order_input(
symbol=args.symbol,
side=args.side,
order_type=args.type,
quantity=args.quantity,
price=args.price,
stop_price=args.stop_price
)
# Display request summary
print_summary(params)
# Initialize Client
client = BinanceFuturesClient(api_key=api_key, api_secret=api_secret)
# Execute Order
logger.info("Initializing order placement from CLI")
response_data = execute_order(client, params)
# Display success
logger.info(f"Order successful. OrderID: {response_data.get('orderId')}")
print_response(success=True, data=response_data)
except ValidationError as e:
logger.warning(f"Validation Error: {str(e)}")
print_response(success=False, error=str(e))
sys.exit(1)
except BinanceAPIError as e:
logger.error(f"API Error [{e.status_code}]: {e.error_code} - {str(e)}")
print_response(success=False, error=str(e))
sys.exit(1)
except Exception as e:
logger.exception("Unexpected error occurred.")
print_response(success=False, error="An unexpected error occurred. Check logs.")
sys.exit(1)
if __name__ == "__main__":
main()