def main(): """Initializes the application """ # Load settings and create the config object config = Configuration() settings = config.settings # Set up logger logs.configure_logging(settings['log_level'], settings['log_mode']) logger = structlog.get_logger() # Configure and run configured behaviour. exchange_interface = ExchangeInterface(config.exchanges) if settings['market_pairs']: market_pairs = settings['market_pairs'] logger.info("Found configured markets: %s", market_pairs) market_data = exchange_interface.get_exchange_markets( markets=market_pairs) else: logger.info("No configured markets, using all available on exchange.") market_data = exchange_interface.get_exchange_markets() # notifier = Notifier(config.notifiers, market_data) thread_list = [] for exchange in market_data: num = 1 for chunk in split_market_data(market_data[exchange]): market_data_chunk = dict() market_data_chunk[exchange] = { key: market_data[exchange][key] for key in chunk } notifier = Notifier(config.notifiers, config.indicators, market_data_chunk) behaviour = Behaviour(config, exchange_interface, notifier) workerName = "Worker-{}".format(num) worker = AnalysisWorker(workerName, behaviour, notifier, market_data_chunk, settings, logger) thread_list.append(worker) worker.daemon = True worker.start() time.sleep(60) num += 1 logger.info('All workers are running!') for worker in thread_list: worker.join()
def __configure_simple_bot(self, behaviour_config): """Configures and returns the bot behaviour class. Args: behaviour_config (dict): A dictionary of configuration values pertaining to the behaviour. Returns: SimpleBotBehaviour: A class of functionality for the rsi bot behaviour. """ exchange_interface = ExchangeInterface(self.config.exchanges) strategy_analyzer = StrategyAnalyzer() notifier = Notifier(self.config.notifiers) db_handler = DatabaseHandler(self.config.database) behaviour = SimpleBotBehaviour( behaviour_config, exchange_interface, strategy_analyzer, notifier, db_handler ) return behaviour
def __configure_default(self, behaviour_config): """Configures and returns the default behaviour class. Args: behaviour_config (dict): A dictionary of configuration values pertaining to the behaviour. Returns: DefaultBehaviour: A class of functionality for the default behaviour. """ exchange_interface = ExchangeInterface(self.config.exchanges) strategy_analyzer = StrategyAnalyzer() notifier = Notifier(self.config.notifiers) behaviour = DefaultBehaviour( behaviour_config, exchange_interface, strategy_analyzer, notifier ) return behaviour
def run_from_config(config_ff, threadID): # Load settings and create the config object config = Configuration(config_ff) settings = config.settings # Set up logger logs.configure_logging(settings['log_level'], settings['log_mode']) logger = structlog.get_logger() logger.info(" %s", threadID) # Configure and run configured behaviour. exchange_interface = ExchangeInterface(config.exchanges) notifier = Notifier(config.notifiers, config) behaviour = Behaviour(config, exchange_interface, notifier) while True: if settings['run_on_start']: start = time.time() behaviour.run(settings['market_pairs'], settings['output_mode']) end = time.time() dif = end - start wait_for = settings['update_interval'] - int(dif) logger.info("Sleeping for %s seconds", wait_for) time.sleep(wait_for) else: logger.info("Run on start not enabled waiting for %s seconds", settings['wait_and_run']) time.sleep(settings['wait_and_run'])
def main(): # Load settings and create the config object secrets = {} if os.path.isfile('secrets.json'): secrets = json.load(open('secrets.json')) config = json.load(open('default-config.json')) config.update(secrets) config['settings']['market_pairs'] = os.environ.get('MARKET_PAIRS', config['settings']['market_pairs']) config['settings']['loglevel'] = os.environ.get('LOGLEVEL', config['settings']['loglevel']) config['settings']['app_mode'] = os.environ.get('APP_MODE', config['settings']['app_mode']) config['exchanges']['bittrex']['required']['key'] = os.environ.get('BITTREX_KEY', config['exchanges']['bittrex']['required']['key']) config['exchanges']['bittrex']['required']['secret'] = os.environ.get('BITTREX_SECRET', config['exchanges']['bittrex']['required']['secret']) config['notifiers']['twilio']['required']['key'] = os.environ.get('TWILIO_KEY', config['notifiers']['twilio']['required']['key']) config['notifiers']['twilio']['required']['secret'] = os.environ.get('TWILIO_SECRET', config['notifiers']['twilio']['required']['secret']) config['notifiers']['twilio']['required']['sender_number'] = os.environ.get('TWILIO_SENDER_NUMBER', config['notifiers']['twilio']['required']['sender_number']) config['notifiers']['twilio']['required']['receiver_number'] = os.environ.get('TWILIO_RECEIVER_NUMBER', config['notifiers']['twilio']['required']['receiver_number']) config['notifiers']['gmail']['required']['username'] = os.environ.get('GMAIL_USERNAME', config['notifiers']['gmail']['required']['username']) config['notifiers']['gmail']['required']['password'] = os.environ.get('GMAIL_PASSWORD', config['notifiers']['gmail']['required']['password']) config['notifiers']['gmail']['required']['destination_emails'] = os.environ.get('GMAIL_DESTINATION_EMAILS', config['notifiers']['gmail']['required']['destination_emails']) # Set up logger logs.configure_logging(config['settings']['loglevel'], config['settings']['app_mode']) logger = structlog.get_logger() exchange_interface = ExchangeInterface(config) strategy_analyzer = StrategyAnalyzer(config) notifier = Notifier(config) # The coin pairs coin_pairs = [] if config['settings']['market_pairs']: coin_pairs = config['settings']['market_pairs'].translate(str.maketrans('', '', whitespace)).split(",") else: user_markets = exchange_interface.get_user_markets() for user_market in user_markets['info']: if 'BTC' in user_market['Currency']: continue market_pair = user_market['Currency'] + '/BTC' coin_pairs.append(market_pair) logger.debug(coin_pairs) while True: get_signal(coin_pairs, strategy_analyzer, notifier)
def configure_rsi_bot(self, behaviour_config): exchange_interface = ExchangeInterface( self.config.fetch_exchange_config()) strategy_analyzer = StrategyAnalyzer(exchange_interface) notifier = Notifier(self.config.fetch_notifier_config()) db_handler = DatabaseHandler(self.config.fetch_database_config()) behaviour = RSIBot(behaviour_config, exchange_interface, strategy_analyzer, notifier, db_handler) return behaviour
def configure_default(self, behaviour_config): exchange_interface = ExchangeInterface( self.config.fetch_exchange_config()) strategy_analyzer = StrategyAnalyzer(exchange_interface) notifier = Notifier(self.config.fetch_notifier_config()) behaviour = DefaultBehaviour(behaviour_config, exchange_interface, strategy_analyzer, notifier) return behaviour
def main(): with open("app/config.yml", 'r') as stream: data_loaded = yaml.load(stream) exchangeInterface = ExchangeInterface(data_loaded['exchanges']) exchange = list(data_loaded['exchanges'].keys())[0] market_pair = data_loaded['settings']['market_pairs'][0] interval = data_loaded['settings']['update_interval'] max_periods = data_loaded['settings']['backtest_periods'] ohlcv = exchangeInterface.get_historical_data(exchange, market_pair, interval, max_periods) first = CDL_Test(ohlcv) cdl_list = list(map(lambda x: eval('talib.' + x), cdl)) params_list = {'trailing_window': [10, 15], 'indicator': cdl_list} #result = first.run_algorithm(params_list) best_sharpe, params = first.optim_algo(params_list) first.optim_grid.sort_values(by='sharpe', ascending=False) result = first.run_algorithm(params) pf.create_full_tear_sheet(result.returns)
class StrategyAnalyzer(): """ Executes the trading strategies and analyzes the results. """ def __init__(self, config): self.exchange_aggregator = ExchangeInterface(config) self.logger = structlog.get_logger() def analyze_breakout(self, coin_pair, period_count=5, time_unit='5m'): breakout_analyzer = Breakout() historical_data = self.exchange_aggregator.get_historical_data( coin_pair=coin_pair, period_count=period_count, time_unit=time_unit) breakout_value, is_breaking_out = breakout_analyzer.find_breakout(historical_data) return breakout_value, is_breaking_out def analyze_rsi(self, coin_pair, period_count=18, time_unit='1h'): rsi_analyzer = RelativeStrengthIndex() historical_data = self.exchange_aggregator.get_historical_data( coin_pair=coin_pair, period_count=period_count, time_unit=time_unit ) rsi_value = rsi_analyzer.find_rsi(historical_data) return rsi_value def analyze_moving_averages(self, coin_pair, period_count=20, time_unit='5m'): ma_analyzer = MovingAverages() historical_data = self.exchange_aggregator.get_historical_data( coin_pair=coin_pair, period_count=period_count, time_unit=time_unit ) sma_value = ma_analyzer.calculate_sma(period_count, historical_data) ema_value = ma_analyzer.calculate_ema(period_count, historical_data) return sma_value, ema_value def analyze_ichimoku_cloud(self, coin_pair): ic_analyzer = IchimokuCloud() base_line_data = self.exchange_aggregator.get_historical_data( coin_pair=coin_pair, period_count=26, time_unit='1d' ) conversion_line_data = self.exchange_aggregator.get_historical_data( coin_pair=coin_pair, period_count=9, time_unit='1d' ) span_b_data = self.exchange_aggregator.get_historical_data( coin_pair=coin_pair, period_count=52, time_unit='1d' ) leading_span_a = ic_analyzer.calculate_leading_span_a(base_line_data, conversion_line_data) leading_span_b = ic_analyzer.calculate_leading_span_b(span_b_data) return leading_span_a, leading_span_b
def __configure_reporter(self, behaviour_config): """Configures and returns the reporter behaviour class. Args: behaviour_config (dict): A dictionary of configuration values pertaining to the behaviour. Returns: ReporterBehaviour: A class of functionality for the reporter behaviour. """ exchange_interface = ExchangeInterface(self.config.exchanges) notifier = Notifier(self.config.notifiers) db_handler = DatabaseHandler(self.config.database) behaviour = ReporterBehaviour(behaviour_config, exchange_interface, notifier, db_handler) return behaviour
def main(): """Initializes the application """ # Load settings and create the config object config = Configuration() settings = config.settings # Set up logger logs.configure_logging(settings['log_level'], settings['log_mode']) logger = structlog.get_logger() # Configure and run configured behaviour. exchange_interface = ExchangeInterface(config.exchanges) notifier = Notifier(config.notifiers) behaviour = Behaviour(config, exchange_interface, notifier) while True: behaviour.run(settings['market_pairs'], settings['output_mode']) logger.info("Sleeping for %s seconds", settings['update_interval']) time.sleep(settings['update_interval'])
def main(): # Load settings and create the config object config = conf.Configuration() settings = config.fetch_settings() exchange_config = config.fetch_exchange_config() notifier_config = config.fetch_notifier_config() behaviour_config = config.fetch_behaviour_config() # Set up logger logs.configure_logging(settings['loglevel'], settings['app_mode']) exchange_interface = ExchangeInterface(exchange_config) strategy_analyzer = StrategyAnalyzer(exchange_interface) notifier = Notifier(notifier_config) behaviour_manager = Behaviour(behaviour_config) behaviour = behaviour_manager.get_behaviour(settings['selected_task']) behaviour.run(settings['symbol_pairs'], settings['update_interval'], exchange_interface, strategy_analyzer, notifier)
def __configure_rsi_bot(self, behaviour_config): """Configures and returns the rsi bot behaviour class. Args: behaviour_config (dict): A dictionary of configuration values pertaining to the behaviour. Returns: RSIBot: A class of functionality for the rsi bot behaviour. """ exchange_interface = ExchangeInterface( self.config.get_exchange_config()) strategy_analyzer = StrategyAnalyzer(exchange_interface) notifier = Notifier(self.config.get_notifier_config()) db_handler = DatabaseHandler(self.config.get_database_config()) behaviour = RSIBot(behaviour_config, exchange_interface, strategy_analyzer, notifier, db_handler) return behaviour
def load_exchange(exchange): global config, market_data, fibonacci, new_results try: single_config = dict() single_config[exchange] = config.exchanges[exchange] single_exchange_interface = ExchangeInterface(single_config) single_market_data = dict() single_market_data[exchange] = market_data[exchange] behaviour = Behaviour(config, single_exchange_interface) new_result = behaviour.run(exchange, single_market_data, fibonacci, config.settings['output_mode']) new_results[exchange] = new_result[exchange] return True except Exception as exc: logger.info('Exception while processing exchange: %s', exchange) #logger.info('%s', exc) raise exc
# Load settings and create the config object config = Configuration() settings = config.settings # Set up logger logs.configure_logging(settings['log_level'], settings['log_mode']) logger = structlog.get_logger() update_interval = ceil(settings['update_interval'] / 60) logger.info('udate interval %d ', update_interval) config_indicators = config.indicators # Configure and run configured behaviour. exchange_interface = ExchangeInterface(config.exchanges) if settings['market_pairs']: market_pairs = settings['market_pairs'] logger.info("Found configured markets: %s", market_pairs) market_data = exchange_interface.get_exchange_markets(markets=market_pairs) else: logger.info("No configured markets, using all available on exchange.") market_data = exchange_interface.get_exchange_markets() #Dict to save user defined fibonacci levels fibonacci = None #Global Telegram Bot Updater updater = None
def main(): # Load settings and create the config object secrets = {} if os.path.isfile('secrets.json'): secrets = json.load(open('secrets.json')) config = json.load(open('default-config.json')) config.update(secrets) config['settings']['market_pairs'] = os.environ.get( 'MARKET_PAIRS', config['settings']['market_pairs']) config['settings']['loglevel'] = os.environ.get('LOGLEVEL', logging.INFO) config['exchanges']['bittrex']['required']['key'] = os.environ.get( 'BITTREX_KEY', config['exchanges']['bittrex']['required']['key']) config['exchanges']['bittrex']['required']['secret'] = os.environ.get( 'BITTREX_SECRET', config['exchanges']['bittrex']['required']['secret']) config['notifiers']['twilio']['required']['key'] = os.environ.get( 'TWILIO_KEY', config['notifiers']['twilio']['required']['key']) config['notifiers']['twilio']['required']['secret'] = os.environ.get( 'TWILIO_SECRET', config['notifiers']['twilio']['required']['secret']) config['notifiers']['twilio']['required'][ 'sender_number'] = os.environ.get( 'TWILIO_SENDER_NUMBER', config['notifiers']['twilio']['required']['sender_number']) config['notifiers']['twilio']['required'][ 'receiver_number'] = os.environ.get( 'TWILIO_RECEIVER_NUMBER', config['notifiers']['twilio']['required']['receiver_number']) config['notifiers']['gmail']['required']['username'] = os.environ.get( 'GMAIL_USERNAME', config['notifiers']['gmail']['required']['username']) config['notifiers']['gmail']['required']['password'] = os.environ.get( 'GMAIL_PASSWORD', config['notifiers']['gmail']['required']['password']) config['notifiers']['gmail']['required'][ 'destination_emails'] = os.environ.get( 'GMAIL_DESTINATION_EMAILS', config['notifiers']['gmail']['required']['destination_emails']) # Set up logger LOGGER = logging.getLogger(__name__) LOGGER.setLevel(config['settings']['loglevel']) LOG_FORMAT = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') LOG_HANDLE = logging.StreamHandler() LOG_HANDLE.setLevel(logging.DEBUG) LOG_HANDLE.setFormatter(LOG_FORMAT) LOGGER.addHandler(LOG_HANDLE) exchange_interface = ExchangeInterface(config) strategy_analyzer = StrategyAnalyzer(config) notifier = Notifier(config) # The coin pairs coin_pairs = [] if config['settings']['market_pairs']: coin_pairs = config['settings']['market_pairs'].translate( str.maketrans('', '', whitespace)).split(",") else: user_markets = exchange_interface.get_user_markets() for user_market in user_markets['info']: if 'BTC' in user_market['Currency']: continue market_pair = user_market['Currency'] + '/BTC' coin_pairs.append(market_pair) LOGGER.debug(coin_pairs) while True: get_signal(coin_pairs, strategy_analyzer, notifier)
def main(): with open("app/config.yml", 'r') as stream: data_loaded = yaml.load(stream) exchangeInterface = ExchangeInterface(data_loaded['exchanges']) exchange = list(data_loaded['exchanges'].keys())[0] market_pair = data_loaded['settings']['market_pairs'][0] interval = data_loaded['settings']['update_interval'] strategies = data_loaded['strategies'] engine = database.engine conn = engine.connect() params = cdl_back_test(exchangeInterface,exchange,market_pair,interval,data_loaded,max_periods=1000,pre_trained=True) database.reset_db() # get historical & live data for the strategies datafeed.start_ticker(exchangeInterface, exchange, market_pair, interval=interval) # start all the strategies strategy_result = start_strategy(exchange, market_pair, interval,strategies) ##################### ### RISK MANAGEMENT ##################### # Position control # let's conjure up a position book fake_position_data = pd.DataFrame({ 'exchange': 'gdax', 'symbol': 'BTC/USD', 'position': 'long', 'amount': 0.1, 'price': 3600}, index=range(1)) # TODO: use config file to list all the exchanges, market pair and update interval # that we need to pull for position control ohlcv_new = datafeed.get_latest_data_from_db(exchange, market_pair, interval) # Look at the collected market data s = db.select([database.OHLCV]) result = conn.execute(s) data = result.fetchall() df = pd.DataFrame(data) df.columns = result.keys() order_book_raw = exchangeInterface.get_order_book('gdax', 'BTC/USD') # purge orders - tbc from logics.risk_management import position_control position_control_simple = position_control.Position_Control(fake_position_data, ohlcv_new, 0.2, 0.2) # produce sell signals only rm_result = position_control_simple.control() # Order execution from logics.risk_management import order_control def order_execution(exchangeInterface, exchange, market_pair, position_book, order_book, orderType='market', ): if (strategy_result['sell'][0] or rm_result['sell'][0]): free_balance = exchangeInterface.get_free_balance(exchange) position = \ position_book.loc[(position_book.exchange == exchange) & (position_book.symbol == market_pair)]['position'][ 0] order_control_simple = order_control.Order_Control(exchange, market_pair, free_balance, position, position_book, order_book) return order_control_simple.simple_control() exec_price, exec_size = order_execution(exchangeInterface, exchange, market_pair, fake_position_data, order_book_raw) ###### ACTUAL ORDER EXECUTION # exchangeInterface.create_order(exchange,market_pair,'limit','buy',exec_size,exec_price) def start_strategy(exchange, market_pair, interval): buy = [] sell = [] def log_result(result): # This is called whenever start_strategy_ returns a result # result_list is modified only by the main process, not the pool workers. buy.append(result['buy']) sell.append(result['sell']) pool = Pool() for key, val in strategies.items(): Backtest_Optim = globals()[val['backtest_optim']] # extract the strategy assert interval == val['interval'], "strategy is not optimized for the given interval" params = {'trailing_window': val['trailing_window'], 'indicator': getattr(talib, val['indicator'])} pool.apply_async(start_strategy_, args=(exchange, market_pair, Backtest_Optim, params, interval), callback=log_result) pool.close() pool.join() return {'buy': buy, 'sell': sell} start_strategy(exchange, market_pair, interval) order_info = exchangeInterface.exchanges['gdax'].fetch_orders('BTC/USD')[0] def log_trade(exchangeInterface, exchange, orderID): """ :param exchangeInterface: :param exchange: :param market_pair: :param orderID: :return: True if the trade is logged, False otherwise """ status = None timeout = tm.time() + 120 while status != 'filled' and tm.time() < timeout: status = exchangeInterface.get_order_info(exchange, orderID)['status'] tm.sleep(exchange.rateLimit / 1000) if status == 'filled': info = exchangeInterface.get_order_info(exchange, orderID)['info'] with database.lock: ins = database.TradeBook.insert().values(info) conn.execute(ins) return True else: return False orderID = exchangeInterface.create_order()[0]['id'] log_trade(exchangeInterface, exchange, orderID)
def __init__(self, config): self.exchange_aggregator = ExchangeInterface(config) self.logger = structlog.get_logger()