def _do_schedule(_schedule): market = _schedule.market ticker = _schedule.ticker collection_name = _schedule.collection_name collection = db.get_collection(collection_name, codec_options=codec_options) sleep(1) while True: if ticker == BinanceClient.KLINE_INTERVAL_1MINUTE or ticker == BinanceClient.KLINE_INTERVAL_5MINUTE: sleep(randrange(2)) elif ticker == BinanceClient.KLINE_INTERVAL_15MINUTE or BinanceClient.KLINE_INTERVAL_30MINUTE: sleep(randrange(20)) else: sleep(randrange(200)) if _schedule.exchange == "binance": try: klines = try_get_klines(_schedule.exchange, market, ticker, get_binance_interval_unit(ticker)) klines = klines[: -1] # we skip the last kline on purpose to have for it a crawling volume except Exception as err: traceback.print_tb(err.__traceback__) logger.exception("{} {} {}".format(_schedule.exchange, collection_name, err.__traceback__)) sleep(randrange(30)) klines = get_binance_klines(market, ticker, get_binance_interval_unit(ticker)) klines = klines[: -1] # we skip the last kline on purpose to have for it a crawling volume elif _schedule.exchange == "kucoin": try: klines = try_get_klines(_schedule.exchange, market, ticker, get_kucoin_interval_unit(ticker)) except Exception: traceback.print_tb(err.__traceback__) logger.exception("{} {} {}".format(_schedule.exchange, collection_name, err.__traceback__)) sleep(randrange(30)) klines = get_kucoin_klines(market, ticker, get_kucoin_interval_unit(ticker)) current_klines = filter_current_klines(klines, collection_name, collection) sleep(15) bd, sd = get_average_depths(_schedule.depth_crawl, _schedule.no_depths) list(map(lambda x: x.add_buy_depth(bd), current_klines)) list(map(lambda x: x.add_sell_depth(sd), current_klines)) list(map(lambda x: x.add_market(market), current_klines)) if _schedule.exchange == "binance": list(map(lambda x: set_trade_volume(_schedule, x), current_klines)) list(map(lambda x: x.add_exchange(_schedule.exchange), current_klines)) persist_klines(current_klines, collection) logger.info("Stored to collection : {} : {} ".format( _schedule.exchange, collection_name)) _sleep_seed = 100 if _schedule.market == "BTCUSDT" and _schedule.ticker == "15m": sleep(2 * 60 + randrange(10)) else: sleep(_schedule.sleep + randrange(100))
def _do_schedule(_schedule): market = _schedule.market ticker = _schedule.ticker collection_name = _schedule.collection_name collection = db.get_collection(collection_name, codec_options=codec_options) sleep(1) klines_socket[market] = {} klines_socket[market][ticker] = {} klines_socket[market][ticker]['properties'] = [_schedule, collection_name, collection] klines_socket[market][ticker]['klines'] = [] bm = BinanceSocketManager(binance_obj.client) bm.start_kline_socket(market, process_kline_socket_message, interval=ticker) bm.start() if _schedule.exchange == "binance": try: klines = try_get_klines(_schedule.exchange, market, ticker, get_binance_interval_unit(ticker)) klines = klines[:-1] # we skip the last kline on purpose to have for it a crawling volume except Exception as err: traceback.print_tb(err.__traceback__) logger.exception("{} {} {}".format(_schedule.exchange, collection_name, err.__traceback__)) sleep(randrange(30)) klines = get_binance_klines(market, ticker, get_binance_interval_unit(ticker)) klines = klines[:-1] # we skip the last kline on purpose to have for it a crawling volume handle_klines(klines, _schedule, collection_name, collection)
def get_bullish_assets(_markets, _ticker): _time_interval = get_binance_interval_unit(_ticker) _bullish_assets = [] for _market in _markets: try: _asset = _market.split("BTC")[0] if _asset: # print(_asset) _klines = get_klines(_market, _ticker, _time_interval) _closes = get_closes(_klines) _opens = get_opens(_klines) _ma100 = talib.MA(_closes, timeperiod=100) _ma50 = talib.MA(_closes, timeperiod=50) _ma20 = talib.MA(_closes, timeperiod=20) _ma7 = talib.MA(_closes, timeperiod=7) _cond1 = bullishness_00(_opens, _closes, _ma100, _ma50, _ma20, _ma7) \ or bullishness_01(_opens, _closes, _ma100, _ma50, _ma20, _ma7) or bullishness_1(_opens, _closes, _ma100, _ma50, _ma20, _ma7) \ or bullishness_2(_opens, _closes, _ma100, _ma50, _ma20, _ma7) or bullishness_3(_opens, _closes, _ma100, _ma50, _ma20, _ma7) if _cond1: _bullish_assets.append( AssetTicker(_asset, _ticker, lowest_ask(_market), time.time())) except Exception: print('Exception for {} in {}'.format(_ticker, _market)) sort_assets(_bullish_assets) return _bullish_assets
def get_tradeable_and_bullish_assets(_markets, _ticker): _time_interval = get_binance_interval_unit(_ticker) _assets = [] for _market in _markets: _asset = _market.split("BTC")[0] try: if _asset: # print(_asset) _klines = get_klines(_market, _ticker, _time_interval) _closes = get_closes(_klines) _opens = get_opens(_klines) _ma100 = talib.MA(_closes, timeperiod=100) _ma50 = talib.MA(_closes, timeperiod=50) _ma20 = talib.MA(_closes, timeperiod=20) _ma7 = talib.MA(_closes, timeperiod=7) _cond1 = bullishness_00(_opens, _closes, _ma100, _ma50, _ma20, _ma7) \ or bullishness_01(_opens, _closes, _ma100, _ma50, _ma20, _ma7) or bullishness_1(_opens, _closes, _ma100, _ma50, _ma20, _ma7) \ or bullishness_2(_opens, _closes, _ma100, _ma50, _ma20, _ma7) or bullishness_3(_opens, _closes, _ma100, _ma50, _ma20, _ma7) _rsi = get_rsi(_market, _ticker, _time_interval) _macd, _macdsignal, _macdhist = talib.MACD(_closes, fastperiod=12, slowperiod=26, signalperiod=9) _cond2 = is_tradeable(_closes, _rsi, _macd, _macdsignal) if _cond1 and _cond2: _assets.append( AssetTicker(_asset, _ticker, lowest_ask(_market), time.time())) except Exception as err: print('Exception for {} in {}'.format(_ticker, _market)) # traceback.print_tb(err.__traceback__) sort_assets(_assets) return _assets
def get_tradeable_assets(_markets, _ticker): _time_interval = get_binance_interval_unit(_ticker) _tradeable_assets = [] for _market in _markets: _asset = _market.split("BTC")[0] try: if _asset: # print(_asset) _closes = get_closes( get_klines(_market, _ticker, _time_interval)) # _rsi = relative_strength_index(_closes) _rsi = get_rsi(_market, _ticker, _time_interval) _macd, _macdsignal, _macdhist = talib.MACD(_closes, fastperiod=12, slowperiod=26, signalperiod=9) if is_tradeable(_closes, _rsi, _macd, _macdsignal): _tradeable_assets.append( AssetTicker(_asset, _ticker, lowest_ask(_market), time.time())) except Exception: print('Value Error for {} in {}'.format(_ticker, _market)) sort_assets(_tradeable_assets) return _tradeable_assets
def buy_local_bottom_bullish_test(_klines, _i): global _trigger global _rsi_low global _rsi_low_fresh global _prev_rsi global _last_ma7_gt_ma100 global _big_volume_sold_out global _bearish_trigger global _prev_rsi_high global _slope_condition logger = setup_logger("test") _btc_value = 0.1 _trade_asset = TradeAsset('ALGO') _trade_asset.set_btc_asset_buy_value(_btc_value) # adjust_buy_asset_btc_volume(_trade_asset, _btc_value) _params = [] strategy = BullishStrategy(_trade_asset, _btc_value, _params) _time_interval = get_binance_interval_unit(strategy.asset.ticker) _time_frame_short = 10 _time_frame_middle = 30 _time_frame_rsi = 50 _time_horizon = 60 _time_horizon_long = 360 try: # strategy.asset.price = lowest_ask(strategy.asset.market) if not is_buy_possible(strategy.asset, strategy.btc_value, strategy.params): strategy.asset.running = False logger.info( "{} buy_local_bottom {} : buy not possible, skipping, exiting". format(strategy.asset.market, strategy)) # sys.exit(0) if not is_bullish_setup(strategy.asset): strategy.asset.running = False logger.info( "{} buy_local_bottom {} : NOT bullish setup, skipping, exiting" .format(strategy.asset.market, strategy)) # sys.exit(0) # _klines = get_klines(strategy.asset, _time_interval) _curr_kline = _klines[-1] _closes = get_closes(_klines) _rsi = relative_strength_index(_closes, _prev_rsi.value, 14, strategy.asset) _max_volume = get_max_volume(_klines, _time_horizon) _rsi_curr = _rsi[-1] _ma7 = talib.MA(_closes, timeperiod=7) _time_curr = _curr_kline[0] _open = float(_curr_kline[1]) _close = _closes[-1] _0time = get_time(_time_curr / 1000) if _rsi_curr > 70: _prev_rsi_high = TimeTuple(_rsi_curr, _time_curr) _rsi_low = False _rsi_low_fresh = False _max_volume = get_max_volume(_klines, _time_horizon) # if _rsi_curr < 33.5 and not is_fresh_test(_prev_rsi_high, _time_frame_rsi, _time_curr): if _rsi_curr < 33.5 and not is_fresh(_prev_rsi_high, _time_frame_rsi): _max_volume_long = get_max_volume(_klines, _time_horizon_long) if volume_condition(_klines, _max_volume_long, 0.9) and not_equal_rsi( _rsi_curr, _rsi_low_fresh): _rsi_low = TimeTuple(_rsi_curr, _time_curr) if _rsi_curr < 33.5 and is_fresh(_prev_rsi_high, _time_frame_middle) and not_equal_rsi( _rsi_curr, _rsi_low): if volume_condition(_klines, _max_volume, 0.9): _rsi_low_fresh = TimeTuple(_rsi_curr, _time_curr) if not _rsi_low and _rsi_curr < 31 and not is_fresh(_prev_rsi_high, _time_frame_rsi) \ and volume_condition(_klines, _max_volume, 0.5) and not_equal_rsi(_rsi_curr, _rsi_low_fresh): if not is_rsi_slope_condition( _rsi, 100, 68, len(_rsi) - 45, -1, _window=10): _rsi_low = TimeTuple(_rsi_curr, _time_curr) else: _slope_condition = TimeTuple(True, _time_curr) if not _rsi_low and _rsi_curr < 20 and not_equal_rsi( _rsi_curr, _rsi_low_fresh): _rsi_low = TimeTuple(_rsi_curr, _time_curr) _c1 = _rsi_low and _rsi_curr < 35 and is_fresh(_rsi_low, _time_frame_rsi) and not is_fresh(_rsi_low, 15) and \ _rsi_curr > _rsi_low.value and not is_fresh(_rsi_low_fresh, _time_frame_middle) _c2 = _rsi_low and _rsi_low_fresh and _rsi_curr > _rsi_low_fresh.value and _rsi_curr > _rsi_low.value and \ not is_fresh(_rsi_low_fresh, _time_frame_middle) _rsi_temp = get_one_of_rsi(_rsi_low_fresh, _rsi_low) _c3 = _rsi_temp and _rsi_curr > _rsi_temp.value and not is_fresh(_rsi_temp, _time_horizon) and \ volume_condition(_klines, _max_volume, 0.9) and _rsi_curr < 33.5 if _c1 or _c2 or _c3: _max_volume_short = get_max_volume(_klines, _time_frame_short) # if _rsi_curr > _rsi_low[0] and volume_condition(_klines, _max_volume, 0.3): # RSI HL if volume_condition(_klines, _max_volume_short, 0.3): # RSI HL _trigger = TimeTuple(True, _time_curr) _max_volume_middle = get_max_volume(_klines, 10) if _rsi_low and _close - _ma7[-1] > 0 and _rsi_curr > _rsi_low.value and \ volume_condition(_klines, _max_volume_middle, 1.0): # reversal _trigger = TimeTuple(True, _time_curr) if _rsi_low and _rsi_low.value < 20 and is_fresh(_rsi_low, 15): _trigger = False if _slope_condition.value: _rsi_low = False _rsi_low_fresh = False _trigger = False if not is_fresh_test(_slope_condition, _time_horizon, _time_curr): _slope_condition = TimeTuple(False, _time_curr) if _rsi_low and is_red_candle(_curr_kline): _green_klines = get_green_candles(_klines) _max_volume_long = get_max_volume(_green_klines, _time_horizon) if volume_condition(_klines, _max_volume_long, 2.1): _rsi_low = False # if _trigger: # i2 = 12 # # dump_variables(_prev_rsi_high, _trigger, _rsi_low, _rsi_low_fresh, # TimeTuple(False, 0), TimeTuple(False, 0), TimeTuple(False, 0)) if _close - _ma7[-1] and is_fresh(_trigger, 15) > 0: logger.info("{} Buy Local Bottom triggered : {} ...".format( strategy.asset.market, strategy)) _la = lowest_ask(strategy.asset.market) strategy.asset.buy_price = _la _possible_buying_quantity = get_buying_asset_quantity( strategy.asset, strategy.btc_value) _quantity_to_buy = adjust_quantity(_possible_buying_quantity, strategy.params) if _quantity_to_buy and is_buy_possible( strategy.asset, strategy.btc_value, strategy.params): strategy.asset.trading = True # _order_id = buy_order(strategy.asset, _quantity_to_buy) # adjust_stop_loss_price(strategy.asset) # adjust_price_profit(strategy.asset) # strategy.set_stop_loss() # wait_until_order_filled(strategy.asset.market, _order_id) # sell_limit(strategy.asset.market, strategy.asset.name, strategy.asset.price_profit) # strategy.set_take_profit() logger.info( "{} Bought Local Bottom {} : price : {} value : {} BTC, exiting" .format(strategy.asset.market, strategy, strategy.asset.buy_price, strategy.btc_value)) strategy.asset.running = False # save_to_file(trades_logs_dir, "buy_klines_{}".format(time.time()), _klines) # sys.exit(0) _prev_rsi = TimeTuple(_rsi, _time_curr) # time.sleep(45) except Exception as err: if isinstance(err, requests.exceptions.ConnectionError): logger.error("Connection problem...") else: traceback.print_tb(err.__traceback__) logger.exception(err.__traceback__)
def buy_local_bottom_bearish_test(_klines, _i): global _prev_rsi_high global _trigger global _rsi_low global _rsi_low_fresh global _prev_rsi global _last_ma7_gt_ma100 global _big_volume_sold_out global _bearish_trigger logger = setup_logger("test") _btc_value = 0.1 _trade_asset = TradeAsset('FTM') _trade_asset.set_btc_asset_buy_value(_btc_value) # adjust_buy_asset_btc_volume(_trade_asset, _btc_value) _params = [] strategy = BullishStrategy(_trade_asset, _btc_value, _params) _time_interval = get_binance_interval_unit(strategy.asset.ticker) _time_frame_middle = 30 _time_frame_rsi = 50 _time_horizon = 60 _time_horizon_long = 360 # while 1: try: strategy.asset.price = lowest_ask(strategy.asset.market) # if not is_buy_possible(strategy.asset, strategy.btc_value, strategy.params): # strategy.asset.running = False # logger.info( # "{} buy_local_bottom : buy not possible, skipping, exiting".format(strategy.asset.market)) # _klines = binance_obj.get_klines_currency(strategy.asset.market, strategy.asset.ticker, _time_interval) _curr_kline = _klines[-1] _closes = get_closes(_klines) _rsi = relative_strength_index(_closes, _prev_rsi.value, 14, strategy.asset) _ma7_curr = talib.MA(_closes, timeperiod=7)[-1] _ma100_curr = talib.MA(_closes, timeperiod=100)[-1] _time_curr = float(_curr_kline[0]) _open = float(_curr_kline[1]) _volume_curr = float(_curr_kline[7]) _close = _closes[-1] _0time = get_time(_time_curr / 1000) _rsi_curr = _rsi[-1] if _rsi_curr < 28: ii = 1 if _ma7_curr > _ma100_curr: _last_ma7_gt_ma100 = TimeTuple(_close, _time_curr) if _last_ma7_gt_ma100.value and is_red_candle( _curr_kline ) and _ma100_curr > _ma7_curr > _close and _rsi_curr < 30: _max_volume_long = get_max_volume(_klines, _time_horizon_long) if volume_condition(_klines, _max_volume_long, 1.2): _big_volume_sold_out = TimeTuple(_volume_curr, _time_curr) if _rsi_curr > 70: _prev_rsi_high = TimeTuple(_rsi_curr, _time_curr) _max_volume = get_max_volume(_klines, _time_horizon) # if _rsi_curr < 33.5 and not is_fresh_test(_prev_rsi_high, _time_frame_rsi, _time_curr): if _rsi_curr < 33.5 and not is_fresh_test(_prev_rsi_high, _time_frame_rsi, _time_curr): _max_volume_long = get_max_volume(_klines, _time_horizon_long) if volume_condition(_klines, _max_volume_long, 0.9) and not_equal_rsi( _rsi_curr, _rsi_low_fresh): _rsi_low = TimeTuple(_rsi_curr, _time_curr) if _rsi_curr < 33.5 and is_fresh_test( _prev_rsi_high, _time_frame_middle, _time_curr) and not_equal_rsi(_rsi_curr, _rsi_low): if volume_condition(_klines, _max_volume, 0.9): _rsi_low_fresh = TimeTuple(_rsi_curr, _time_curr) if not _rsi_low and _rsi_curr < 31 and not is_fresh_test(_prev_rsi_high, _time_frame_rsi, _time_curr) \ and volume_condition(_klines, _max_volume, 0.5) and not_equal_rsi(_rsi_curr, _rsi_low_fresh): _rsi_low = TimeTuple(_rsi_curr, _time_curr) if not _rsi_low and _rsi_curr < 20 and not_equal_rsi( _rsi_curr, _rsi_low_fresh): _rsi_low = TimeTuple(_rsi_curr, _time_curr) _c1 = _rsi_low and _rsi_curr < 33.5 and is_fresh_test(_rsi_low, _time_frame_rsi, _time_curr) and not is_fresh_test(_rsi_low, 15, _time_curr) and \ _rsi_curr > _rsi_low.value and not is_fresh_test(_rsi_low_fresh, _time_frame_middle, _time_curr) _c2 = _rsi_low and _rsi_low_fresh and _rsi_curr > _rsi_low_fresh.value and _rsi_curr > _rsi_low.value and not is_fresh_test( _rsi_low_fresh, _time_frame_middle, _time_curr) _rsi_temp = get_one_of_rsi(_rsi_low_fresh, _rsi_low) _c3 = _rsi_temp and _rsi_curr > _rsi_temp.value and not is_fresh_test( _rsi_temp, _time_horizon, _time_curr) and volume_condition( _klines, _max_volume, 0.9) and _rsi_curr < 33.5 if _c1 or _c2 or _c3: _max_volume_short = get_max_volume(_klines, 10) # if _rsi_curr > _rsi_low[0] and volume_condition(_klines, _max_volume, 0.3): # RSI HL if volume_condition(_klines, _max_volume_short, 0.3): # RSI HL _trigger = TimeTuple(True, _time_curr) _max_volume_middle = get_max_volume(_klines, 15) if _rsi_low and _close - _ma7_curr > 0 and _rsi_curr > _rsi_low.value and volume_condition( _klines, _max_volume_middle, 1.0): # reversal _trigger = TimeTuple(True, _time_curr) if _big_volume_sold_out.value: if price_drop(_last_ma7_gt_ma100.value, _close, 0.08): _bearish_trigger = TimeTuple(True, _time_curr) if _rsi_low and _rsi_low.value < 20 and is_fresh_test( _rsi_low, _time_frame_middle, _time_curr): _trigger = False if _trigger: i2 = 12 if _trigger and _close - _ma7_curr > 0 and is_fresh_test( _bearish_trigger, _time_frame_middle, _time_curr): logger.info("{} Buy Local Bottom triggered...".format( strategy.asset.market)) _la = lowest_ask(strategy.asset.market) strategy.asset.buy_price = _la _possible_buying_quantity = get_buying_asset_quantity( strategy.asset, strategy.btc_value) _quantity_to_buy = adjust_quantity(_possible_buying_quantity, strategy.params) if _quantity_to_buy and is_buy_possible( strategy.asset, strategy.btc_value, strategy.params): strategy.asset.trading = True # _order_id = buy_order(strategy.asset, _quantity_to_buy) adjust_stop_loss_price(strategy.asset) adjust_price_profit(strategy.asset) strategy.set_stop_loss() # wait_until_order_filled(strategy.asset.market, _order_id) # sell_limit(strategy.asset.market, strategy.asset.name, strategy.asset.price_profit) strategy.set_take_profit() logger.info( "{} Bought Local Bottom : price : {} value : {} BTC, exiting" .format(strategy.asset.market, strategy.asset.buy_price, strategy.btc_value)) strategy.asset.running = False # save_to_file(trades_logs_dir, "buy_klines_{}".format(time.time()), _klines) _prev_rsi = TimeTuple(_rsi_curr, _time_curr) # time.sleep(1) except Exception as err: if isinstance(err, requests.exceptions.ConnectionError): logger.error("Connection problem...") else: traceback.print_tb(err.__traceback__) logger.exception(err.__traceback__)