金融量化分析【day112】:量化平台的使用-初始化函数

 一、set_benchmark - 设置基准

1、实现代码

# 导入函数库
import jqdata

#初始化函数,设定基准等等
def initialize(context):
    set_benchmark('000300.XSHG')

2、输出(红色的折现就是基准收益)

3、api相关说明文档

二、get_industry_stocks - 获取行业成份股

1、实现代码

# 导入函数库
import jqdata

#初始化函数,设定基准等等
def initialize(context):
    set_benchmark('000300.XSHG')
    g.security = get_index_stocks('000300.XSHG')
    print(g.security)

2、输出结果如下

2018-10-12 00:00:00 - INFO  - [u'000001.XSHE', u'000002.XSHE', u'000060.XSHE', u'000063.XSHE', u'000069.XSHE', u'000100.XSHE', u'000157.XSHE', u'000166.XSHE', u'000333.XSHE', u'000338.XSHE', u'000402.XSHE', u'000413.XSHE', u'000415.XSHE', u'000423.XSHE', u'000425.XSHE', u'000503.XSHE', u'000538.XSHE', u'000540.XSHE', u'000559.XSHE', u'000568.XSHE', u'000623.XSHE', u'000625.XSHE', u'000627.XSHE', u'000630.XSHE', u'000651.XSHE', u'000671.XSHE', u'000709.XSHE', u'000723.XSHE', u'000725.XSHE', u'000728.XSHE', u'000768.XSHE', u'000776.XSHE', u'000783.XSHE', u'000786.XSHE', u'000792.XSHE', u'000826.XSHE', u'000839.XSHE', u'000858.XSHE', u'000876.XSHE', u'000895.XSHE', u'000898.XSHE', u'000938.XSHE', u'000959.XSHE', u'000961.XSHE', u'000963.XSHE', u'000983.XSHE', u'001965.XSHE', u'001979.XSHE', u'002007.XSHE', u'002008.XSHE', u'002024.XSHE', u'002027.XSHE', u'002044.XSHE', u'002050.XSHE', u'002065.XSHE', u'002074.XSHE', u'002081.XSHE', u'002085.XSHE', u'002142.XSHE', u'002146.XSHE', u'002153.XSHE', u'002202.XSHE', u'002230.XSHE', u'002236.XSHE', u'002241.XSHE', u'002252.XSHE', u'002294.XSHE', u'002304.XSHE', u'002310.XSHE', u'002352.XSHE', u'002385.XSHE', u'002411.XSHE', u'002415.XSHE', u'002450.XSHE', u'002456.XSHE', u'002460.XSHE', u'002466.XSHE', u'002468.XSHE', u'002470.XSHE', u'002475.XSHE', u'002493.XSHE', u'002500.XSHE', u'002508.XSHE', u'002555.XSHE', u'002558.XSHE', u'002572.XSHE', u'002594.XSHE', u'002601.XSHE', u'002602.XSHE', u'002608.XSHE', u'002624.XSHE', u'002625.XSHE', u'002673.XSHE', u'002714.XSHE', u'002736.XSHE', u'002739.XSHE', u'002797.XSHE', u'002925.XSHE', u'300003.XSHE', u'300015.XSHE', u'300017.XSHE', u'300024.XSHE', u'300027.XSHE', u'300033.XSHE', u'300059.XSHE', u'300070.XSHE', u'300072.XSHE', u'300122.XSHE', u'300124.XSHE', u'300136.XSHE', u'300144.XSHE', u'300251.XSHE', u'300408.XSHE', u'300433.XSHE', u'600000.XSHG', u'600008.XSHG', u'600009.XSHG', u'600010.XSHG', u'600011.XSHG', u'600015.XSHG', u'600016.XSHG', u'600018.XSHG', u'600019.XSHG', u'600023.XSHG', u'600025.XSHG', u'600028.XSHG', u'600029.XSHG', u'600030.XSHG', u'600031.XSHG', u'600036.XSHG', u'600038.XSHG', u'600048.XSHG', u'600050.XSHG', u'600061.XSHG', u'600066.XSHG', u'600068.XSHG', u'600085.XSHG', u'600089.XSHG', u'600100.XSHG', u'600104.XSHG', u'600109.XSHG', u'600111.XSHG', u'600115.XSHG', u'600118.XSHG', u'600153.XSHG', u'600157.XSHG', u'600170.XSHG', u'600176.XSHG', u'600177.XSHG', u'600188.XSHG', u'600196.XSHG', u'600208.XSHG', u'600219.XSHG', u'600221.XSHG', u'600233.XSHG', u'600271.XSHG', u'600276.XSHG', u'600297.XSHG', u'600309.XSHG', u'600332.XSHG', u'600339.XSHG', u'600340.XSHG', u'600346.XSHG', u'600352.XSHG', u'600362.XSHG', u'600369.XSHG', u'600372.XSHG', u'600373.XSHG', u'600376.XSHG', u'600383.XSHG', u'600390.XSHG', u'600398.XSHG', u'600406.XSHG', u'600415.XSHG', u'600436.XSHG', u'600438.XSHG', u'600482.XSHG', u'600487.XSHG', u'600489.XSHG', u'600498.XSHG', u'600516.XSHG', u'600518.XSHG', u'600519.XSHG', u'600522.XSHG', u'600535.XSHG', u'600547.XSHG', u'600549.XSHG', u'600570.XSHG', u'600583.XSHG', u'600585.XSHG', u'600588.XSHG', u'600606.XSHG', u'600637.XSHG', u'600660.XSHG', u'600663.XSHG', u'600674.XSHG', u'600682.XSHG', u'600688.XSHG', u'600690.XSHG', u'600703.XSHG', u'600704.XSHG', u'600705.XSHG', u'600739.XSHG', u'600741.XSHG', u'600795.XSHG', u'600804.XSHG', u'600809.XSHG', u'600816.XSHG', u'600820.XSHG', u'600837.XSHG', u'600867.XSHG', u'600886.XSHG', u'600887.XSHG', u'600893.XSHG', u'600900.XSHG', u'600909.XSHG', u'600919.XSHG', u'600926.XSHG', u'600958.XSHG', u'600959.XSHG', u'600977.XSHG', u'600999.XSHG', u'601006.XSHG', u'601009.XSHG', u'601012.XSHG', u'601018.XSHG', u'601021.XSHG', u'601088.XSHG', u'601099.XSHG', u'601108.XSHG', u'601111.XSHG', u'601117.XSHG', u'601155.XSHG', u'601166.XSHG', u'601169.XSHG', u'601186.XSHG', u'601198.XSHG', u'601211.XSHG', u'601212.XSHG', u'601216.XSHG', u'601225.XSHG', u'601228.XSHG', u'601229.XSHG', u'601238.XSHG', u'601288.XSHG', u'601318.XSHG', u'601328.XSHG', u'601333.XSHG', u'601336.XSHG', u'601360.XSHG', u'601377.XSHG', u'601390.XSHG', u'601398.XSHG', u'601555.XSHG', u'601600.XSHG', u'601601.XSHG', u'601607.XSHG', u'601611.XSHG', u'601618.XSHG', u'601628.XSHG', u'601633.XSHG', u'601668.XSHG', u'601669.XSHG', u'601688.XSHG', u'601718.XSHG', u'601727.XSHG', u'601766.XSHG', u'601788.XSHG', u'601800.XSHG', u'601808.XSHG', u'601818.XSHG', u'601828.XSHG', u'601838.XSHG', u'601857.XSHG', u'601866.XSHG', u'601877.XSHG', u'601878.XSHG', u'601881.XSHG', u'601888.XSHG', u'601898.XSHG', u'601899.XSHG', u'601901.XSHG', u'601919.XSHG', u'601933.XSHG', u'601939.XSHG', u'601958.XSHG', u'601985.XSHG', u'601988.XSHG', u'601989.XSHG', u'601991.XSHG', u'601992.XSHG', u'601997.XSHG', u'601998.XSHG', u'603160.XSHG', u'603260.XSHG', u'603288.XSHG', u'603799.XSHG', u'603833.XSHG', u'603858.XSHG', u'603993.XSHG']
获取行业成股

3、api相关说明文档

三、log.info

1、代码

def initialize(context):
    set_benchmark('000300.XSHG')
    g.security = get_index_stocks('000300.XSHG')
    print(g.security)
    set_option('use_real_price',True)
    set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003,close_today_commission=0, min_commission=5),type='stock')
    log.info('Hello word')

2、输出

2018-10-12 00:00:00 - INFO  - [u'000001.XSHE', u'000002.XSHE', u'000060.XSHE', u'000063.XSHE', u'000069.XSHE', u'000100.XSHE', u'000157.XSHE', u'000166.XSHE', u'000333.XSHE', u'000338.XSHE', u'000402.XSHE', u'000413.XSHE', u'000415.XSHE', u'000423.XSHE', u'000425.XSHE', u'000503.XSHE', u'000538.XSHE', u'000540.XSHE', u'000559.XSHE', u'000568.XSHE', u'000623.XSHE', u'000625.XSHE', u'000627.XSHE', u'000630.XSHE', u'000651.XSHE', u'000671.XSHE', u'000709.XSHE', u'000723.XSHE', u'000725.XSHE', u'000728.XSHE', u'000768.XSHE', u'000776.XSHE', u'000783.XSHE', u'000786.XSHE', u'000792.XSHE', u'000826.XSHE', u'000839.XSHE', u'000858.XSHE', u'000876.XSHE', u'000895.XSHE', u'000898.XSHE', u'000938.XSHE', u'000959.XSHE', u'000961.XSHE', u'000963.XSHE', u'000983.XSHE', u'001965.XSHE', u'001979.XSHE', u'002007.XSHE', u'002008.XSHE', u'002024.XSHE', u'002027.XSHE', u'002044.XSHE', u'002050.XSHE', u'002065.XSHE', u'002074.XSHE', u'002081.XSHE', u'002085.XSHE', u'002142.XSHE', u'002146.XSHE', u'002153.XSHE', u'002202.XSHE', u'002230.XSHE', u'002236.XSHE', u'002241.XSHE', u'002252.XSHE', u'002294.XSHE', u'002304.XSHE', u'002310.XSHE', u'002352.XSHE', u'002385.XSHE', u'002411.XSHE', u'002415.XSHE', u'002450.XSHE', u'002456.XSHE', u'002460.XSHE', u'002466.XSHE', u'002468.XSHE', u'002470.XSHE', u'002475.XSHE', u'002493.XSHE', u'002500.XSHE', u'002508.XSHE', u'002555.XSHE', u'002558.XSHE', u'002572.XSHE', u'002594.XSHE', u'002601.XSHE', u'002602.XSHE', u'002608.XSHE', u'002624.XSHE', u'002625.XSHE', u'002673.XSHE', u'002714.XSHE', u'002736.XSHE', u'002739.XSHE', u'002797.XSHE', u'002925.XSHE', u'300003.XSHE', u'300015.XSHE', u'300017.XSHE', u'300024.XSHE', u'300027.XSHE', u'300033.XSHE', u'300059.XSHE', u'300070.XSHE', u'300072.XSHE', u'300122.XSHE', u'300124.XSHE', u'300136.XSHE', u'300144.XSHE', u'300251.XSHE', u'300408.XSHE', u'300433.XSHE', u'600000.XSHG', u'600008.XSHG', u'600009.XSHG', u'600010.XSHG', u'600011.XSHG', u'600015.XSHG', u'600016.XSHG', u'600018.XSHG', u'600019.XSHG', u'600023.XSHG', u'600025.XSHG', u'600028.XSHG', u'600029.XSHG', u'600030.XSHG', u'600031.XSHG', u'600036.XSHG', u'600038.XSHG', u'600048.XSHG', u'600050.XSHG', u'600061.XSHG', u'600066.XSHG', u'600068.XSHG', u'600085.XSHG', u'600089.XSHG', u'600100.XSHG', u'600104.XSHG', u'600109.XSHG', u'600111.XSHG', u'600115.XSHG', u'600118.XSHG', u'600153.XSHG', u'600157.XSHG', u'600170.XSHG', u'600176.XSHG', u'600177.XSHG', u'600188.XSHG', u'600196.XSHG', u'600208.XSHG', u'600219.XSHG', u'600221.XSHG', u'600233.XSHG', u'600271.XSHG', u'600276.XSHG', u'600297.XSHG', u'600309.XSHG', u'600332.XSHG', u'600339.XSHG', u'600340.XSHG', u'600346.XSHG', u'600352.XSHG', u'600362.XSHG', u'600369.XSHG', u'600372.XSHG', u'600373.XSHG', u'600376.XSHG', u'600383.XSHG', u'600390.XSHG', u'600398.XSHG', u'600406.XSHG', u'600415.XSHG', u'600436.XSHG', u'600438.XSHG', u'600482.XSHG', u'600487.XSHG', u'600489.XSHG', u'600498.XSHG', u'600516.XSHG', u'600518.XSHG', u'600519.XSHG', u'600522.XSHG', u'600535.XSHG', u'600547.XSHG', u'600549.XSHG', u'600570.XSHG', u'600583.XSHG', u'600585.XSHG', u'600588.XSHG', u'600606.XSHG', u'600637.XSHG', u'600660.XSHG', u'600663.XSHG', u'600674.XSHG', u'600682.XSHG', u'600688.XSHG', u'600690.XSHG', u'600703.XSHG', u'600704.XSHG', u'600705.XSHG', u'600739.XSHG', u'600741.XSHG', u'600795.XSHG', u'600804.XSHG', u'600809.XSHG', u'600816.XSHG', u'600820.XSHG', u'600837.XSHG', u'600867.XSHG', u'600886.XSHG', u'600887.XSHG', u'600893.XSHG', u'600900.XSHG', u'600909.XSHG', u'600919.XSHG', u'600926.XSHG', u'600958.XSHG', u'600959.XSHG', u'600977.XSHG', u'600999.XSHG', u'601006.XSHG', u'601009.XSHG', u'601012.XSHG', u'601018.XSHG', u'601021.XSHG', u'601088.XSHG', u'601099.XSHG', u'601108.XSHG', u'601111.XSHG', u'601117.XSHG', u'601155.XSHG', u'601166.XSHG', u'601169.XSHG', u'601186.XSHG', u'601198.XSHG', u'601211.XSHG', u'601212.XSHG', u'601216.XSHG', u'601225.XSHG', u'601228.XSHG', u'601229.XSHG', u'601238.XSHG', u'601288.XSHG', u'601318.XSHG', u'601328.XSHG', u'601333.XSHG', u'601336.XSHG', u'601360.XSHG', u'601377.XSHG', u'601390.XSHG', u'601398.XSHG', u'601555.XSHG', u'601600.XSHG', u'601601.XSHG', u'601607.XSHG', u'601611.XSHG', u'601618.XSHG', u'601628.XSHG', u'601633.XSHG', u'601668.XSHG', u'601669.XSHG', u'601688.XSHG', u'601718.XSHG', u'601727.XSHG', u'601766.XSHG', u'601788.XSHG', u'601800.XSHG', u'601808.XSHG', u'601818.XSHG', u'601828.XSHG', u'601838.XSHG', u'601857.XSHG', u'601866.XSHG', u'601877.XSHG', u'601878.XSHG', u'601881.XSHG', u'601888.XSHG', u'601898.XSHG', u'601899.XSHG', u'601901.XSHG', u'601919.XSHG', u'601933.XSHG', u'601939.XSHG', u'601958.XSHG', u'601985.XSHG', u'601988.XSHG', u'601989.XSHG', u'601991.XSHG', u'601992.XSHG', u'601997.XSHG', u'601998.XSHG', u'603160.XSHG', u'603260.XSHG', u'603288.XSHG', u'603799.XSHG', u'603833.XSHG', u'603858.XSHG', u'603993.XSHG']

2018-10-12 00:00:00 - INFO  - Hello word
注意看最后一行

3、api相关说明文档

四、handle_data

1、代码

def initialize(context):
    set_benchmark('000300.XSHG')
    g.security = get_index_stocks('000300.XSHG')
    set_option('use_real_price',True)
    set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003,close_today_commission=0, min_commission=5),type='stock')
    
def handle_data(context, data):
    print("Hello word")

2、输出

3、api相关说明文档

context: Context对象, 存放有当前的账户/标的持仓信息 data: 一个字典(dict), key是股票代码, value是当时的SecurityUnitData 对象. 存放前一个单位时间(按天回测, 是前一天, 按分钟回测, 则是前一分钟) 的数据. 注意:

  • 为了加速, data 里面的数据是按需获取的, 每次 handle_data 被调用时, data 是空的 dict, 当你使用 data[security] 时该 security 的数据才会被获取.
  • data 只在这一个时间点有效, 请不要存起来到下一个 handle_data 再用
  • 注意, 要获取回测当天的开盘价/是否停牌/涨跌停价, 请使用 [get_current_data]

五、set_order_cost - 设置佣金/印花税

1、代码

# 导入函数库
import jqdata

#初始化函数,设定基准等等
def initialize(context):
    set_benchmark('000300.XSHG')
    g.security = get_index_stocks('000300.XSHG')
    set_option('use_real_price',True)
    set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003,close_today_commission=0, min_commission=5),type='stock')
    
def handle_data(context, data):
    print(context.portfolio.positions)

2、输出

因为还没有交易,所以是空

3、api相关说明文档

 

 

 

 

posted @ 2018-10-25 17:14  活的潇洒80  阅读(688)  评论(0编辑  收藏  举报