-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbittrex_api_driver.py
92 lines (70 loc) · 2.97 KB
/
bittrex_api_driver.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
# crypto alert bot
# report sharp price changes as well as daily updates via iphone app
# goal of this is simply to set up a bot that will call the bittrex api every 10 minutes
# for a price update on a range of coins and add those numbers to a database
import requests
import time
import hashlib
import hmac
class Bittrex:
# init with bittrex public and private keys
def __init__(self, api_key, secret):
self.api_key = api_key
self.secret_key = secret
# return status code, the most recent price of symbol from bittrex
# creates a GET request to the bittrex api for a symbol to the markets/{symbol}/trades endpoint
# described here: https://bittrex.github.io/api/v3
# good resource for understanding the encryption process: https://stackoverflow.com/questions/43559332/python-3-hash-hmac-sha512
def get_price(self, symbol):
# endpoint
url = "https://api.bittrex.com/v3/markets/" + symbol + "/trades"
# current time in milliseconds
cTime = int(time.time() * 1000)
cTime = str(cTime)
# empty hash value
hash = hashlib.sha512()
hash = hash.hexdigest()
# add all this together and encrypt with secret
preSign = cTime + url + "GET" + hash
# encrypt with secret key
postSign = hmac.new(self.secret_key.encode(), preSign.encode(), hashlib.sha512).hexdigest()
# json header as specified by bittrex
header = {
'Api-Key': self.api_key,
'Api-Timestamp': cTime,
'Api-Content-Hash': hash,
'Api-Signature': postSign,
}
# get request to bittrex endpoint
request = requests.get(url, headers = header)
if request.status_code != 200:
return request.status_code, None
# most recent price is first elt in list
return request.status_code, request.json()[0]['rate']
# return candle data for symbol
def get_candles(self, symbol):
# endpoint
url = "https://api.bittrex.com/v3/markets/" + symbol + "/candles/HOUR_1/recent"
# current time in milliseconds
cTime = int(time.time() * 1000)
cTime = str(cTime)
# empty hash value
hash = hashlib.sha512()
hash = hash.hexdigest()
# add all this together and encrypt with secret
preSign = cTime + url + "GET" + hash
# encrypt with secret key
postSign = hmac.new(self.secret_key.encode(), preSign.encode(), hashlib.sha512).hexdigest()
# json header as specified by bittrex
header = {
'Api-Key': self.api_key,
'Api-Timestamp': cTime,
'Api-Content-Hash': hash,
'Api-Signature': postSign,
}
# get request to bittrex endpoint
request = requests.get(url, headers = header)
if request.status_code != 200:
return request.status_code, None
# most recent price is first elt in list
return request.status_code, request.json()