def get_markets_list(base='BTC', exchange='bittrex'): """ Gets all coins from a certain market. Args: - base: if you want just one market. Ex: BTC. Empty for all markets. Returns: - list of markets. - False if unsupported exchange. """ ret = False if exchange == 'bittrex': try: bt = Bittrex('', '') log.debug("Connected to Bittrex.") ret = [ i['MarketName'] for i in bt.get_markets()['result'] if i['MarketName'].startswith(base) ] except Exception as e: log.exception(f"Unable to connect to Bittrex - {e}") elif exchange == 'binance': try: bnb = Binance('', '') log.debug("Connected to Binance.") ret = [ i['symbol'] for i in bnb.get_all_tickers() if i['symbol'].endswith(base) ] except Exception as e: log.exception(f"Unable to connect to Binance - {e}") return ret
def get_markets_list(base='BTC', exchange='bittrex'): ''' Gets all coins from a certain market. Args: - base: if you want just one market. Ex: BTC. Empty for all markets. Returns: - list of markets. - False if unsupported exchange. ''' ret = False if exchange == 'bittrex': try: bt = Bittrex('', '') log("[INFO] Connected to Bittrex.", 1) ret = [ i['MarketName'] for i in bt.get_markets()['result'] if i['MarketName'].startswith(base) ] except Exception as e: log("[ERROR] Connecting to Bittrex...", 0) elif exchange == 'binance': try: bnb = Binance('', '') log("[INFO] Connected to Binance.", 1) ret = [ i['symbol'] for i in bnb.get_all_tickers() if i['symbol'].endswith(base) ] except Exception as e: log("[ERROR] Connecting to Binance...", 0) return ret
def realtime(exchanges, entry_funcs, exit_funcs, trading_markets=None, interval=var.default_interval, smas=var.default_smas, emas=var.default_volume_emas, refresh_interval=10, simulation=True, main_coins=("BTC", "USDT")): """ Bot using realtime data, doesn't need DB or csv files to work. Args: exchanges(list): list of exchanges. entry_funcs(list): list of entry functions to test. exit_funcs(list): list of entry functions to test. trading_markets(string): list with markets to backtest or empty to run all available markets. interval(string): time between measures. smas(list): list of SMA values to use. emas(list): list of EMA values to use. refresh_interval(int): Data refresh rate. simulation(bool): Defines if it's running as a simulation or real money mode. main_coins(tuple): tuple of main coins. """ markets = [] if trading_markets is None: trading_markets = [] signal.signal(signal.SIGINT, signal_handler) validate = smas[-1] + 5 nr_exchanges = len(exchanges) if not isinstance(exchanges, list): exchanges = [exchanges] if not isinstance(entry_funcs, list): entry_funcs = [entry_funcs] if not isinstance(exit_funcs, list): exit_funcs = [exit_funcs] portfolio = {} # Owned coins list. coins = {} res_abs = 0 # Bittrex exchange if "bittrex" in exchanges: log.debug("Starting Bot with Bittrex") if simulation: log.debug("[MODE] Simulation") bt = Bittrex('', '') nr_exchanges -= 1 else: log.debug("[MODE] Simulation") try: bt = Btr(var.btr_ky, var.btr_sct) except Exception as e: log.error(f"Unable to connect to Bittrex: {e}") return 1 # Binance exchange if "binance" in exchanges: log.debug("Starting Bot with Binance") if simulation: log.debug("[MODE] Simulation") try: bnb = Binance('', '') nr_exchanges -= 1 except Exception as e: log.error(f"Unable to connect to Binance: {e}") else: log.debug("[MODE] Real Money") if var.desktop_info: desktop_notification({ 'type': 'info', 'title': 'Crypto Algo Trading', 'message': '[MODE] Real Money' }) try: bnb = Bnb() except Exception as e: log.error(f"Unable to connect to Binance - {e}") return 1 if not nr_exchanges: log.error('sin exchanges Jose') sys.exit(1) while True: start_time = time() if "bittrex" in exchanges: markets += bt.get_market_summaries()['result'] if "binance" in exchanges: markets += bnb.get_ticker() for market in markets: if 'MarketName' in market: market_name = 'BT_' + str(market['MarketName']) elif 'symbol' in market: market = binance2btrx(market) market_name = 'BN_' + market['MarketName'] global_market_name = str(market['MarketName']) # Check if it's on of the trading pairs. if len(trading_markets) > 0: if market_name not in trading_markets: continue # Checks if pair is included in main coins. if (market_name.startswith('BT_') and market_name.split('-')[0] in main_coins) or \ (market_name.startswith('BN_') and market_name.endswith(main_coins)): # Checks if market already exists in analysed coins. if market_name in coins: # Checks if has enough data to analyse. if coins[market_name] == validate: locals()[market_name] = pd.DataFrame.append( locals()[market_name], [market]).tail(validate) # If not, adds data and keep going. else: locals()[market_name] = pd.DataFrame.append( locals()[market_name], [market]) coins[market_name] += 1 continue # If not adds coin to system. else: locals()[market_name] = pd.DataFrame([market]) coins[market_name] = 1 continue if '-' in market_name: # Renames OpenBuy and OpenSell in Bittrex data = locals()[market_name].rename(index=str, columns={ "OpenBuyOrders": "OpenBuy", "OpenSellOrders": "OpenSell" }) else: data = locals()[market_name] # Checks if coin is in portfolio and looks for a sell opportunity. if market_name in portfolio: # Needed to make use of stop loss and trailing stop loss functions. if portfolio[market_name]['max_price'] < data.Bid.iloc[-1]: portfolio[market_name]['max_price'] = data.Bid.iloc[-1] if is_time_to_exit( data, exit_funcs, smas, bought_at=float( portfolio[market_name]['bought_at']), max_price=float( portfolio[market_name]['max_price']), count=portfolio[market_name]['count'], stop=var.stop_type): if not simulation: # Binance market if market_name.startswith('BN_'): # Market Sell success, sell_res = bnb.sell( market_name.replace('BN_', '')) # portfolio[market_name]['quantity'] # data.Bid.iloc[-1]) if success: sold_at = float( sell_res['fills'][0]['price']) # Bittrex market elif market_name.startswith('BT_'): success, sell_res = bt.sell( market_name.replace('BT_', ''), portfolio[market_name]['quantity'], data.Bid.iloc[-1]) if success: sold_at = sell_res if not success: log.error(f'[ERROR] {sell_res}') continue log.info( f'[SELL] {global_market_name} @ {sold_at}') if var.desktop_info: desktop_notification({ 'type': 'sell', 'title': global_market_name, 'message': f'Sold @ {sold_at}' }) res_abs = (float(sell_res['cummulativeQuoteQty']) / float(sell_res['executedQty']) - portfolio[market_name]['bought_at'] ) * float(sell_res['executedQty']) res = ( (sold_at - portfolio[market_name]['bought_at']) / portfolio[market_name]['bought_at']) * 100 # SIMULATION else: log.info( f'[SELL] {global_market_name} @ {data.Bid.iloc[-1]}' ) res = ((data.Bid.iloc[-1] - portfolio[market_name]['bought_at']) / portfolio[market_name]['bought_at']) * 100 if var.commission: res -= var.bnb_commission log.info(f'[P&L] {global_market_name} > {res:.2f}%') # Hard coded to USDT log.debug( f"[ {'+' if res>0 else '-'} ] {res_abs:.2f} {sell_res['fills'][-1]['commissionAsset']}" ) if var.desktop_info: desktop_notification({ 'type': 'P&L', 'profit%': res, 'profit': res_abs, 'title': global_market_name, 'message': f"P&L = {res:.2f}% | {res_abs} {sell_res['fills'][-1]['commissionAsset']}" }) locals()[market_name].to_csv( f"df_{market_name}-{ctime(time())}.csv") del locals()[market_name] del portfolio[market_name] coins.pop(market_name) # If it's not time to exit, increment count. else: portfolio[market_name]['count'] += 1 # If the coin is not on portfolio, checks if it's time to buy. else: if is_time_to_buy(data, entry_funcs, smas): # REAL if not simulation: # Binance if market_name.startswith('BN_'): # Limit buy # success, msg = bnb.buy(market, data.Ask.iloc[-1]*1.01) # Market buy success, ret = bnb.buy( market_name.replace('BN_', '')) # Bittrex elif market_name.startswith('BT_'): success, ret = bt.buy( market_name.replace('BT_', ''), data.Ask.iloc[-1] * 1.01) if success: # TODO - Implement portfolio for Bittrex portfolio[market_name] = { 'bought_at': float(ret['fills'][0]['price']), 'max_price': float(ret['fills'][0]['price']), 'quantity': float(ret['executedQty']), 'count': 0 } log.info( f"[BUY] {global_market_name} @ {portfolio[market_name]['bought_at']}" ) if var.desktop_info: desktop_notification({ 'type': 'buy', 'title': global_market_name, 'message': f"Buy @ {portfolio[market_name]['bought_at']}" }) elif 'error' in ret: log.info( f"[ERROR] Unable to buy {global_market_name} @ {data.Ask.iloc[-1]}" ) log.info(f" [MSG] {ret['error']}") # SIMULATION else: portfolio[market_name] = { 'bought_at': data.Ask.iloc[-1], 'max_price': data.Ask.iloc[-1], 'quantity': 1, 'count': 0 } log.info( f'[BUY] {global_market_name} @ {data.Ask.iloc[-1]}' ) del markets markets = [] # In case of processing time is bigger than *refresh_interval* doesn't sleep. if refresh_interval - (time() - start_time) >= 0: sleep(refresh_interval - (time() - start_time))