def main(): from data.const import get_trade_calendar from datetime import time trade_cal = get_trade_calendar() def transform_tick(tick): ret = None if tick in ['000001', '000300']: ret = ''.join((tick, '_SH_IX')) else: ret = ''.join((tick, '_SZ_IX')) return ret today = str(datetime.now().date()) today_idx = trade_cal.index.searchsorted(today) yesterday = str(trade_cal.index[today_idx - 1].date()) tomorrow = str(trade_cal.index[today_idx + 1].date()) from_date, to_date = None, None if datetime.now().time() > time(15, 10, 0, 0): # 过了15:10 from_date, to_date = today, tomorrow else: from_date, to_date = yesterday, today index_list = ['000001', '000300', '399001', '399005', '399006'] get_config_func = make_config(CONFIG_FILE) ret = predict(ticks=index_list, look_back=519, index=True, get_config_func=get_config_func) ret = ret.sort_values(by='dt') print(ret) engines = [engine_company_local, engine_company_outer] for engine in engines: Base.metadata.bind = engine # Base.metadata.create_all(engine) Session.configure(bind=engine) session = Session() IndexPrediction().to_sql(session, ret) hist_rec = pd.read_sql_table('index_prediction', engine_company_outer) win_ratio = (hist_rec.dropna() .query('dt>"2014-01-01"') .groupby('secu') .apply(lambda df: np.sum( np.isclose(df.pred, df.fact)) * 1.0 / len(df)) .to_frame() .rename(columns={0: 'accuracy'}) .reset_index() ) tomorrow_pred = ret.query("""dt == '{}'""".format(to_date)) to_seven_liu = pd.merge(tomorrow_pred, win_ratio, on='secu') logger.info(u'明日预测结果:\n{}'.format(to_seven_liu))
def main(): from data.const import get_trade_calendar from datetime import time trade_cal = get_trade_calendar() def transform_tick(tick): ret = None if tick in ['000001', '000300']: ret = ''.join((tick, '_SH_IX')) else: ret = ''.join((tick, '_SZ_IX')) return ret today = str(datetime.now().date()) today_idx = trade_cal.index.searchsorted(today) yesterday = str(trade_cal.index[today_idx - 1].date()) tomorrow = str(trade_cal.index[today_idx + 1].date()) from_date, to_date = None, None if datetime.now().time() > time(15, 10, 0, 0): # 过了15:10 from_date, to_date = today, tomorrow else: from_date, to_date = yesterday, today # tomorrow = str(trade_cal.index[today_idx + 1].date()) index_list = ['000001', '000300', '399001', '399005', '399006'] # index_list = ['000001'] # today = str(datetime.now().date()) ret = [] for idx in index_list: ret.append(predict_history(idx, from_date=from_date, to_date=to_date, is_index=True, data_source=['mysql', 'local'], config_params=config_params)) df = pd.concat(ret) # df.index = df.index.format() df.reset_index(inplace=True) cols_to_rename = {'dates': 'dt', 'index': 'dt', 'prediction': 'pred', 'facts': 'fact', 'tick': 'secu'} df.rename(columns=cols_to_rename, inplace=True) logger.debug('df in predict local is \n{}'.format(df)) df.dt = df.dt.apply(lambda x: str(x.date())) df.secu = df.secu.apply(transform_tick) df = df.where(pd.notnull(df), None) df = df.loc[:, ['dt', 'secu', 'pred', 'fact']] print(df.sort_values(by=['dt', 'secu'])) # Session.configure(bind=engine_phil_machine_test) engines = [ # engine_phil_machine_test, engine_phil_machine, # engine_company_local, # engine_company_outer, ] for engine in engines: Base.metadata.bind = engine Base.metadata.create_all(engine) Session.configure(bind=engine) session = Session() IndexPrediction().to_sql(session, df)