def lts_price_analysis(date_str): print 'Enter Lts_price_analysis.' global session_common global filter_date_str server_host = server_constant_local.get_server_model('host') session_common = server_host.get_db_session('common') if date_str is None or date_str == '': filter_date_str = date_utils.get_today_str('%Y-%m-%d') else: filter_date_str = date_str build_instrument_db_dict() instrument_file_list = FileUtils(DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('HUABAO_INSTRUMENT', filter_date_str) market_file_list = FileUtils(DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('HUABAO_MARKET', filter_date_str) lts_file_list = [] lts_file_list.extend(instrument_file_list) lts_file_list.extend(market_file_list) for qd_file in lts_file_list: read_position_file_lts('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, qd_file)) update_structured_fund() update_db() session_common.commit() server_host.close() print 'Exit Lts_price_analysis.'
def ctp_price_analysis(date_str): print 'Enter ctp_price_analysis.' server_host = server_constant_local.get_server_model('host') global session_common session_common = server_host.get_db_session('common') global filter_date_str if date_str is None or date_str == '': filter_date_str = now.strftime('%Y-%m-%d') else: filter_date_str = date_str build_instrument_db_dict() instrument_file_list = FileUtils(DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('CTP_INSTRUMENT', filter_date_str) market_file_list = FileUtils(DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('CTP_MARKET', filter_date_str) ctp_file_list = [] ctp_file_list.extend(instrument_file_list) ctp_file_list.extend(market_file_list) for ctp_file in ctp_file_list: read_price_file_ctp('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, ctp_file)) set_main_submain() update_db() session_common.commit() server_host.close() print 'Exit ctp_price_analysis.'
def get_select_file_by_folder_key(folder, key): date = datetime.now().strftime('%Y%m%d') date = ''.join(date.split('-')) date_tab = '-'.join([date[:4], date[4:6], date[6:8]]) select_file_list = FileUtils(folder).filter_file(key, date) select_file_list_2 = FileUtils(folder).filter_file(key, date_tab) select_file_list.extend(select_file_list_2) select_file_list = map(lambda x: os.path.join(folder, x), select_file_list) return select_file_list
def lts_update_index(date_str): if date_str is None or date_str == '': filter_date_str = now.strftime('%Y-%m-%d') else: filter_date_str = date_str server_host = server_constant_local.get_server_model('host') session_common = server_host.get_db_session('common') instrument_list = [] sf_instrument_list = [] of_instrument_list = [] market_info_list = [] index_market_list = [] lts_file_list = FileUtils(DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('HUABAO_INSTRUMENT', filter_date_str) for lts_file_name in lts_file_list: lts_file_path = '%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, lts_file_name) temp_instrument_list, temp_sf_list, temp_of_list, temp_market_list, temp_index_market_list = \ __read_lts_file(lts_file_path) instrument_list.extend(temp_instrument_list) sf_instrument_list.extend(temp_sf_list) of_instrument_list.extend(temp_of_list) market_info_list.extend(temp_market_list) index_market_list.extend(temp_index_market_list) instrument_info_df = pd.DataFrame(instrument_list) exchange_id_df = instrument_info_df.apply(lambda row: __format_exchange_id(row['ExchangeID']), axis=1) instrument_info_df.insert(0, 'exchange_id', exchange_id_df) filter_instrument_df = instrument_info_df[instrument_info_df['exchange_id'].isin(filter_exchange_list)] filter_instrument_df = filter_instrument_df[filter_instrument_df['IsTrading'] == '1'] filter_instrument_df.loc[:, 'type_id'] = filter_instrument_df.apply(lambda row: __format_type_id(row['ProductID']), axis=1) common_stock_df = filter_instrument_df[filter_instrument_df['type_id'] == instrument_type_enums.CommonStock].copy() common_stock_df = __format_common_stock_df(common_stock_df) option_df = filter_instrument_df[filter_instrument_df['type_id'] == instrument_type_enums.Option].copy() option_df = __format_option_df(option_df, filter_date_str) convertible_bond_df = filter_instrument_df[filter_instrument_df['type_id'] == instrument_type_enums.ConvertableBond].copy() convertible_bond_df = __format_convertible_bond_df(convertible_bond_df) mmf_df = filter_instrument_df[filter_instrument_df['type_id'] == instrument_type_enums.MMF].copy() mmf_df = __format_mmf_df(mmf_df) format_instrument_df = pd.concat([common_stock_df, convertible_bond_df, option_df, mmf_df], sort=False).fillna('') instrument_db_dict = __query_instrument_dict(session_common) __add_instrument_db(session_common, instrument_db_dict, format_instrument_df) market_info_df = pd.DataFrame(market_info_list, columns=['InstrumentID', 'PreClosePrice', 'PreSettlementPrice', 'UpperLimitPrice', 'LowerLimitPrice', 'ClosePrice', 'Volume']) index_market_df = pd.DataFrame(index_market_list, columns=['InstrumentID', 'PreClosePrice', 'ClosePrice', 'Volume']) market_info_df = pd.concat([market_info_df, index_market_df], sort=False).fillna('') fund_instrument_df = __format_fund_instrument(sf_instrument_list, of_instrument_list) __update_instrument(session_common, instrument_db_dict, market_info_df, fund_instrument_df) session_common.commit()
def ctp_position_analysis(): print 'Enter ctp_position_analysis.' server_host = server_constant_local.get_server_model('host') global session_common session_common = server_host.get_db_session('common') global session_portfolio session_portfolio = server_host.get_db_session('portfolio') global session_om session_om = server_host.get_db_session('om') update_sql = "update portfolio.account_trade_restrictions set TODAY_OPEN = 0, TODAY_CANCEL=0 where 1=1" session_portfolio.execute(update_sql) ctp_position_file_list = FileUtils( DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('CTP_POSITION', filter_date_str) for ctp_file in ctp_position_file_list: read_position_file_ctp('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, ctp_file)) session_common.commit() session_portfolio.commit() session_om.commit() server_host.close() print 'Exit ctp_position_analysis.'
def lts_price_analysis_add(date_str): print 'Enter Lts_price_analysis_add.' global session_common global filter_date_str server_host = server_constant_local.get_server_model('host') session_common = server_host.get_db_session('common') if date_str is None or date_str == '': filter_date_str = now.strftime('%Y-%m-%d') else: filter_date_str = date_str build_instrument_db_dict() get_instrument_max_id() instrument_file_list = FileUtils( DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('HUABAO_INSTRUMENT', filter_date_str) for qd_file in instrument_file_list: read_position_file_lts('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, qd_file)) session_common.commit() server_host.close() insert_server_db() print 'Exit Lts_price_analysis_add.'
def run_common_screen_cmd(service, cmd): screen_manager(service, cmd) today = datetime.now().strftime('%Y%m%d') select_file_list = FileUtils(tradeplat_log_folder).filter_file( service, today) time.sleep(5) # too fast will not record info read_mainframe_log = [ 'cd %s;tail -200 %s' % (tradeplat_log_folder, select_file_list[-1]) ] log_info = host_servert.run_cmd_str(read_mainframe_log) pattern = 'Receive Command=[%s]' % cmd log_info = log_info.split('\n') record_info = [] record_or_not = False for line in log_info: line = line.replace('\n', '') line = line.replace('\r', '') if line == '': continue if line == pattern: record_or_not = True if record_or_not: record_info.append(line) result = '\n'.join(record_info) return result
def ctp_price_analysis_add(date_str): print 'Enter ctp_price_analysis_add.' global session_common global session_basicinfo global filter_date_str server_host = server_constant_local.get_server_model('host') session_common = server_host.get_db_session('common') session_basicinfo = server_host.get_db_session('basic_info') if date_str is None or date_str == '': filter_date_str = now.strftime('%Y-%m-%d') else: filter_date_str = date_str instrument_file_list = FileUtils( DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('CTP_INSTRUMENT', filter_date_str) build_future_db_dict() get_instrument_max_id() for ctp_file in instrument_file_list: read_price_file_ctp('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, ctp_file)) server_host.close() insert_server_db() print 'Exit ctp_price_analysis_add.'
def show_log(select_log, show_num='20'): filename = '' file_folder = '' server_list = get_service_list('host') server_list = [x.app_name for x in server_list] if select_log in server_list: file_folder = tradeplat_log_folder date = datetime.now().strftime('%Y%m%d') file_list = FileUtils(file_folder).filter_file(select_log, date) if len(file_list) == 0: return '%s has no log' % select_log filename = file_list[-1] print file_folder, filename elif select_log in os.listdir(datafetcher_messagefile_folder): file_folder = datafetcher_messagefile_folder filename = select_log elif select_log == 'DataFetcher': return 'never work now' elif select_log == 'tool_log': file_folder = eod_log_path filename = 'tool.log' elif select_log == 'eod_log': file_folder = eod_log_path filename = 'eod.log' elif select_log == 'account_log': file_folder = eod_log_path filename = 'account.log' elif select_log == 'cmd_log': file_folder = eod_log_path filename = 'cmd.log' # show_log_cmd = 'cd %s;tail -%s %s' % (file_folder, show_num, filename) show_file_path = os.path.join(file_folder, filename) file_ = file(show_file_path, 'r') content = file_.readlines() content = content[-1 * int(show_num):] content = map(lambda x: x.replace('\n', ''), content) # content = host_server_constant.run_shell_cmd([show_log_cmd]) print '=======================' # content = content.split('\n') content = '<br>'.join(content) if content == '': content = '%s has no content' % filename return content
def lts_position_analysis(): print 'Enter Lts_position_analysis.' server_host = server_constant_local.get_server_model('host') global session_portfolio session_portfolio = server_host.get_db_session('portfolio') global session_om session_om = server_host.get_db_session('om') filter_date_str = date_utils.get_today_str('%Y-%m-%d') lts_position_file_list = FileUtils(DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('HUABAO_POSITION', filter_date_str) for lts_file in lts_position_file_list: read_position_file_lts('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, lts_file), filter_date_str) session_portfolio.commit() session_om.commit() server_host.close() print 'Exit Lts_position_analysis.'
def __account_position_enter(add_flag): print 'Enter xtp_position_analysis.' global session_portfolio, session_om server_host = server_constant_local.get_server_model('host') session_portfolio = server_host.get_db_session('portfolio') session_om = server_host.get_db_session('om') xtp_position_file_list = FileUtils( DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('PROXYXTP_POSITION', filter_date_str) for xtp_file in xtp_position_file_list: read_position_file( '%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, xtp_file), add_flag) session_portfolio.commit() session_om.commit() server_host.close() print 'Exit xtp_position_analysis.'
def stk_position_analysis(): print 'Enter Stk_position_analysis.' host_server_model = server_constant_local.get_server_model('host') global session_om session_om = host_server_model.get_db_session('om') global session_portfolio session_portfolio = host_server_model.get_db_session('portfolio') stk_position_file_list = FileUtils( DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('PROXYKMA_POSITION', today_str) for stk_file in stk_position_file_list: read_position_file_stk('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, stk_file)) session_om.commit() session_portfolio.commit() print 'Exit Stk_position_analysis.'
def guosen_position_analysis(): print 'Enter guosen_position_analysis.' server_host = server_constant_local.get_server_model('host') global session_portfolio session_portfolio = server_host.get_db_session('portfolio') global session_om session_om = server_host.get_db_session('om') guosen_position_file_list = FileUtils( DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('GUOXIN_POSITION', today_str) for guosen_file in guosen_position_file_list: read_position_file_guosen( '%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, guosen_file)) session_portfolio.commit() session_om.commit() server_host.close() print 'Exit guosen_position_analysis.'
def ts_position_analysis(): print 'Enter ts_position_analysis.' server_host = server_constant_local.get_server_model('host') global session_common session_common = server_host.get_db_session('common') global session_portfolio session_portfolio = server_host.get_db_session('portfolio') global session_om session_om = server_host.get_db_session('om') __build_ticker_exchange(server_host) ts_position_file_list = FileUtils( DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('ysposition', filter_date_str) for ts_file in ts_position_file_list: read_position_file_ts('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, ts_file)) session_common.commit() session_portfolio.commit() session_om.commit() server_host.close() print 'Exit ts_position_analysis.'
def ctp_update_index(date_str): if date_str is None or date_str == '': filter_date_str = now.strftime('%Y-%m-%d') else: filter_date_str = date_str server_host = server_constant_local.get_server_model('host') session_common = server_host.get_db_session('common') session_basicinfo = server_host.get_db_session('basic_info') trading_df = __query_trading_df(session_basicinfo) instrument_info_list = [] market_info_list = [] ctp_file_list = FileUtils(DATAFETCHER_MESSAGEFILE_FOLDER).filter_file( 'CTP_INSTRUMENT', filter_date_str) for ctp_file_name in ctp_file_list: ctp_file_path = '%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, ctp_file_name) temp_instrument_list, temp_market_list = __read_ctp_file(ctp_file_path) instrument_info_list.extend(temp_instrument_list) market_info_list.extend(temp_market_list) instrument_info_df = pd.DataFrame(instrument_info_list) market_info_df = pd.DataFrame(market_info_list, columns=[ 'InstrumentID', 'PreClosePrice', 'PreSettlementPrice', 'UpperLimitPrice', 'LowerLimitPrice', 'ClosePrice', 'Volume' ]) filter_instrument_df = instrument_info_df[ instrument_info_df['ProductClass'].isin(['1', '2'])] exchange_id_df = filter_instrument_df.apply( lambda row: __format_exchange_id(row['ExchangeID']), axis=1) filter_instrument_df.insert(0, 'exchange_id', exchange_id_df) filter_instrument_df = filter_instrument_df[ filter_instrument_df['exchange_id'].isin(future_exchange_list)] filter_instrument_df.loc[:, 'type_id'] = filter_instrument_df.apply( lambda row: __format_type_id(row['ProductClass']), axis=1) filter_instrument_df.loc[:, 'market_sector_id'] = 1 filter_instrument_df.loc[:, 'market_status_id'] = 2 filter_instrument_df.loc[:, 'round_lot_size'] = 1 filter_instrument_df.loc[:, 'crncy'] = 'CNY' # 临到期cu的round_lot_size单独设置 filter_instrument_df.loc[(filter_instrument_df['ProductID'] == 'cu') & ( filter_instrument_df['ExpireDate'][:7] == now.strftime('%Y-%m')), 'round_lot_size'] = 5 # filter_instrument_df.loc[filter_instrument_df['LongMarginRatio'] > 10000, 'LongMarginRatio'] = 0 # filter_instrument_df.loc[filter_instrument_df['ShortMarginRatio'] > 10000, 'ShortMarginRatio'] = 0 filter_instrument_df.loc[:, 'put_call'] = None filter_instrument_df.loc[:, 'strike'] = None filter_instrument_df.loc[:, 'option_margin_factor1'] = None filter_instrument_df.loc[:, 'option_margin_factor2'] = None filter_instrument_df.loc[:, 'session_ticker_type'] = filter_instrument_df.\ apply(lambda row: __format_session_ticker_type(row['type_id'], row['ProductID'], row['UnderlyingInstrID']), axis=1) filter_instrument_df = pd.merge(filter_instrument_df, trading_df, how='left', on=['session_ticker_type']) filter_instrument_df = pd.merge(filter_instrument_df, market_info_df, how='left', on=['InstrumentID']) future_instrument_df = filter_instrument_df[ filter_instrument_df['type_id'] == instrument_type_enums.Future].copy() future_instrument_df = __format_future_df(future_instrument_df) option_instrument_df = filter_instrument_df[ filter_instrument_df['type_id'] == instrument_type_enums.Option].copy() option_instrument_df = __format_option_df(option_instrument_df, future_instrument_df) format_instrument_df = pd.concat( [future_instrument_df, option_instrument_df]) instrument_db_dict = __query_instrument_dict(session_common) __save_instrument(session_common, instrument_db_dict, format_instrument_df) # instrument_df['InstrumentID'] = instrument_df['InstrumentID'].astype(str) # instrument_index_df = instrument_df.set_index(['InstrumentID', ]) session_common.commit()
dict_key = '%s|%s' % (future.ticker, future.exchange_id) shf_db_dict[dict_key] = future def insert_server_db(): if len(instrument_insert_list) == 0: return for server_model in server_constant_local.get_all_server(): server_session = server_model.get_db_session('common') for future_db in instrument_insert_list: server_session.add(future_db.copy()) server_session.commit() if __name__ == '__main__': print 'Enter femas_price_analysis_add.' server_host = server_constant_local.get_server_model('host') session_common = server_host.get_db_session('common') build_future_db_dict() get_instrument_max_id() femas_td_file_list = FileUtils(DATAFETCHER_MESSAGEFILE_FOLDER).filter_file( 'Femas_TD', today_str) for femas_td_file in femas_td_file_list: read_price_file_femas('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, femas_td_file)) session_common.commit() server_host.close() insert_server_db() print 'Exit femas_price_analysis_add.'
session_om.add(order_db) for trade_db in trade_list: session_om.add(trade_db) for position_db in position_list: session_portfolio.add(position_db) def update_position_prev_net(account_id): update_sql = "Update portfolio.account_position Set PREV_NET = YD_POSITION_LONG - YD_POSITION_SHORT where ID=%s \ and DATE='%s' and symbol != 'CNY'" % (account_id, today_str) session_portfolio.execute(update_sql) if __name__ == '__main__': print 'Enter femas_position_analysis.' server_host = server_constant_local.get_server_model('host') session_portfolio = server_host.get_db_session('portfolio') session_om = server_host.get_db_session('om') femas_position_file_list = FileUtils( DATAFETCHER_MESSAGEFILE_FOLDER).filter_file('FEMAS_POSITION', today_str) for femas_file in femas_position_file_list: read_position_file_femas('%s/%s' % (DATAFETCHER_MESSAGEFILE_FOLDER, femas_file)) session_portfolio.commit() session_om.commit() server_host.close() print 'Exit femas_position_analysis.'
def rebuild_index(self): FileUtils(self.__target_folder).clear_folder() self.__rebuild_vwap_factor()