Cleaned up code

This commit is contained in:
Logan G 2021-02-03 15:00:44 -07:00
parent 5299661844
commit aa084ec729
Signed by: logan
GPG key ID: E328528C921E7A7A
3 changed files with 47 additions and 46 deletions

View file

@ -1,47 +1,51 @@
from pykrakenapi import KrakenAPI
import pandas
import krakenex
import time
import pandas, krakenex, time, numpy
class Kraken:
def __init__(self):
self.krakenAPI = krakenex.API()
self.krakenAPI.load_key("kraken.key")
self.krakenWrapper = KrakenAPI(self.krakenAPI)
self.getBalances()
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 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
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 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 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)
# 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=30).mean()
self.shortEMA = self.prices.ewm(span=5).mean()
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()
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)
# 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.getBalances()
self.get_balances()
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)
# 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.getBalances()
self.get_balances()

21
main.py
View file

@ -3,6 +3,9 @@ import time, threading, pandas, numpy
from api.kraken import Kraken
from api.fake import Fake
def percent_difference(first, second):
return abs(first-second)/((first+second)/2)
if __name__ == "__main__":
currency_buy = "XXBT"
currency_sell = "ZUSD"
@ -13,34 +16,32 @@ if __name__ == "__main__":
time.sleep(1)
k.getPrices(currency_pair)
k.get_ohlc_prices(currency_pair)
print("3")
time.sleep(1)
while True:
#print("Main loop")
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)
k.calculateEMA()
k.get_current_price(currency_pair)
k.calculate_ema()
# If we have crypto, check for sell behaviour, if we have fiat, check for buy behavior, otherwise, get really confused
if k.balances["vol"][currency_buy] > 10**-5:
# Sell shit
# If short term change dips below long term change, sell
if float(k.shortEMA.tail(1)[0]) < float(k.longEMA.tail(1)[0]) and abs(float(k.shortEMA.tail(1)[0])-float(k.longEMA.tail(1)[0]))/((float(k.shortEMA.tail(1)[0])+float(k.longEMA.tail(1)[0]))/2) > 0.001:
if k.short_ema.tail(1)[0][0] < k.long_ema.tail(1)[0][0] and percent_difference(k.short_ema.tail(1)[0][0], k.long_ema.tail(1)[0][0]) > 0.001:
print("Selling shit")
k.sellOrder(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_buy])
k.sell_order(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_buy])
print(k.balances)
elif k.balances["vol"][currency_sell] > 10**-2:
# Buy shit
# If short term goes above long term, buy
if float(k.shortEMA.tail(1)[0]) > float(k.longEMA.tail(1)[0]) and abs(float(k.shortEMA.tail(1)[0])-float(k.longEMA.tail(1)[0]))/((float(k.shortEMA.tail(1)[0])+float(k.longEMA.tail(1)[0]))/2) > 0.001:
if k.short_ema.tail(1)[0][0] > k.long_ema.tail(1)[0][0] and percent_difference(k.short_ema.tail(1)[0][0], k.long_ema.tail(1)[0][0]) > 0.001:
print("Buying shit")
k.buyOrder(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_sell] * 0.95 * (1/k.prices.tail(1)[0][0]))
k.buy_order(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_sell] * 0.95 * (1/k.prices.tail(1)[0][0]))
print(k.balances)
else:

16
plot.py
View file

@ -1,6 +1,5 @@
import time, threading
import time, threading, pandas
import matplotlib.pyplot as plt
import pandas
from api.kraken import Kraken
from api.fake import Fake
@ -15,22 +14,19 @@ if __name__ == "__main__":
time.sleep(1)
k.getPrices(currency_pair, time=round(time.time())-(24*3600), interval=1)
k.get_ohlc_prices(currency_pair, time=round(time.time())-(24*3600), interval=1)
print(2)
time.sleep(1)
current_price = float(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)
k.get_current_price(currency_pair)
print(3)
k.calculateEMA()
k.calculate_ema()
print(4)
plt.plot(k.shortEMA)
plt.plot(k.longEMA)
plt.plot(k.short_ema)
plt.plot(k.long_ema)
plt.plot(k.prices)
plt.show()