-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtcxindiabot.py
More file actions
368 lines (331 loc) · 13.9 KB
/
Copy pathbtcxindiabot.py
File metadata and controls
368 lines (331 loc) · 13.9 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import urllib,urllib2
import requests
import json
import time
import hashlib
import hmac
import logging
import config
import base64
class Btcxindia:
def __init__(self):
#config variables
self.apikey = config.apikey
self.apisecret = config.apisecret
self.clientid = config.clientid
#ticker variables
self.high = 0
self.low = 0
self.avg = 0
self.totalvolume24 = 0
self.currentvolume = 0
self.lasttradedprice = 0
self.lasttradedtime = 0
self.bid = 0
self.ask = 0
#hourly ticker variables
self.high_hour = 0
self.low_hour = 0
self.avg_hour = 0
self.totalvolumehour = 0
#orderbook variables
self.orderbooktime = 0
self.orderbookbids = []
self.orderbookasks = []
self.trades = []
#balance variables
self.balanceinr = -1
self.balancebtc = -1
self.balancetradefee = -1
self.balance_error = -1
#pending orders variables
self.pending_orders = []
def trade(self,buyorsell,volume,price):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce,
"type":buyorsell,
"volume":volume,
"price":price}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/trade/',parameters)
resp=json.load(response)
print resp
if resp[u'status']=='success':
trade_orderid = resp[u'order_id']
print resp
logging.info('btcxindia TRADE CONNECTION SUCCESSFUL')
response.close()
return trade_orderid
else:
print 'RESPONSE STATUS = ERROR'
return -1
except:
etype, foo, traceback = sys.exc_info()
print 'foo %s' % foo
def tradehistory(self,buyorsell,volume,price):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce,
"type":buyorsell,
"volume":volume,
"price":price}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/trade/',parameters)
resp=json.load(response)
print resp
if resp[u'status']=='success':
trade_orderid = resp[u'order_id']
print resp
logging.info('btcxindia TRADE CONNECTION SUCCESSFUL')
response.close()
return trade_orderid
else:
print 'RESPONSE STATUS = ERROR'
return -1
except:
etype, foo, traceback = sys.exc_info()
print 'foo %s' % foo
def balance(self):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/ac_balance/',parameters)
resp = json.load(response)
if response.getcode()==200:
if resp[u'status'] == 'success':
logging.info('btcxindia BALANCE CHECK SUCCESSFUL')
response.close()
return resp[u'inr'],resp[u'btc'],resp[u'pending_inr'],resp[u'pending_btc']
else:
return -1,-1,-1,-1
except urllib2.URLError, e:
logging.warning('btcxindia BALANCE CHECK CONNECTION ERROR. Error Code:%r' % e)
print
print 'btcxindia BALANCE CHECK CONNECTION ERROR. Error Code:%r' % e
return -2,e,e,e
def pendingorders(self):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/my_pending_orders/',parameters)
resp=json.load(response)
if response.getcode()==200:
self.pending_orders = resp
logging.info('btcxindia fetch pending orders SUCCESSFUL')
response.close()
return [[x[u'order_id'],x[u'volume'],x[u'order_type'],x[u'inr_btc'],x[u'time']] for x in resp]
else:
return -1
except urllib2.URLError, e:
logging.warning('btcxindia fetch pending orders CONNECTION ERROR. Error Code: %r' % e)
print
print 'btcxindia fetch pending orders CONNECTION ERROR. Error Code:%r' % e
return -2
def cancelorder(self,orderid):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce,
"order_id":orderid}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/cancel_order/',parameters)
resp=json.load(response)
if response.getcode()==200:
print resp
logging.info('btcxindia CANCEL ORDER SUCCESSFUL')
response.close()
print
print resp
return resp[u'status']
else:
return -1
except urllib2.URLError, e:
logging.warning('btcxindia CANCEL ORDER CONNECTION ERROR. Error Code:%r' % e)
print
print 'btcxindia CANCEL ORDER CONNECTION ERROR. Error Code: + %r' % e
return -2
def transactions(self,typeoftransaction):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce,
"type":typeoftransaction}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/transactions/',parameters)
resp=json.load(response)
if response.getcode()==200:
#if self.typeoftransaction == 'trade':
#if self.typeoftransaction == 'deposit':
#if self.typeoftransaction == 'withdrawal':
logging.info('btcxindia transactions SUCCESSFUL')
print
print 'btcxindia transactions SUCCESSFUL'
response.close()
except urllib2.URLError, e:
logging.warning('btcxindia transactions CONNECTION ERROR. Error Code:%r' % e)
print
print 'btcxindia transactions CONNECTION ERROR. Error Code:%r' % e
def initiatewithdrawal(self,btcorinr,amount,address):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce,
"type":btcorinr,
"amount":amount,
"address":address}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/withdrawalinitiate/',parameters)
resp=json.load(response)
if response.getcode()==200:
logging.info('btcxindia withdrawal initiation successful SUCCESSFUL')
print
print 'btcxindia withdrawal initiation successful SUCCESSFUL'
response.close()
except urllib2.URLError, e:
logging.warning('btcxindia withdrawal initiation CONNECTION ERROR. Error Code:%r' % e)
print
print 'btcxindia withdrawal initiation CONNECTION ERROR. Error Code:%r' % e
def confirmwithdrawal(self,withdrawal):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce,
"withdrawal_code":withdrawalcode}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/withdrawal_confirm/',parameters)
resp=json.load(response)
if response.getcode()==200:
logging.info('btcxindia withdrawal confirmation SUCCESSFUL')
print
print 'btcxindia withdrawal confirmation SUCCESSFUL'
response.close()
except urllib2.URLError, e:
logging.warning('btcxindia withdrawal confirmation CONNECTION ERROR. Error Code:%r' % e)
print
print 'btcxindia withdrawal confirmation CONNECTION ERROR. Error Code:%r' % e
def wallet(self,btcorinr):
nonce = int(time.time())
Data = str(nonce) + self.apikey + self.clientid
signature = base64.b64encode(hmac.new(self.apisecret,Data,digestmod=hashlib.sha256).digest())
parameters = {"key":self.apikey,
"signature":signature,
"nonce":nonce,
"type":btcorinr}
parameters = urllib.urlencode(parameters)
try:
response = urllib2.urlopen('https://api.btcxindia.com/wallet/',parameters)
resp=json.load(response)
print
print resp
logging.info('btcxindia wallet check SUCCESSFUL')
print
print 'btcxindia wallet check SUCCESSFUL'
response.close()
except urllib2.URLError, e:
logging.warning('btcxindia wallet check CONNECTION ERROR. Error Code:%r' % e)
print
print 'btcxindia wallet check CONNECTION ERROR. Error Code:%r' % e
def ticker(self):
try:
response = urllib2.urlopen('https://api.btcxindia.com/ticker/')
resp=json.load(response)
self.high = resp[u'high']
self.low = resp[u'low']
self.avg = resp[u'avg']
self.totalvolume24 = resp[u'total_volume_24h']
self.currentvolume = resp[u'current_volume']
self.lasttradedprice = resp[u'last_traded_price']
self.lasttradedtime = resp[u'last_traded_time']
self.bid = resp[u'bid']
self.ask = resp[u'ask']
logging.info('btcxindia Ticker Update SUCCESSFUL')
response.close()
return 1
except urllib2.URLError, e:
print ("There was an error: %r" % e)
logging.warning('btcxindia Ticker CONNECTION ERROR. Error Code:')
return e
def hourlyticker(self):
try:
response = urllib2.urlopen('https://api.btcxindia.com/ticker_hour/')
resp=json.load(response)
self.high_hour = resp[u'high']
self.low_hour = resp[u'low']
self.avg_hour = resp[u'avg']
self.totalvolumehour = resp[u'total_volume_hour']
logging.info('btcxindia HourlyTicker Update SUCCESSFUL')
response.close()
return 1
except urllib2.URLError, e:
print ("There was an error: %r" % e)
logging.warning('btcxindia Hourly Ticker CONNECTION ERROR. Error Code:')
return e
def orderbook(self):
try:
response = urllib2.urlopen('https://api.btcxindia.com/order_book/')
resp=json.load(response)
self.orderbooktime = resp[u'timestamp']
self.orderbookbids = resp[u'bids']
self.orderbookasks = resp[u'asks']
logging.info('btcxindia Orderbook Update SUCCESSFUL')
response.close()
return 1
except urllib2.URLError, e:
logging.warning('btcxindia orderbook CONNECTION ERROR. Error Code:%r' % e)
return e
def tradesnow(self):
try:
response = urllib2.urlopen('https://api.btcxindia.com/trades')
resp=json.load(response)
self.trades = [[x[u'time'],x[u'volume'],x[u'price']] for x in resp]
logging.info('btcxindia Tradesnow Update SUCCESSFUL')
response.close()
return 1
except urllib2.URLError, e:
logging.warning('btcxindia Tradesnow CONNECTION ERROR. Error Code:%r' % e)
return e
def updatedata(self):
self.ticker()
self.hourlyticker()
self.orderbook()
self.tradesnow()
while True:
time.sleep(10)
self.ticker()
time.sleep(10)
self.hourlyticker()
time.sleep(10)
self.orderbook()
time.sleep(10)
self.tradesnow()