def cm_bal(task): amounts, balances = app.account_total() previous_amounts = db.config(Const.BALANCE, {}) previous_bal_hash = db.config(Const.BALANCE_HASH, "") total = app.floor(sum(x[1] for x in amounts), 2) if task.params and len(task.params) > 0 and task.params[0] == 'short': return f"Balance: {total}" account_bal_hash = util_balance_hash(balances) is_hash_matching = account_bal_hash == previous_bal_hash msg_lines = [] for x in amounts: prc_diff = app.floor((x[1] - previous_amounts[x[0]]) * 100 / previous_amounts[x[0]]) \ if x[0] in previous_amounts else 'NA' bal_str = f'{x[0]} -> {x[1]} ({prc_diff})' if is_hash_matching else f'{x[0]} -> {x[1]}' if x[1] > 10: msg_lines.append(bal_str) msg_lines.append("." * 15) msg_lines.append(f'Total: {total}') msg = "\n".join(msg_lines) if not is_hash_matching: db.set_config(Const.BALANCE, {x[0]: x[1] for x in amounts}) db.set_config(Const.BALANCE_HASH, account_bal_hash) log.info("saved new balances!") return msg
def telegram_poller(): """ - continuously listen to the messages on the telegram chat - create tasks for the messages - submit to the thread pool - wait 3 seconds and re-do the same """ threading.current_thread().setName("Telegram") try: log.info("Starting Telegram Poller") while True: last_updated_id = db.config(Const.TELEGRAM_UPDATE_ID) msgs = app.get_messages(last_updated_id, timeout=15) for message in msgs: current_time = int(time.time()) message.source = "telegram" # Don't process Old messages last_updated_id = message.update_id if not message['text'] or message.date and message.date < ( current_time - 11): log.info('discarding: %s ', message) db.set_config(Const.TELEGRAM_UPDATE_ID, last_updated_id + 1) continue db.set_config(Const.TELEGRAM_UPDATE_ID, last_updated_id + 1) processor.process_message(message) except Exception as e1: log.exception(e1) app.send_msg("Error occured !", accessControl.adminChatId)
def cm_price_alerts(task): params = overlap(task.params, ['5m', 6, 0], [str, int, float]) messages = [] tickers = app.tickers() for x in db.config(Const.SYMBOLS, []): base_currency = 'USDT' if f'{x}USDT' in tickers else 'BTC' symbol = x + base_currency msg = app.price_alert(symbol=symbol, timeframe=params[0], count=int(params[1]), threshold=float(params[2])) if msg: messages.append(msg) if len(messages) > 0: up = list(filter(lambda y: y[0] == '⬆', messages)) down = list(filter(lambda y: y[0] != '⬆', messages)) result = ['- ' * 10, f'Coins Activity: {params[0]} x {params[1]}'] result.append('- ' * 10) result.extend(up) result.append('- ' * 10) result.extend(down) return "\n".join(result)
def cm_balance_checkpoint(task): amounts, balances = app.account_total() total = sum([x[1] for x in amounts]) balances = db.config('balcheckpoints', []) while len(balances) > 500: balances.pop(0) balances.append(total) db.set_config('balcheckpoints', balances)
def cm_rm_symbols(task): symbols = set(db.config(Const.SYMBOLS, [])) for x in list(task.params): log.debug("removing symbol %s", x) symbols.discard(x.upper()) db.set_config(Const.SYMBOLS, list(symbols))
def cm_add_symbols(task): symbols = set(db.config(Const.SYMBOLS, ['ADA'])) for x in list(task.params): log.debug("adding symbol %s", x) symbols.add(x.upper()) db.set_config(Const.SYMBOLS, list(symbols))
def cm_current_prices(task): tickers = app.tickers() msg = "" for x in db.config(Const.SYMBOLS, []): symbol = app.symbol_with_currency(x) price = tickers[symbol] price_USDT = price if 'USDT' in symbol else price * tickers['BTCUSDT'] msg += f"{symbol}: {app.floor(price_USDT, 4)}, \n" return msg
def create_chart_internal(task, chart='ta', draw=True): params = task.params log.debug("charting: %s", params) if not params[0]: raise Exception("The Asset name is needed") asset = params[0] files = [] signals = [] msg1 = msg2 = msg3 = None if chart == 'ta': if params[1] == 'all': fig, ax = plt.subplots(3, 4) if draw else (None, [[None] * 4] * 3) msg1 = charts.ta(asset, '1h', params[2], fig, ax[0]) msg2 = charts.ta(asset, '4h', params[2], fig, ax[1]) msg3 = charts.ta(asset, '1d', params[2], fig, ax[2]) else: fig, ax = plt.subplots(4) if draw else (None, [None] * 4) msg1 = charts.ta(asset, params[1], params[2], fig, ax) if msg1: signals.append(msg1) if msg2: signals.append(msg2) if msg3: signals.append(msg3) if draw: filename = 'charts/' + asset + "_" + str( int(round(time.time() * 1000))) + '.png' fig.savefig(filename, dpi=300) files.append((filename, asset)) elif chart == 'candles': if params[0] == 'ALLBTC' or params[0] == 'ALLUSDT': for symbol in db.config(Const.SYMBOLS): filename, msg = charts.candles( app.symbol_with_currency(symbol), params[1], params[2]) files.append((filename, app.symbol_with_currency(symbol))) else: filename, msg = charts.candles(asset, params[1], params[2]) files.append((filename, asset)) if msg: signals.append(msg) for filename in files: app.send_photo( filename[0], caption=f'Chart: {filename[1]}, {params[1]} x {params[2]}', chat_id=task.message.chat.id) os.remove(filename[0]) return "\n".join(signals)
def snapshot_total(self): tickers = self.tickers() snapshot = db.config(Const.SNAPSHOT, {}) amounts = list( (x, self.to_amount(x, snapshot[x], tickers)) for x in snapshot) amounts_str = [f'{x[0]} -> {x[1]}' for x in amounts] total = sum(x[1] for x in amounts) amounts_str.append("." * 15) amounts_str.append(f'Snapshot: {self.floor(total)}') msg = "\n".join(amounts_str) return msg
def cm_macd(task): params = overlap(task.params, [None, '1h', 1000], [str, str, int]) log.debug(params) if (params[0].lower() == "all"): params[0] = ",".join(db.config(Const.SYMBOLS, [])) messages = [] for symbol in params[0].split(","): symbol_with_currency = app.symbol_with_currency(symbol) df = app.dataframe( app.klines(symbol_with_currency, params[1], params[2])) key = f'{symbol_with_currency}{params[1]}' resp = charts.macd_x_over(df) signal, signal_time = resp['signal'], resp['time'] old = _macd_data[key] if not old or (old[0] != signal and old[1] < signal_time): _macd_data[key] = signal, signal_time messages.append(f"{symbol}: {signal} at {signal_time}") if len(messages) > 0: return "\n".join([f"{params[1]} -> "] + messages)
def cm_balance_chart(task): filename = charts.balance_chart(db.config('balcheckpoints', [])) app.send_photo(filename, caption=f'Balance Chart', chat_id=task.message.chat.id) os.remove(filename)