More updates

This commit is contained in:
Logan G 2021-01-31 14:17:25 -07:00
parent 4e67fb531a
commit d017883a15
Signed by: logan
GPG key ID: E328528C921E7A7A
3 changed files with 74 additions and 18 deletions

View file

@ -7,14 +7,14 @@ class Fake:
self.krakenAPI.load_key("kraken.key") self.krakenAPI.load_key("kraken.key")
self.krakenWrapper = KrakenAPI(self.krakenAPI) self.krakenWrapper = KrakenAPI(self.krakenAPI)
self.balances = {'XXBT': self.krakenWrapper.get_ticker_information(pair="XXBTZUSD")["c"][0][0] * 100, 'ZUSD': 0.0} self.balances = {'XXDG': self.krakenWrapper.get_ticker_information(pair="XDGUSD")["c"][0][0] * 100, 'ZUSD': 0.0}
def buyOrder(self, amount, pair): def buyOrder(self, amount, pair):
self.balances[0:3] += float(self.krakenWrapper.get_ticker_information(pair="XXBTZUSD")["c"][0][0]) * amount self.balances[0:3] += float(self.krakenWrapper.get_ticker_information(pair=pair)["c"][0][0]) * amount
self.balances[4:7] -= amount self.balances[4:7] -= amount
def sellOrder(self, amount, pair): def sellOrder(self, amount, pair):
self.balances[0:3] -= 1/float(self.krakenWrapper.get_ticker_information(pair="XXBTZUSD")["c"][0][0]) * amount self.balances[0:3] -= 1/float(self.krakenWrapper.get_ticker_information(pair=pair)["c"][0][0]) * amount
self.balances[4:7] += amount self.balances[4:7] += amount

View file

@ -12,19 +12,23 @@ class Kraken:
# Updates the current wallet prices from the internet # Updates the current wallet prices from the internet
def updateBalances(self): def updateBalances(self):
__newBalances = self.krakenWrapper.get_account_balance() __newBalances = self.krakenWrapper.get_account_balance()
print(self.balances.loc["XXBT","vol"]-__newBalances.loc["XXBT","vol"]) #print(self.balances.loc["XXBT","vol"]-__newBalances.loc["XXBT","vol"])
print(self.balances.loc["ZUSD","vol"]-__newBalances.loc["ZUSD","vol"]) #print(self.balances.loc["XXDG","vol"]-__newBalances.loc["XXDG","vol"])
#print(self.balances.loc["ZUSD","vol"]-__newBalances.loc["ZUSD","vol"])
self.balances = __newBalances 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
# 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): def calculateEMA(self, shortTime=round(time.time())-(7*24*3600), longTime=round(time.time())-(30*24*3600), interval=60):
self.prices = self.krakenWrapper.get_ohlc_data("XXBTZUSD", interval=interval, since=longTime)[0].close.iloc[::-1] #self.prices = self.krakenWrapper.get_ohlc_data("XXBTZUSD", interval=interval, since=longTime)[0].close.iloc[::-1]
#self.longEMA = self.prices.ewm(span=longTime/60/interval/2).mean()
#self.shortEMA = self.prices.truncate(before=time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(shortTime))).ewm(span=(shortTime-longTime)/60/interval/2).mean()
self.longEMA = self.prices.ewm(span=7*24).mean() self.longEMA = self.prices.ewm(span=1*24).mean()
self.shortEMA = self.prices.ewm(span=30*24).mean() self.shortEMA = self.prices.ewm(span=6).mean()

66
main.py
View file

@ -1,15 +1,67 @@
import time import time, threading
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pandas
from api.kraken import Kraken from api.kraken import Kraken
from api.fake import Fake from api.fake import Fake
glubul = ":)"
if __name__ == "__main__": if __name__ == "__main__":
k = Kraken() currency_buy = "XXDG"
k.calculateEMA() currency_sell = "ZUSD"
currency_pair = "XDGUSD"
plt.plot(k.shortEMA) k = Kraken()
plt.plot(k.longEMA) print("1")
plt.plot(k.prices) k.updateBalances()
plt.show() print("2")
time.sleep(1)
k.getPrices(currency_pair)
print("3")
k.calculateEMA()
print("4")
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)
'''
while True:
# 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
# TODO: Needs to check if fresh start
# If short term change dips below long term change, sell
if(float(k.shortEMA.tail(1)[0]) < float(k.longEMA.tail(1)[0])):
#k.krakenWrapper.add_standard_order(currency_pair, "sell", "market", volume=k.balances["vol"][currency_buy], validate=True)
print("Selling shit")
k.balances["vol"][currency_sell] += k.balances["vol"][currency_buy] * k.prices.tail(1)[0]
k.balances["vol"][currency_buy] = 0
print(k.balances)
elif k.balances["vol"][currency_sell] > 10**-2:
# Buy shit
if(float(k.shortEMA.tail(1)[0]) > float(k.longEMA.tail(1)[0])):
#k.krakenWrapper.add_standard_order(currency_pair, "buy", "market", volume=k.balances["vol"][currency_sell], validate=True)
print("Buying shit")
k.balances["vol"][currency_buy] += k.balances["vol"][currency_sell] * (1/k.prices.tail(1)[0])
k.balances["vol"][currency_sell] = 0
print(k.balances)
else:
raise Exception("There is no currency in this account.")
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.calculateEMA(currency_pair)
time.sleep(3)
'''
plt.plot(k.shortEMA)
plt.plot(k.longEMA)
plt.plot(k.prices)
plt.show()