CryptoBot/api/kraken.py

53 lines
2.3 KiB
Python

from pykrakenapi import KrakenAPI
import pandas, krakenex, time, numpy
class Kraken:
def __init__(self):
self.kraken_api = krakenex.API()
self.kraken_api.load_key("kraken.key")
self.kraken_wrapper = KrakenAPI(self.kraken_api)
self.get_balances()
# Updates the current wallet prices from the internet
def get_balances(self):
self.balances = self.kraken_wrapper.get_account_balance()
# Gets a table of the price history of the pair since time at intervals of interval minutes
def get_ohlc_prices(self, pair, time=round(time.time())-(30*24*3600), interval=60):
self.prices = self.kraken_wrapper.get_ohlc_data(pair, interval=interval, since=time)[0].close.iloc[::-1]
# Gets the current trading price of the currency pair
def get_current_price(self, pair):
current_price = numpy.float64(self.kraken_wrapper.get_ticker_information(pair)["c"][0][0])
new_row = pandas.DataFrame([[current_price]], index=[pandas.to_datetime(time.time(), unit = "s")])
self.prices = pandas.concat([self.prices, pandas.DataFrame(new_row)], ignore_index=False)
def get_fee(self, pair):
self.fee = self.kraken_wrapper.get_tradable_asset_pairs(pair=pair)["fees"][0][0][1]
# Calculates the exponential moving average by grabbing data from Kraken on the conversion rate every interval minutes.
def calculate_ema(self, short_time=5, long_time=30):
self.long_ema = self.prices.ewm(span=long_time).mean()
self.short_ema = self.prices.ewm(span=short_time).mean()
# Buys crypto
def buy_order(self, pair, currency_buy, currency_sell, amount, order_type="market"):
self.kraken_wrapper.add_standard_order(pair, "buy", order_type, volume=amount, validate=True)
#self.balances["vol"][currency_buy] += amount
#self.balances["vol"][currency_sell] -= amount * self.prices.tail(1)[0]
time.sleep(1)
self.get_balances()
# Sells crypto
def sell_order(self, pair, currency_buy, currency_sell, amount, order_type="market"):
self.kraken_wrapper.add_standard_order(pair, "sell", order_type, volume=amount, validate=True)
#self.balances["vol"][currency_buy] -= amount
#self.balances["vol"][currency_sell] += amount * self.prices.tail(1)[0]
time.sleep(1)
self.get_balances()