CryptoBot/main.py

82 lines
3 KiB
Python
Raw Normal View History

import time, threading, pandas, numpy, enum
2021-01-31 03:15:56 -05:00
from api.kraken import Kraken
from api.fake import Fake
class state(enum.Enum):
UNKNOWN = enum.auto(),
BUYING = enum.auto(),
SELLING = enum.auto()
2021-02-03 17:00:44 -05:00
2021-01-31 03:18:06 -05:00
if __name__ == "__main__":
current_state = state.UNKNOWN
previous_state = state.UNKNOWN
currency_buy = "XETH"
2021-01-31 16:17:25 -05:00
currency_sell = "ZUSD"
currency_pair = "ETHUSD"
#buy_price = 0.0
2021-01-31 16:17:25 -05:00
2021-01-31 03:18:06 -05:00
k = Kraken()
2021-02-04 03:14:39 -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-02-04 03:14:39 -05:00
k.get_fee(currency_pair)
print(2)
time.sleep(1)
2021-02-03 17:00:44 -05:00
k.get_ohlc_prices(currency_pair)
2021-02-04 03:14:39 -05:00
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-03 17:00:44 -05:00
k.get_current_price(currency_pair)
k.calculate_ema()
2021-02-02 20:11:26 -05:00
current_short_ema = k.short_ema.tail(1)[0][0] * (1-k.fee/100)
current_long_ema = k.long_ema.tail(1)[0][0]
current_price = k.prices.tail(1)[0][0]
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:
# If the current (now previous) state was buying, set the previous state to the "current" state, then set the current state to the current one (selling)
if current_state == state.BUYING:
previous_state = state.BUYING
current_state = state.SELLING
2021-01-31 16:17:25 -05:00
# Sell shit
# If the short term EMA (accounting for the fee) is less than the long term EMA,
# and there is more than a 0.1% difference between short and long term EMA (accounting for the fee), sell
if (current_short_ema < current_long_ema and current_long_ema/current_short_ema - 1 > 0.001):
2021-01-31 16:17:25 -05:00
print("Selling shit")
2021-02-03 17:00:44 -05:00
k.sell_order(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:
# If the current (now previous) state was selling, set the previous state to the "current" state, then set the current state to the current one (buying)
if current_state == state.SELLING:
previous_state = state.SELLING
current_state = state.BUYING
2021-01-31 16:17:25 -05:00
# Buy shit
# If the previous state is known, and the short term EMA (accounting for the fee) is greater than the long term EMA,
# and there is more than a 0.1% difference between short and long term EMA (accounting for the fee), buy
if previous_state != state.UNKNOWN and current_short_ema > current_long_ema and current_short_ema/current_long_ema - 1 > 0.001:
2021-01-31 16:17:25 -05:00
print("Buying shit")
k.buy_order(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_sell] * 0.98 * (1/current_price))
#buy_price = 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)