In [11]:
#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 init():
    user_log.info("test Factor init")


def compute(startdt,enddt):
    '''
    PE
    :param startdt:
    :param enddt:
    :return:
    '''
    user_log.info("testFactor compute")
    _cnnConf = {"host":"172.18.44.231","port":3306,"db":"jydb","user":"quant1","passwd":"quant1@591"}
    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__":
    fValue = compute(datetime(2015,6,8),datetime(2015,7,1).date())
    # _fValue_aDay = fValue.iloc[0]
    print(fValue)
    print(fValue.index)


            000001.XSHE  000002.XSHE  000004.XSHE  000005.XSHE  000006.XSHE  \
date                                                                          
2015-06-08      12.1409      11.9479     749.2220     277.9779      37.5411   
2015-06-09      11.9233          NaN     723.4597     283.1991      38.6546   
2015-06-10      11.7126      11.5318     726.6359     288.6292      37.5411   
2015-06-11      11.5722          NaN     771.1022     295.1035      37.1434   
2015-06-12      11.5932      11.5021     833.7432     293.2238      38.6148   
2015-06-15      11.1789      11.1083     796.8644     281.1106      36.3282   
2015-06-16      10.9823      10.6848     751.6923     253.7514      33.2859   
2015-06-17      11.0455      10.8334     792.2766     255.4222      34.3398   
2015-06-18      10.7997      10.5362     763.5147     246.0240      34.3994   
2015-06-19      10.2731      10.0681     694.8743     235.9992      30.9197   
2015-06-23      10.4978      10.6031     700.1679     225.1391      30.9794   
2015-06-24      10.6172          NaN     708.1083     230.5692      31.3969   
2015-06-25      10.4416      10.5510     682.5225     231.4045      29.7267   
2015-06-26       9.6692       9.9789     614.2350     208.2223      26.7639   
2015-06-29       9.5217      10.1721     663.4655     187.3374      24.5568   
2015-06-30       9.7606      10.1642     332.6255     185.7177      25.5511   
2015-07-01       9.3444       9.9752     308.4346     167.1054      24.2707   

            000007.XSHE  000008.XSHE  000009.XSHE  000010.XSHE  000011.XSHE  \
date                                                                          
2015-06-08    -222.2834     363.2539     123.4276    -136.1338      31.9124   
2015-06-09    -222.2834     363.2539     126.7816    -131.4110      29.9876   
2015-06-10    -222.2834     363.2539     124.9928    -133.3167      29.8196   
2015-06-11    -222.2834     363.2539     127.4524    -135.9681      29.5140   
2015-06-12    -222.2834     363.2539     133.6014    -145.6623      29.6821   
2015-06-15    -222.2834     363.2539     131.3654    -139.1166      30.4764   
2015-06-16    -222.2834     363.2539     124.8810    -129.9195      29.9570   
2015-06-17    -222.2834     363.2539     126.1108    -134.1452      30.5223   
2015-06-18    -222.2834     363.2539     121.1915    -128.1795      31.8513   
2015-06-19    -222.2834     363.2539     109.0612    -115.3367      28.6738   
2015-06-23    -222.2834     363.2539     110.4028    -106.2225      26.9323   
2015-06-24    -222.2834     363.2539     111.4090    -108.9567      27.7572   
2015-06-25    -222.2834     363.2539     104.5333    -103.5710      26.8254   
2015-06-26    -222.2834     363.2539      94.0800     -93.2139      24.1367   
2015-06-29    -222.2834     363.2539      84.6887     -83.9340      21.7230   
2015-06-30    -199.8390     312.2970     135.4924     -95.5746      26.3827   
2015-07-01    -199.8390     312.2970     121.9515     -87.5290      23.7444   

               ...       603918.XSHG  603939.XSHG  603968.XSHG  603969.XSHG  \
date           ...                                                            
2015-06-08     ...           61.5673     106.8384      73.2864      94.1698   
2015-06-09     ...           67.7332     106.8384      66.6908      94.6961   
2015-06-10     ...           74.4996     106.8384      73.3581      96.1287   
2015-06-11     ...           81.9589     106.8384      80.6975      97.1812   
2015-06-12     ...           90.1571     106.8384      74.2721      95.0177   
2015-06-15     ...           99.1635     106.8384      66.8431      90.3107   
2015-06-16     ...          109.0706     106.8384      67.5780      81.4521   
2015-06-17     ...          119.9708     106.8384      67.7662      83.0601   
2015-06-18     ...          131.9794     106.8384      60.9913      77.4175   
2015-06-19     ...          145.1889     106.8384      55.2650      69.6699   
2015-06-23     ...          159.7147     106.8384      57.7473      68.8220   
2015-06-24     ...          175.6955     106.8384      57.2275      72.5058   
2015-06-25     ...          159.2298     106.8384      53.4458      67.7988   
2015-06-26     ...          143.3183     106.8384      48.1048      61.0160   
2015-06-29     ...          128.9773     106.8384      43.3015      54.9056   
2015-06-30     ...          144.7298      99.2712      47.7510      64.4928   
2015-07-01     ...          130.2568      99.2712      44.6703      59.6968   

            603979.XSHG  603988.XSHG  603989.XSHG  603993.XSHG  603997.XSHG  \
date                                                                          
2015-06-08          NaN      81.0957      84.9424      52.4581      72.7186   
2015-06-09          NaN      81.4786      80.1424      53.0088      75.5650   
2015-06-10          NaN      83.5590      85.3038      52.4291      79.1386   
2015-06-11          NaN      88.5876      93.8308      51.7915      83.4601   
2015-06-12          NaN      86.9028      87.3367      52.1683      79.1593   
2015-06-15          NaN      80.7256      78.6064      53.3856      71.2642   
2015-06-16          NaN      82.9080      72.8465      48.0818      67.4620   
2015-06-17          NaN      91.2040      78.3918      49.0092      71.3265   
2015-06-18          NaN      84.4141      70.5538      46.9515      65.6960   
2015-06-19          NaN      75.9778      66.7590      43.6475      59.1306   
2015-06-23          NaN      70.1068      70.5199      44.0532      61.7900   
2015-06-24          NaN      75.5694      69.3340      45.6183      62.9119   
2015-06-25          NaN      72.2382      64.3195      41.8795      58.5696   
2015-06-26          NaN      65.0144      57.8932      37.7061      52.7106   
2015-06-29          NaN      71.4342      52.0993      33.9384      47.4333   
2015-06-30      34.3506      97.4956      58.9481      49.5859      54.7675   
2015-07-01      37.7926      98.7472      56.1062      44.8118      50.7246   

            603998.XSHG  
date                     
2015-06-08     103.6254  
2015-06-09     103.6254  
2015-06-10     103.6254  
2015-06-11     103.6254  
2015-06-12     103.6254  
2015-06-15     103.6254  
2015-06-16     103.6254  
2015-06-17     103.6254  
2015-06-18     103.6254  
2015-06-19     103.6254  
2015-06-23     103.6254  
2015-06-24     103.6254  
2015-06-25     103.6254  
2015-06-26     103.6254  
2015-06-29     103.6254  
2015-06-30     106.9554  
2015-07-01      96.2615  

[17 rows x 2779 columns]
DatetimeIndex(['2015-06-08', '2015-06-09', '2015-06-10', '2015-06-11',
               '2015-06-12', '2015-06-15', '2015-06-16', '2015-06-17',
               '2015-06-18', '2015-06-19', '2015-06-23', '2015-06-24',
               '2015-06-25', '2015-06-26', '2015-06-29', '2015-06-30',
               '2015-07-01'],
              dtype='datetime64[ns]', name='date', freq=None)

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

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

factor_file_path = "./testFactor.ipynb"#"./rqalpha/examples/testFactor.py"

evaluate_file(factor_file_path=factor_file_path,config=config,config_file = "E:/evilAlpha/rqalpha/rqalpha/config_factor.yml")


haha: E:/evilAlpha/rqalpha/rqalpha/config_factor.yml
2010-06-01 INFO test Factor init
2010-06-01 INFO testFactor compute
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-14-a4bbf7fb7e9c> in <module>()
     11 factor_file_path = "./testFactor.ipynb"#"./rqalpha/examples/testFactor.py"
     12 
---> 13 evaluate_file(factor_file_path=factor_file_path,config=config,config_file = "E:/evilAlpha/rqalpha/rqalpha/config_factor.yml")

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\mod\rqalpha_mod_alphaStar_factors\__init__.py in evaluate_file(factor_file_path, config, config_file)
    101     clear_all_cached_functions()
    102     evaluateRun(fname,config)
--> 103 
    104 @DeprecationWarning
    105 def evaluate_code(fname,code, config=None):

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\mod\rqalpha_mod_alphaStar_factors\evaluate.py in evaluateRun(fname, config, source_code, user_funcs)
     82     except CustomException as e:
     83         code = _exception_handler(e)
---> 84         mod_handler.tear_down(code, e)
     85     except Exception as e:
     86         exc_type, exc_val, exc_tb = sys.exc_info()

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\mod\rqalpha_mod_alphaStar_factors\evaluate.py in getFactorsTmp(env, source_code, user_funcs, config)
    249     for _instrument in _alInstruments:
    250         _abar = env.data_proxy.history_bars(_instrument.order_book_id, _len, '1d', field = ['datetime',config.base.price], dt = config.base.end_date,
--> 251                      skip_suspended=False, include_now=True,
    252                      adjust_type='post')
    253         if _abar is None:

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\mod\rqalpha_mod_alphaStar_factors\factor.py in compute(self, startdt, enddt)
     41         '''
     42         with ModifyExceptionFromType(EXC_TYPE.USER_EXC):
---> 43             return self._compute(startdt, enddt)
     44 

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\rqalpha-3.0.7-py3.5.egg\rqalpha\user_module.py in compute(startdt, enddt)
     44       "2015-06-18    -222.2834     363.2539     121.1915    -128.1795      31.8513   \n",
     45       "2015-06-19    -222.2834     363.2539     109.0612    -115.3367      28.6738   \n",
---> 46       "2015-06-23    -222.2834     363.2539     110.4028    -106.2225      26.9323   \n",
     47       "2015-06-24    -222.2834     363.2539     111.4090    -108.9567      27.7572   \n",
     48       "2015-06-25    -222.2834     363.2539     104.5333    -103.5710      26.8254   \n",

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\cursors.py in execute(self, query, args)
    164         query = self.mogrify(query, args)
    165 
--> 166         result = self._query(query)
    167         self._executed = query
    168         return result

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\cursors.py in _query(self, q)
    320         conn = self._get_db()
    321         self._last_executed = q
--> 322         conn.query(q)
    323         self._do_get_result()
    324         return self.rowcount

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\connections.py in query(self, sql, unbuffered)
    854                 sql = sql.encode(self.encoding, 'surrogateescape')
    855         self._execute_command(COMMAND.COM_QUERY, sql)
--> 856         self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    857         return self._affected_rows
    858 

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\connections.py in _read_query_result(self, unbuffered)
   1055         else:
   1056             result = MySQLResult(self)
-> 1057             result.read()
   1058         self._result = result
   1059         if result.server_status is not None:

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\connections.py in read(self)
   1345                 self._read_load_local_packet(first_packet)
   1346             else:
-> 1347                 self._read_result_packet(first_packet)
   1348         finally:
   1349             self.connection = None

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\connections.py in _read_result_packet(self, first_packet)
   1411         self.field_count = first_packet.read_length_encoded_integer()
   1412         self._get_descriptions()
-> 1413         self._read_rowdata_packet()
   1414 
   1415     def _read_rowdata_packet_unbuffered(self):

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\connections.py in _read_rowdata_packet(self)
   1445         rows = []
   1446         while True:
-> 1447             packet = self.connection._read_packet()
   1448             if self._check_packet_is_eof(packet):
   1449                 self.connection = None  # release reference to kill cyclic reference.

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\connections.py in _read_packet(self, packet_type)
    985         buff = b''
    986         while True:
--> 987             packet_header = self._read_bytes(4)
    988             if DEBUG: dump_packet(packet_header)
    989 

D:\Program Files\Anaconda3\envs\rqalpha\lib\site-packages\pymysql\connections.py in _read_bytes(self, num_bytes)
   1016 
   1017     def _read_bytes(self, num_bytes):
-> 1018         self._sock.settimeout(self._read_timeout)
   1019         while True:
   1020             try:

KeyboardInterrupt: