def logging_changes(temporalities, strategy, crypto): logging_message( "-------------------------------------------------------------------> %s actualizado" % crypto) message = '%s %s RSI:\n' % (temporalities['micro']['1m']['trade']['close'], temporalities['micro']['1m']['fingerprint']) for size in strategy['available_sizes']: length = get_type_trade(size, temporalities) rsi = temporalities[length][size]['trade']['RSI'] message += "%s %s |" % ('🟢' if rsi else '🔴', size.ljust(3)) for size in strategy['value_sizes']: length = get_type_trade(size, temporalities) t = temporalities[length][size]['analysis']['mid']['trend'] vt = temporalities[length][size]['analysis']['mid']['volume_trend'] s = temporalities[length][size]['analysis']['mid']['support'] r = temporalities[length][size]['analysis']['mid']['resistance'] b = temporalities[length][size]['analysis']['mid']['secure_buy'] p = temporalities[length][size]['analysis']['mid']['profit'] pv = temporalities[length][size]['analysis']['mid']['profit_value'] v = temporalities[length][size]['analysis']['mid']['volume'] sl = temporalities[length][size]['analysis']['mid']['stop_loss'] m = temporalities[length][size]['analysis']['mid']['mean'] message += "\n%s | t: %s | vt: %s | b: %s | v: %s | s: %s | r: %s | p: %s | pv: %s | sl: %s | m: %s" % ( size.ljust(3), ' 🟢 ' if t else ' 🔴 ', ' 🟢 ' if vt else ' 🔴 ', ' 🟢 ' if b else ' 🔴 ', ' 🟢 ' if v else ' 🔴 ', s, r, p, pv, sl, m) logging_message(message)
def update_indicators(self, size, last_row): length = get_type_trade(size, self.temporalities) self.temporalities[length][size]['fingerprint'] = last_row['time'] self.temporalities[length][size][ 'last_minute'] = datetime.datetime.strptime( str(self.temporalities['micro']['1m']['fingerprint']), '%Y-%m-%d %H:%M:%S').minute self.temporalities[length][size]['trend'] = last_row['trend'] self.temporalities[length][size]['trade']['high'] = last_row['high'] self.temporalities[length][size]['trade']['low'] = last_row['low'] self.temporalities[length][size]['trade']['close'] = last_row['close'] self.temporalities[length][size]['trade']['RSI'] = last_row['RSIs'] self.temporalities[length][size]['trade']['RSIs'] = last_row['RSI_ups'] self.temporalities[length][size]['trade']['RSI_value'] = last_row[ 'RSI'] self.temporalities[length][size]['trade']['bbm'] = last_row['bb_bbm'] self.temporalities[length][size]['trade']['bm_trend'] = last_row['b_m'] self.temporalities[length][size]['trade']['bbh'] = last_row['bb_bbh'] self.temporalities[length][size]['trade']['bbl'] = last_row['bb_bbl'] self.temporalities[length][size]['trade']['pvt'] = last_row['pvt'] self.temporalities[length][size]['trade']['pvt_t'] = last_row['pvt_t'] self.temporalities[length][size]['trade']['mean_f'] = last_row['f'] self.temporalities[length][size]['trade']['Momentum'] = last_row[ 'momentum_s'] self.temporalities[length][size]['trade']['Momentums'] = last_row[ 'momentum_ups'] self.temporalities[length][size]['trade']['Momentum_value'] = last_row[ 'momentum'] self.temporalities[length][size]['trade']['variation'] = last_row[ 'close_variation'] self.temporalities[length][size]['trade']['time'] = last_row[ 'momentum_t'] self.temporalities[length][size]['trade']['ema'] = last_row['buy_ema']
def check_rsi(temporalities, available_sizes): for size in available_sizes: length = get_type_trade(size, temporalities) if temporalities[length][size]['trade']['RSI']: continue else: return False return True
def start(self, chat_ids=None): if not self.process_is_started: self.chat_ids = chat_ids self.process_is_started = True while True: for size, options in self.configuration.items(): if size in self.strategy['available_sizes']: data = get_binance_symbol_data(symbol=self.symbol, kline_size=size, auto_increment=False, save=False, sma=options['days_s']) options['data'] = analysis( df=data, ma_f=options['sma_f'], ma_s=options['sma_s'], period=self.strategy['period'][self.crypto]) df = options['data'] last_row = df.iloc[-1, :] self.update_indicators(last_row=last_row, size=size) length = get_type_trade(size, self.temporalities) time.sleep(2) vp = get_volume_profile(df) self.temporalities[length][size][ 'analysis'] = get_volume_analisys( vp, self.temporalities[length][size]['trade'] ['close']) # set_risk(self.crypto, trades, self.trade) # self.evaluate_trend() if self.first_iteration: self.decide(testing=False) time.sleep(2) logging_changes(temporalities=self.temporalities, strategy=self.strategy, crypto=self.crypto) self.trade['last_risk'] = self.trade['risk'] self.trade['last_trend'] = self.trade['trend'] time.sleep(2) self.first_iteration = True else: # send_messages(trade=self.trade, chat_ids=self.chat_ids, message="Monitoreando %s " % self.crypto) print('Ya se ha iniciado el monitoreo de %s' % self.symbol)
def check_if_update(size, crypto, strategy, temporalities): if size not in strategy['available_sizes']: return False else: try: if size in strategy['reload_sizes']: size = '1m' current_time = datetime.datetime.utcnow() period = size[-1] t = int(size[:-1]) delta = None full = True if period == 'm': full = True delta = timedelta(minutes=t) if period == 'h': full = True delta = timedelta(hours=t) if period == 'd': full = False delta = timedelta(days=t) if period == 'w': full = False delta = timedelta(weeks=t) lt = str(temporalities[get_type_trade( size, temporalities)][size]['fingerprint']) if full: last_time = datetime.datetime.strptime(lt, '%Y-%m-%d %H:%M:%S') else: last_time = datetime.datetime.strptime(lt, '%Y-%m-%d') current_time = datetime.datetime(current_time.year, current_time.month, current_time.day) updatable = current_time - delta return True if updatable >= last_time else False except Exception as e: print("Error al revisar actualizaciones") print(e) return False
def load_test_data(items, trades, symbol): for time, options in items: df = pd.read_csv(get_file_name(symbol, time, 'sma-%s' % options['days_t'])) vp = get_volume_profile(df) trades[get_type_trade(time, trades)][time]['data'] = df trades[get_type_trade(time, trades)][time]['data_vp'] = vp
def backtesting(self, chat_ids, download=True): self.chat_ids = chat_ids print('Obteniendo Datos de %s' % self.crypto) if download: download_test_data(self.symbol, self.configuration.items(), self.indicators, period=self.strategy['period'][self.crypto]) print('Cargando datos de %s' % self.crypto) # Get Test Data load_test_data(self.configuration.items(), self.temporalities, self.symbol) main = self.temporalities[get_type_trade( '1m', self.temporalities)]['1m']['data'] tod = datetime.datetime.now() d = datetime.timedelta(days=15) timestamp = tod - d print("Initial Analysis: ", timestamp, self.crypto) main = main[main['timestamp'] >= str(timestamp)] self.process_is_started = True self.first_iteration = True for index, row in main.iterrows(): print(self.temporalities['micro']['1m']['fingerprint']) self.update_indicators(last_row=row, size='1m') self.temporalities['micro']['1m'][ 'analysis'] = get_volume_analisys( self.temporalities['micro']['1m']['data_vp'], self.temporalities['micro']['1m']['trade']['close']) self.update_indicators(last_row=get_last_row_dataframe_by_time( self.temporalities, '5m', row['timestamp']), size='5m') self.temporalities['micro']['5m'][ 'analysis'] = get_volume_analisys( self.temporalities['micro']['5m']['data_vp'], self.temporalities['micro']['5m']['trade']['close']) self.update_indicators(last_row=get_last_row_dataframe_by_time( self.temporalities, '15m', row['timestamp']), size='15m') self.temporalities['short']['15m'][ 'analysis'] = get_volume_analisys( self.temporalities['short']['15m']['data_vp'], self.temporalities['short']['15m']['trade']['close']) self.update_indicators(last_row=get_last_row_dataframe_by_time( self.temporalities, '30m', row['timestamp']), size='30m') self.temporalities['short']['30m'][ 'analysis'] = get_volume_analisys( self.temporalities['short']['30m']['data_vp'], self.temporalities['short']['30m']['trade']['close']) self.update_indicators(last_row=get_last_row_dataframe_by_time( self.temporalities, '1h', row['timestamp']), size='1h') self.temporalities['medium']['1h'][ 'analysis'] = get_volume_analisys( self.temporalities['medium']['1h']['data_vp'], self.temporalities['medium']['1h']['trade']['close']) self.update_indicators(last_row=get_last_row_dataframe_by_time( self.temporalities, '4h', row['timestamp']), size='4h') self.temporalities['medium']['4h'][ 'analysis'] = get_volume_analisys( self.temporalities['medium']['4h']['data_vp'], self.temporalities['medium']['4h']['trade']['close']) self.update_indicators(last_row=get_last_row_dataframe_by_time( self.temporalities, '1d', row['timestamp']), size='1d') self.temporalities['large']['1d'][ 'analysis'] = get_volume_analisys( self.temporalities['large']['1d']['data_vp'], self.temporalities['large']['1d']['trade']['close']) self.update_indicators(last_row=get_last_row_dataframe_by_time( self.temporalities, '1w', row['timestamp']), size='1w') self.temporalities['large']['1w'][ 'analysis'] = get_volume_analisys( self.temporalities['large']['1w']['data_vp'], self.temporalities['large']['1w']['trade']['close']) self.decide(testing=True) df = pd.DataFrame(self.testing, columns=self.result_indicators) print('Saved results') df.to_csv('backtesting/trades_%s.csv' % self.symbol, index=False) save_result(df=df, symbol=self.symbol, crypto=self.crypto) show_stats(effectivity=self.effectivity, crypto=self.crypto, trade=self.trade, chat_ids=self.chat_ids) show_operative(trade=self.trade, process_is_started=self.process_is_started, symbol=self.symbol, operative=self.operative, chat_ids=self.chat_ids, temporalities=self.temporalities) self.operative = False