import time, threading, pandas, numpy, enum from api.kraken import Kraken from api.fake import Fake class state(enum.Enum): UNKNOWN = enum.auto(), BUYING = enum.auto(), SELLING = enum.auto() if __name__ == "__main__": current_state = state.UNKNOWN previous_state = state.UNKNOWN currency_buy = "XETH" currency_sell = "ZUSD" currency_pair = "ETHUSD" #buy_price = 0.0 k = Kraken() print(1) time.sleep(1) k.get_fee(currency_pair) print(2) time.sleep(1) k.get_ohlc_prices(currency_pair) print(3) time.sleep(1) while True: k.get_current_price(currency_pair) k.calculate_ema() 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] # 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 # 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): print("Selling shit") k.sell_order(currency_pair, currency_buy, currency_sell, k.balances["vol"][currency_buy]) print(k.balances) 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 # 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: 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] print(k.balances) else: raise Exception("There is no currency in this account.") time.sleep(3)