CryptoBot/api/kraken.py

48 lines
2.1 KiB
Python
Raw Normal View History

2021-01-31 03:15:56 -05:00
from pykrakenapi import KrakenAPI
import pandas
import krakenex
import time
class Kraken:
def __init__(self):
self.krakenAPI = krakenex.API()
self.krakenAPI.load_key("kraken.key")
self.krakenWrapper = KrakenAPI(self.krakenAPI)
2021-02-01 22:34:35 -05:00
self.getBalances()
2021-01-31 03:15:56 -05:00
# Updates the current wallet prices from the internet
2021-02-01 00:19:16 -05:00
def getBalances(self):
2021-01-31 03:15:56 -05:00
__newBalances = self.krakenWrapper.get_account_balance()
2021-01-31 16:17:25 -05:00
#print(self.balances.loc["XXBT","vol"]-__newBalances.loc["XXBT","vol"])
#print(self.balances.loc["XXDG","vol"]-__newBalances.loc["XXDG","vol"])
#print(self.balances.loc["ZUSD","vol"]-__newBalances.loc["ZUSD","vol"])
2021-01-31 03:15:56 -05:00
self.balances = __newBalances
2021-01-31 16:17:25 -05:00
# Gets a table of the price history of the pair since time at intervals of interval minutes
def getPrices(self, pair, time=round(time.time())-(30*24*3600), interval=60):
self.prices = self.krakenWrapper.get_ohlc_data(pair, interval=interval, since=time)[0].close.iloc[::-1]
2021-01-31 03:15:56 -05:00
2021-01-31 16:17:25 -05:00
# Calculates the exponential moving average by grabbing data from Kraken on the conversion rate every interval minutes.
2021-01-31 03:15:56 -05:00
def calculateEMA(self, shortTime=round(time.time())-(7*24*3600), longTime=round(time.time())-(30*24*3600), interval=60):
2021-02-01 22:34:35 -05:00
self.longEMA = self.prices.ewm(span=12).mean()
self.shortEMA = self.prices.ewm(span=3).mean()
2021-01-31 03:15:56 -05:00
2021-02-01 23:37:04 -05:00
def buyOrder(self, currency_pair, currency_buy, currency_sell, amount, ordertype="market"):
2021-02-02 17:48:04 -05:00
self.krakenWrapper.add_standard_order(currency_pair, "buy", ordertype, volume=amount, validate=False)
#self.balances["vol"][currency_buy] += amount
#self.balances["vol"][currency_sell] -= amount * self.prices.tail(1)[0]
time.sleep(1)
self.getBalances()
2021-01-31 03:15:56 -05:00
2021-02-01 23:37:04 -05:00
def sellOrder(self, currency_pair, currency_buy, currency_sell, amount, ordertype="market"):
2021-02-02 17:48:04 -05:00
self.krakenWrapper.add_standard_order(currency_pair, "sell", ordertype, volume=amount, validate=False)
#self.balances["vol"][currency_buy] -= amount
#self.balances["vol"][currency_sell] += amount * self.prices.tail(1)[0]
time.sleep(1)
self.getBalances()
2021-01-31 03:15:56 -05:00