CryptoBot/test.py

68 lines
2.8 KiB
Python
Raw Normal View History

from pykrakenapi import KrakenAPI
import time, threading, pandas, krakenex, numpy
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]
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()
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()
if __name__ == "__main__":
currency_buy = "XXBT"
currency_sell = "ZUSD"
currency_pair = "XXBTZUSD"
k = Kraken()
k.getPrices(currency_pair)
# OFFENSIVE CODE
current_price = numpy.float64(k.krakenWrapper.get_ticker_information(currency_pair)["c"][0][0])
new_row = pandas.DataFrame([[current_price]], index=[pandas.to_datetime(time.time(), unit = "s")])
k.prices = pandas.concat([k.prices, pandas.DataFrame(new_row)], ignore_index=False)
# END OFFENSIVE CODE
k.calculateEMA()
if k.balances["vol"][currency_buy] > 10**-5:
k.sellOrder(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_buy])
elif k.balances["vol"][currency_sell] > 10**-2:
k.buyOrder(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_sell] * (1/k.prices.tail(1)[0][0]))