In [6]:
#coding=utf-8

"""
@author: evilXu
@file: testFactor.py
@time: 2017/11/16 17:48
@description: 
"""

from rqalpha.api import *
import traceback
from datetime import *
import pymysql
import pandas as pd
from rqalpha.utils.logger import user_log


def dependency():
    return []


def compute(startdt,enddt,context):
    '''
    PE
    :param startdt:
    :param enddt:
    :return:
    '''
    user_log.info("testFactor compute")
    #context.config 对应配置的extra部分
    _cnnConf = {"host":context.config.jydb,"port":3306,"db":"jydb","user":"liangh","passwd":"huaxun!@#db"}
    jyConn = pymysql.connect(host=_cnnConf.get("host"), port=_cnnConf.get("port") \
                                  , db=_cnnConf.get("db"), user=_cnnConf.get("user"),
                                  passwd=_cnnConf.get("passwd"), charset='utf8')
    _category = [1, ]
    _sectors = [1, 2, 6]
    _sql = "SELECT p.TradingDay,p.PE,a.SecuCode,a.SecuMarket" \
                   " FROM LC_DIndicesForValuation as p inner join secumain as a "\
                    "on a.innerCode=p.innerCode where a.SecuMarket in (83,90) " \
                   "and a.SecuCategory in (%s) and a.ListedSector in (%s) " \
                   "and a.ListedState!=9 and p.TradingDay between '%s' and '%s'" % (
                        ",".join([str(i) for i in _category]), ",".join([str(i) for i in _sectors])
                        ,startdt.strftime('%Y-%m-%d'),
                       enddt.strftime('%Y-%m-%d'))

    # print(_sql)
    _res = []
    _res_tmp = []
    try:
        _now = datetime.now()
        _cursor = jyConn.cursor()
        _cursor.execute(_sql)
        for _row in _cursor:
            _res_tmp.append({
                "code": _row[2] + "." + market(_row[3]),  # secumain.code(_row[0],_row[13]),#
                "date": _row[0],
                "value": float(_row[1])
            })
    except Exception as e:
        traceback.print_exc()
        return

    _res_tmp = sorted(_res_tmp,key=lambda x:x['date'])
    _res = []
    if len(_res_tmp) >0:
        _lastDt = _res_tmp[0]['date']
        _adt = {"date":_lastDt}
        for item in _res_tmp:
            if item['date'] != _lastDt:
                _res.append(_adt)
                _lastDt = item['date']
                _adt = {"date": _lastDt}
            else:
                _adt.update({item['code']:item['value']})
        if len(_adt) > 1:
            _res.append(_adt)
    if len(_res) < 1:
        return pd.DataFrame()
    return pd.DataFrame(_res).set_index(['date'])
    # print("testFactor")
                        
def market(market=90):
    if market == 83:
        return 'XSHG'
    elif market == 90:
        return 'XSHE'
    else:
        return ""

if __name__ == "__main__":
    from rqalpha.utils import RqAttrDict
    config= {"factor_data_path":"E:\\evilAlpha\\test","factor_data_init_date":"2017-01-01","extra":{"jydb":"172.18.44.5"}}
    conf = RqAttrDict(config)
    from rqalpha.mod.rqalpha_mod_alphaStar_factors.factor_context import FactorContext
    context = FactorContext(conf)
    context.registerDepending(dependency())
    fValue = compute(datetime(2017,6,8),datetime(2017,7,1),context)
    # _fValue_aDay = fValue.iloc[0]
    print(fValue)
    print(fValue.index)
    from rqalpha.mod.rqalpha_mod_alphaStar_factors.factor_data import FactorData
    FactorData(fname="testFactor",path=conf.factor_data_path,defaultInitDate = conf.factor_data_init_date).append(fValue)


            000001.XSHE  000002.XSHE  000004.XSHE  000005.XSHE  000006.XSHE  \
date                                                                          
2017-06-08       6.8977      11.0313      74.6204      53.0669      14.3318   
2017-06-09          NaN      11.0313      74.6204      53.2679      14.7690   
2017-06-12          NaN      11.4700      74.6204      51.6598      14.4775   
2017-06-13          NaN      11.6286      74.6204      52.8659      14.5423   
2017-06-14          NaN      11.2850      74.6204      52.3634      14.4127   
2017-06-15          NaN      11.1211      67.1584      53.2679      14.4775   
2017-06-16          NaN      11.0683      60.4425      53.5694      14.1536   
2017-06-19          NaN      11.0894      55.9653      54.7755      14.3318   
2017-06-20          NaN      11.1159      56.0275      54.7755      14.3642   
2017-06-21          NaN      11.1211      55.0740      54.0720      14.1860   
2017-06-22          NaN      11.5651      53.9961      53.5694      13.7650   
2017-06-23          NaN      12.7227      52.6903      52.9664      13.9107   
2017-06-26          NaN      13.9966      54.1620      53.3684      14.0889   
2017-06-27          NaN      13.4997      53.8511      53.0669      14.1212   
2017-06-28          NaN      13.2143      53.7889      52.5644      14.1051   
2017-06-29          NaN      13.2989      52.9598      52.7654      14.3642   
2017-06-30          NaN      11.9982      66.1924    -153.7430      12.8424   

            000007.XSHE  000008.XSHE  000009.XSHE  000010.XSHE  000011.XSHE  \
date                                                                          
2017-06-08      77.8426      39.1940      68.7342      92.1288      18.3552   
2017-06-09      77.8426      39.4643      68.9158      92.8254      19.4547   
2017-06-12      77.8426      39.2481      67.2814      89.3423      18.7368   
2017-06-13      77.8426      39.4643      69.5514      90.7356      18.6732   
2017-06-14      77.8426      39.6806      70.8225      90.2131      18.0553   
2017-06-15      77.8426      39.7346      70.7317      91.7805      18.1825   
2017-06-16      77.8426      39.7346      70.8225      91.6063      18.1007   
2017-06-19      77.8426      40.0590      71.0949      91.2580      19.0639   
2017-06-20      77.8426      39.9509      71.9121      91.6063      19.4092   
2017-06-21      77.8426      39.8427      71.3673      91.0839      19.1457   
2017-06-22      77.8426      39.1399      70.0961      89.1681      18.3824   
2017-06-23      77.8426      38.8696      71.2765      88.1232      18.5914   
2017-06-26      77.8426      39.9509      74.4545      89.3423      18.7641   
2017-06-27      77.8426      39.6265      73.0925      89.3423      19.0457   
2017-06-28      77.8426      39.3021      73.0017      88.1232      18.7731   
2017-06-29      77.8426      39.1940      73.0017      88.6457      18.3370   
2017-06-30      62.2812      38.1776      72.1653    -129.3607      16.4530   

               ...       603987.XSHG  603988.XSHG  603989.XSHG  603990.XSHG  \
date           ...                                                            
2017-06-08     ...           51.6006     233.7065      38.8069      70.4559   
2017-06-09     ...           49.5705     233.9646      39.4571      68.9226   
2017-06-12     ...           47.6616     232.2318      39.5725      66.5863   
2017-06-13     ...           49.1160     234.0015      40.5794      67.7909   
2017-06-14     ...           48.5706     234.8863      40.0969      68.1377   
2017-06-15     ...           49.6311     239.0155      40.2647      69.7805   
2017-06-16     ...           49.0857     238.4994      40.2438      70.3281   
2017-06-19     ...           48.9948     238.8681      40.2018      70.1273   
2017-06-20     ...           48.9948     239.3105      39.9291      70.5654   
2017-06-21     ...           48.8433     238.2413      40.9884      75.2627   
2017-06-22     ...           47.5707     234.0752      39.6459      73.5455   
2017-06-23     ...           47.0253     231.5682      40.2333      71.9021   
2017-06-26     ...           47.4192     231.3838      41.3660      72.1606   
2017-06-27     ...           47.7828     231.3838      41.4709      71.9390   
2017-06-28     ...           46.8435     230.0197      41.8275      71.7544   
2017-06-29     ...           46.6011     229.9091      41.9114      71.3112   
2017-06-30     ...           44.7973     214.8588      41.2032      64.6018   

            603991.XSHG  603993.XSHG  603996.XSHG  603997.XSHG  603998.XSHG  \
date                                                                          
2017-06-08      79.0908      48.4260      42.9950      37.7319      84.2009   
2017-06-09      79.6351      48.7744      42.7216      36.2035      83.1152   
2017-06-12      75.8059      49.0066      42.2494      35.1288      82.6327   
2017-06-13      80.5098      52.2582      42.9453      35.0333      83.1755   
2017-06-14      82.3757      52.8389      42.8956      34.6512      83.5374   
2017-06-15      84.3972      53.6518      42.1997      34.7706      82.9343   
2017-06-16      81.1123      58.5292      42.4482      34.1020      82.5724   
2017-06-19      80.9763      57.4841      42.4731      34.2452      82.9343   
2017-06-20      83.6392      57.7163      42.3488      34.8661      87.7595   
2017-06-21      83.3671      57.8325      41.9512      34.8184      86.0104   
2017-06-22      76.9721      56.4389      41.4293      34.7467      84.4422   
2017-06-23      76.1363      56.0905      40.3109      34.7229      84.6231   
2017-06-26      76.1363      56.5550      41.6032      34.6512      84.0199   
2017-06-27      80.0044      57.1357      41.6281      34.6035      84.5628   
2017-06-28      79.0519      56.2066      42.1749      34.6512      83.4168   
2017-06-29      77.0499      57.7163      43.1441      34.6512      83.0549   
2017-06-30      80.3493      64.6718      42.1228      33.4757      76.6954   

            603999.XSHG  
date                     
2017-06-08      67.4683  
2017-06-09      66.9036  
2017-06-12      64.7111  
2017-06-13      66.7375  
2017-06-14      68.5646  
2017-06-15      75.4078  
2017-06-16      72.5177  
2017-06-19      73.1821  
2017-06-20      73.0160  
2017-06-21      72.0526  
2017-06-22      70.8567  
2017-06-23      71.1889  
2017-06-26      71.7536  
2017-06-27      73.6471  
2017-06-28      72.5177  
2017-06-29      73.7136  
2017-06-30      73.6309  

[17 rows x 3279 columns]
DatetimeIndex(['2017-06-08', '2017-06-09', '2017-06-12', '2017-06-13',
               '2017-06-14', '2017-06-15', '2017-06-16', '2017-06-19',
               '2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23',
               '2017-06-26', '2017-06-27', '2017-06-28', '2017-06-29',
               '2017-06-30'],
              dtype='datetime64[ns]', name='date', freq=None)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-e48eb4e6b075> in <module>()
     95     print(fValue.index)
     96     from rqalpha.mod.rqalpha_mod_alphaStar_factors.factor_data import FactorData
---> 97     FactorData(fname="testFactor",path=conf.factor_data_path,defaultInitDate = conf.factor_data_init_date).append(fValue)
     98 

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.9-py3.5.egg\rqalpha\mod\rqalpha_mod_alphaStar_factors\factor_data.py in append(self, datas)
     88         # print(idxs, years)
     89         for i in range(len(years)):
---> 90             self._appendAyear(years[i],datas.iloc[idxs[i]:idxs[i + 1]])
     91 
     92     def _appendAyear(self, year, datas=pd.DataFrame()):

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.9-py3.5.egg\rqalpha\mod\rqalpha_mod_alphaStar_factors\factor_data.py in _appendAyear(self, year, datas)
     96         if os.path.exists(_file):
     97             df = pd.read_hdf(_file)
---> 98             df = df.append(datas, verify_integrity=True)
     99             df.to_hdf(_file,key="root", mode='w')
    100         else:

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pandas\core\frame.py in append(self, other, ignore_index, verify_integrity)
   5192             to_concat = [self, other]
   5193         return concat(to_concat, ignore_index=ignore_index,
-> 5194                       verify_integrity=verify_integrity)
   5195 
   5196     def join(self, other, on=None, how='left', lsuffix='', rsuffix='',

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
    210                        keys=keys, levels=levels, names=names,
    211                        verify_integrity=verify_integrity,
--> 212                        copy=copy)
    213     return op.get_result()
    214 

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pandas\core\reshape\concat.py in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy)
    361         self.copy = copy
    362 
--> 363         self.new_axes = self._get_new_axes()
    364 
    365     def get_result(self):

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pandas\core\reshape\concat.py in _get_new_axes(self)
    441                 new_axes[i] = ax
    442 
--> 443         new_axes[self.axis] = self._get_concat_axis()
    444         return new_axes
    445 

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pandas\core\reshape\concat.py in _get_concat_axis(self)
    498                                                   self.levels, self.names)
    499 
--> 500         self._maybe_check_integrity(concat_axis)
    501 
    502         return concat_axis

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pandas\core\reshape\concat.py in _maybe_check_integrity(self, concat_index)
    507                 overlap = concat_index.get_duplicates()
    508                 raise ValueError('Indexes have overlapping values: '
--> 509                                  '{overlap!s}'.format(overlap=overlap))
    510 
    511 

ValueError: Indexes have overlapping values: DatetimeIndex(['2017-06-08', '2017-06-09', '2017-06-12', '2017-06-13',
               '2017-06-14', '2017-06-15', '2017-06-16', '2017-06-19',
               '2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23',
               '2017-06-26', '2017-06-27', '2017-06-28', '2017-06-29',
               '2017-06-30'],
              dtype='datetime64[ns]', freq=None)

In [1]:
# evaluateFileDemo
from rqalpha.mod.rqalpha_mod_alphaStar_factors import evaluate_file

config = {
  "base": {
    "start_date": "2017-06-01",
    "end_date": "2017-7-01",
  },
}

factor_file_path = "./testFactor.ipynb"

evaluate_file(factor_file_path=factor_file_path,config=config,config_file = "../config_factor.yml")


WARNING: better_exceptions will only inspect code from the command line
         when using: `python -m better_exceptions'. Otherwise, only code
         loaded from files will be inspected!
[2018-03-06 13:30:33.757451] DEBUG: system_log: evaluateRun
2017-06-01 INFO test Factor init
2017-06-01 INFO testFactor compute
[2018-03-06 13:31:46.095229] INFO: system_log: get factor value success
没有 002926.XSHE 相关数据
[2018-03-06 13:32:30.666509] INFO: system_log: 2017-06-01 00:00:00, factorValue len:3279, yieldData len:3554
[2018-03-06 13:32:30.741739] INFO: system_log: 2017-06-08 00:00:00, factorValue len:3279, yieldData len:3554
[2018-03-06 13:32:30.812192] INFO: system_log: 2017-06-15 00:00:00, factorValue len:3279, yieldData len:3554
[2018-03-06 13:32:30.883380] INFO: system_log: 2017-06-22 00:00:00, factorValue len:3279, yieldData len:3554

In [2]:
%load_ext rqalpha.mod.rqalpha_mod_alphaStar_factors
%evaluate -s 20170101 -e 20170131 -f ./testFactor.ipynb -rt r --config ../config_factor.yml


2017-01-03 INFO test Factor init
2017-01-03 INFO testFactor compute
[2018-02-07 18:00:01.171061] INFO: system_log: get factor value success
没有 002926.XSHE 相关数据
[2018-02-07 18:00:53.747568] INFO: system_log: 2017-01-03 00:00:00, factorValue len:3088, yieldData len:3554
[2018-02-07 18:00:53.815246] INFO: system_log: 2017-01-10 00:00:00, factorValue len:3088, yieldData len:3554
[2018-02-07 18:00:53.877411] INFO: system_log: 2017-01-17 00:00:00, factorValue len:3088, yieldData len:3554