<commit_before>#!/usr/bin/python
#coding=utf-8

'''
因子工具类

'''

import common.TushareBasedata as TushareBasedata

'''
获取基本因子的值
'''
def get_basic_factor_val(factor_name, time, stock_list):
    t = TushareBasedata()
    trade_date_str = time
    df = None
    if len(time) == 1:
        df = t.get_factor_data_by_stocklist(trade_date_str, stock_list, factor_name, [1])
    elif len(time) == 2:
        start_date_str = time[0]
        end_date_str = time[1]
        df = t.get_factor_data_by_date(stock_list[0], start_date_str, end_date_str, factor_name, [1])
    return df<commit_msg>修改回测函数<commit_after>#!/usr/bin/python
#coding=utf-8

'''
因子工具类

'''

import common.TushareBasedata as TushareBasedata
import common.BaseTools as bt

from datetime import datetime
import pandas as pd

'''
获取基本因子的值
'''
def get_basic_factor_val(factorname, date_list, stock_list):
    t = TushareBasedata.TushareBasedata()
    factorDf = pd.DataFrame()
    conn = bt.getConnection()
    if len(date_list) > 1:
        start_date_str = datetime.strftime(date_list[0], '%Y-%m-%d')
        end_date_str = datetime.strftime(date_list[1], '%Y-%m-%d')
        datetimeDf = bt.getTradeDay(conn, start_date_str, end_date_str, type=1)
        # print datetimeDf
        datetimelist = datetimeDf.tradedate.tolist()
        for d in datetimelist:
            trade_date_str = datetime.strftime(d, '%Y-%m-%d')
            #for factorname in factorname_list:
            df = t.get_factor_data_by_stocklist(trade_date_str, stock_list, factorname, [1])

            # df['STOCKCODE']=df.index
            df['date'] = d
            df = df.reset_index(drop = True)
            #print df
            if len(factorDf)==0:
                factorDf = df
            else:
                factorDf = pd.concat([factorDf, df])

    return factorDf

if __name__ == "__main__":
    startdate = datetime.strptime('2017-3-1', '%Y-%m-%d')
    enddate = datetime.strptime('2017-3-2', '%Y-%m-%d')
    print get_basic_factor_val('roe', [startdate, enddate], ['600725.SZ', '600306.SZ'])
{'fraction_non_alphanumeric': 0.1324828263002944, 'fraction_numerical': 0.019136408243375858, 'mean_word_length': 3.0296442687747036, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3177193', 'n_tokens_mistral': 783, 'n_tokens_neox': 762, 'n_words': 137}