def __build_db_dict(self, server_host): type_list = [ Instrument_Type_Enums.Future, Instrument_Type_Enums.CommonStock ] self.__instrument_dict = query_instrument_dict('host', type_list) special_ticker_list = [] session_jobs = server_host.get_db_session('jobs') for special_ticker in session_jobs.query(SpecialTickers).filter( SpecialTickers.date == self.__date_str): special_ticker_list.append(special_ticker) self.__suspend_stock_list = [ x.ticker for x in special_ticker_list if 'Suspend' in x.describe ] custom_log.log_info_job('Suspend Stock List:%s' % ','.join(self.__suspend_stock_list)) self.__st_stock_list = [ x.ticker for x in special_ticker_list if 'ST' in x.describe ] custom_log.log_info_job('ST Stock List:%s' % ','.join(self.__st_stock_list)) self.__low_stop_stock_list = [ x.ticker for x in special_ticker_list if 'Low_Stop' in x.describe ] custom_log.log_info_job('Low_Stop Stock List:%s' % ','.join(self.__low_stop_stock_list)) self.__high_stop_stock_list = [ x.ticker for x in special_ticker_list if 'High_Stop' in x.describe ] custom_log.log_info_job('High_Stop Stock List:%s' % ','.join(self.__high_stop_stock_list))
def index_constitute_report_job(server_list, date_str=None): if date_str is None: date_str = date_utils.get_next_trading_day('%Y-%m-%d') global server_host, instrument_db_dict server_host = server_constant.get_server_model('host') instrument_db_dict = query_instrument_dict('host') fund_dict = query_fund_dict(server_list, date_str) report_message_list = daily_return_calculation(fund_dict, date_str) report_message_list.sort() html_list = __build_collection_html(report_message_list) title = 'server_name,fund_name,strategy_group_name,stock_value_total,hedge_value_total,net_value,\ CSI300_value,ZZ500_value,Other_value,CSI300_weight,ZZ500_weight,Other_weight,IF,IC,CSI300_IF,ZZ500_IC,\ Other_IC,IF_diff,IC_idff' report_message_list.insert(0, title) file_save_path = '%s/log/report.csv' % EOD_PROJECT_FOLDER with open(file_save_path, 'w') as fr: fr.write('\n'.join(report_message_list)) email_utils4.send_email_path(u'股票仓位分指数构成报告', html_list, file_save_path, 'html') server_host.close()
def __build_report_files(self): global instrument_db_dict instrument_db_dict = query_instrument_dict('host', [Instrument_Type_Enums.CommonStock, ]) log_file_list = [x for x in os.listdir(self.__unzip_folder_path)] log_file_list.sort() position_list = [] order_list = [] for log_file_name in log_file_list: if 'StrategyLoader' not in log_file_name: continue log_file_path = '%s/%s' % (self.__unzip_folder_path, log_file_name) with open(log_file_path) as fr: for line in fr.readlines(): if 'OnStratStart' in line: param_set_dict = self.__format_param_line(line) if not param_set_dict: continue param_set = param_set_dict['param_set'] elif 'SIDSImpl' in line and 'Info:' in line: position_list.append(line) elif 'SIDSImpl' in line and 'Posi:' in line: position_list.append(line) elif 'SIDSImpl' in line and 'Event:' in line: order_list.append(line) for account_name in self.__account_list: filter_position_list = [x for x in position_list if account_name in x] filter_order_list = [x for x in order_list if account_name in x] self.__read_message_list(account_name, filter_position_list, filter_order_list, param_set)
def __query_instrument_df(): instrument_dict = query_instrument_dict( 'host', [Instrument_Type_Enums.Index, Instrument_Type_Enums.CommonStock]) instrument_list = [[str(k), float(v.prev_close)] for (k, v) in instrument_dict.items() if v.prev_close is not None] instrument_df = pd.DataFrame(instrument_list, columns=['symbol', 'prev_close']) return instrument_df
def algo_file_compare_job(): now_date_str = date_utils.get_today_str('%Y-%m-%d') type_list = [ Instrument_Type_Enums.CommonStock, ] global instrument_dict instrument_dict = query_instrument_dict('host', type_list) server_host = server_constant.get_server_model('host') email_message_list = [] session_jobs = server_host.get_db_session('jobs') query = session_jobs.query(StrategyAccountInfo) for strategy_account_info_db in query: pf_account_dict = dict() server_name = strategy_account_info_db.server_name server_model = server_constant.get_server_model(server_name) session_portfolio = server_model.get_db_session('portfolio') query_pf_account = session_portfolio.query(PfAccount) for pf_account_db in query_pf_account.filter( PfAccount.group_name == strategy_account_info_db.group_name): pf_account_dict[pf_account_db.fund_name] = pf_account_db for number_str in strategy_account_info_db.all_number.split(','): fund_name = '%s_%s-%s-%s-' % ( strategy_account_info_db.strategy_name, number_str, strategy_account_info_db.group_name, strategy_account_info_db.fund) target_position_list = __get_target_position_list( server_name, fund_name) pf_position_list = __get_pf_position_list(server_name, fund_name, pf_account_dict) fund_compare_result, trade_info_list, error_flag = __compare_strategy_position( pf_position_list, target_position_list) if error_flag: email_message_list.append('<h>%s</h>' % fund_name) email_message_list.extend(fund_compare_result) if len(trade_info_list) == 0: continue classa_folder_path = '%s/%s/%s_repair' % ( STOCK_SELECTION_FOLDER, server_name, now_date_str.replace('-', '')) if not os.path.exists(classa_folder_path): os.mkdir(classa_folder_path) file_path = '%s/%s.txt' % (classa_folder_path, fund_name) with open(file_path, 'w+') as fr: fr.write('\n'.join(trade_info_list)) server_model.close() email_utils8.send_email_group_all('待交易股票列表', ''.join(email_message_list), 'html') server_host.close()
def __load_from_db(self): self.__future_dict = query_instrument_dict('host', [ Instrument_Type_Enums.Future, ]) with StockWindUtils() as stock_wind_utils: ticker_type_list = [ const.INSTRUMENT_TYPE_ENUMS.CommonStock, const.INSTRUMENT_TYPE_ENUMS.Future ] common_ticker_list = stock_wind_utils.get_ticker_list( ticker_type_list) self.__ticker_price_dict = stock_wind_utils.get_close_dict( self.__date_str, common_ticker_list)
def __init__(self, server_name): self.email_content_list = [] self.server_name = server_name self.date_str = date_utils.get_today_str('%Y-%m-%d') self.date_str2 = date_utils.get_today_str('%Y%m%d') self.multifactor_parameter_list = [] self.leadLag_parameter_list = [] self.basket_parameter_list = [] self.tradeplat_file_folder = TRADEPLAT_FILE_FOLDER_TEMPLATE % server_name self.local_backup_folder = STOCK_INTRADAY_BACKUP_FOLDER if not os.path.exists(self.tradeplat_file_folder): os.mkdir(self.tradeplat_file_folder) self.parameter_dict_file_path = self.tradeplat_file_folder + '/parameter_dict.csv' self.multifactor_folder = self.tradeplat_file_folder + '/MultiFactorWeightData' self.cfg_folder = self.tradeplat_file_folder + '/cfg' self.highfreq_folder = self.cfg_folder + '/intraday_multifactor/HighFreqCalculator' self.leadlag_folder = self.cfg_folder + '/intraday_leadlag/HighFreqCalculator' for temp_folder in (self.multifactor_folder, self.highfreq_folder, self.leadlag_folder): if os.path.exists(temp_folder): shutil.rmtree(temp_folder, True) os.mkdir(temp_folder) type_list = [Instrument_Type_Enum.CommonStock, ] self.instrument_dict = query_instrument_dict('host', type_list) self.parameter_title_list = [] self.parameter_dict = dict() with open(self.parameter_dict_file_path, 'rb') as fr: file_items = fr.readlines() for i in range(0, len(file_items)): if i == 0: self.parameter_title_list = file_items[i].replace('\n', '').split(',') else: line_items = file_items[i].replace('\n', '').split(',') self.parameter_dict[line_items[0]] = file_items[i].replace('\n', '')
def __build_phone_trades(server_name, compare_result_list, pf_account_dict, pf_position_list): pf_position_dict = dict() for pf_position_info in pf_position_list: pf_account_id, fund, ticker, pf_long, pf_short = pf_position_info dict_key = '%s|%s' % (fund, ticker) pf_position_dict.setdefault(dict_key, []).append(pf_position_info) default_account_dict = dict() for (account_id, pf_account_db) in pf_account_dict.items(): if pf_account_db.group_name == 'manual': fund_name = pf_account_db.fund_name.split('-')[2] default_account_dict[fund_name] = pf_account_db instrument_dict = query_instrument_dict('host') phone_trade_list = [] for compare_info_item in compare_result_list: fund, ticker, real_long, real_short, pf_long, pf_short, diff = compare_info_item if ticker not in instrument_dict: custom_log.log_error_job('Server:%s, UnFind Ticker:%s' % (server_name, ticker)) continue instrument_db = instrument_dict[ticker] # *暂只处理股票的不一致 if instrument_db.type_id != Instrument_Type_Enums.CommonStock: continue # 期货和期权需要单独设置tradetype future_flag = False if instrument_db.type_id in [ Instrument_Type_Enums.Future, Instrument_Type_Enums.Option ]: future_flag = True prev_close = instrument_db.prev_close if prev_close is None: continue if pf_long > real_long: diff_long = pf_long - real_long find_key = '%s|%s' % (fund, ticker) pf_position_list = pf_position_dict[find_key] for pf_position_info in pf_position_list: pf_account_id, fund, ticker, item_long, item_short = pf_position_info if item_long == 0: continue pf_account_info = pf_account_dict[pf_account_id] phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = fund phone_trade_info.strategy1 = '%s.%s' % ( pf_account_info.group_name, pf_account_info.name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Sell phone_trade_info.tradetype = Trade_Type_Enums.Close if future_flag else Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = min(item_long, diff_long) phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = server_name phone_trade_info.exprice = prev_close phone_trade_list.append(phone_trade_info) diff_long = max(diff_long - item_long, 0) if diff_long == 0: break if real_long > pf_long: default_pf_account = default_account_dict[fund] phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = fund phone_trade_info.strategy1 = '%s.%s' % ( default_pf_account.group_name, default_pf_account.name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Buy phone_trade_info.tradetype = Trade_Type_Enums.Open if future_flag else Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = real_long - pf_long phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = server_name phone_trade_info.exprice = prev_close phone_trade_list.append(phone_trade_info) if pf_short > real_short: diff_short = pf_short - real_short find_key = '%s|%s' % (fund, ticker) pf_position_list = pf_position_dict[find_key] for pf_position_info in pf_position_list: pf_account_id, fund, ticker, item_long, item_short = pf_position_info if item_short == 0: continue pf_account_info = pf_account_dict[pf_account_id] phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = fund phone_trade_info.strategy1 = '%s.%s' % ( pf_account_info.group_name, pf_account_info.name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Buy phone_trade_info.tradetype = Trade_Type_Enums.Close if future_flag else Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = min(item_short, diff_short) phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = server_name phone_trade_info.exprice = prev_close phone_trade_list.append(phone_trade_info) diff_short = max(diff_short - item_short, 0) if diff_short == 0: break if real_short > pf_short: default_pf_account = default_account_dict[fund] phone_trade_info = PhoneTradeInfo() phone_trade_info.fund = fund phone_trade_info.strategy1 = '%s.%s' % ( default_pf_account.group_name, default_pf_account.name) phone_trade_info.symbol = ticker phone_trade_info.direction = Direction_Enums.Sell phone_trade_info.tradetype = Trade_Type_Enums.Open if future_flag else Trade_Type_Enums.Normal phone_trade_info.hedgeflag = Hedge_Flag_Type_Enums.Speculation phone_trade_info.exqty = real_short - pf_short phone_trade_info.iotype = IO_Type_Enums.Inner1 phone_trade_info.server_name = server_name phone_trade_info.exprice = prev_close phone_trade_list.append(phone_trade_info) return phone_trade_list
def __get_prev_close_dict(): type_list = [ Instrument_Type_Enums.CommonStock, ] instrument_dict = query_instrument_dict('host', type_list) return instrument_dict