-
Notifications
You must be signed in to change notification settings - Fork 41
/
CoreFunctionality.py
229 lines (161 loc) · 6.83 KB
/
CoreFunctionality.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
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 4 03:53:20 2018
@author: Khera
"""
import time
import binance
from binance.client import Client
from binance.enums import *
portion = 0.01
minimumBnb = 0.03
def getMarketData(client, exclude):
tickers = client.get_orderbook_tickers()
for i in tickers:
if 'ETH' in i['symbol']:
if i['symbol'].index('ETH') != 0:
i['symbol'] = i['symbol'].replace('ETH','-ETH')
if 'BTC' in i['symbol']:
if i['symbol'].index('BTC') != 0:
i['symbol'] = i['symbol'].replace('BTC','-BTC')
if 'BNB' in i['symbol']:
if i['symbol'].index('BNB') != 0:
i['symbol'] = i['symbol'].replace('BNB','-BNB')
if 'USDT' in i['symbol']:
if i['symbol'].index('USDT') != 0:
i['symbol'] = i['symbol'].replace('USDT','-USDT')
for i in tickers:
if '-' not in i['symbol']:
if 'ETH' in i['symbol']:
if i['symbol'].index('ETH') != 0:
i['symbol'] = i['symbol'].replace('ETH','-ETH')
if 'BTC' in i['symbol']:
if i['symbol'].index('BTC') != 0:
i['symbol'] = i['symbol'].replace('BTC','-BTC')
if 'BNB' in i['symbol']:
if i['symbol'].index('BNB') != 0:
i['symbol'] = i['symbol'].replace('BNB','-BNB')
if 'USDT' in i['symbol']:
if i['symbol'].index('USDT') != 0:
i['symbol'] = i['symbol'].replace('USDT','-USDT')
for i in tickers:
if '123456' in i['symbol']:
tickers.remove(i)
if 'RPX' in i['symbol']:
tickers.remove(i)
if 'VEN' in i['symbol']:
tickers.remove(i)
if 'HSR' in i['symbol']:
tickers.remove(i)
# if exclude == True:
# if 'BNB' in i['symbol']:
# tickers.remove(i)
return tickers
def threeCurrencyArb(tickers, minimumPercentage, startingBal):
btc = []
found = False
for i in tickers:
# if 'ETH' in i['symbol']:
# eth.append(i)
if 'BTC' in i['symbol']:
btc.append(i)
# if 'BNB' in i['symbol']:
# bnb.append(i)
#
# if 'USDT' in i['symbol']:
# usdt.append(i)
currencies = [btc]
starting = startingBal
results = []
for i in currencies:
marker1 = ""
if i == 0:
marker1 = "BTC"
elif i == 1:
marker1 = "ETH"
elif i == 2:
marker1 = "BNB"
elif i == 3:
marker1 = "USDT"
for j in i:
jmarker = j['symbol'].split('-')
ask1 = j['askPrice']
ask1qty = j['askQty']
bid1 = j['bidPrice']
for k in tickers:
kmarker = k['symbol'].split('-')
if kmarker[0] == jmarker[0] and kmarker[1] != jmarker[1] and found == False:
ask2 = k['askPrice']
bid2 = k['bidPrice']
bid2qty = k['bidQty']
for l in tickers:
lmarker = l['symbol'].split('-')
ask3 = l['askPrice']
bid3 = l['bidPrice']
bid3qty = l['bidQty']
if lmarker[0] == kmarker[1] and lmarker[1] == jmarker[1] and found == False:
step1 = (starting*0.999)/float(ask1)
step2 = (step1*0.999)*float(bid2)
step3 = (step2*0.999)*float(bid3)
profitloss = (step3-starting)/starting
if profitloss > minimumPercentage:
percentageGain = (profitloss)*100
found = True
trade1 = [j['symbol'],ask1,bid1]
trade2 = [k['symbol'],ask2,bid2]
trade3 = [l['symbol'],ask3,bid3]
trade4 = ['NONE',0,0]
PL = [profitloss]
output= [trade1,trade2,trade3,trade4,PL]
results.append(output)
#print("Route: " + j['symbol'] +' -> '+ k['symbol'] + ' -> '+ l['symbol'] + ' has profit/loss of ' + str(percentageGain) +"%")
return results
def checkOpenOrders(client, market):
market = market.replace("-","")
orders = client.get_open_orders(symbol=market)
return orders
def getCoinBalance(client, currency):
balance = float(client.get_asset_balance(asset=currency)['free'])
return balance
def checkIntMarket(client, market):
info = client.get_symbol_info(market)
isInt = False
intmarket = info['filters'][1]['stepSize']
if float(intmarket) == 1:
isInt = True
elif intmarket !=1:
isInt = False
return isInt
def checkstep(client, market):
info = client.get_symbol_info(market)
stepSize = info['filters'][1]['stepSize']
return stepSize
def executeTrade(client, market, currentAsset, tradeType, tradePrice, firstTrade):
tradeMarket = market.replace("-","")
intmarket = checkIntMarket(client,tradeMarket)
balance = getCoinBalance(client, currentAsset)
if firstTrade == True:
balance = balance/portion
if tradeType == 0:#sell
if currentAsset == 'BNB':
qtySell = balance - minimumBnb
else:
qtySell = balance
if intmarket == True:
qtySell = int(qtySell)
elif currentAsset == 'ETH':
qtySell2 = float("%.3f" % qtySell)
if qtySell2>qtySell:
qtySell = "%.3f" % (float(qtySell)-0.0005)
else:
qtySell = "%.3f" % qtySell
else:
qtySell = "%.2f" % qtySell
order = client.order_market_sell(symbol=tradeMarket, quantity=qtySell)
if tradeType == 1:# buy
qtyBuy = balance/tradePrice
if intmarket == True:
qtyBuy = int(qtyBuy)
else:
qtyBuy = "%.2f" % qtyBuy
order = client.order_market_buy(symbol=tradeMarket,quantity=qtyBuy)