Example #1
0
def get_latest_temp_datas(write_file_name=fc.ALL_TEMP_FILE,
                          data_dir=fc.ALL_TEMP_DIR,
                          files=[]):
    """
    获取所有回测最后一个K线的数据:特定目录下
    """
    #columns = ['date', 'close', 'id', 'trade', 'p_change', 'position', 'operation', 's_price', 'b_price', 'profit', 'cum_prf', 'fuli_prf', 'hold_count']
    columns = pds.get_data_columns(dest_dir=data_dir)
    #df = combine_file(tail_num=1,dest_dir=data_dir,keyword='bs_',prefile_slip_num=3,columns=columns)
    df = combine_file(tail_num=1,
                      dest_dir=data_dir,
                      keyword='',
                      prefile_slip_num=0,
                      columns=columns,
                      file_list=files)
    if df.empty:
        return df
    df['counts'] = df.index
    df = df[columns + ['counts', 'code']]
    df['code'] = df['code'].apply(lambda x: pds.format_code(x))
    df['name'] = df['code'].apply(
        lambda x: pds.format_name_by_code(x, CHINESE_DICT))
    df = df.set_index('code')
    if write_file_name:
        df.to_csv(write_file_name, encoding='utf-8')
    return df
def get_latest_yh_k_stocks(write_file_name=fc.ALL_YH_KDATA_FILE,
                           data_dir=fc.YH_SOURCE_DATA_DIR):
    """
    获取所有银河最后一个K线的数据:特定目录下
    """
    columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'amount']
    columns = pds.get_data_columns(dest_dir=data_dir)
    df = combine_file(tail_num=1,
                      dest_dir=data_dir,
                      keyword='',
                      prefile_slip_num=0,
                      columns=columns)
    if df.empty:
        return df
    df['counts'] = df.index
    df = df[['date', 'open', 'high', 'low', 'close', 'volume', 'amount'] +
            ['counts', 'code']]
    df['code'] = df['code'].apply(lambda x: pds.format_code(x))
    df['name'] = df['code'].apply(
        lambda x: pds.format_name_by_code(x, CHINESE_DICT))
    df = df.set_index('code')
    """
    if CHINESE_DICT:#添加中文代码
            try:
                tail_df['name'] = CHINESE_DICT[prefile_name]
            except:
                tail_df['name'] = '某指数'
    """
    if write_file_name:
        try:
            df.to_csv(write_file_name, encoding='utf-8')
        except Exception as e:
            print('get_latest_yh_k_stocks: ', e)
    return df
def get_latest_yh_k_stocks_from_csv(file_name=fc.ALL_YH_KDATA_FILE):
    """
    获取股票K线数据,数据来源银河证券
    """
    #file_name = 'C:/work/result/all_yh_stocks.csv'
    #columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'amount']+['counts','code']
    columns = pds.get_data_columns(
        dest_dir=fc.YH_SOURCE_DATA_DIR) + ['counts', 'code']
    #try:
    if True:
        df = pd.read_csv(file_name)  #,usecols=columns)
        #print(df)
        #print(type(df['code']))
        df['code'] = df['name'].apply(lambda x: pds.format_code(x))
        df['name'] = df['code'].apply(
            lambda x: pds.format_name_by_code(x, CHINESE_DICT))
        df = df.set_index('code')
        return df
Example #4
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