-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderbookDepthBot.py
More file actions
85 lines (68 loc) · 2.6 KB
/
orderbookDepthBot.py
File metadata and controls
85 lines (68 loc) · 2.6 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
orderbookDepths = [.05, .5]
def initPoloConnection():
from poloniex import Poloniex
return Poloniex()
def getCoinNames():
api = initPoloConnection()
coinList = []
coins = api.return24hVolume()
for market in coins:
if "BTC_" in market:
coinList.append(market)
return coinList
def getBidDepthRatio(orderbook):
bids, asks = [orderbook["bids"], orderbook["asks"]]
price = (float(bids[0][0]) + float(asks[0][0])) / 2
totalBidVol = sum([float(bid[1]) * float(bid[0]) for bid in bids])
volsAtDepths = [totalBidVol * orderbookDepths[0], totalBidVol * orderbookDepths[1]]
secondLevel = False
bidVol = 0
pricesAtDepths = []
for bid in bids:
if (not secondLevel) and bidVol >= volsAtDepths[0]:
pricesAtDepths.append(float(bid[0]))
secondLevel = True
if secondLevel and bidVol >= volsAtDepths[1]:
pricesAtDepths.append(float(bid[0]))
break
bidVol += float(bid[1]) * float(bid[0])
pricesAtDepthsRatio = pricesAtDepths[0]/pricesAtDepths[1]
return pricesAtDepthsRatio
def getAskDepthRatio(orderbook):
bids, asks = [orderbook["bids"], orderbook["asks"]]
price = (float(bids[0][0]) + float(asks[0][0])) / 2
totalAskVol = sum([float(ask[1]) * price for ask in asks])
volsAtDepths = [totalAskVol * orderbookDepths[0], totalAskVol * orderbookDepths[1]]
secondLevel = False
askVol = 0
pricesAtDepths = []
for ask in asks:
if (not secondLevel) and askVol >= volsAtDepths[0]:
pricesAtDepths.append(float(ask[0]))
secondLevel = True
if secondLevel and askVol >= volsAtDepths[1]:
pricesAtDepths.append(float(ask[0]))
break
askVol += float(ask[1]) * price
pricesAtDepthsRatio = pricesAtDepths[1]/pricesAtDepths[0]
return pricesAtDepthsRatio
def getCoinDepthRatio(pair):
api = initPoloConnection()
orderbook = api.returnOrderBook(pair, depth=10000000)
bidDepthRatio = getBidDepthRatio(orderbook)
askDepthRatio = getAskDepthRatio(orderbook)
averageDepthRatio = askDepthRatio * bidDepthRatio
return averageDepthRatio
def getAllCoinDepthRatios():
coinRatios = {}
coinNames = getCoinNames()
for coin in coinNames:
coinRatios[coin] = getCoinDepthRatio(coin)
return coinRatios
def printAllCoinRatios():
from operator import itemgetter
allCoinRatios = getAllCoinDepthRatios()
for coin in sorted(allCoinRatios.items(), key=itemgetter(1)):
print(coin[0].replace("BTC_", "").lower() + ": " + str(allCoinRatios[coin[0]]))
if __name__ == "__main__":
printAllCoinRatios()