-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoconvert-kraken-XMR-BTC-to-USD.py
72 lines (58 loc) · 2.09 KB
/
autoconvert-kraken-XMR-BTC-to-USD.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
import urllib.parse
import hashlib
import hmac
import base64
import time
import os
import requests
os.environ['API_KEY_KRAKEN'] = '' # This is your "API Key"
os.environ['API_SEC_KRAKEN'] = '' # This is your Kraken "Private key" NOT
# YOUR MONERO PRIVATE KEY
api_url = "https://api.kraken.com"
api_key = os.environ['API_KEY_KRAKEN']
api_sec = os.environ['API_SEC_KRAKEN']
def get_kraken_signature(urlpath, data, secret):
postdata = urllib.parse.urlencode(data)
encoded = (str(data['nonce']) + postdata).encode()
message = urlpath.encode() + hashlib.sha256(encoded).digest()
mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
sigdigest = base64.b64encode(mac.digest())
return sigdigest.decode()
def kraken_request(uri_path, data, api_key, api_sec):
headers = {}
headers['API-Key'] = api_key
headers['API-Sign'] = get_kraken_signature(uri_path, data, api_sec)
req = requests.post((api_url + uri_path), headers=headers, data=data)
return req
# https://docs.kraken.com/rest/#tag/User-Data/operation/getAccountBalance
resp = kraken_request('/0/private/Balance', {
"nonce": str(int(1000*time.time()))
}, api_key, api_sec)
data = resp.json()
# https://docs.kraken.com/rest/#tag/User-Trading/operation/addOrder
try:
amount_to_sell_xmr = data['result']['XXMR']
except KeyError:
print('There is no XMR balance')
else:
amount_to_sell_xmr = data['result']['XXMR']
resp = kraken_request('/0/private/AddOrder', {
"nonce": str(int(1000*time.time())),
"ordertype": "market",
"type": "sell",
"volume": amount_to_sell_xmr,
"pair": "XMRUSD"
}, api_key, api_sec)
try:
amount_to_sell_btc = data['result']['XXBT']
except KeyError:
print('There is no BTC balance')
else:
amount_to_sell_btc = data['result']['XXBT']
resp = kraken_request('/0/private/AddOrder', {
"nonce": str(int(1000*time.time())),
"ordertype": "market",
"type": "sell",
"volume": amount_to_sell_btc,
"pair": "XBTUSD"
}, api_key, api_sec)