def __init__(self): """ Futu Trading Engine Constructor """ self.config = configparser.ConfigParser() self.config.read("config.ini") self.quote_ctx = OpenQuoteContext( host=self.config['FutuOpenD.Config'].get('Host'), port=self.config['FutuOpenD.Config'].getint('Port')) self.trade_ctx = OpenHKTradeContext( host=self.config['FutuOpenD.Config'].get('Host'), port=self.config['FutuOpenD.Config'].getint('Port')) self.username = self.config['FutuOpenD.Credential'].get('Username') # self.password = self.config['FutuOpenD.Credential'].get('Password') self.password_md5 = self.config['FutuOpenD.Credential'].get( 'Password_md5') self.futu_data = data_engine.DatabaseInterface( database_path=self.config['Database'].get('Database_path')) self.default_logger = logger.get_logger("futu_trade") self.trd_env = TrdEnv.REAL if self.config.get( 'FutuOpenD.Config', 'TrdEnv') == 'REAL' else TrdEnv.SIMULATE # Futu-Specific Variables self.market_list = [ Market.HK, Market.US, Market.SH, Market.SZ, Market.HK_FUTURE, Market.SG, Market.JP ] self.security_type_list = [ SecurityType.BOND, SecurityType.BWRT, SecurityType.STOCK, SecurityType.WARRANT, SecurityType.IDX, SecurityType.ETF, SecurityType.FUTURE, SecurityType.PLATE, SecurityType.PLATESET ] self.reference_type_list = [ SecurityReferenceType.WARRANT, SecurityReferenceType.FUTURE ]
def update_filtered_equity_pools(self): """ Use User-Defined Filters to filter bad equities away. Based on history data extracted from Yahoo Finance :return: Filtered Stock Code List in Futu Stock Code Format """ pool = Pool(cpu_count()) filtered_stock_list = pool.map(self.validate_stock_individual, self.full_equity_list) pool.close() pool.join() # filtered_stock_list = [] # for stock_code in self.full_equity_list: # filtered_stock_list.append(self.validate_stock_individual(stock_code)) # Remove Redundant Records (If Exists) database = data_engine.DatabaseInterface( database_path=self.config['Database'].get('Database_path')) database.delete_stock_pool_from_date(date.today().strftime("%Y-%m-%d")) database.commit() # Flatten Nested List for sublist in filtered_stock_list: for record in sublist: database.add_stock_pool(date.today().strftime("%Y-%m-%d"), record[0], record[1]) self.default_logger.info( f"Added Filtered Stock {record[1]} based on Filter {record[0]}" ) database.commit()
def main(): # Initialize Argument Parser parser = argparse.ArgumentParser() parser.add_argument("-u", "--update", help="Daily Update Data (Execute Before Market Starts)", action="store_true") parser.add_argument("-fu", "--force_update", help="Force Update All Data Up to Max. Allowed Years (USE WITH CAUTION)", action="store_true") parser.add_argument("-d", "--database", help="Store All CSV Data to Database", action="store_true") # Retrieve file names for all strategies as the argument option strategy_list = [Path(file_name).name[:-3] for file_name in glob.glob("./strategies/*.py") if "__init__" not in file_name and "Strategies" not in file_name] parser.add_argument("-s", "--strategy", type=str, choices=strategy_list, help="Execute HFT using Pre-defined Strategy") # Retrieve file names for all strategies as the argument option filter_list = [Path(file_name).name[:-3] for file_name in glob.glob("./filters/*.py") if "__init__" not in file_name and "Filters" not in file_name] parser.add_argument("-f", "--filter", type=str, choices=filter_list, nargs="+", help="Filter Stock List based on Pre-defined Filters") parser.add_argument("-en", "--email_name", type=str, help="Name of the applied stock filtering techniques") # Evaluate Arguments args = parser.parse_args() # Initialization Connection futu_trade = trading_engine.FutuTrade() email_handler = email_engine.Email() # Initialize Config Parser config = configparser.ConfigParser() config.read("config.ini") # Initialize Stock List stock_list = json.loads(config.get('TradePreference', 'StockList')) if not stock_list: stock_list = data_engine.DatabaseInterface( database_path=config.get('Database', 'Database_path')).get_stock_list() if args.filter: filtered_stock_list = init_stock_filter(args.filter) filtered_stock_dict = YahooFinanceInterface.get_stocks_email(filtered_stock_list) subscription_list = json.loads(config.get('Email', 'SubscriptionList')) for subscriber in subscription_list: filter_name = args.email_name if args.email_name else "Default Stock Filter" email_handler.write_daily_stock_filter_email(subscriber, filter_name, filtered_stock_dict) if args.update: # Daily Update Data daily_update_data(futu_trade=futu_trade, stock_list=stock_list, force_update=args.force_update) if args.database: # Update ALl Data to Database futu_trade.store_all_data_database() if args.strategy: # Initialize Strategies stock_list = filtered_stock_list if args.filter else stock_list stock_list.extend(data_engine.YahooFinanceInterface.get_top_30_hsi_constituents()) init_day_trading(futu_trade, stock_list, args.strategy) futu_trade.display_quota()
def __init__(self): """ Futu Trading Engine Constructor """ self.config = configparser.ConfigParser() self.config.read("config.ini") self.quote_ctx = OpenQuoteContext( host=self.config['FutuOpenD.Config'].get('Host'), port=self.config['FutuOpenD.Config'].getint('Port')) self.trade_ctx = OpenHKTradeContext( host=self.config['FutuOpenD.Config'].get('Host'), port=self.config['FutuOpenD.Config'].getint('Port')) self.username = self.config['FutuOpenD.Credential'].get('Username') # self.password = self.config['FutuOpenD.Credential'].get('Password') self.password_md5 = self.config['FutuOpenD.Credential'].get( 'Password_md5') self.futu_data = data_engine.DatabaseInterface( database_path=self.config['Database'].get('Database_path')) self.default_logger = logger.get_logger("futu_trade") self.trd_env = TrdEnv.REAL if self.config.get( 'FutuOpenD.Config', 'TrdEnv') == 'REAL' else TrdEnv.SIMULATE
def daily_update_data(futu_trade, force_update: bool = False): # Daily Update Filtered Security # filters = list(__init_filter(filter_name='all')) # stock_filter = StockFilter(stock_filters=filters) # stock_filter.update_filtered_equity_pools() # Daily Update Stock Info (Need to Rethink!!!) # stock_filter.update_stock_info() # Daily Update HKEX Security List & Subscribed Data data_engine.HKEXInterface.update_security_list_full() # Daily Update FuTu Historical Data stock_list = data_engine.DatabaseInterface( database_path='./database/stock_data.sqlite').get_stock_list() for stock_code in stock_list: futu_trade.update_DW_data(stock_code, force_update=force_update, k_type=KLType.K_DAY) futu_trade.update_DW_data(stock_code, force_update=force_update, k_type=KLType.K_WEEK) futu_trade.update_1M_data(stock_code, force_update=force_update)