In [3]:
#coding=utf-8

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


def dependency():
    return []

def market(market=90):
    if market == 83:
        return 'XSHG'
    elif market == 90:
        return 'XSHE'
    else:
        return ""

def compute(startdt,enddt,context):
    '''
    PE
    :param startdt:
    :param enddt:
    :return:
    '''
    #context.config 对应配置的extra部分
    jydbConf = context.config.jydb
    _jyConnStr = "mysql+pymysql://%s:%s@%s:%s/%s"%(jydbConf.user,jydbConf.passwd,jydbConf.host,jydbConf.port,jydbConf.db)
    engine = create_engine(_jyConnStr)
    
    _category = [1, ]
    _sectors = [1, 2, 6]
    _sql = "SELECT p.TradingDay as date,p.PE as value,a.SecuCode as code,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' order by p.TradingDay asc" % (
                        ",".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 = pd.read_sql(sql=_sql,con = engine)
#     market = {90:"XSHE",83:"XSHG"}
    _res.code = _res.code + "." + _res.SecuMarket.apply(market)
    _res = _res.drop(['SecuMarket'],axis=1).set_index(['date','code']).unstack(level=-1)
    _res.columns = _res.columns.droplevel(level=0)
    return _res

In [4]:
# test
config= {"extra":{"jydb":{"host":"172.18.44.5","port":3306,"user":"liangh","passwd":"huaxun!@#db","db":"jydb"}}}
from rqalpha.utils import RqAttrDict
conf = RqAttrDict(config)
from rqalpha.mod.rqalpha_mod_alphaStar_factors.factor_context import FactorContext
context = FactorContext(conf)
context.registerDepending(dependency())
res =  compute(datetime(2017,1,1),datetime(2017,2,1),context)
res.head(10)


Out[4]:
code 000001.XSHE 000002.XSHE 000004.XSHE 000005.XSHE 000006.XSHE 000007.XSHE 000008.XSHE 000009.XSHE 000010.XSHE 000011.XSHE ... 603986.XSHG 603987.XSHG 603988.XSHG 603989.XSHG 603990.XSHG 603993.XSHG 603996.XSHG 603997.XSHG 603998.XSHG 603999.XSHG
date
2017-01-03 6.9596 10.8855 94.9829 66.5377 16.7670 77.2657 50.2749 96.3312 160.1587 32.5653 ... 91.2327 65.4678 1136.0236 36.9102 85.8684 51.2801 47.7668 25.6374 158.9808 129.2768
2017-01-04 6.9596 10.9485 95.5171 68.2912 16.9924 78.3611 50.5441 98.0810 160.9811 32.9684 ... 91.2327 64.7987 1249.6898 37.0290 86.1638 51.9639 48.5919 25.8974 159.2468 131.2987
2017-01-05 6.9672 10.9905 94.9616 67.9015 17.0271 77.6802 50.6517 97.0680 160.5699 32.6325 ... 91.2327 62.7326 1240.2708 36.0587 86.7079 51.9639 49.2757 25.8064 157.2961 129.7070
2017-01-06 6.9368 10.8382 93.9359 68.4861 17.3565 75.3415 51.0285 96.7917 158.5139 34.0097 ... 91.2327 58.8757 1209.1403 35.6132 82.4480 51.5536 49.1814 25.5074 155.7887 130.8255
2017-01-09 6.9520 10.8487 91.9059 68.8757 17.2525 74.2757 51.2977 97.3442 159.9531 34.2112 ... 91.2327 58.6199 1290.0796 35.7914 82.2614 51.8271 49.2521 25.5854 156.9414 135.3427
2017-01-10 6.9520 10.8067 92.4187 68.2912 16.9230 73.3876 50.9747 97.8047 158.5139 32.6157 ... 91.2327 58.1673 1292.6339 36.0884 82.6501 52.3741 48.8749 25.5464 157.4734 133.0626
2017-01-11 6.9444 10.7122 90.7092 66.8299 16.7843 73.5653 50.3826 95.0419 152.3461 31.1545 ... 91.2327 57.6360 1303.1704 35.9498 81.5928 54.2886 47.4367 25.0263 166.5175 128.9326
2017-01-12 6.9520 10.7122 89.8545 66.1480 16.1254 72.8548 50.3826 93.6605 149.4677 30.9698 ... 91.2327 57.0260 1344.5181 35.9795 81.6706 53.1946 47.1302 24.9093 162.2615 129.1477
2017-01-13 6.9596 11.4526 87.6108 65.0764 15.3105 70.4865 49.5752 92.7395 145.1502 28.7361 ... 91.2327 54.0940 1297.7425 34.3558 77.1618 53.6048 48.0968 23.6613 160.6654 125.9642
2017-01-16 6.9444 11.0273 81.7558 62.7383 14.6516 70.4865 47.0991 88.2269 140.0104 27.2581 ... 91.2327 51.0243 1281.2992 32.6528 72.9796 54.5621 49.1106 22.3612 144.7053 115.6392

10 rows × 3088 columns


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

config = {
  "base": {
    "start_date": "2018-01-01",
    "end_date": "2018-04-01",
  },
}

factor_file_path = "./pe.ipynb"

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


2018-01-02 INFO pe compute,startdt:2018-01-02 00:00:00,enddt:2018-03-14
[2018-05-03 11:43:03.315052] INFO: system_log: get factor value success
没有 000003.XSHE 相关数据
没有 000013.XSHE 相关数据
没有 000015.XSHE 相关数据
没有 000047.XSHE 相关数据
没有 000405.XSHE 相关数据
没有 000412.XSHE 相关数据
没有 000508.XSHE 相关数据
没有 000542.XSHE 相关数据
没有 000556.XSHE 相关数据
没有 000588.XSHE 相关数据
没有 000621.XSHE 相关数据
没有 000653.XSHE 相关数据
没有 000658.XSHE 相关数据
没有 000660.XSHE 相关数据
没有 000675.XSHE 相关数据
没有 000689.XSHE 相关数据
没有 000730.XSHE 相关数据
没有 600625.XSHG 相关数据
没有 600632.XSHG 相关数据
没有 600646.XSHG 相关数据
没有 600669.XSHG 相关数据
没有 600670.XSHG 相关数据
没有 600709.XSHG 相关数据
没有 600813.XSHG 相关数据
没有 600878.XSHG 相关数据

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