CryptoBot/api/kraken.py
2021-01-31 22:19:16 -07:00

43 lines
2.1 KiB
Python

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)
# Updates the current wallet prices from the internet
def getBalances(self):
__newBalances = self.krakenWrapper.get_account_balance()
#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"])
self.balances = __newBalances
# 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]
# Calculates the exponential moving average by grabbing data from Kraken on the conversion rate every interval minutes.
def calculateEMA(self, shortTime=round(time.time())-(7*24*3600), longTime=round(time.time())-(30*24*3600), interval=60):
#self.prices = self.krakenWrapper.get_ohlc_data("XXBTZUSD", interval=interval, since=longTime)[0].close.iloc[::-1]
self.longEMA = self.prices.ewm(span=1*24).mean()
self.shortEMA = self.prices.ewm(span=6).mean()
def buyOrder(self, pair, currency_buy, currency_sell, amount, type="market"):
#self.krakenWrapper.add_standard_order(currency_pair, "sell", type, volume=k.balances["vol"][currency_buy], validate=True)
self.balances["vol"][currency_buy] += amount
self.balances["vol"][currency_sell] -= amount * self.prices.tail(1)[0]
def sellOrder(self, pair, currency_buy, currency_sell, amount, type="market"):
#self.krakenWrapper.add_standard_order(currency_pair, "buy", type, volume=k.balances["vol"][currency_sell], validate=True)
self.balances["vol"][currency_buy] -= amount
self.balances["vol"][currency_sell] += amount * self.prices.tail(1)[0]