Esempio n. 1
0
def sys_risk_analyse(is_realtime_update=False):
    """ 
    when less then -ultimate_coefficient*sys_risk_range, will be zero position; 
    when greater then ultimate_coefficient*sys_risk_range, will be max position; 
    others, will be linearly increased by sys_score
    """
    """
    :param max_position: float type,
    :param ultimate_coefficient: float type,
    :return: position,sys_score,is_sys_risk
    """
    shzh_weight=0.65
    #is_sys_risk=False
    shz_code_str='999999'
    print(shz_code_str,'----------------------------------')
    shz_stock=tds.Stockhistory(shz_code_str,'D')
    #print(shz_stock.h_df.tail(10))
    if is_realtime_update:
        k_data=get_real_stock_k(shz_code_str='999999')
        k_data=['2016/04/21',2954.38,2990.69,2935.05,3062.58,189000000,2.1115e+11]
        shz_stock.update_realtime_hist_df(k_data)
    #print(shz_stock.h_df.tail(10))
    #shangzheng_ma_score,shangzheng_score,k_position=shz_stock.get_market_score()
    shz_stock.form_temp_df(shz_code_str)
    #print(shangzheng_ma_score,shangzheng_score,k_position)
    scz_code_str='399001'
    zxb_code_str='399005'
    chy_code_str='399006'
    chy_code_str='300044'
    print(chy_code_str,'----------------------------------')
    chy_stock=tds.Stockhistory(chy_code_str,'D')
    if is_realtime_update:
        k_data=get_real_stock_k(shz_code_str='399006')
        #k_data=['2016/04/21',2954.38,2990.69,2935.05,3062.58,189000000,2.1115e+11]
        shz_stock.update_realtime_hist_df(k_data)
    #chuangye_ma_score,chuangye_score,k_position=chy_stock.get_market_score()
    chy_stock.form_temp_df(chy_code_str)
    #print(chuangye_ma_score,chuangye_score,k_position)
    #sys_score=round(0.65*shangzheng_score+0.35*chuangye_score,2)  #-5 ~5
    chy_first_date=chy_stock.temp_hist_df.head(1).iloc[0].date
    shz_temp_df=shz_stock.temp_hist_df.set_index('date')
    chy_temp_df=chy_stock.temp_hist_df.set_index('date')
    #shz_temp_df=shz_stock.temp_hist_df.tail(1000).set_index('date')
    #chy_temp_df=chy_stock.temp_hist_df.tail(1000).set_index('date')
    shz_temp_df=shz_temp_df.fillna(0)
    chy_temp_df=chy_temp_df.fillna(0)
    shz_temp_df['sys_score']=shzh_weight*shz_temp_df['k_score']+(1-shzh_weight)*chy_temp_df['k_score']
    shz_temp_df['position']=shzh_weight*shz_temp_df['position']+(1-shzh_weight)*chy_temp_df['position']
    shz_temp_df['operation']=shz_temp_df['position']-shz_temp_df['position'].shift(1)
    shz_temp_df.to_csv('shz_temp_df.csv')
    sys_df=shz_temp_df[['sys_score','position','operation']].round(3)
    sys_df.to_csv('sys.csv')
    return sys_df
Esempio n. 2
0
def back_test_one_stock(stock_symbol,
                        rate_to_confirm=0.0001,
                        temp_dir=fc.ALL_TEMP_DIR,
                        bs_temp_dir=fc.ALL_BACKTEST_DIR):
    if stock_symbol == '000029' and source == 'easyhistory':
        return
    s_stock = tds.Stockhistory(stock_symbol,
                               'D',
                               test_num=0,
                               source='yh',
                               rate_to_confirm=0.01)
    if s_stock.h_df.empty:
        print('New stock %s and no history data' % stock_symbol)
        return
    result_df = s_stock.form_temp_df(stock_symbol)
    s_stock.form_regression_result(save_dir=bs_temp_dir,
                                   rate_to_confirm=0.0001)
    #recent_trend = s_stock.get_recent_trend(num=20,column='close')
    s_stock.diff_ma_score(ma=[10, 30, 60, 120, 250],
                          target_column='close',
                          win_num=5)
    temp_hist_df = s_stock.temp_hist_df.set_index('date')
    try:
        temp_hist_df.to_csv(temp_dir + '%s.csv' % stock_symbol)
    except:
        pass
    """
    temp_hist_df_tail = temp_hist_df.tail(1)
    temp_hist_df_tail['code'] = stock_symbol
    """
    return
Esempio n. 3
0
 def filter(self, filter_type, symbols=None):
     target_symbols = []
     if symbols and isinstance(symbols, str):
         target_symboles = [symbols]
     elif symbols and isinstance(symbols, str):
         target_symboles = symbols
     else:
         target_symboles = self.get_all_symbols()
     stock_hist_anlyse = tds.Stockhistory(code_str='000001', ktype='D')
     for symbol in target_symboles:
         pass
     return
Esempio n. 4
0
def back_test_dapan(test_codes,
                    k_num=0,
                    source='yh',
                    rate_to_confirm=0.01,
                    processor_id=0):
    i = 0
    for stock_symbol in test_codes:
        if stock_symbol == '000029' and source == 'easyhistory':
            continue
        print(i, stock_symbol)
        s_stock = tds.Stockhistory(stock_symbol,
                                   'D',
                                   test_num=k_num,
                                   source=source,
                                   rate_to_confirm=rate_to_confirm)
        if s_stock.h_df.empty:
            print('New stock %s and no history data' % stock_symbol)
            continue
        if True:
            if dapan_stocks and (stock_symbol in dapan_stocks):
                dapan_criteria = (
                    (s_stock.temp_hist_df['o_change'] > 0.30) &
                    (s_stock.temp_hist_df['pos20'].shift(1) <= 1.0))
                dapan_regress_column_type = 'open'
                dapan_high_o_df, dapan_high_open_columns = s_stock.regress_common(
                    dapan_criteria,
                    post_days=[0, -1, -2, -3, -4, -5, -10, -20, -60],
                    regress_column=dapan_regress_column_type,
                    base_column='open',
                    fix_columns=[
                        'date', 'close', 'p_change', 'o_change', 'position',
                        'pos20', 'oo_chg', 'oh_chg', 'ol_chg', 'oc_chg'
                    ])
                dapan_high_o_df['code'] = stock_symbol
                dapan_high_o_df['ho_index'] = np.where(
                    dapan_high_o_df['pos20'] <= 0, 0,
                    (dapan_high_o_df['o_change'] /
                     dapan_high_o_df['pos20']).round(2))
                dapan_ho_df = dapan_ho_df.append(dapan_high_o_df)
            else:
                pass
Esempio n. 5
0
def get_all_regress_summary(given_stocks=[],
                            confirm=0.01,
                            dest_file=fc.ALL_SUMMARY_FILE):
    all_result_df = tds.pd.DataFrame({})
    """
    latest_temp_df = tds.pd.read_csv( fc.ALL_TEMP_FILE)
    latest_temp_df['code'] = latest_temp_df['code'].apply(lambda x: pds.format_code(x))
    stock_codes = latest_temp_df['code'].values.tolist()
    latest_temp_df = latest_temp_df.set_index('code')
    #print(latest_temp_df.ix['000014'].date)
    """
    #given_stocks = ['000001','000002']
    for stock_symbol in given_stocks:
        s_stock = tds.Stockhistory(stock_symbol,
                                   'D',
                                   test_num=0,
                                   source='yh',
                                   rate_to_confirm=confirm)
        result_series = s_stock.get_regression_result(
            rate_to_confirm=confirm,
            refresh_regression=False,
            from_csv=True,
            bs_csv_dir=fc.ALL_BACKTEST_DIR,
            temp_csv_dir=fc.ALL_TEMP_DIR)
        if not result_series.empty:
            test_result_df = tds.pd.DataFrame({stock_symbol: result_series}).T
            all_result_df = all_result_df.append(test_result_df,
                                                 ignore_index=False)
    if dest_file:
        try:
            all_result_df['code'] = all_result_df.index
            all_result_df['name'] = all_result_df['code'].apply(
                lambda x: pds.format_name_by_code(x, CHINESE_DICT))
            del all_result_df['code']
            #dest_file = 'D:/work/result/all_summary1.csv'
            all_result_df.to_csv(dest_file, encoding='utf-8')
        except:
            pass
    return all_result_df
Esempio n. 6
0
def get_stock_position(stock_synbol='399006',is_realtime_update=False,index_weight=0.65):
    """ 
    when less then -ultimate_coefficient*sys_risk_range, will be zero position; 
    when greater then ultimate_coefficient*sys_risk_range, will be max position; 
    others, will be linearly increased by sys_score
    """
    """
    :param stock_synbol: str type, 个股指数
    :param is_realtime_update: bool type, 是否实时更新仓位
    :param index_weight: float type, 指数仓位占的比重
    :return: position,sys_score,is_sys_risk
    """
    refer_index='999999'    #参考指数
    if stock_synbol<'000999':#深证指数
        refer_index = '399001'
    elif stock_synbol>'002000' and stock_synbol<'002999':#中小板指数
        refer_index ='399005'
    elif stock_synbol>'300000' and stock_synbol<'309999':#创业板指数
        refer_index = '399006'
    elif stock_synbol > '600000' and stock_synbol<'609999':#上证指数
        refer_index = '999999'
    else:#返回系统仓位
        stock_synbol='399006'
        pass
    #index_weight=0.65#指数占的比重
    #is_sys_risk=False
    print('refer_index=%s' % refer_index,'----------------------------------')
    index_stock=tds.Stockhistory(refer_index,'D')
    #print(shz_stock.h_df.tail(10))
    if is_realtime_update:
        k_data=get_real_stock_k(code_str=refer_index)
        k_data=['2016/04/21',2954.38,2990.69,2935.05,3062.58,189000000,2.1115e+11]
        index_stock.update_realtime_hist_df()
    #print(shz_stock.h_df.tail(10))
    #shangzheng_ma_score,shangzheng_score,k_position=index_stock.get_market_score()
    index_stock.form_temp_df(refer_index)
    max_series,max_value,min_series,min_value=index_stock.get_max(column_name='close',latest_num=10)
    #print(max_series,max_value,min_series,min_value)
    max_series,max_value,min_series,min_value=index_stock.get_max(column_name='close',latest_num=20)
    #print(max_series,max_value,min_series,min_value)
    max_series,max_value,min_series,min_value=index_stock.get_max(column_name='close',latest_num=30)
    #index_stock.boduan_analyze()
    #print(max_series,max_value,min_series,min_value)
    #print(shangzheng_ma_score,shangzheng_score,k_position)
    print(stock_synbol,'----------------------------------')
    s_stock=tds.Stockhistory(stock_synbol,'D')
    if is_realtime_update:
        k_data=get_real_stock_k(code_str=stock_synbol)
        #k_data=['2016/04/21',2954.38,2990.69,2935.05,3062.58,189000000,2.1115e+11]
        s_stock.update_realtime_hist_df()
    #chuangye_ma_score,chuangye_score,k_position=s_stock.get_market_score()
    s_stock.is_island_reverse_up()
    s_stock.form_temp_df(stock_synbol)
    index_temp_df=index_stock.temp_hist_df.set_index('date')
    if s_stock.temp_hist_df.empty:
        index_temp_df['sys_score']=index_temp_df['k_score']
        index_temp_df['position']=index_temp_df['position']
        index_temp_df['operation']=index_temp_df['position']-index_temp_df['position'].shift(1)
        return index_temp_df[['sys_score','position','operation']].round(3)
    #print(chuangye_ma_score,chuangye_score,k_position)
    #sys_score=round(0.65*shangzheng_score+0.35*chuangye_score,2)  #-5 ~5
    #stock_first_date=stock.temp_hist_df.head(1).iloc[0].date
    stock_temp_df=s_stock.temp_hist_df.set_index('date')
    #shz_temp_df=shz_stock.temp_hist_df.tail(1000).set_index('date')
    #chy_temp_df=chy_stock.temp_hist_df.tail(1000).set_index('date')
    i_temp_df=index_temp_df.fillna(0)
    s_temp_df=stock_temp_df.fillna(0)
    s_temp_df['sys_score']=index_weight*i_temp_df['k_score']+(1-index_weight)*s_temp_df['k_score']
    s_temp_df['pos']=index_weight*i_temp_df['position']+(1-index_weight)*s_temp_df['position']
    s_temp_df['oper']=i_temp_df['position']-i_temp_df['position'].shift(1)
    s_temp_df.to_csv('%s.csv'%stock_synbol)
    select_columns=['close','p_change','gap','star','star_chg','ma5_chg','ma10_chg','k_rate','p_rate','island','atr_in','reverse','cross1','cross2','cross3','sys_score','pos','oper','std','tangle_p']
    stock_df=s_temp_df[select_columns].round(3)
    stock_df.to_csv('stock_%s.csv' % stock_synbol)
    print(stock_df.tail(80))
    return stock_df
Esempio n. 7
0
def back_test_stocks(test_codes,
                     k_num=0,
                     source='yh',
                     rate_to_confirm=0.01,
                     processor_id=0,
                     save_type='',
                     all_result_columns=[],
                     trend_columns=[],
                     all_temp_columns=[],
                     deep_star_columns=[]):
    i = 0
    ma_num = 20
    regress_column_type = 'close'
    all_result_df = tds.pd.DataFrame({}, columns=all_result_columns)
    all_trend_result_df = tds.pd.DataFrame({}, columns=trend_columns)
    all_temp_hist_df = tds.pd.DataFrame({}, columns=all_temp_columns)
    #deep_star_columns = ['date','close','p_change','o_change','position','low_high_open','high_o_day0','high_o_day1','high_o_day3',
    #               'high_o_day5','high_o_day10','high_o_day20','high_o_day50']
    #deep_star_columns = []
    deep_star_df = tds.pd.DataFrame({}, columns=deep_star_columns)
    print('processor_id=%s : %s' % (processor_id, test_codes))
    for stock_symbol in test_codes:
        if stock_symbol == '000029' and source == 'easyhistory':
            continue
        print('processor_id=%s :%s,%s' % (processor_id, i, stock_symbol))
        s_stock = tds.Stockhistory(stock_symbol,
                                   'D',
                                   test_num=k_num,
                                   source=source,
                                   rate_to_confirm=rate_to_confirm)
        if s_stock.h_df.empty:
            print('New stock %s and no history data' % stock_symbol)
            continue
        if True:
            #try:
            result_df = s_stock.form_temp_df(stock_symbol)
            test_result = s_stock.regression_test(rate_to_confirm)
            recent_trend = s_stock.get_recent_trend(num=ma_num, column='close')
            s_stock.diff_ma(ma=[10, 30], target_column='close', win_num=5)
            temp_hist_df = s_stock.temp_hist_df.set_index('date')
            #temp_hist_df.to_csv('C:/hist/day/temp/%s.csv' % stock_symbol)
            temp_hist_df_tail = temp_hist_df.tail(1)
            temp_hist_df_tail['code'] = stock_symbol
            all_temp_hist_df = all_temp_hist_df.append(temp_hist_df_tail)
            #columns = ['close','p_change','o_change','position','low_high_open','high_o_day0','high_o_day1','high_o_day3','high_o_day5','high_o_day10','high_o_day20']
            #high_o_df,high_open_columns = s_stock.regress_high_open(regress_column = regress_column_type,base_column='open')
            #criteria = s_stock.temp_hist_df['low_high_open']!= 0
            criteria = ((s_stock.temp_hist_df['star_l'] > 0.50) &
                        (s_stock.temp_hist_df['l_change'] < -3.0) &
                        (s_stock.temp_hist_df['pos20'].shift(1) < 0.2))
            high_o_df, high_open_columns = s_stock.regress_common(
                criteria,
                post_days=[0, -1, -2, -3, -4, -5, -10, -20, -60],
                regress_column=regress_column_type,
                base_column='close',
                fix_columns=[
                    'date', 'close', 'p_change', 'o_change', 'position',
                    'pos20', 'MAX20high', 'star_l'
                ])
            high_o_df['code'] = stock_symbol
            high_o_df['star_index'] = np.where(
                high_o_df['pos20'] <= 0, 0,
                (high_o_df['star_l'] / high_o_df['pos20'] *
                 ((high_o_df['MAX20high'] - high_o_df['close']) /
                  high_o_df['MAX20high'])).round(2))
            deep_star_df = deep_star_df.append(high_o_df)
            i = i + 1
            if test_result.empty:
                pass
            else:
                test_result_df = tds.pd.DataFrame(test_result.to_dict(),
                                                  columns=all_result_columns,
                                                  index=[stock_symbol])
                all_result_df = all_result_df.append(test_result_df,
                                                     ignore_index=False)
            if recent_trend.empty:
                pass
            else:
                trend_result_df = tds.pd.DataFrame(recent_trend.to_dict(),
                                                   columns=trend_columns,
                                                   index=[stock_symbol])
                all_trend_result_df = all_trend_result_df.append(
                    trend_result_df, ignore_index=False)
        #except:
        #    print('Regression test exception for stock: %s' % stock_symbol)
        if save_type == 'csv':  #write to csv
            all_temp_hist_df_file_name = 'C:/work/temp1/all_temp_hist_%s' % processor_id + '.csv'
            all_result_df_file_name = 'C:/work/temp1/all_result_%s' % processor_id + '.csv'
            deep_star_df_file_name = 'C:/work/temp1/deep_star_%s' % processor_id + '.csv'
            all_trend_result_df_file_name = 'C:/work/temp1/all_trend_result_%s' % processor_id + '.csv'
            all_temp_hist_df.to_csv(all_temp_hist_df_file_name)
            all_result_df.to_csv(all_result_df_file_name)
            deep_star_df.to_csv(deep_star_df_file_name)
            all_trend_result_df.to_csv(all_trend_result_df_file_name)

    return all_temp_hist_df, all_result_df, deep_star_df, all_trend_result_df
Esempio n. 8
0
def back_test(k_num=0,given_codes=[],except_stocks=['000029'], type='stock', source='easyhistory',rate_to_confirm = 0.01,dapan_stocks=['000001','000002']):
    """
高于三天收盘最大值时买入,低于三天最低价的最小值时卖出: 33策略
    """
    """
    :param k_num: string type or int type: mean counts of history if int type; mean start date of history if date str
    :param given_codes: str type, 
    :param except_stocks: list type, 
    :param type: str type, force update K data from YH
    :return: source: history data from web if 'easyhistory',  history data from YH if 'YH'
    """
    #addition_name = ''
    #if type == 'index':
    addition_name = type
    all_codes = []
    all_stop_codes = []
    all_stocks = []
    all_trade_codes = []
    #print('source=',source)
    if source =='yh' or source=='YH':
        hist_dir='C:/中国银河证券海王星/T0002/export/'
        #print(given_codes,except_stocks)
        all_stop_codes,all_stocks1 = get_stopped_stocks(given_codes,except_stocks,hist_dir)
        #print('all_stocks1=',('150251'  in all_stocks1))
        all_trade_codes = list(set(all_stocks1).difference(set(all_stop_codes)))
    else:
        hist_dir='C:/hist/day/data/'
        all_stop_codes,all_stocks = get_stopped_stocks(given_codes,except_stocks,hist_dir)
        #print('all_stocks2=',('150251'  in all_stocks))
        all_trade_codes = list(set(all_stocks).difference(set(all_stop_codes)))
    #print('all_trade_codes=',('150251'  in all_trade_codes))
    #all_codes = ['300128', '002288', '002156', '300126','300162','002717','002799','300515','300516','600519',
    #            '000418','002673','600060','600887','000810','600115','600567','600199','000596','000538','002274','600036','600030','601398']
    column_list = ['count', 'mean', 'std', 'max', 'min', '25%','50%','75%','cum_prf',
                   'fuli_prf','yearly_prf','last_trade_date','last_trade_price','min_hold_count',
                   'max_hold_count','avrg_hold_count','this_hold_count','exit','enter',
                   'position','max_amount_rate','max_amount_distance','break_in', 
                   'break_in_count','break_in_date', 'break_in_distance','success_rate','days']
    all_result_df = tds.pd.DataFrame({}, columns=column_list)
    i=0
    trend_column_list = ['count', 'mean','chg_fuli', 'std', 'min', '25%', '50%', '75%', 'max', 'c_state',
                        'c_mean', 'pos_mean', 'ft_rate', 'presure', 'holding', 'close','cont_num','amount_rate','ma_amount_rate']
    all_trend_result_df = tds.pd.DataFrame({}, columns=trend_column_list)
    all_temp_hist_df = tds.pd.DataFrame({}, columns=[])
    ma_num = 20
    stock_basic_df=ts.get_stock_basics()
    basic_code = stock_basic_df['name'].to_dict()
    basic_code_keys = basic_code.keys()
    #print('all_trade_codes=',all_trade_codes)
    #high_open_columns = ['date','close','p_change','o_change','position','low_high_open','high_o_day0','high_o_day1','high_o_day3',
    #               'high_o_day5','high_o_day10','high_o_day20','high_o_day50']
    high_open_columns = []
    deep_star_df = tds.pd.DataFrame({}, columns=high_open_columns)
    dapan_ho_df = tds.pd.DataFrame({}, columns=high_open_columns)
    regress_column_type = 'close'
    for stock_symbol in all_trade_codes:
        if stock_symbol=='000029' and source=='easyhistory':
            continue
        print(i,stock_symbol)
        s_stock=tds.Stockhistory(stock_symbol,'D',test_num=k_num,source=source,rate_to_confirm=rate_to_confirm)
        if True:
        #try:
            result_df = s_stock.form_temp_df(stock_symbol)
            test_result = s_stock.regression_test(rate_to_confirm)
            recent_trend = s_stock.get_recent_trend(num=ma_num,column='close')
            s_stock.diff_ma(ma=[10,30],target_column='close',win_num=5)
            temp_hist_df = s_stock.temp_hist_df.set_index('date')
            #temp_hist_df.to_csv('C:/hist/day/temp/%s.csv' % stock_symbol)
            temp_hist_df_tail = temp_hist_df.tail(1)
            temp_hist_df_tail['code'] = stock_symbol
            all_temp_hist_df= all_temp_hist_df.append(temp_hist_df_tail)
            #columns = ['close','p_change','o_change','position','low_high_open','high_o_day0','high_o_day1','high_o_day3','high_o_day5','high_o_day10','high_o_day20']
            #high_o_df,high_open_columns = s_stock.regress_high_open(regress_column = regress_column_type,base_column='open')
            #criteria = s_stock.temp_hist_df['low_high_open']!= 0
            criteria = ((s_stock.temp_hist_df['star_l']> 0.50) & (s_stock.temp_hist_df['l_change']<-3.0) & (s_stock.temp_hist_df['pos20'].shift(1)<0.2))
            high_o_df,high_open_columns = s_stock.regress_common(criteria,post_days=[0,-1,-2,-3,-4,-5,-10,-20,-60],regress_column = regress_column_type,
                       base_column='close',fix_columns=['date','close','p_change','o_change','position','pos20','MAX20high','star_l'])
            high_o_df['code'] = stock_symbol
            high_o_df['star_index'] = np.where(high_o_df['pos20']<=0,0,(high_o_df['star_l']/high_o_df['pos20']*((high_o_df['MAX20high']-high_o_df['close'])/high_o_df['MAX20high'])).round(2))
            deep_star_df= deep_star_df.append(high_o_df)
            
            if dapan_stocks and (stock_symbol in dapan_stocks):
                dapan_criteria = ((s_stock.temp_hist_df['o_change']> 0.30) & (s_stock.temp_hist_df['pos20'].shift(1)<=1.0))
                dapan_regress_column_type = 'open'
                dapan_high_o_df,dapan_high_open_columns = s_stock.regress_common(dapan_criteria,post_days=[0,-1,-2,-3,-4,-5,-10,-20,-60],regress_column = dapan_regress_column_type,
                           base_column='open',fix_columns=['date','close','p_change','o_change','position','pos20','oo_chg','oh_chg','ol_chg','oc_chg'])
                dapan_high_o_df['code'] = stock_symbol
                dapan_high_o_df['ho_index'] = np.where(dapan_high_o_df['pos20']<=0,0,(dapan_high_o_df['o_change']/dapan_high_o_df['pos20']).round(2))
                dapan_ho_df= dapan_ho_df.append(dapan_high_o_df)
            else:
                pass
            
            #print(test_result)
            #print(recent_trend)
            i = i+1
            if test_result.empty:
                pass
            else: 
                test_result_df = tds.pd.DataFrame(test_result.to_dict(), columns=column_list, index=[stock_symbol])
                all_result_df = all_result_df.append(test_result_df,ignore_index=False)
            if recent_trend.empty:
                pass
            else:
                trend_result_df = tds.pd.DataFrame(recent_trend.to_dict(), columns=trend_column_list, index=[stock_symbol])
                all_trend_result_df = all_trend_result_df.append(trend_result_df,ignore_index=False)
        #except:
        #    print('Regression test exception for stock: %s' % stock_symbol)
        
        
    #print(result_df.tail(20))
    #all_result_df = all_result_df.sort_index(axis=0, by='sum', ascending=False)
    
    all_result_df = all_result_df.sort_values(axis=0, by='cum_prf', ascending=False)
    all_trend_result_df = all_trend_result_df.sort_values(axis=0, by='chg_fuli', ascending=False)
    result_summary = all_result_df.describe()
    
    
    result_codes = all_result_df.index.values.tolist()
    result_codes_dict = {}
    on_trade_dict = {}
    valid_dict = {}
    for code in result_codes:
        if code in basic_code_keys:
            result_codes_dict[code] = basic_code[code]
        else:
            result_codes_dict[code] = 'NA'
        if code in all_stop_codes:
            on_trade_dict[code] = 1
        else:
            on_trade_dict[code] = 0
        if code in except_stocks:
            valid_dict[code] = 1
        else:
            valid_dict[code] = 0
    """
    all_temp_dict = {} 
    all_temp_codes = all_temp_hist_df.index.values.tolist()
    for code in result_codes:
        if code in all_temp_codes:
            all_temp_dict[code]= basic_code[code]
        else:
            result_codes_dict[code] = 'NA'
    all_temp_hist_df['name'] = tds.Series(result_codes_dict,index=all_result_df.index)
    """
    #print(result_codes_dict)
    #print(tds.pd.DataFrame(result_codes_dict, columns=['name'], index=list(result_codes_dict.keys())))
    #all_result_df['name'] = result_codes_dict
    all_result_df['name'] = tds.Series(result_codes_dict,index=all_result_df.index)
    deep_star_df['name'] = tds.Series(result_codes_dict,index=deep_star_df.index)
    deep_star_df = deep_star_df[['code','name','star_index']+high_open_columns]
    
    dapan_codes_dict = {}
    if dapan_ho_df.empty:
        pass
    else:
        for code in dapan_stocks:
            if code in basic_code_keys:
                dapan_codes_dict[code] = basic_code[code]
            else:
                dapan_codes_dict[code] = 'NA'
        dapan_ho_df['name'] = tds.Series(dapan_codes_dict,index=dapan_ho_df.index)
        dapan_ho_df = dapan_ho_df[['code','name','ho_index']+dapan_high_open_columns]
    
    all_trend_result_df['name'] = tds.Series(result_codes_dict,index=all_trend_result_df.index)
    all_result_df['stopped'] = tds.Series(on_trade_dict,index=all_result_df.index)
    all_trend_result_df['stopped'] = tds.Series(on_trade_dict,index=all_trend_result_df.index)
    all_result_df['invalid'] = tds.Series(valid_dict, index=all_result_df.index)
    all_trend_result_df['invalid'] = tds.Series(valid_dict, index=all_trend_result_df.index)
    all_result_df['max_r'] = all_result_df['max']/all_result_df['cum_prf']
    ma_c_name = '%s日趋势数' % ma_num
    trend_column_chiness = {'count':ma_c_name, 'mean': '平均涨幅','chg_fuli': '复利涨幅', 'std': '标准差', 'min': '最小涨幅', '25%': '25%', '50%': '50%', '75%': '75%', 'max': '最大涨幅', 'c_state': '收盘价状态',
                        'c_mean': '平均收盘价', 'pos_mean': '平均仓位', 'ft_rate': '低点反弹率', 'presure': '压力', 'holding': '支撑', 'close': '收盘价','cont_num': '连涨天数', 'name': '名字', 'stopped': '停牌','invalid': '除外',
                        'amount_rate':'量比','ma_amount_rate':'短长量比'}
    print(all_trend_result_df)
    all_trend_result_df_chinese = all_trend_result_df.rename(index=str, columns=trend_column_chiness)
    print(all_result_df)
    print(all_result_df.describe())
    if isinstance(k_num, str):
        k_num = k_num.replace('/','').replace('-','')
    latest_date_str = pds.tt.get_latest_trade_date(date_format='%Y/%m/%d')
    latest_date_str = latest_date_str.replace('/','').replace('-','')
    rate_to_confirm_str = '%s' % rate_to_confirm
    rate_to_confirm_str = 'rate' + rate_to_confirm_str.replace('.', '_')
    #print('latest_date_str=',latest_date_str)
    tail_name = '%s_from_%s_%s.csv' % (latest_date_str,k_num,rate_to_confirm_str)
    #all_result_df['yearly_prf'] = all_result_df['fuli_prf']**(1.0/(all_result_df['days']/365.0))
    result_column_list = ['count','name', 'mean', 'std', 'max', 'min', 'cum_prf',
                   'fuli_prf','yearly_prf','success_rate','last_trade_date','last_trade_price','min_hold_count',
                   'max_hold_count','avrg_hold_count','this_hold_count','exit','enter',
                   'position','max_amount_rate','max_amount_distance','break_in', 
                   'break_in_count','break_in_date', 'break_in_distance',
                   'stopped','invalid','max_r','25%','50%','75%',]
    all_result_df = all_result_df[result_column_list]
    all_result_df.to_csv('./temp/regression_test_' + addition_name +tail_name)
    deep_star_df.to_csv('./temp/pos20_star_%s'% regress_column_type + addition_name +tail_name)
    dapan_ho_df.to_csv('./temp/dapan_high_open_%s'% regress_column_type + addition_name +tail_name)
    if all_result_df.empty:
        pass
    else:
        consider_df = all_result_df[(all_result_df['max_amount_rate']>2.0) & (all_result_df['position']>0.35) & (all_result_df['stopped']==0) & (all_result_df['invalid']==0)]# & (all_result_df['last_trade_price'] ==0)]
        consider_df.to_csv('./temp/consider_' + addition_name +tail_name)
        
        active_df = all_result_df[(all_result_df['max_r']<0.4)  & (all_result_df['name']!='NA') & # (all_result_df['min']>-0.08)  & (all_result_df['position']>0.35) &
                                  (all_result_df['max']>(3.9 *all_result_df['min'].abs())) & (all_result_df['invalid']==0) &(all_result_df['stopped']==0)]
        active_df['active_score'] = active_df['fuli_prf']/active_df['max_r']/active_df['std']*active_df['fuli_prf']/active_df['cum_prf']
        active_df = active_df.sort_values(axis=0, by='active_score', ascending=False)
        active_df.to_csv('./temp/active_' + addition_name +tail_name)
        tupo_df = all_result_df[(all_result_df['break_in_distance']!=0) &(all_result_df['break_in_distance']<=20) & 
                                (all_result_df['position']>0.35) & (all_result_df['stopped']==0) & 
                                (all_result_df['invalid']==0) & (all_result_df['name']!='NA') & (all_result_df['last_trade_price']!=0)]# & (all_result_df['last_trade_price'] ==0)]
        tupo_df.to_csv('./temp/tupo_' + addition_name +tail_name)
        
        
    result_summary.to_csv('./temp/result_summary_' + addition_name +tail_name)
    all_trend_result_df_chinese.to_csv('./temp/trend_result_%s' % ma_num + addition_name +'%s_to_%s_%s.csv' % (k_num,latest_date_str,rate_to_confirm_str))
    if not all_temp_hist_df.empty:
        #all_temp_hist_df = all_temp_hist_df[column_list]
        all_temp_hist_df = all_temp_hist_df.set_index('code')
        all_temp_hist_df.to_csv('./temp/all_temp_' + addition_name +tail_name)
        reverse_df = all_temp_hist_df[(all_temp_hist_df['reverse']>0) & 
                                      (all_temp_hist_df['LINEARREG_ANGLE8']<-2.0) &
                                      (all_temp_hist_df['position']>0.35)]#
        #reverse_df['r_sort'] = reverse_df['star_chg']/reverse_df['pos20']
        reverse_df.to_csv('./temp/reverse_df_' + addition_name +tail_name)
        
        long_turn_min_angle = -0.5
        short_turn_min_angle = 0.2
        ma30_df = all_temp_hist_df[(all_temp_hist_df['LINEARREG_ANGLE14MA120']>long_turn_min_angle) & 
                                   (all_temp_hist_df['LINEARREG_ANGLE14MA250']>long_turn_min_angle) &
                                   (all_temp_hist_df['LINEARREG_ANGLE14MA60']>long_turn_min_angle) &
                                   (all_temp_hist_df['LINEARREG_ANGLE14MA30']<1.0) &
                                   (all_temp_hist_df['LINEARREG_ANGLE5MA5']>short_turn_min_angle) &
                                   (all_temp_hist_df['LINEARREG_ANGLE6MA10']>short_turn_min_angle) &
                                   (all_temp_hist_df['LINEARREG_ANGLE5MA5']>=all_temp_hist_df['LINEARREG_ANGLE6MA10']) &
                                   (all_temp_hist_df['LINEARREG_ANGLE8ROC1']>0.0) &
                                   (all_temp_hist_df['close']>all_temp_hist_df['ma30']) &
                                (all_temp_hist_df['position']>0.35)]#
        ma30_df.to_csv('./temp/ma30_df_' + addition_name +tail_name)
    
    return all_result_df


#back_test(k_num='2015/08/30',given_codes=['000004','000005'],except_stocks=['000029'], type='stock', source='YH')
Esempio n. 9
0
def get_latest_temp_df(today_df_high_open=[]):
    all_trade_code = [
        '600460', '002075', '600367', '002797', '002224', '600393', '002523',
        '300201', '600262', '600671', '002796', '603029', '603101', '300509',
        '300511', '603822', '603528', '603799', '603399', '300340', '300510',
        '600891', '600639', '002165', '600540', '600392', '600012', '300341',
        '300493', '000518', '300014', '601000', '300395', '002670', '600438',
        '002771', '002155', '000005', '603188', '000890', '000570', '002070',
        '002297', '300481', '600975', '300307', '603901', '000910', '000799',
        '002038', '002010', '300267', '300483', '300394', '300100', '002509',
        '002364', '002173', '600715', '002279', '601958', '300507', '002340',
        '600507', '300449', '002108', '002631', '600335', '002512', '002567',
        '000421', '300373', '002722', '002237', '300258', '000037', '300389',
        '300278', '002709', '300409', '000066', '600330', '300004', '600160',
        '002696', '002082', '002204', '600733', '600156', '600547', '600382',
        '300438', '600021', '002076', '000425', '002676', '300508', '600623',
        '300327', '300131', '300040', '000611', '002006', '600767', '600490',
        '601199', '600352', '002100', '300134', '600057', '300068', '002750',
        '000526', '600869', '300318', '002335', '600883', '002713', '600359',
        '002379', '002057', '300219', '300351', '300088', '300429', '002309',
        '002049', '000606', '002715', '000099', '300037', '300342', '000055',
        '000988', '002273', '002466', '603368', '603678', '000586', '600522',
        '002132', '002341', '002245', '600370', '600836', '300376', '300143',
        '603117', '000839', '600745', '000813', '600425', '002041', '002147',
        '600575', '002136', '002440', '600258', '002716', '002664', '002646',
        '002422', '002514', '600489', '000504', '002234', '002196', '002215',
        '002601', '600260', '600250', '000838', '002222', '600885', '002249',
        '603338', '002674', '600590', '300123', '600117', '603726', '603398',
        '601700', '603011', '300073', '002299', '600054', '600201', '000683',
        '002752', '000810', '000936', '000533', '002589', '002217', '002229',
        '600137', '300273', '603026', '600693', '002153', '002015', '000796',
        '600619', '600366', '002124', '002074', '002596', '600562', '600641',
        '002580', '601113', '600053', '600785', '600493', '300118', '300266',
        '603198', '600992', '300474', '603589', '002312', '000597', '002545',
        '300360', '000782', '002350', '000008', '000516', '600505', '601069',
        '000545', '000880', '300012', '002135', '002023', '300207', '600305',
        '603729', '002756', '600615', '002460', '000046', '002296', '603969',
        '600083', '300250', '000567', '603006', '300283', '002103', '002056',
        '603778', '600313', '002532', '002307', '002730', '002571', '002557',
        '002508', '002473', '002672', '002418', '600882', '300420', '300393',
        '300017', '300243', '600006', '000078', '600529', '600467', '600183',
        '600232', '002758', '300427', '002271', '600782', '600620', '002590',
        '002708', '002706', '601003', '600829', '600198', '002489', '300132',
        '002628', '600987', '002548', '300485', '600311', '002091', '002793',
        '300008', '002534', '002684', '000679', '600399', '002463', '002395',
        '002324', '300280', '000856', '600961', '002541', '002648', '600309',
        '000831', '002111', '600990', '002387', '600896', '002125', '002652',
        '002115', '000150', '600821', '300137', '603568', '603828', '002597',
        '000061', '601689', '600386', '603919', '603899', '300116', '600797',
        '002456', '000733', '002043', '002725', '300330', '002189', '000715',
        '300048', '002367', '600423', '600582', '603309', '600241', '300034',
        '300223', '600618', '300198', '000748', '600712', '600568', '600068',
        '600834', '002605', '000937', '601311', '300178', '002419', '000888',
        '600594', '300402', '300237', '000686', '300398', '000848', '603128',
        '000852', '000677', '603333', '600080', '600593', '600862', '600702',
        '000420', '600967', '601880', '002639', '600703', '002625', '600543',
        '600076', '000678', '600444', '600559', '000797', '600605', '600638',
        '300505', '000780', '600272', '300056', '600395', '300156', '600796',
        '002772', '600112', '600617', '600343', '300107', '600275', '002088',
        '600790', '000158', '000665', '002470', '603798', '300080', '600293',
        '000688', '002381', '000595', '300259', '600694', '601222', '600249',
        '600461', '002094', '300498', '600988', '600722', '600480', '002318',
        '601398', '603308', '002538', '002231', '600207', '600552', '300133',
        '600680', '300148', '300326', '002685', '002726', '600558', '300261',
        '300170', '000998', '600267', '002468', '002479', '002518', '600180',
        '603600', '002721', '300444', '300375', '600317', '300421', '300268',
        '601008', '300407', '603355', '002477', '600270', '000922', '002531',
        '002099', '002579', '600086', '002020', '002635', '603588', '002643',
        '000815', '002186', '002086', '600401', '000705', '600436', '600872',
        '601006', '300368', '603868', '002360', '601388', '002163', '002391',
        '600685', '600854', '000096', '000708', '600219', '600648', '600973',
        '300482', '603023', '000430', '000557', '002087', '300235', '600976',
        '002667', '600486', '600778', '002614', '300195', '600809', '600055',
        '603718', '600758', '600545', '300274', '000619', '600662', '002282',
        '002496', '300035', '300128', '002200', '002408', '300334', '002502',
        '002443', '002167', '002436', '300227', '300269', '000426', '000969',
        '002411', '601226', '600165', '600825', '600815', '300305', '000912',
        '300425', '000762', '002346', '000534', '603366', '600845', '000973',
        '600791', '600036', '002656', '000862', '002310', '000820', '002465',
        '600336', '002275', '002775', '603223', '601872', '002256', '603567',
        '000029', '300321', '600580', '600833', '002382', '002574', '600478',
        '600610', '300328', '603099', '000673', '002003', '600521', '002723',
        '002438', '002521', '002277', '601886', '002384', '002116', '300026',
        '000019', '600331', '600810', '600735', '600064', '600284', '002434',
        '600766', '002671', '000050', '000546', '603315', '600398', '000596',
        '000886', '600476', '600770', '601798', '300058', '600560', '000584',
        '603028', '300359', '000498', '000697', '002174', '300440', '603997',
        '300119', '002079', '002678', '600128', '002254', '600622', '603456',
        '000010', '600146', '002738', '002537', '600841', '600589', '000661',
        '000833', '000738', '000423', '002742', '002618', '300193', '600200',
        '002458', '002166', '000712', '300271', '601233', '600897', '600687',
        '300403', '600288', '300371', '603701', '600028', '300066', '002516',
        '300101', '300159', '002033', '600210', '600133', '600387', '002292',
        '300091', '603077', '600354', '000065', '000058', '600356', '601566',
        '002641', '600186', '600650', '300329', '600634', '002334', '600706',
        '300234', '600082', '300325', '000062', '002113', '300196', '002097',
        '600500', '600235', '603300', '000011', '002561', '600678', '000895',
        '600088', '601899', '600819', '000927', '002252', '300072', '603518',
        '603116', '000825', '000981', '300108', '002790', '000735', '000903',
        '002677', '002464', '000967', '002146', '002118', '600221', '300350',
        '601789', '002024', '002311', '002454', '002592', '300031', '600851',
        '600114', '600531', '600261', '000532', '601336', '002689', '600895',
        '600776', '300365', '002484', '600039', '600418', '600369', '300408',
        '300054', '002779', '300145', '600138', '600130', '600333', '300463',
        '002327', '000157', '300106', '600327', '600017', '002578', '300182',
        '002151', '000809', '000659', '600993', '002185', '601890', '002258',
        '601877', '002724', '600372', '600020', '000819', '603018', '002753',
        '002747', '600121', '600572', '002121', '002504', '002130', '000822',
        '300344', '600748', '600246', '002494', '002714', '600105', '000561',
        '601166', '601668', '600225', '600677', '601800', '000926', '002317',
        '603979', '600865', '002322', '002718', '300475', '000626', '603088',
        '603806', '600812', '002276', '300491', '300129', '300357', '000039',
        '600135', '300296', '000999', '002270', '300506', '002760', '600071',
        '600587', '603508', '000042', '600816', '002455', '603066', '000976',
        '002208', '000021', '300304', '600742', '603686', '002080', '002453',
        '603616', '600195', '600131', '600391', '000023', '600717', '600316',
        '300249', '002300', '300199', '601155', '002178', '600095', '600389',
        '601777', '000881', '002042', '300114', '600890', '002637', '600101',
        '002026', '000777', '002045', '600893', '000798', '600242', '600238',
        '000710', '601012', '002662', '300281', '600405', '002161', '300070',
        '002361', '603369', '300297', '002182', '600613', '002081', '300415',
        '300239', '603939', '600523', '000669', '300293', '002485', '600421',
        '601021', '300165', '000049', '002140', '603703', '600859', '000869',
        '300374', '000566', '300049', '600107', '600211', '600506', '002588',
        '300397', '300358', '002370', '601318', '300489', '600005', '600008',
        '600031', '600035', '600048', '600097', '600113', '600120', '600122',
        '600123', '600129', '600139', '600149', '600153', '600168', '600170',
        '600173', '600178', '600182', '600189', '600209', '600215', '600218',
        '600226', '600228', '600231', '600298', '600315', '600318', '600321',
        '600326', '600355', '600358', '600363', '600365', '600381', '600385',
        '600397', '600408', '600409', '600455', '600468', '600469', '600470',
        '600485', '600488', '600509', '600511', '600533', '600539', '600556',
        '600565', '600571', '600576', '600577', '600592', '600609', '600614',
        '600630', '600636', '600640', '600644', '600645', '600653', '600663',
        '600676', '600679', '600683', '600688', '600696', '600714', '600728',
        '600734', '600738', '600749', '600751', '600753', '600768', '600777',
        '600792', '600808', '600822', '600843', '600848', '600856', '600857',
        '600868', '600870', '600873', '600877', '600898', '600966', '600999',
        '601001', '601005', '601018', '601058', '601088', '601101', '601168',
        '601179', '601288', '601333', '601599', '601601', '601666', '601939',
        '601988', '603008', '603268', '603318', '603519', '603611', '603808',
        '603838', '603869', '603918', '603993', '000002', '000004', '000007',
        '000038', '000070', '000089', '000159', '000166', '000401', '000409',
        '000415', '000416', '000505', '000509', '000511', '000524', '000525',
        '000537', '000540', '000547', '000553', '000555', '000565', '000568',
        '000571', '000581', '000582', '000598', '000608', '000610', '000612',
        '000617', '000628', '000629', '000633', '000635', '000636', '000638',
        '000651', '000667', '000670', '000693', '000717', '000718', '000720',
        '000729', '000732', '000736', '000752', '000755', '000757', '000766',
        '000790', '000806', '000818', '000835', '000836', '000883', '000887',
        '000893', '000901', '000911', '000917', '000920', '000923', '000925',
        '000932', '000950', '000953', '000955', '000962', '000968', '000975',
        '000997', '002012', '002044', '002046', '002052', '002065', '002069',
        '002072', '002089', '002101', '002110', '002112', '002129', '002131',
        '002133', '002134', '002145', '002154', '002159', '002164', '002175',
        '002190', '002199', '002201', '002203', '002207', '002209', '002210',
        '002211', '002212', '002219', '002230', '002246', '002250', '002264',
        '002272', '002278', '002280', '002283', '002288', '002289', '002291',
        '002306', '002314', '002319', '002331', '002347', '002349', '002352',
        '002354', '002355', '002371', '002373', '002386', '002389', '002405',
        '002429', '002439', '002447', '002469', '002482', '002491', '002492',
        '002499', '002505', '002507', '002515', '002517', '002524', '002540',
        '002542', '002547', '002554', '002555', '002569', '002570', '002573',
        '002587', '002591', '002599', '002608', '002611', '002612', '002617',
        '002619', '002630', '002647', '002654', '002655', '002663', '002675',
        '002682', '002686', '002691', '002694', '002700', '002703', '002712',
        '002735', '002739', '002759', '002762', '002766', '300005', '300019',
        '300025', '300038', '300039', '300043', '300044', '300045', '300046',
        '300053', '300057', '300061', '300062', '300067', '300069', '300071',
        '300104', '300110', '300113', '300121', '300125', '300142', '300152',
        '300158', '300163', '300173', '300176', '300180', '300184', '300185',
        '300217', '300220', '300221', '300225', '300226', '300242', '300251',
        '300265', '300276', '300282', '300285', '300290', '300292', '300295',
        '300298', '300299', '300300', '300308', '300323', '300331', '300332',
        '300335', '300338', '300339', '300362', '300366', '300370', '300391',
        '300405', '300410', '300411', '300416', '300423', '300426', '300428',
        '300432', '300434', '300437', '300441', '300450', '300457', '300462',
        '300464', '300465', '300472', '300488', '300495', '002791', '600754',
        '002761', '002695', '600184', '000014', '603085', '000028', '002002',
        '600482', '000958', '002609', '600525', '002171', '002527', '002014',
        '002441', '000722', '300144', '300210', '002137', '002168', '600628',
        '300492', '000756', '600611', '300486', '300029', '000685', '600658',
        '002734', '000520', '600152', '600960', '601766', '002444', '603599',
        '600188', '600508', '601015', '300007', '600736', '600661', '002372',
        '002021', '600273', '600664', '000036', '000691', '002581', '000837',
        '002651', '300490', '000607', '601727', '601857', '603108', '002263',
        '002128', '603199', '002751', '600236', '600268', '603608', '300211',
        '002506', '600280', '600828', '603818', '600125', '600148', '600463',
        '000800', '300233', '600379', '600026', '000422', '000739', '300042',
        '600750', '300141', '000913', '002150', '002301', '300456', '600555',
        '600655', '300314', '002550', '300169', '002692', '600066', '300424',
        '300294', '300494', '601328', '000850', '601010', '601106', '000737',
        '600798', '002025', '600396', '002549', '600340', '002393', '600368',
        '000572', '002615', '002749', '000889', '601567', '600794', '000902',
        '600187', '600276', '000560', '002139', '300448', '002428', '002559',
        '600965', '000821', '300473', '600308', '002417', '600449', '600063',
        '600746', '002358', '600900', '600400', '000151', '300115', '002493',
        '603169', '300127', '000933', '002363', '002187', '002226', '000035',
        '000939', '601888', '300090', '000538', '600255', '002792', '600118',
        '600561', '600802', '600986', '000723', '300153', '300286', '002600',
        '300417', '600755', '600150', '600325', '002067', '002259', '600716',
        '000012', '002423', '600106', '601818', '000001', '601169', '603311',
        '600495', '601985', '000100', '300404', '002623', '600875', '000034',
        '600616', '601116', '002497', '600033', '300167', '300442', '600237',
        '002085', '300102', '000698', '601801', '000905', '002528', '002658',
        '600827', '600917', '000541', '601808', '601007', '600126', '600824',
        '601989', '002227', '600084', '603861', '600513', '600078', '002788',
        '002036', '300310', '000709', '002633', '300363', '600459', '300228',
        '300194', '600197', '600708', '601933', '002450', '600892', '000576',
        '300364', '002098', '002144', '601258', '000726', '300253', '601118',
        '600350', '600548', '601608', '600332', '002181', '600971', '601919',
        '002170', '603989', '600019', '000931', '300136', '300311', '600784',
        '600879', '000630', '600997', '300175', '300317', '603017', '600222',
        '000861', '600569', '300111', '002148', '600307', '603100', '601186',
        '002698', '600781', '000680', '600199', '600022', '603168', '603898',
        '600282', '601898', '600823', '600175', '002486', '600861', '600077',
        '002092', '000040', '600668', '002039', '000731', '601901', '000590',
        '300433', '600515', '002255', '300459', '000632', '002034', '600252',
        '600290', '000652', '000793', '000916', '002260', '600269', '002247',
        '002780', '000792', '002027', '000428', '000753', '002071', '002375',
        '600081', '000528', '600884', '600433', '002737', '000985', '600586',
        '600196', '300316', '002690', '000795', '600099', '600637', '002510',
        '601588', '600876', '002058', '601188', '000877', '000897', '600015',
        '600151', '600765', '000882', '600361', '601992', '600428', '002741',
        '300479', '600050', '600731', '601965', '000982', '600894', '002406',
        '000876', '600248', '002732', '000898', '002344', '601717', '000045',
        '600295', '002767', '002198', '600855', '300200', '002195', '002126',
        '000906', '600682', '002449', '002778', '300396', '600193', '601616',
        '600537', '002681', '600837', '000826', '603883', '002442', '300303',
        '002645', '600380', '601216', '601928', '002281', '601390', '300064',
        '300399', '002397', '300447', '600707', '600660', '002404', '002657',
        '002253', '000989', '002594', '002054', '601968', '300097', '002487',
        '600959', '002446', '603158', '600166', '002765', '002028', '300179',
        '600323', '600351', '600089', '002376', '300181', '000900', '600223',
        '000707', '601158', '000963', '000928', '002392', '000419', '002315',
        '600278', '600835', '000529', '002598', '600475', '002047', '300124',
        '002284', '000682', '002152', '600362', '600838', '600720', '000069',
        '002238', '300353', '600067', '601636', '000713', '000952', '600783',
        '603520', '601126', '000868', '601969', '300478', '600881', '000060',
        '300255', '600759', '300430', '300312', '600978', '603606', '000828',
        '002529', '300206', '600051', '600579', '600635', '600498', '300414',
        '600739', '002402', '002179', '002223', '300151', '000408', '000600',
        '300461', '600239', '002728', '300126', '600143', '600322', '600100',
        '000929', '002236', '002390', '000022', '600787', '000536', '300023',
        '300496', '601788', '000783', '002035', '600517', '300154', '002736',
        '600769', '600429', '002558', '002083', '600185', '601211', '002336',
        '600376', '600567', '300471', '600277', '002066', '002622', '600814',
        '601009', '600256', '600481', '002269', '600377', '002342', '002740',
        '000517', '000970', '300262', '600266', '002533', '600127', '001979',
        '002009', '600583', '002228', '002316', '000429', '600096', '002437',
        '300011', '600388', '000858', '002638', '000666', '300096', '600004',
        '601558', '300218', '600649', '002501', '002117', '002420', '000609',
        '300079', '601117', '000601', '002062', '002603', '600871', '601929',
        '000603', '000563', '002235', '603010', '300455', '000543', '002433',
        '603167', '002763', '600177', '600698', '002016', '002060', '002746',
        '000506', '002693', '600119', '600704', '600573', '603288', '002022',
        '600958', '600741', '601908', '002295', '603118', '000591', '000523',
        '300087', '600535', '000776', '600302', '300092', '600466', '603027',
        '002004', '600141', '601618', '000779', '600681', '600724', '600108',
        '601999', '002424', '600329', '300147', '600038', '300160', '601519',
        '000656', '000514', '300103', '600056', '300446', '002120', '603789',
        '000006', '002193', '300161', '600353', '601518', '601678', '600085',
        '002162', '002457', '000671', '300212', '300500', '603012', '300214',
        '300468', '000935', '002194', '300452', '002365', '000018', '600684',
        '601368', '601900', '300388', '002308', '000655', '000663', '600624',
        '000701', '600665', '000816', '603698', '600328', '002585', '300024',
        '600844', '000863', '603766', '002522', '600016', '002329', '600858',
        '000725', '600596', '300120', '300086', '002244', '601555', '000063',
        '000400', '002339', '300002', '002467', '300502', '002755', '002595',
        '300487', '300197', '002666', '002233', '600600', '600867', '600984',
        '000156', '002785', '002781', '002748', '000605', '000020', '600805',
        '002345', '002498', '603669', '002214', '002239', '000592', '300451',
        '000672', '000948', '002399', '600756', '601718', '600510', '601002',
        '600243', '601225', '002064', '002415', '603126', '300172', '000544',
        '002526', '603558', '002562', '300445', '600757', '600874', '300146',
        '600774', '600251', '002795', '603688', '600980', '600477', '603598',
        '002398', '603030', '000507', '000759', '300291', '002068', '002707',
        '600320', '600518', '300319', '601107', '600803', '600190', '600846',
        '603166', '000801', '002013', '002286', '000681', '002368', '600070',
        '600446', '000589', '600420', '600110', '300139', '300084', '300347',
        '002109', '300032', '600730', '600000', '002225', '600109', '600516',
        '600062', '000768', '300315', '000921', '002472', '600439', '601669',
        '600690', '601208', '300377', '600711', '002176', '600729', '002362',
        '600435', '600073', '000959', '300254', '600820', '603328', '000623',
        '002451', '600503', '600795', '300503', '600979', '002261', '300385',
        '600606', '300348', '600079', '002650', '000949', '600483', '300208',
        '002789', '601231', '300240', '600864', '600551', '000501', '000957',
        '002285', '000338', '600501', '603618', '000056', '600850', '600167',
        '000859', '002773', '600642', '603001', '600415', '600880', '603696',
        '002333', '000778', '002409', '600403', '002141', '300301', '000411',
        '002157', '002337', '601998', '002513', '002426', '600291', '002727',
        '000625', '600723', '300287', '300443', '000153', '600718', '002380',
        '002711', '600773', '600213', '603555', '002019', '600292', '002184',
        '000559', '002266', '002030', '600998', '000088', '000788', '000908',
        '600360', '600578', '002556', '000930', '600830', '600191', '002448',
        '600626', '002564', '300077', '300117', '600963', '002500', '600526',
        '002328', '002156', '600761', '002705', '600345', '000918', '600652',
        '002343', '600227', '000031', '000719', '002604', '300309', '601377',
        '601866', '002471', '300075', '603999', '600487', '300343', '601633',
        '000407', '600496', '002332', '000301', '000965', '600074', '002338',
        '000668', '002172', '002632', '300372', '000899', '600499', '600601',
        '002330', '300166', '600643', '603111', '000548', '300400', '000919',
        '001696', '600657', '000009', '300059', '300001', '600279', '000620',
        '300256', '600557', '300003', '600779', '002687', '600069', '600030',
        '600426', '300333', '600299', '300222', '601218', '300098', '300289',
        '002073', '300231', '000413', '002421', '002503', '300183', '000987',
        '300284', '600416', '002218', '002119', '600889', '600982', '002474',
        '603866', '601991', '002293', '300149', '300174', '300155', '600743',
        '002197', '603566', '002017', '002410', '600220', '600818', '600527',
        '300205', '002461', '601600', '000700', '600203', '600667', '000573',
        '300099', '002377', '002325', '300422', '600563', '300229', '600536',
        '002142', '002583', '002452', '002535', '600104', '600528', '600705',
        '002008', '002432', '000488', '000539', '002096', '300387', '600651',
        '600023', '603299', '002575', '300168', '300216', '600764', '002084',
        '600621', '601515', '002321', '300244', '000502', '002077', '300190',
        '600027', '600647', '000811', '600061', '000980', '002183', '600727',
        '600283', '002078', '603123', '300413', '300412', '000564', '002149',
        '600747', '000786', '600697', '300324', '002313', '600737', '600629',
        '002051', '600826', '601016', '002188', '600060', '000027', '000531',
        '300055', '002553', '600970', '002107', '600259', '600018', '002268',
        '601688', '002192', '000519', '600233', '002511', '002326', '600549',
        '600158', '300013', '002414', '600532', '000716', '600699', '601677',
        '002191', '601011', '000728', '300082', '600406', '002320', '002673',
        '000403', '002029', '002490', '002357', '000650', '002011', '600789',
        '000851', '002478', '002102', '000979', '300089', '000966', '300105',
        '000977', '600157', '000909', '002063', '002530', '000513', '000803',
        '002566', '002719', '300074', '300162', '300439', '000616', '002572',
        '000613', '002697', '600458', '000960', '002061', '002495', '601628',
        '002205', '300028', '300109', '601198', '002262', '600348', '000690',
        '002095', '600011', '000830', '300279', '600410', '600801', '600422',
        '000550', '300484', '600208', '000878', '603015', '002776', '002221',
        '601100', '000758', '600162', '601996', '600037', '002104', '300453',
        '600847', '000692', '600691', '600831', '002394', '000554', '002232',
        '600159', '300436', '600780', '300238', '000032', '000938', '600059',
        '002680', '002602', '002653', '000090', '002305', '600303', '300392',
        '300171', '000030', '002629', '600887', '002031', '600072', '002546',
        '002475', '600338', '600337', '300022', '300095', '600289', '600116',
        '603885', '002520', '300016', '002180', '300245', '000791', '300047',
        '600588', '600612', '002627', '600811', '002769', '300224', '600373',
        '300352', '601099', '300320', '600007', '600863', '002374', '600479',
        '600029', '000043', '603021', '000750', '001896', '000915', '002274',
        '300027', '002177', '002582', '600497', '600689', '600538', '300247',
        '000068', '002243', '600566', '000627', '002396', '002053', '300470',
        '002783', '002430', '601020', '000026', '002298', '600090', '300337',
        '600094', '600257', '600075', '002661', '600962', '002782', '002400',
        '600530', '601028', '002544', '002240', '600217', '603800', '600132',
        '600740', '600674', '002251', '300009', '601313', '000812', '300213',
        '300188', '002660', '600155', '002616', '300302', '002768', '600985',
        '002304', '601369', '300345', '300349', '000503', '600456', '600172',
        '600010', '300135', '000789', '002206', '002294', '300230', '002621',
        '600585', '603222', '002640', '603003', '601038', '600595', '600886',
        '600169', '002216', '000721', '000333', '603005', '600202', '300085',
        '300020', '002093', '300204', '300112', '000978', '000727', '000761',
        '603998', '002568', '002138', '000530', '600462', '002733', '000785',
        '603025', '002551', '002202', '000823', '600888', '300236', '000599',
        '300497', '002552', '002586', '300418', '002610', '300209', '000807',
        '300015', '600673', '600695', '000587', '300030', '300094', '002037',
        '300263', '300499', '000760', '603636', '600111', '600807', '002403',
        '002401', '300480', '600692', '002613', '600995', '002351', '002158',
        '000558', '002476', '000551', '600519', '300177', '002539', '600804',
        '300093', '002665', '002302', '002644', '000510', '300257', '002303',
        '000417', '002383', '000639', '000703', '600297', '002624', '300306',
        '603227', '300322', '300006', '300150', '002050', '300010', '603009',
        '600271', '300052', '000961', '002483', '002090', '002659', '002359',
        '300036', '600491', '002105', '300380', '601339', '002353', '603377',
        '300050', '601607', '000860', '600448', '300460', '600171', '600860',
        '600058', '603020', '002435', '300076', '300275', '002565', '002607',
        '002005', '300379', '600604', '603609', '300248', '300354', '600666',
        '600192', '600686', '000751', '300382', '300272', '300390', '600176',
        '600654', '600853', '000016', '600229', '002248', '600312', '002369',
        '002445', '002018', '002385', '002563', '300041', '000676', '600009',
        '002481', '002040', '002462', '002220', '600771', '002683', '002122',
        '000875', '300313', '300469', '600093', '600584', '002407', '002160',
        '002114', '002743', '002480', '600287', '002267', '601699', '000402',
        '002378', '300431', '603002', '603788', '300406', '300386', '002626',
        '000996', '002593', '601098', '002543', '600216', '000017', '600969',
        '601177', '002348', '300202', '600983', '002242', '300260', '600310',
        '603699', '600240', '600052', '300501', '300065', '600206', '002701',
        '002001', '600713', '002584', '601579', '300164', '600775', '000410',
        '002729', '600800', '600599', '601111', '000552', '000702', '600285',
        '002576', '300467', '600098', '002127', '600378', '000767', '300336',
        '002032', '000885', '600163', '300021', '300384', '300051', '600383',
        '002106', '002169', '300383', '000615', '300401', '002241', '601139',
        '600598', '002636', '002649', '002699', '002366', '600726', '002560',
        '300346', '300252', '600744', '300138', '000048', '002620', '603306',
        '002770', '000983', '000622', '300288', '600115', '600633', '601137',
        '000059', '002787', '002642', '300466', '300078', '300246', '002287',
        '300355', '000993', '600161', '300203', '002777', '300369', '300241',
        '002519', '300033', '600103', '300191', '002786', '002669', '603968',
        '300270', '603019', '600602', '601238', '000802', '300130', '300232',
        '600502', '002265', '002143', '002290', '002745', '300140', '002702',
        '002536', '002388', '000657', '002055', '002059', '600281', '002688',
        '002679', '000951', '600839', '002668', '300187', '002123', '002427',
        '000404', '002416', '300018', '300477', '000995', '000025', '000711',
        '000631', '002007', '300081', '600512', '002356', '601799', '300367',
        '300458', '002606', '002731', '002459', '603000', '600719', '002577',
        '603601', '600371', '600763', '000521', '002717', '000971', '000695',
        '000662', '002488', '600300', '000990', '600597', '300157', '600419',
        '002757', '002048', '002425', '300277', '300476', '000829', '300083',
        '000418', '600981', '002412', '002431', '000892', '600570', '000593',
        '603889', '300356', '300378', '300435', '300381', '000687', '300419',
        '000637', '000585', '300215', '300264', '600136', '002634', '300122',
        '603996', '002413', '600452', '300189', '603022', '002323', '300192',
        '002213', '603988', '000972', '300063', '603936', '600550'
    ]
    today_df_high_open = [
        '002797', '002224', '300201', '600671', '002796', '603029', '603101',
        '300511', '603822', '603528', '603799', '300340', '300510', '300341',
        '300493', '601000', '600438', '000005', '002070', '300483', '300100',
        '002509', '600715', '601958', '300507', '002631', '002709', '300004',
        '600733', '600021', '000425', '600623', '002006', '600767', '000606',
        '002273', '002132', '600836', '002136', '000504', '002601', '603011',
        '000533', '002015', '600562', '600493', '002545', '000516', '000545',
        '002023', '000046', '002672', '300420', '300393', '600198', '002489',
        '002534', '600990', '600896', '603828', '002189', '603309', '300198',
        '002605', '000420', '002639', '600543', '600076', '300505', '300056',
        '600343', '600694', '603308', '300261', '000815', '300368', '002391',
        '600758', '000619', '300227', '000973', '002465', '002003', '002384',
        '600735', '002671', '000697', '600146', '002537', '600841', '000738',
        '600133', '600356', '300234', '603300', '000011', '000981', '002311',
        '002454', '002779', '300145', '600372', '002504', '600246', '000561',
        '600677', '300475', '600071', '002453', '600316', '002300', '600095',
        '300070', '600107', '600189', '000628', '300276', '600184', '002137',
        '300486', '600960', '300007', '600661', '300233', '002150', '600066',
        '000560', '600118', '603861', '002170', '300433', '002260', '002247',
        '600765', '600361', '000906', '002681', '000963', '000529', '002598',
        '300126', '000601', '000563', '601908', '000523', '600724', '300147',
        '300160', '300002', '600984', '000020', '600420', '002338', '300333',
        '603866', '300155', '600667', '603299', '002575', '000980', '000786',
        '002107', '002268', '300162', '002305', '002475', '600094', '601028',
        '300188', '002640', '300085', '000978', '300257', '002624', '300306',
        '002359', '300275', '002565', '300390', '002445', '300313', '002560',
        '601238', '000802', '000662', '002757', '300381', '002323'
    ]
    all_stop_code = [
        '002507', '300019', '300163', '600468', '000887', '600645', '002766',
        '600732', '000555', '000693', '300472', '000568', '600091', '300423',
        '002207', '002499', '000537', '002175', '002554', '600614', '600153',
        '002675', '600355', '600873', '000911', '300405', '300220', '600806',
        '600234', '000968', '000524', '300488', '002352', '600701', '002129',
        '603779', '600265', '002209', '600571', '399106', '600321', '002439',
        '300366', '600113', '601599', '002291', '300282', '000033', '300057',
        '300176', '002264', '000962', '002491', '600209', '600817', '002131',
        '600636', '600546', '000004', '000755', '300044', '600182', '600866',
        '002349', '002599', '600485', '300265', '000582', '000818', '000553',
        '000732', '600656', '002203', '002219', '002319', '300432', '600898',
        '002739', '000836', '000932', '000155', '000617', '399006', '600592',
        '600577', '300071', '300025', '002686', '002798', '002112', '002354',
        '601018', '600129', '399001', '000007', '002145', '002630', '600035',
        '002110', '600230', '600539', '300434', '002212', '300285', '300457',
        '000790', '002663', '000635', '002735', '000571', '600469', '300298',
        '300104', '000505', '300339', '603918', '600375', '603318', '300038',
        '002608', '300338', '000415', '000923', '600228', '002555', '300437',
        '600346', '600738', '002591', '600734', '300391', '002272', '600358',
        '002373', '600768', '600509', '603519', '000511', '000401', '000509',
        '600751', '600247', '300495', '002347', '600339', '002762', '002246',
        '600556', '300441', '000636', '300184', '300512', '002405', '300299',
        '002190', '300465', '002386', '300125', '300362', '002306', '600653',
        '300462', '300142', '600533', '002046', '600120', '300061', '600149',
        '000002', '600728', '002012', '002044', '002505', '399008', '300332',
        '002691', '300067', '000608', '002617', '600603', '300450', '600363',
        '000720', '002759', '000038', '002164', '600870', '600215', '600488',
        '000670', '300046', '603808', '002089', '300308', '600714', '300045',
        '002612', '600749', '603611', '002230', '000159', '601001', '002611',
        '603993', '002469', '000717', '000547', '002524', '002134', '002647',
        '000752', '002371', '600777', '300300', '002065', '600640', '300251',
        '300225', '000950', '300062', '600301', '300242', '000651', '600520',
        '000565', '000917', '002288', '300185', '000920', '600576', '600581',
        '002199', '002694', '600097', '600675', '300292', '300428', '300323',
        '399005', '002072', '002587', '300053', '002331', '601168', '002547',
        '002492', '601918', '600408', '300426', '300226', '002482', '002654',
        '000953', '600318', '600753', '603339', '000610', '300290', '300113',
        '000409', '300043', '000975', '600877', '300331', '000757', '600857',
        '600721', '600760', '002101', '603838', '300411', '600843', '000893',
        '002283', '002703', '600696', '600822', '300464', '000612', '002389',
        '600306', '000300', '600145', '600319', '600511', '000806', '000736',
        '000997', '600139', '600122', '002569', '600212', '999999', '399007',
        '000955', '002069', '000718', '603268', '000766', '600226', '300005',
        '002211', '002515', '300335', '002289', '600793', '002429', '002712',
        '002355', '600455', '600608', '002447', '600725', '600390', '002542',
        '600663', '600173', '300410', '300173', '600856', '600710', '603959',
        '002201', '600432', '399004', '603869', '002278', '600381', '000835',
        '000638', '002052', '002655', '300221', '300110', '300180', '000633',
        '600848', '600385', '600179', '600365', '002314', '300295', '002573',
        '002280', '000925', '002682', '002619'
    ]
    today_df_high_open = all_trade_code
    today_df_high_open = list(
        set(today_df_high_open).difference(set(all_stop_code)))
    if today_df_high_open:
        first_code = today_df_high_open[0]
        stock_hist_anlyse = tds.Stockhistory(code_str=first_code, ktype='D')
        result_df = stock_hist_anlyse.form_temp_df(first_code)
        result_df = result_df.tail(1)
        #temp_df['code'] = pd.Series({'code':[first_code]})
        result_df.insert(2, 'code',
                         pd.Series([first_code], index=result_df.index.values))
        #temp_df['code'] = pd.Series([first_code],index=temp_df.index.values)
        result_df = result_df.set_index('code')
        if len(today_df_high_open) == 1:
            return result_df
        today_df_high_open.pop(0)
        column_list = result_df.columns.values.tolist()
        atr_in_data = {}
        atr_in_df = pd.DataFrame(data=atr_in_data, columns=column_list)
        for high_open_code in today_df_high_open:
            temp_df = stock_hist_anlyse.form_temp_df(high_open_code)
            temp_df = temp_df.tail(1)
            temp_df.insert(
                2, 'code',
                pd.Series([high_open_code], index=temp_df.index.values))
            temp_df = temp_df.set_index('code')
            if temp_df.loc[high_open_code, 'atr_in'] > 0:
                atr_in_df = atr_in_df.append(temp_df, ignore_index=False)
            #print(temp_df)
            result_df = result_df.append(temp_df, ignore_index=False)
        print(result_df)
        print('atr in df :')
        print(atr_in_df)

        island_df = result_df[result_df.island > 0]
        reverse_df = result_df[result_df.reverse > 0]
        atr_in_df = result_df[(result_df.atr_in > 0)
                              & (result_df.position > 0.5)]
        tangle_p_df = result_df[result_df.tangle_p > 0]
        gt2_rmb_df = result_df[result_df.gt2_rmb > 0]
        gt3_rmb_df = result_df[result_df.gt3_rmb > 0]
        island_df.to_csv('island.csv')
        reverse_df.to_csv('reverse.csv')
        atr_in_df.to_csv('atr_in.csv')
        tangle_p_df.to_csv('tangle_p.csv')
        gt2_rmb_df.to_csv('gt2_rmb.csv')
        gt2_rmb_df.to_csv('gt3_rmb.csv')

        print(island_df)
        print(reverse_df)
        print(atr_in_df)
        result_df.to_csv('result.csv')
        return result_df
    else:
        column_list = [
            'close', 'p_change', 'rmb_rate', 'gap', 'star', 'star_h',
            'star_chg', 'ma5_chg', 'ma10_chg', 'k_rate', 'p_rate', 'island',
            'atr_in', 'reverse', 'cross1', 'cross2', 'cross3', 'k_score',
            'position', 'operation', 'std', 'tangle_p', 'tangle_p1', 'gt2_rmb',
            'gt3_rmb'
        ]
        empty_data = {}
        empty_df = pd.DataFrame(data=empty_data, columns=column_list)
        return empty_df
Esempio n. 10
0
import tradeStrategy as tds

import pandas as pd
import file_config as fc

stock_symbol = '600742'
"""
import pandas as pd
file_name = 'C:/work/temp/bs_%s.csv' % stock_symbol
temp_hist_df = pd.read_csv(file_name,index_col=0)
print(temp_hist_df)
"""

s_stock = tds.Stockhistory(stock_symbol,
                           'D',
                           test_num=0,
                           source='yh',
                           rate_to_confirm=0.01)

result_df = s_stock.form_temp_df(stock_symbol)
test_result = s_stock.regression_test0(0.01)
s_stock.form_regression_temp_df(rate_to_confirm=0.0001)
s_stock.form_regression_result(save_dir='C:/work/temp/')
test_result = s_stock.get_regression_result(refresh_regression=False,
                                            rate_to_confirm=0.0001,
                                            from_csv=True,
                                            temp_csv_dir='C:/work/temp/')
test_result = s_stock.get_and_save_regression_result()
test_result = s_stock.get_regression_result_from_csv()

ALL_BACKTEST_DIR = 'D:/work/backtest/'