47 lines
2.1 KiB
Python
47 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)
|
|
self.getBalances()
|
|
|
|
|
|
# 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.longEMA = self.prices.ewm(span=12).mean()
|
|
self.shortEMA = self.prices.ewm(span=3).mean()
|
|
|
|
|
|
def buyOrder(self, currency_pair, currency_buy, currency_sell, amount, ordertype="market"):
|
|
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()
|
|
|
|
def sellOrder(self, currency_pair, currency_buy, currency_sell, amount, ordertype="market"):
|
|
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()
|
|
|