CryptoBot/main.py

50 lines
2 KiB
Python
Raw Normal View History

2021-02-03 16:15:05 -05:00
import time, threading, pandas, numpy
2021-01-31 03:15:56 -05:00
from api.kraken import Kraken
from api.fake import Fake
2021-01-31 03:18:06 -05:00
if __name__ == "__main__":
2021-02-01 22:34:35 -05:00
currency_buy = "XXBT"
2021-01-31 16:17:25 -05:00
currency_sell = "ZUSD"
2021-02-01 22:34:35 -05:00
currency_pair = "XXBTZUSD"
2021-01-31 16:17:25 -05:00
2021-01-31 03:18:06 -05:00
k = Kraken()
2021-01-31 16:17:25 -05:00
print("1")
2021-02-02 18:14:39 -05:00
2021-01-31 16:17:25 -05:00
time.sleep(1)
2021-02-02 20:11:26 -05:00
2021-01-31 16:17:25 -05:00
k.getPrices(currency_pair)
print("3")
2021-02-02 20:11:26 -05:00
2021-02-02 17:51:46 -05:00
time.sleep(1)
2021-01-31 16:17:25 -05:00
while True:
2021-02-01 22:34:35 -05:00
#print("Main loop")
2021-02-03 16:15:05 -05:00
current_price = numpy.float64(k.krakenWrapper.get_ticker_information(currency_pair)["c"][0][0])
2021-02-01 00:19:16 -05:00
new_row = pandas.DataFrame([[current_price]], index=[pandas.to_datetime(time.time(), unit = "s")])
2021-02-02 20:11:26 -05:00
2021-02-01 00:19:16 -05:00
k.prices = pandas.concat([k.prices, pandas.DataFrame(new_row)], ignore_index=False)
2021-02-01 23:23:32 -05:00
k.calculateEMA()
2021-02-02 20:11:26 -05:00
2021-01-31 16:17:25 -05:00
# 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:
2021-01-31 16:17:25 -05:00
print("Selling shit")
2021-02-01 00:19:16 -05:00
k.sellOrder(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_buy])
2021-01-31 16:17:25 -05:00
print(k.balances)
2021-02-02 18:14:39 -05:00
2021-01-31 16:17:25 -05:00
elif k.balances["vol"][currency_sell] > 10**-2:
# Buy shit
2021-02-02 20:11:26 -05:00
# 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:
2021-01-31 16:17:25 -05:00
print("Buying shit")
2021-02-03 16:15:05 -05:00
k.buyOrder(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_sell] * 0.95 * (1/k.prices.tail(1)[0][0]))
2021-01-31 16:17:25 -05:00
print(k.balances)
2021-02-02 18:14:39 -05:00
2021-01-31 16:17:25 -05:00
else:
raise Exception("There is no currency in this account.")
2021-02-02 20:11:26 -05:00
2021-01-31 16:17:25 -05:00
time.sleep(3)