-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrders.py
268 lines (228 loc) · 6.5 KB
/
Orders.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
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
import config
from numpy import *
import math
from decimal import *
from binance.exceptions import BinanceAPIException as e
from Messages import Messages
from Balance import Balance
import time
#Define API keys
client = config.bclient
class Orders:
#Join assets into pair variable
@staticmethod
def pair(asseta, assetb):
pair = asseta + assetb
return pair
@staticmethod
def exchangeinfo(pair):
try:
exchangeinfo = client.get_exchange_info()
if pair != "":
for market in exchangeinfo['symbols']:
if market["symbol"] == pair:
return market
return exchangeinfo
except e:
print(e.message)
return
# Get Market Price for Given Pair
@staticmethod
def getpairprice(pair):
try:
prices = client.get_ticker()
for i in prices:
if i['symbol'] == pair:
price = float(i['lastPrice'])
return price
except e:
print(e.status_code)
print(e.message)
exit()
@staticmethod
def maxusquantity(balance, market, pairinfo):
# Get Max quantity and lot size for USDT buy order
quantity = round(float(balance), 2) / float(market)
#print(type(quantity))
#print('max buy qty', quantity)
for filt in pairinfo['filters']:
if filt['filterType'] == 'LOT_SIZE':
#print(filt['stepSize'])
ticks = filt['stepSize'].find('1') - 1
#print('Ticks', ticks)
ticks = float(ticks)
quantity = math.floor(quantity * 10 ** ticks) / float(10 ** ticks)
#print('Quantity', quantity)
percent = (quantity * 0.2) / 100
quantity = Balance.format(quantity - percent)
#print('New quantity is', quantity)
# Set precision for BTC Trades
quantity = Decimal(str(quantity)).quantize(Decimal('.000001'), ROUND_DOWN)
#print('max buy order',quantity)
return float(quantity)
@staticmethod
def maxcoinquantity(asset):
balance = client.get_asset_balance(asset)
balance = float(balance['free'])
qty = Balance.format(balance)
qty = float(qty)
percent = (float(qty) * 0.2) / 100
#print('percent', type(percent))
quantity = qty - percent
#print(type(quantity))
quantity = Balance.format(quantity)
return float(quantity)
# Get minimal Amount to sell order on ETH trade ETHBTC Pair
@staticmethod
def minamount1(pair, price, symbolinfo):
minord = symbolinfo['filters'][2]['minNotional']
minorder1 = float(minord)
minorder1 = minorder1/price
minorder1 = float(("%.8f" % minorder1))
minorder1 = Orders.lotamount(minorder1, symbolinfo)
print('Minimum order for ',pair,' is:',minorder1)
return minorder1
@staticmethod
def lotamount(balance, symbolinfo):
coins_available = balance
for filt in symbolinfo['filters']:
if filt['filterType'] == 'LOT_SIZE':
ticks = filt['stepSize'].find('1') - 1
#print('Tick size', ticks)
order_quantity = math.floor(coins_available * 10 ** ticks) / float(10 ** ticks)
return order_quantity
# Get minimal Amount to buy order on USDT for ETHUSDT trade
@staticmethod
def pricerange(balance, symbolinfo):
coins_available = float(balance)
for filt in symbolinfo['filters']:
if filt['filterType'] == 'PRICE_FILTER':
ticks = int(filt['minPrice'].find('1') - 1)
#print('Tick size', ticks)
#print('%.*f' % (ticks, coins_available))
minprice = math.floor(coins_available * 10 ** ticks) / float(10 ** ticks)
return minprice
@staticmethod
def buylimitorder(pair, quantity, mrktprc):
try:
results = {}
percentage = (mrktprc * 0.2) / 100
percentage = float(("%.8f" % percentage))
orderprice = mrktprc - percentage
orderprice = ("%.2f" % orderprice)
#print('Market', mrktprc, 'with order', orderprice)
#print('Quantity:',quantity)
orderId = client.order_limit_buy(
symbol=pair,
quantity=quantity,
price=orderprice)
print('==========Buying=================')
print('Market price right now is:', mrktprc)
print('Trying to buy order for:', orderprice)
results['orderId'] = int(orderId['orderId'])
results['orderprice'] = orderprice
if 'msg' in orderId:
Messages.get(orderId['msg'])
print(orderId['msg'])
# Buy order created
return results
except e:
print('buylimitorder error')
print(e.status_code)
print(e.message)
return
@staticmethod
def selllimitorder(pair, quantity, mrktprc):
try:
results = {}
percentage = (mrktprc * 0.15) / 100
percentage = float(("%.8f" % percentage))
orderprice = mrktprc + percentage
orderprice = ("%.2f" % orderprice)
orderId = client.order_limit_sell(
symbol=pair,
quantity=quantity,
price=orderprice)
print('============Selling==============')
print('Market price right now is:',mrktprc)
print('Trying to sell order for:',orderprice)
#print('OrderId is:', orderId['orderId'])
results['orderId'] = int(orderId['orderId'])
results['orderprice'] = orderprice
print(results['orderId'])
if 'msg' in orderId:
Messages.get(orderId['msg'])
print(orderId['msg'])
# Buy order created
return results
except e:
print('selllimitorder error')
print(e.status_code)
print(e.message)
return
@staticmethod
def buymarketorder(pair, quantity):
try:
orderId = client.order_market_buy(
symbol=pair,
quantity=quantity)
#print('Buy orderId is:',orderId['status'])
if 'msg' in orderId:
Messages.get(orderId['msg'])
# Buy order created
return float(orderId['price'])
except e:
print(e.status_code)
print(e.message)
return
@staticmethod
def sellmarketorder(pair, quantity):
try:
orderId = client.order_market_sell(
symbol=pair,
quantity=quantity)
#print ('OrderId is:',orderId['orderId'])
if 'msg' in orderId:
Messages.get(orderId['msg'])
# Buy order created
return float(orderId['price'])
except e:
print(e.status_code)
print(e.message)
return
@staticmethod
def cancelorder(pair, orderId):
try:
order = client.cancel_order(
symbol=pair,
orderId=orderId)
if 'msg' in order:
Messages.get(order['msg'])
#print('Order didnt go through so its cancelled')
return order#['clientOrderId']
except e:
print(e.status_code)
print(e.message)
@staticmethod
def checkorderstatus(pair, orderId):
order = client.get_order(
symbol=pair,
orderId=orderId)
#print(order)
#print('Order Status',order['status'])
return order['status']
@staticmethod
def allorders():
try:
order = client.get_open_orders()
'''for i in order:
print(i['symbol'])
items = int(i[0])
print(items)'''
if 'msg' in order:
Messages.get(order['msg'])
return False
return order
except e:
print('Status Code',e.status_code)
print('Message',e.message)