CryptoBot/api/kraken.py

54 lines
2.3 KiB
Python
Raw Normal View History

2021-01-31 03:15:56 -05:00
from pykrakenapi import KrakenAPI
2021-02-03 17:00:44 -05:00
import pandas, krakenex, time, numpy
2021-01-31 03:15:56 -05:00
class Kraken:
def __init__(self):
2021-02-03 17:00:44 -05:00
self.kraken_api = krakenex.API()
self.kraken_api.load_key("kraken.key")
self.kraken_wrapper = KrakenAPI(self.kraken_api)
self.get_balances()
2021-02-01 22:34:35 -05:00
2021-01-31 03:15:56 -05:00
# Updates the current wallet prices from the internet
2021-02-03 17:00:44 -05:00
def get_balances(self):
self.balances = self.kraken_wrapper.get_account_balance()
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
2021-02-03 17:00:44 -05:00
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]
2021-01-31 03:15:56 -05:00
2021-02-03 17:00:44 -05:00
# 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")])
2021-01-31 16:17:25 -05:00
2021-02-03 17:00:44 -05:00
self.prices = pandas.concat([self.prices, pandas.DataFrame(new_row)], ignore_index=False)
2021-01-31 03:15:56 -05:00
def get_fee(self, pair):
self.fee = k.kraken_wrapper.get_tradable_asset_pairs(pair=currency_pair)["fees"][0][0][1]
2021-02-03 17:00:44 -05:00
# 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)
2021-02-02 17:48:04 -05:00
#self.balances["vol"][currency_buy] += amount
#self.balances["vol"][currency_sell] -= amount * self.prices.tail(1)[0]
time.sleep(1)
2021-02-03 17:00:44 -05:00
self.get_balances()
2021-01-31 03:15:56 -05:00
2021-02-03 17:00:44 -05:00
# 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)
2021-02-02 17:48:04 -05:00
#self.balances["vol"][currency_buy] -= amount
#self.balances["vol"][currency_sell] += amount * self.prices.tail(1)[0]
time.sleep(1)
2021-02-03 17:00:44 -05:00
self.get_balances()
2021-01-31 03:15:56 -05:00