diff --git a/api/kraken.py b/api/kraken.py index 3906bdd..bd88bf5 100644 --- a/api/kraken.py +++ b/api/kraken.py @@ -26,7 +26,9 @@ class Kraken: self.prices = pandas.concat([self.prices, pandas.DataFrame(new_row)], ignore_index=False) - + def get_fee(self, pair): + self.fee = k.kraken_wrapper.get_tradable_asset_pairs(pair=currency_pair)["fees"][0][0][1] + # Calculates the exponential moving average by grabbing data from Kraken on the conversion rate every interval minutes. def calculate_ema(self, short_time=5, long_time=30): self.long_ema = self.prices.ewm(span=long_time).mean() diff --git a/main.py b/main.py index 3b219d7..6ea2f62 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,11 @@ import time, threading, pandas, numpy from api.kraken import Kraken from api.fake import Fake -def percent_difference(first, second): - return abs(first-second)/((first+second)/2) if __name__ == "__main__": + current_state = "Unknown" + previous_state = "Unknown" + currency_buy = "XXBT" currency_sell = "ZUSD" currency_pair = "XXBTZUSD" @@ -22,24 +23,37 @@ if __name__ == "__main__": time.sleep(1) while True: - #print("Main loop") - k.get_current_price(currency_pair) k.calculate_ema() + k.get_fee() + # 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 current_state == "Buying": + previous_state = "Buying" + + current_state = "Selling" + # Sell shit - # If short term change dips below long term change, sell - 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: + # If the previous state is known, and 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 previous_state != "Unknown" and k.short_ema.tail(1)[0][0] * (1-self.fee/100) < k.long_ema.tail(1)[0][0] and k.long_ema.tail(1)[0][0]/(k.short_ema.tail(1)[0][0] * (1-self.fee/100)) - 1 > 0.001: + if k.short_ema.tail(1)[0][0] 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 current_state == "Selling": + previous_state = "Selling" + + current_state = "Buying" + # Buy shit - # If short term goes above long term, buy - 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: + # 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 != "Unknown" and k.short_ema.tail(1)[0][0] * (self.fee/100+1) > k.long_ema.tail(1)[0][0] and (k.short_ema.tail(1)[0][0] * (self.fee/100+1))/k.long_ema.tail(1)[0][0] - 1 > 0.001: print("Buying shit") 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)