-
Notifications
You must be signed in to change notification settings - Fork 1
/
APIAggregator_github.py
184 lines (168 loc) · 7.13 KB
/
APIAggregator_github.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
# -*- coding: utf8 -*-
import time
import hashlib
import hmac
import requests
import json
import os
#apikey and secretkey settings
Binance_apikey = os.environ['Binance_apikey']
Binance_secretkey = os.environ['Binance_secretkey']
OKEX_apikey = os.environ['OKEX_apikey']
OKEX_secretkey = os.environ['OKEX_secretkey']
BitMEX_apikey = os.environ['BitMEX_apikey']
BitMEX_secretkey = os.environ['BitMEX_secretkey']
BitMEX2_apikey = os.environ['BitMEX2_apikey']
BitMEX2_secretkey = os.environ['BitMEX2_secretkey']
Bibox_apikey = os.environ['Bibox_apikey']
Bibox_secretkey = os.environ['Bibox_secretkey']
IDEX_wallet = os.environ['IDEX_wallet']
Balance_array = ['','',0,0,0]*999
CoinPrice_array = ['',0]*9999
ETHPrice = 0
# coinmarketcap coin price getter
j=0
for i in range(0,99):
url = "https://api.coinmarketcap.com/v2/ticker/?start=" + str(1+i*100) + "&limit=100"
CoinPrice_response = json.loads(requests.get(url).text)["data"]
try:
if len(CoinPrice_response) == 0 :
break
except:
break
for key in CoinPrice_response:
symbol = CoinPrice_response[key]["symbol"]
bal = CoinPrice_response[key]["quotes"]["USD"]["price"]
CoinPrice_array[j] = [symbol, bal]
if symbol == "ETH": ETHPrice = bal
j=j+1
CoinPrice_num = j
CoinPrice_array = CoinPrice_array[0:CoinPrice_num]
# Binance
def create_sha256_signature(secret, message):
bsecret = bytes(secret , 'utf8')
bmessage = bytes(message , 'utf8')
return hmac.new(bsecret, bmessage, hashlib.sha256).hexdigest()
Binance_headers = {'X-MBX-APIKEY': str(Binance_apikey)}
Binance_currentTime = str(int(time.time()*1000))
Binance_params = "timestamp=" + Binance_currentTime
Binance_params = {'timestamp': str(Binance_currentTime), 'signature':create_sha256_signature(Binance_secretkey,Binance_params)}
Binance_response = requests.get("https://api.Binance.com/api/v3/account", params=Binance_params, headers=Binance_headers)
Binance_balance = json.loads(Binance_response.text)['balances']
j = 0
for i in range(0,len(Binance_balance)-1):
bal = float(Binance_balance[i]['free'])
bal = bal + float(Binance_balance[i]['locked'])
if bal > 0:
symbol = Binance_balance[i]['asset']
for k in range(0,CoinPrice_num):
if CoinPrice_array[k][0] == symbol:
price = CoinPrice_array[k][1]
usdvalue = price * bal
break
Balance_array[j] = ['Binance',symbol, bal, price, usdvalue]
j=j+1
# OKEX
def buildMySignOKEX(params,secretKey):
sign = ''
for key in sorted(params.keys()):
sign += key + '=' + str(params[key]) +'&'
return hashlib.md5((sign+'secret_key='+secretKey).encode("utf8")).hexdigest().upper()
OKEX_params = {'api_key': str(OKEX_apikey)}
OKEX_params = {'api_key': str(OKEX_apikey), 'sign': str(buildMySignOKEX(OKEX_params, OKEX_secretkey))}
OKEX_response = requests.post("https://www.okex.com/api/v1/userinfo.do", data=OKEX_params)
OKEX2_response = requests.post("https://www.okex.com/api/v1/wallet_info.do", data=OKEX_params)
OKEX_balance_borrow = json.loads(OKEX_response.text)['info']['funds']['borrow']
OKEX_balance_free = json.loads(OKEX_response.text)['info']['funds']['free']
OKEX_balance_freezed = json.loads(OKEX_response.text)['info']['funds']['freezed']
OKEX2_balance_holds = json.loads(OKEX2_response.text)['info']['funds']['holds']
OKEX2_balance_free = json.loads(OKEX2_response.text)['info']['funds']['free']
for key in OKEX_balance_borrow:
bal = float(OKEX_balance_borrow[key]) + float(OKEX_balance_free[key]) + float(OKEX_balance_freezed[key])
try:
bal = bal + float(OKEX2_balance_free[key]) + float(OKEX2_balance_holds[key])
except:
pass
if bal > 0:
symbol = key.upper()
for k in range(0,CoinPrice_num):
if CoinPrice_array[k][0] == symbol:
price = CoinPrice_array[k][1]
usdvalue = price * bal
break
Balance_array[j] = ['OKEX',symbol, bal, price, usdvalue]
j=j+1
# BitMEX
BitMEX_url = "https://www.BitMEX.com/api/v1/user/margin"
BitMEX_verb = "GET"
BitMEX_path = "/api/v1/user/margin"
BitMEX_expires = str(int(time.time())+5)
BitMEX_data = ""
BitMEX_signature = create_sha256_signature(BitMEX_secretkey, BitMEX_verb + BitMEX_path + BitMEX_expires + BitMEX_data)
BitMEX_headers = {'api-expires':str(BitMEX_expires), 'api-key':str(BitMEX_apikey), 'api-signature':BitMEX_signature}
BitMEX_response = requests.get(BitMEX_url, headers=BitMEX_headers)
symbol = "BTC"
bal1 = float(json.loads(BitMEX_response.text)['marginBalance'])
BitMEX2_url = "https://www.BitMEX.com/api/v1/user/margin"
BitMEX2_verb = "GET"
BitMEX2_path = "/api/v1/user/margin"
BitMEX2_expires = str(int(time.time())+5)
BitMEX2_data = ""
BitMEX2_signature = create_sha256_signature(BitMEX2_secretkey, BitMEX2_verb + BitMEX2_path + BitMEX2_expires + BitMEX2_data)
BitMEX2_headers = {'api-expires':str(BitMEX2_expires), 'api-key':str(BitMEX2_apikey), 'api-signature':BitMEX2_signature}
BitMEX2_response = requests.get(BitMEX2_url, headers=BitMEX2_headers)
bal2 = float(json.loads(BitMEX2_response.text)['marginBalance'])
bal = (bal1 + bal2) / 100000000
for k in range(0,CoinPrice_num):
if CoinPrice_array[k][0] == symbol:
price = CoinPrice_array[k][1]
usdvalue = price * bal
break
Balance_array[j] = ['BitMEX',symbol, bal, price, usdvalue]
j=j+1
# Bibox
def create_md5_signature(secret, message):
bsecret = bytes(secret , 'utf8')
bmessage = bytes(message , 'utf8')
return hmac.new(bsecret, bmessage, hashlib.md5).hexdigest()
Bibox_cmds = '[{"cmd":"transfer/coinList", "body":{}}]'
Bibox_sign = create_md5_signature(Bibox_secretkey, Bibox_cmds)
Bibox_params = {"cmds": Bibox_cmds,"apikey":Bibox_apikey,"sign":Bibox_sign}
Bibox_response = requests.post("https://api.bibox.com/v1/transfer", data=Bibox_params)
Bibox_balance = json.loads(Bibox_response.text)
for i in range(0,999):
try:
bal = float(Bibox_balance['result'][0]['result'][i]['totalBalance'])
if bal > 0:
symbol = Bibox_balance['result'][0]['result'][i]['symbol']
for k in range(0,CoinPrice_num):
if CoinPrice_array[k][0] == symbol:
price = CoinPrice_array[k][1]
usdvalue = price * bal
break
Balance_array[j] = ['Bibox',symbol, bal, price, usdvalue]
j=j+1
except:
break
# IDEX
IDEX_params = {"address" : str(IDEX_wallet)}
IDEX_response = requests.post("https://api.idex.market/returnCompleteBalances", json=IDEX_params)
IDEX_balance = json.loads(IDEX_response.text)
for key in IDEX_balance:
bal = float(IDEX_balance[key]["available"]) + float(IDEX_balance[key]["onOrders"])
symbol = key
for k in range(0,CoinPrice_num):
if CoinPrice_array[k][0] == symbol:
price = CoinPrice_array[k][1]
usdvalue = price * bal
break
Balance_array[j] = ['IDEX',symbol, bal, price, usdvalue]
j=j+1
# sum up
Balance_num = j
Balance_array = Balance_array[0:Balance_num]
print(Balance_array)
sum_usdvalue=0
for i in range(0,len(Balance_array)):
sum_usdvalue = sum_usdvalue+Balance_array[i][4]
print(sum_usdvalue / ETHPrice)