This commit is contained in:
Eric Meehan 2024-08-24 16:59:09 -04:00
parent 367af65bfb
commit 83c519fa29
5 changed files with 70 additions and 0 deletions

3
.env_example Normal file
View File

@ -0,0 +1,3 @@
KRAKEN_URL = "https://api.kraken.com"
KRAKEN_API_TOKEN =
KRAKEN_API_SEC =

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*__pycache__*
venv/*
.env

52
Kraken.py Normal file
View File

@ -0,0 +1,52 @@
import base64
import hashlib
import hmac
import os
import requests
import time
import urllib.parse
class Kraken():
ASSET_PAIRS = {
'XBTUSD': 'XXBTZUSD'
}
def __init__(self):
self.api_url = 'https://api.kraken.com/'
self.api_token = os.getenv('KRAKEN_API_TOKEN')
self.api_sec = os.getenv('KRAKEN_API_SEC')
def _get_kraken_signature(self, urlpath, data, secret):
return base64.b64encode(
hmac.new(
base64.b64decode(secret),
urlpath.encode() + hashlib.sha256(
(str(data['nonce']) + urllib.parse.urlencode(data)).encode()
).digest(),
hashlib.sha512
).digest()).decode()
def _kraken_request(self, uri_path, data):
return requests.post(
(self.api_url + uri_path),
headers = {
'API-Key': self.api_sec,
'API-Sign': self._get_kraken_signature(uri_path, data, self.api_sec)
},
data=data
)
def get_account_balance(self):
return self._kraken_request('/0/private/Balance', {'nonce': str(int(1000*time.time()))})
def ohlc(self, asset_pair):
resp = requests.get(f'{self.api_url}/0/public/OHLC?pair={asset_pair}').json()
return {
'time': [int(each[0]) for each in resp['result'][self.ASSET_PAIRS[asset_pair]]],
'open': [float(each[1]) for each in resp['result'][self.ASSET_PAIRS[asset_pair]]],
'high': [float(each[2]) for each in resp['result'][self.ASSET_PAIRS[asset_pair]]],
'low': [float(each[3]) for each in resp['result'][self.ASSET_PAIRS[asset_pair]]],
'close': [float(each[4]) for each in resp['result'][self.ASSET_PAIRS[asset_pair]]],
'volume': [float(each[5]) for each in resp['result'][self.ASSET_PAIRS[asset_pair]]],
'count': [float(each[6]) for each in resp['result'][self.ASSET_PAIRS[asset_pair]]]
}

8
linear_regression.py Normal file
View File

@ -0,0 +1,8 @@
import numpy
from scipy import stats
def linear_regression(timestamps, values):
x = numpy.array(timestamps)
y = numpy.array(values)
return stats.linregress(x, y)

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
numpy
python-dotenv
requests
scipy