Some updates

This commit is contained in:
Logan G 2021-01-31 01:15:56 -07:00
parent edb2f20d9c
commit a60881f804
Signed by: logan
GPG key ID: E328528C921E7A7A
5 changed files with 68 additions and 36 deletions

24
api/fake.py Normal file
View file

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

31
api/kraken.py Normal file
View file

@ -0,0 +1,31 @@
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)
# Updates the current wallet prices from the internet
def updateBalances(self):
__newBalances = self.krakenWrapper.get_account_balance()
print(self.balances.loc["XXBT","vol"]-__newBalances.loc["XXBT","vol"])
print(self.balances.loc["ZUSD","vol"]-__newBalances.loc["ZUSD","vol"])
self.balances = __newBalances
# Calculates
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.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.shortEMA = self.prices.ewm(span=30*24).mean()

View file

@ -1,35 +0,0 @@
from tenacity import retry, wait_exponential
import krakenex, threading
class Kraken:
def __init__(self):
self.krakenAPI = krakenex.API()
self.krakenAPI.load_key("kraken.key")
self.callCounter = 0
self.callCooldown = 2
self.callMaximum = 15
threading.Timer(self.callCooldown, self.decrementCallCounter).start()
def decrementCallCounter(self):
#print(self.callCounter)
if self.callCounter > 0:
self.callCounter -= 1
threading.Timer(self.callCooldown, self.decrementCallCounter).start()
@retry(wait=wait_exponential(multiplier=1, min=1, max=10))
def getBalances(self):
# Garbage way of handling this, but better than 15 minutes of :(
if self.callCounter >= self.callMaximum:
time.sleep(self.callCooldown)
self.balances = self.krakenAPI.query_private("Balance")
self.callCounter += 1

14
main.py
View file

@ -1 +1,13 @@
from .kraken import Kraken
import time
import matplotlib.pyplot as plt
from api.kraken import Kraken
from api.fake import Fake
k = Kraken()
k.calculateEMA()
plt.plot(k.shortEMA)
plt.plot(k.longEMA)
plt.plot(k.prices)
plt.show()