forked from jasonsu0211230/crytoTrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.py
129 lines (114 loc) · 4.71 KB
/
function.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
sig_eth_balance = encrypt('GET|/api/v2/account|access_key='+api_key_otc+'¤cy=eth',api_secret_otc)
sig_usdt_balance = encrypt('GET|/api/v2/account|access_key='+api_key_otc+'¤cy=usdt',api_secret_otc)
sig_order = encrypt('GET|/api/v2/orders|access_key='+api_key_otc+'&market=ethusdt',api_secret_otc)
def get_price(exchange):
if exchange=='otc':
payload = {
'market':'ethusdt',
'limit' : 1
}
eth_usdt_depth = dict()
try:
res = requests.get('https://bb.otcbtc.com/api/v2/depth',params=payload)
eth_usdt_depth = json.loads(res.text)
except:
print('loads error')
return 1
if 'bids' in eth_usdt_depth:
data_dict['otc']['buy1_price'] = eth_usdt_depth['bids'][0][0]
data_dict['otc']['buy1_quntity'] = eth_usdt_depth['bids'][0][1]
if 'asks' in eth_usdt_depth:
data_dict['otc']['sell1_price'] = eth_usdt_depth['asks'][0][0]
data_dict['otc']['sell1_quntity'] = eth_usdt_depth['asks'][0][1]
if exchange=='hit':
try:
order = client_hit.get_orderbook('ETHUSD')
except:
print('hit get price error')
return 1
data_dict['hit']['buy1_price'] = order['bid'][0]['price']
data_dict['hit']['buy1_quntity'] = order['bid'][0]['size']
data_dict['hit']['sell1_price'] = order['ask'][0]['price']
data_dict['hit']['sell1_quntity'] = order['ask'][0]['size']
return 0
def get_eth_balance(exchange):
if exchange=='otc':
payload= {'access_key':api_key_otc,
'currency':'eth',
'signature':sig_eth_balance
}
e_dict = dict()
try:
res = requests.get('https://bb.otcbtc.com/api/v2/account',params=payload)
e_dict = json.loads(res.text)
except:
print('loads error')
return 1
data_dict['otc']['eth_balance'] = e_dict['balance']
if exchange=='hit':
eth_balance = 0.0
try:
balances = client_hit.get_trading_balance()
except:
print('otc error')
return 1
for balance in balances:
if balance['currency'] == 'ETH':
eth_balance = float(balance['available'])
data_dict['hit']['eth_balance'] = eth_balance
return 0
def get_usdt_balance(exchange):
if exchange=='otc':
payload= {'access_key':api_key_otc,
'currency':'usdt',
'signature':sig_usdt_balance
}
e_dict = dict()
try:
res = requests.get('https://bb.otcbtc.com/api/v2/account',params=payload)
e_dict = json.loads(res.text)
except:
print('loads error')
return 1
data_dict['otc']['usdt_balance'] = e_dict['balance']
if exchange=='hit':
usdt_balance = 0.0
try:
balances = client_hit.get_trading_balance()
except:
print('otc error')
return 1
for balance in balances:
if balance['currency'] == 'USDT':
usdt_balance = float(balance['available'])
data_dict['hit']['usdt_balance'] = usdt_balance
return 0
def get_order(exchange):
if exchange=='otc':
payload = {
'access_key':api_key_otc,
'signature':sig_order,
'market':'ethusdt'
}
eth_usdt_dict = dict()
try:
res = requests.get('https://bb.otcbtc.com/api/v2/orders',params=payload)
eth_usdt_dict = json.loads(res.text)
except:
print('loads error')
return 1
data_dict['otc']['order_now'][0]={'id':0,'buyorsell':'NULL','price':0,'quntity':0}
for index in range(len(eth_usdt_dict)):
data_dict['otc']['order_now'][index] = { 'id':eth_usdt_dict[index][u'id'],'buyorsell': eth_usdt_dict[index][u'side'],'price' :eth_usdt_dict[index][u'price'],'quntity' :eth_usdt_dict[index][u'volume'] }
print data_dict['otc']['order_now'][0]
if exchange=='hit':
try:
o = client_hit.get_active_order('ETHUSD')
except:
print 'hit order error'
return 1
data_dict['hit']['order_now'][0]={'id':0,'buyorsell':'NULL','price':0,'quntity':0}
for index in range(len(o)):
data_dict['hit']['order_now'][index] = {'id':o[index]['clientOrderId'],'buyorsell':o[index]['side'],'price':o[index]['price'],'quntity':o[index]['quantity']}
print data_dict['hit']['order_now'][0]
return 0