-
Notifications
You must be signed in to change notification settings - Fork 16
/
Binance_Spot_Order_Helper.py
112 lines (92 loc) · 2.97 KB
/
Binance_Spot_Order_Helper.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
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
import ccxt
import json
from pprint import pprint
from ccxt import binance
print('CCXT Version:', ccxt.__version__)
exchange = ccxt.binance({
'apiKey': '',
'secret': '',
'enableRateLimit': True, # https://github.com/ccxt/ccxt/wiki/Manual#rate-limit
'options': {
'defaultType': 'spot',
},
})
exchange.set_sandbox_mode(True) # comment if you're not using the testnet
markets = exchange.load_markets()
exchange.verbose = True # debug output
balance = exchange.fetch_balance()
pprint(balance)
usdt = 0.0
for i in balance.items():
#print(i)
#print("i[0]", i[0])
#print("i[1]", i[1])
if i[0] == 'free':
print(i[1])
usdt = (i[1]['USDT'])
print("Current balance in USDT", usdt)
print("Current market items")
print("Searching if BTC/USDT is available for trading")
btcusdt_found = False
print(exchange.markets.items())
for line in exchange.markets.items():
print("line", line)
if line[0] == "BTC/USDT":
print("BTC/USDT found (available for trading)")
btcusdt_found = True
break
if btcusdt_found is False:
exit(-1)
#exit(-2)
btcusdt_price = 0.0
quantity_to_buy = 0
usdt_to_invest = 100
print("Getting price for BTC/USDT")
print(exchange.fetch_ticker("BTC/USDT"))
ticker = exchange.fetch_ticker("BTC/USDT")
print("ticker", ticker)
print(ticker["symbol"], "sell price", ticker["bid"], "buy price", ticker["ask"], "close price", ticker["close"])
btcusdt_price = ticker["ask"]
print("Buy price for BTC/USDT", btcusdt_price)
quantity_to_buy = usdt_to_invest/btcusdt_price
print("Quantity of BTC/USDT to buy for ", usdt_to_invest, "usdt", "=", quantity_to_buy)
print("End getting price for BTC/USDT")
#exit(-3)
#markets = binance.load_markets()
#print(markets)
symbol = 'BTC/USDT'
type = 'market' # or 'market'
side = 'buy' # or 'buy'
amount = quantity_to_buy
price = None # or None
# extra params and overrides if needed
params = {
'test': False, # test if it's valid, but don't actually place it
}
print("Order sending")
order = exchange.create_order(symbol, type, side, amount, price, params)
print("Order sent")
print("Order details")
print(order)
print("Details of the executed order received from server:")
origQty = order["info"]["origQty"]
executedQty = order["info"]["executedQty"]
print("origQty", origQty, "executedQty", executedQty)
status = order["info"]["status"]
print("status received from the server", status)
trades = order["trades"]
fees_paid = 0.0
fees_currency = ""
for line in trades:
print("line", line["fees"])
for subline in line["fees"]:
fees_paid = subline["cost"]
fees_currency = subline["currency"]
print("fees paid", fees_paid, "fees currency", fees_currency)
break
if origQty == executedQty:
print("Quantity asked has been ok for the server")
elif origQty > executedQty:
print("Quantity asked is greater than the one the server is telling us")
elif origQty < executedQty:
print("Quantity asked is lower than the one the server is telling us")