2021-02-10 18:08:40 -05:00
import time , threading , pandas , numpy , enum
2021-01-31 03:15:56 -05:00
from api . kraken import Kraken
from api . fake import Fake
2021-02-10 18:08:40 -05:00
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__ " :
2021-02-10 18:08:40 -05:00
current_state = state . UNKNOWN
previous_state = state . UNKNOWN
2021-02-04 03:06:53 -05:00
2021-02-10 18:08:40 -05:00
currency_buy = " XETH "
2021-01-31 16:17:25 -05:00
currency_sell = " ZUSD "
2021-02-10 18:08:40 -05:00
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
2021-02-10 18:08:40 -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 :
2021-02-10 18:08:40 -05:00
# 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
2021-02-04 03:06:53 -05:00
2021-02-10 18:08:40 -05:00
current_state = state . SELLING
2021-02-04 03:06:53 -05:00
2021-01-31 16:17:25 -05:00
# Sell shit
2021-02-04 03:09:59 -05:00
# If the short term EMA (accounting for the fee) is less than the long term EMA,
2021-02-04 03:06:53 -05:00
# and there is more than a 0.1% difference between short and long term EMA (accounting for the fee), sell
2021-02-10 18:08:40 -05:00
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 :
2021-02-10 18:08:40 -05:00
# 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
2021-02-04 03:06:53 -05:00
2021-02-10 18:08:40 -05:00
current_state = state . BUYING
2021-02-04 03:06:53 -05:00
2021-01-31 16:17:25 -05:00
# Buy shit
2021-02-04 03:06:53 -05:00
# 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
2021-02-10 18:08:40 -05:00
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 " )
2021-02-10 18:08:40 -05:00
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 )