-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbTrade.py
60 lines (57 loc) · 2.57 KB
/
bTrade.py
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
from binance.client import Client
from auth.binance_auth import *
from binance.exceptions import BinanceAPIException
from binance.enums import *
def cancelOrder(client) :
try :
orders = client.get_open_margin_orders(symbol='XRPUSDT')
# Filter open orders to include only sell orders
open_orders = [order for order in orders if order['side'] == 'BUY']
open_orders.sort(key=lambda x: x['time'])
if open_orders:
# Extract the oldest order
oldest_order_id = open_orders[0]['orderId']
# Cancel the oldest order
cancel_response = client.cancel_order(symbol=symbol, orderId=oldest_order_id)
print(f"Oldest order (ID: {oldest_order_id}) has been canceled.")
else:
print("No open orders found to cancel.")
except BinanceAPIException as e:
# Extract and print only the error code
error_code = e.code
print(f"An error occurred: {error_code}")
def getInfo():
# Initialize Binance client
client = load_binance_creds('auth/auth.yml')
# Fetch margin account balance
account_info = client.get_margin_account()
# Filter assets for USDT and XRP
assets_to_check = ['USDT', 'XRP']
filtered_assets = [asset for asset in account_info['userAssets'] if asset['asset'] in assets_to_check]
# Print balance for filtered assets
for asset in filtered_assets:
print(f"Asset: {asset['asset']}, Borrowed: {asset['borrowed']}, Free: {asset['free']}, Interest: {asset['interest']}, Locked: {asset['locked']}")
def bTrade(midPrice) :
# Initialize Binance client
client = load_binance_creds('auth/auth.yml')
# Define trade parameters
symbol = 'XRPUSDT' # Trading pair
side = 'BUY' # Buy or sell
quantity = 20 # Quantity of XRP to buy
price = round(midPrice , 4) # None for market order, specify price for limit order
leverage = 3 # Leverage level (e.g., 3x leverage)
# Place margin trade
try :
order = client.create_margin_order(
symbol=symbol,
side=SIDE_BUY,
type=ORDER_TYPE_LIMIT,
timeInForce=TIME_IN_FORCE_GTC,
quantity=quantity,
price=price
)
print("Type of order : " + str(order['side']) + "\nValue of order : " + str(order['price']))
except BinanceAPIException as e:
if e.code == -2010 :
cancelOrder(client)
print(f"An error occurred: {e}")