Added get_fee function, states and trading now accounts for fees

The Kraken wrapper can now get the trading fee for a crypto pair

The bot uses this when making decisions to buy or sell

The bot on startup will now not initially do anything, it will instead
wait until the state changes between buy and sell to do anything in
order to prevent regrettable trades.
This commit is contained in:
Logan G 2021-02-04 01:06:53 -07:00
parent aa084ec729
commit 9e6368cf2f
Signed by: logan
GPG key ID: E328528C921E7A7A
2 changed files with 25 additions and 9 deletions

View file

@ -26,7 +26,9 @@ class Kraken:
self.prices = pandas.concat([self.prices, pandas.DataFrame(new_row)], ignore_index=False) 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. # 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): def calculate_ema(self, short_time=5, long_time=30):
self.long_ema = self.prices.ewm(span=long_time).mean() self.long_ema = self.prices.ewm(span=long_time).mean()

30
main.py
View file

@ -3,10 +3,11 @@ 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__":
current_state = "Unknown"
previous_state = "Unknown"
currency_buy = "XXBT" currency_buy = "XXBT"
currency_sell = "ZUSD" currency_sell = "ZUSD"
currency_pair = "XXBTZUSD" currency_pair = "XXBTZUSD"
@ -22,24 +23,37 @@ if __name__ == "__main__":
time.sleep(1) time.sleep(1)
while True: while True:
#print("Main loop")
k.get_current_price(currency_pair) k.get_current_price(currency_pair)
k.calculate_ema() 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 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:
if current_state == "Buying":
previous_state = "Buying"
current_state = "Selling"
# Sell shit # Sell shit
# If short term change dips below long term change, sell # If the previous state is known, and the short term EMA (accounting for the fee) is less than the long term EMA,
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: # 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") print("Selling shit")
k.sell_order(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:
if current_state == "Selling":
previous_state = "Selling"
current_state = "Buying"
# Buy shit # Buy shit
# If short term goes above long term, buy # If the previous state is known, and the short term EMA (accounting for the fee) is greater than the long term EMA,
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: # 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") 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])) 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)