Cleaned up code
This commit is contained in:
parent
5299661844
commit
aa084ec729
3 changed files with 47 additions and 46 deletions
|
@ -1,47 +1,51 @@
|
||||||
from pykrakenapi import KrakenAPI
|
from pykrakenapi import KrakenAPI
|
||||||
import pandas
|
import pandas, krakenex, time, numpy
|
||||||
import krakenex
|
|
||||||
import time
|
|
||||||
|
|
||||||
class Kraken:
|
class Kraken:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.krakenAPI = krakenex.API()
|
self.kraken_api = krakenex.API()
|
||||||
self.krakenAPI.load_key("kraken.key")
|
self.kraken_api.load_key("kraken.key")
|
||||||
self.krakenWrapper = KrakenAPI(self.krakenAPI)
|
self.kraken_wrapper = KrakenAPI(self.kraken_api)
|
||||||
self.getBalances()
|
self.get_balances()
|
||||||
|
|
||||||
|
|
||||||
# Updates the current wallet prices from the internet
|
# Updates the current wallet prices from the internet
|
||||||
def getBalances(self):
|
def get_balances(self):
|
||||||
__newBalances = self.krakenWrapper.get_account_balance()
|
self.balances = self.kraken_wrapper.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
|
# 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):
|
def get_ohlc_prices(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]
|
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.
|
# 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 calculate_ema(self, short_time=5, long_time=30):
|
||||||
self.longEMA = self.prices.ewm(span=30).mean()
|
self.long_ema = self.prices.ewm(span=long_time).mean()
|
||||||
self.shortEMA = self.prices.ewm(span=5).mean()
|
self.short_ema = self.prices.ewm(span=short_time).mean()
|
||||||
|
|
||||||
|
|
||||||
def buyOrder(self, currency_pair, currency_buy, currency_sell, amount, ordertype="market"):
|
# Buys crypto
|
||||||
self.krakenWrapper.add_standard_order(currency_pair, "buy", ordertype, volume=amount, validate=False)
|
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_buy] += amount
|
||||||
#self.balances["vol"][currency_sell] -= amount * self.prices.tail(1)[0]
|
#self.balances["vol"][currency_sell] -= amount * self.prices.tail(1)[0]
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
self.getBalances()
|
self.get_balances()
|
||||||
|
|
||||||
def sellOrder(self, currency_pair, currency_buy, currency_sell, amount, ordertype="market"):
|
# Sells crypto
|
||||||
self.krakenWrapper.add_standard_order(currency_pair, "sell", ordertype, volume=amount, validate=False)
|
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_buy] -= amount
|
||||||
#self.balances["vol"][currency_sell] += amount * self.prices.tail(1)[0]
|
#self.balances["vol"][currency_sell] += amount * self.prices.tail(1)[0]
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
self.getBalances()
|
self.get_balances()
|
||||||
|
|
||||||
|
|
21
main.py
21
main.py
|
@ -3,6 +3,9 @@ import time, threading, pandas, numpy
|
||||||
from api.kraken import Kraken
|
from api.kraken import Kraken
|
||||||
from api.fake import Fake
|
from api.fake import Fake
|
||||||
|
|
||||||
|
def percent_difference(first, second):
|
||||||
|
return abs(first-second)/((first+second)/2)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
currency_buy = "XXBT"
|
currency_buy = "XXBT"
|
||||||
currency_sell = "ZUSD"
|
currency_sell = "ZUSD"
|
||||||
|
@ -13,34 +16,32 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
k.getPrices(currency_pair)
|
k.get_ohlc_prices(currency_pair)
|
||||||
print("3")
|
print("3")
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
#print("Main loop")
|
#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.get_current_price(currency_pair)
|
||||||
|
k.calculate_ema()
|
||||||
k.prices = pandas.concat([k.prices, pandas.DataFrame(new_row)], ignore_index=False)
|
|
||||||
k.calculateEMA()
|
|
||||||
|
|
||||||
# If we have crypto, check for sell behaviour, if we have fiat, check for buy behavior, otherwise, get really confused
|
# 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:
|
if k.balances["vol"][currency_buy] > 10**-5:
|
||||||
# Sell shit
|
# Sell shit
|
||||||
# If short term change dips below long term change, sell
|
# 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")
|
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)
|
print(k.balances)
|
||||||
|
|
||||||
elif k.balances["vol"][currency_sell] > 10**-2:
|
elif k.balances["vol"][currency_sell] > 10**-2:
|
||||||
# Buy shit
|
# Buy shit
|
||||||
# If short term goes above long term, buy
|
# 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")
|
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)
|
print(k.balances)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
16
plot.py
16
plot.py
|
@ -1,6 +1,5 @@
|
||||||
import time, threading
|
import time, threading, pandas
|
||||||
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
|
||||||
|
@ -15,22 +14,19 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
time.sleep(1)
|
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)
|
print(2)
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
current_price = float(k.krakenWrapper.get_ticker_information(currency_pair)["c"][0][0])
|
k.get_current_price(currency_pair)
|
||||||
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)
|
|
||||||
print(3)
|
print(3)
|
||||||
|
|
||||||
k.calculateEMA()
|
k.calculate_ema()
|
||||||
print(4)
|
print(4)
|
||||||
|
|
||||||
plt.plot(k.shortEMA)
|
plt.plot(k.short_ema)
|
||||||
plt.plot(k.longEMA)
|
plt.plot(k.long_ema)
|
||||||
plt.plot(k.prices)
|
plt.plot(k.prices)
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue