Skip to content

附:交并集投资组合回测结果

ChannelCMT edited this page Jun 3, 2019 · 1 revision
import pandas as pd

Union = pd.read_excel("Union.xlsx", index_col="trade_date")
Intersection = pd.read_excel("Intersection.xlsx", index_col="trade_date")
Union
000001.SZ 000002.SZ 000008.SZ 000009.SZ 000012.SZ 000024.SZ 000027.SZ 000039.SZ 000046.SZ 000060.SZ ... 601992.SH 601997.SH 601998.SH 603000.SH 603160.SH 603288.SH 603699.SH 603858.SH 603885.SH 603993.SH
trade_date
20140103 1 1 0 0 0 1 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20140106 0 1 0 0 0 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140107 0 1 0 0 1 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140108 0 1 0 0 1 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140109 0 1 0 0 0 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140110 0 1 0 0 0 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140113 0 1 0 0 1 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140114 0 1 0 0 0 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140115 0 1 0 0 1 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140116 0 1 0 0 0 1 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20140117 0 1 0 0 0 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140120 0 1 0 0 0 1 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140121 0 1 0 0 0 1 0 1 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140122 0 1 0 0 0 1 0 1 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140123 0 0 0 0 0 0 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140124 0 0 0 0 0 0 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140127 0 0 0 0 0 0 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140128 0 0 0 0 0 0 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140129 0 0 0 0 0 0 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140130 0 0 0 0 1 0 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140207 0 1 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20140210 0 1 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20140211 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
20140212 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
20140213 0 1 0 0 0 0 0 0 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140214 0 1 0 0 0 1 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
20140217 0 1 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
20140218 0 1 0 0 0 1 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
20140219 0 1 0 0 0 1 0 1 0 0 ... 1 0 0 0 0 0 0 0 0 0
20140220 0 1 0 0 0 1 0 1 0 0 ... 1 0 0 0 0 0 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
20170821 1 1 0 0 0 0 0 0 0 0 ... 0 1 1 0 0 0 0 0 0 0
20170822 1 1 0 0 0 0 0 0 0 0 ... 0 1 1 0 0 0 0 0 0 0
20170823 1 1 0 0 0 0 0 0 0 0 ... 0 1 1 0 0 0 0 0 0 0
20170824 1 1 0 0 0 0 0 0 0 0 ... 0 1 1 0 0 0 0 0 0 0
20170825 1 0 0 0 0 0 0 0 0 0 ... 0 1 1 0 0 0 0 0 0 0
20170828 1 1 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20170829 1 0 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20170830 0 0 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20170831 1 0 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20170901 1 0 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20170904 1 0 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20170905 0 1 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20170906 1 0 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 0 0 0 0 0
20170907 0 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170908 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170911 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170912 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170913 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170914 1 0 0 0 0 0 0 0 0 0 ... 1 1 1 0 0 0 0 0 0 0
20170915 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170918 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170919 0 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170920 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170921 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170922 1 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 0 0 0 0 0
20170925 1 0 0 0 0 0 0 0 0 0 ... 1 1 1 0 0 0 0 0 0 0
20170926 1 0 0 0 0 0 0 0 0 0 ... 1 1 1 0 0 0 0 0 0 0
20170927 1 0 0 0 0 0 0 0 0 0 ... 1 1 1 0 0 0 0 0 0 0
20170928 1 0 0 0 0 0 0 0 0 0 ... 1 1 1 0 0 0 0 0 0 0
20170929 1 0 0 0 0 0 0 0 0 0 ... 1 1 1 0 0 0 0 0 0 0

916 rows × 456 columns

from datetime import datetime, timedelta
import numpy as np

def change_columns_index(signal):
    new_names = {}
    for c in signal.columns:
        if c.endswith('SZ'):
            new_names[c] = c.replace('SZ', 'XSHE')
        elif c.endswith('SH'):
            new_names[c] = c.replace('SH', 'XSHG')
    signal = signal.rename_axis(new_names, axis=1)
    signal.index = pd.Index(map(lambda x: datetime.strptime(str(x),"%Y%m%d") , signal.index))
    signal.index = pd.Index(map(lambda x: x+timedelta(hours=15) , signal.index))
    return signal

def get_daily_weight(data,period):
    data = data.fillna(0)
    weight_list = []
    for time_index, weight in data.iterrows():
        weight_result = (weight/weight.sum())
        weight_list.append(weight_result.to_dict())
    stock_df = pd.DataFrame(weight_list, index=data.index)
    if period>1:
        stock_df = pd.concat([pd.DataFrame(0,columns=stock_df.columns,index=list(range(1,period))),stock_df],axis=0)
    target = (pd.rolling_apply(stock_df, period, sum).dropna(how="all")/period).replace(0,np.nan)
    return target
Union_weight =  get_daily_weight(change_columns_index(Union),period=30)
Intersection_weight =  get_daily_weight(change_columns_index(Intersection),period=30)
Union_weight.head()
/home/xinger/anaconda3/envs/IIA/lib/python3.6/site-packages/ipykernel/__main__.py:25: FutureWarning: pd.rolling_apply is deprecated for DataFrame and will be removed in a future version, replace with 
	DataFrame.rolling(window=30,center=False).apply(func=<builtin_function_or_method>,args=<tuple>,kwargs=<dict>)
000001.XSHE 000002.XSHE 000008.XSHE 000009.XSHE 000012.XSHE 000024.XSHE 000027.XSHE 000039.XSHE 000046.XSHE 000060.XSHE ... 601992.XSHG 601997.XSHG 601998.XSHG 603000.XSHG 603160.XSHG 603288.XSHG 603699.XSHG 603858.XSHG 603885.XSHG 603993.XSHG
2014-01-03 15:00:00 0.000412 0.000412 NaN NaN NaN 0.000412 NaN NaN NaN NaN ... 0.000412 NaN 0.000412 NaN NaN NaN NaN NaN NaN NaN
2014-01-06 15:00:00 0.000412 0.000823 NaN NaN NaN 0.000823 NaN NaN NaN NaN ... 0.000823 NaN 0.000412 NaN NaN NaN NaN NaN NaN NaN
2014-01-07 15:00:00 0.000412 0.001235 NaN NaN 0.000412 0.001235 NaN NaN NaN NaN ... 0.001235 NaN 0.000412 NaN NaN NaN NaN NaN NaN NaN
2014-01-08 15:00:00 0.000412 0.001622 NaN NaN 0.000799 0.001622 NaN NaN NaN NaN ... 0.001622 NaN 0.000412 NaN NaN NaN NaN NaN NaN NaN
2014-01-09 15:00:00 0.000412 0.002024 NaN NaN 0.000799 0.002024 NaN NaN NaN NaN ... 0.002024 NaN 0.000412 NaN NaN NaN NaN NaN NaN NaN

5 rows × 456 columns

并集组合(不对冲)

import rqalpha
from rqalpha.api import *

#读取文件位置
def init(context):
    context.codes = Union_weight
    context.stocks = None
    context.pos_weight = 0.9
    scheduler.run_daily(find_pool)
    

def find_pool(context, bar_dict):
    try:
        codes = context.codes.loc[context.now].dropna()
        context.stocks = codes
    except:
        context.stocks = None

def handle_bar(context, bar_dict):
    buy(context, bar_dict)


def buy(context, bar_dict):
    pool = context.stocks
    if pool is not None:
        for ins, percentage in pool.items():
            order_target_percent(ins, percentage*context.pos_weight)
        for ins in context.portfolio.positions:
            if ins not in context.stocks:
                order_target_percent(ins, 0)

config = {
  "base": {
    "start_date": "2014-01-03",
    "end_date": "2018-01-31",
    "accounts": {'stock':10000000},
    "benchmark": "000300.XSHG"
  },
  "extra": {
    "log_level": "error",
  },
    
  "mod": {
    "sys_analyser": {
      #"enabled": True,
      "plot": True
    }
  }
}

rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)
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!

{'sys_analyser': {'benchmark_portfolio':              cash  market_value  static_unit_net_value  total_value  \
  date                                                                  
  2014-01-03  745.3    9999254.70                  1.000  10000000.00   
  2014-01-06  745.3    9771663.60                  1.000   9772408.90   
  2014-01-07  745.3    9768870.00                  0.977   9769615.30   
  2014-01-08  745.3    9785937.15                  0.977   9786682.45   
  2014-01-09  745.3    9699990.30                  0.979   9700735.60   
  2014-01-10  745.3    9624170.25                  0.970   9624915.55   
  2014-01-13  745.3    9575413.20                  0.962   9576158.50   
  2014-01-14  745.3    9659090.25                  0.958   9659835.55   
  2014-01-15  745.3    9642023.10                  0.966   9642768.40     
  ...           ...           ...                    ...          ...   
  2018-01-17  745.3   18543043.80                  1.859  18543789.10   
  2018-01-18  745.3   18644748.30                  1.854  18645493.60   
  2018-01-19  745.3   18705771.00                  1.865  18706516.30   
  2018-01-22  745.3   18929259.00                  1.871  18930004.30   
  
              unit_net_value       units  
  date                                    
  2014-01-03        1.000000  10000000.0  
  2014-01-06        0.977241  10000000.0  
  2014-01-07        0.976962  10000000.0  
  2014-01-08        0.978668  10000000.0  
  2014-01-09        0.970074  10000000.0  
  2014-01-10        0.962492  10000000.0  
  2014-01-13        0.957616  10000000.0  
  2014-01-14        0.965984  10000000.0  
  2014-01-15        0.964277  10000000.0  
  2014-01-16        0.965543  10000000.0  
  ...                    ...         ...   
  2018-01-16        1.858897  10000000.0  
  2018-01-17        1.854379  10000000.0  
  2018-01-18        1.864549  10000000.0  
  2018-01-19        1.870652  10000000.0  
  2018-01-22        1.893000  10000000.0  
  
  [991 rows x 6 columns],
  'portfolio':                    cash  market_value  static_unit_net_value   total_value  \
  date                                                                         
  2014-01-03  9736148.000  2.634470e+05                  1.000  9.999595e+06   
  2014-01-06  9429722.000  5.606790e+05                  1.000  9.990401e+06   
  2014-01-07  9125957.000  8.627950e+05                  0.999  9.988752e+06   
  2014-01-08  8829838.000  1.151396e+06                  0.999  9.981234e+06   
  2014-01-09  8517260.115  1.455680e+06                  0.998  9.972940e+06   
  ...                 ...           ...                    ...           ...   
  2018-01-15  2080687.653  2.057292e+07                  2.266  2.265361e+07   
  2018-01-16  2080687.653  2.087998e+07                  2.265  2.296067e+07   
  2018-01-17  2080687.653  2.087841e+07                  2.296  2.295910e+07   
  2018-01-18  2080687.653  2.102167e+07                  2.296  2.310235e+07   
  2018-01-19  2080687.653  2.104398e+07                  2.310  2.312467e+07   
  2018-01-22  2080687.653  2.114470e+07                  2.312  2.322539e+07   
  
              unit_net_value       units  
  date                                    
  2014-01-03        0.999960  10000000.0  
  2014-01-06        0.999040  10000000.0  
  2014-01-07        0.998875  10000000.0  
  2014-01-08        0.998123  10000000.0   
  ...                    ...         ...  
  2018-01-11        2.259800  10000000.0  
  2018-01-12        2.265512  10000000.0  
  2018-01-15        2.265361  10000000.0  
  2018-01-16        2.296067  10000000.0  
  2018-01-17        2.295910  10000000.0  
  2018-01-18        2.310235  10000000.0  
  2018-01-19        2.312467  10000000.0  
  2018-01-22        2.322539  10000000.0  
  
  [991 rows x 6 columns],
  'stock_account':                    cash  dividend_receivable  market_value   total_value  \
  date                                                                       
  2014-01-03  9736148.000                  0.0  2.634470e+05  9.999595e+06   
  2014-01-06  9429722.000                  0.0  5.606790e+05  9.990401e+06   
  2014-01-07  9125957.000                  0.0  8.627950e+05  9.988752e+06   
  2014-01-08  8829838.000                  0.0  1.151396e+06  9.981234e+06   
  2014-01-09  8517260.115                  0.0  1.455680e+06  9.972940e+06   
  2014-01-10  8220735.115                  0.0  1.743951e+06  9.964686e+06   
  2014-01-13  7923490.365                  0.0  2.039548e+06  9.963038e+06   
  2014-01-14  7641301.300                  0.0  2.343817e+06  9.985118e+06   
  2014-01-15  7330628.300                  0.0  2.639467e+06  9.970095e+06     
  ...                 ...                  ...           ...           ...   
  2018-01-17  2080687.653                  0.0  2.087841e+07  2.295910e+07   
  2018-01-18  2080687.653                  0.0  2.102167e+07  2.310235e+07   
  2018-01-19  2080687.653                  0.0  2.104398e+07  2.312467e+07   
  2018-01-22  2080687.653                  0.0  2.114470e+07  2.322539e+07   
  
              transaction_cost  
  date                          
  2014-01-03           405.000  
  2014-01-06           415.000  
  2014-01-07           410.000  
  2014-01-08           425.000  
  2014-01-09           440.885  
  2014-01-10           455.000  
  2014-01-13           441.750  
  ...                      ...  
  2018-01-12             0.000  
  2018-01-15             0.000  
  2018-01-16             0.000  
  2018-01-17             0.000  
  2018-01-18             0.000  
  2018-01-19             0.000  
  2018-01-22             0.000  
  
  [991 rows x 5 columns],
  'stock_positions':             avg_price  last_price  market_value order_book_id  quantity symbol
  date                                                                          
  2014-01-03     11.930       11.93        3579.0   000001.XSHE     300.0   平安银行
  2014-01-03      7.840        7.84        3136.0   000002.XSHE     400.0    万科A
  2014-01-03     20.190       20.19        2019.0   000024.XSHE     100.0   招商地产
  2014-01-03     14.070       14.07        2814.0   000063.XSHE     200.0   中兴通讯
  2014-01-03      5.150        5.15        3605.0   000069.XSHE     700.0   华侨城A
  2014-01-03      5.330        5.33        3198.0   000157.XSHE     600.0   中联重科
  2014-01-03     18.330       18.33        3666.0   000338.XSHE     200.0   潍柴动力
  2014-01-03      7.500        7.50        3000.0   000425.XSHE     400.0   徐工机械
  2014-01-03      9.740        9.74        2922.0   000630.XSHE     300.0   铜陵有色
  2014-01-03     30.990       30.99        3099.0   000651.XSHE     100.0   格力电器
  2014-01-03      7.920        7.92        3168.0   000656.XSHE     400.0   金科股份
  2014-01-03     12.100       12.10        3630.0   000776.XSHE     300.0   广发证券
  2014-01-03      4.680        4.68        3276.0   000933.XSHE     700.0   神火股份
  2014-01-03      7.150        7.15        3575.0   000937.XSHE     500.0   冀中能源
  2014-01-03     10.500       10.50        3150.0   000960.XSHE     300.0   锡业股份
  2014-01-03      6.920        6.92        3460.0   000961.XSHE     500.0   中南建设
  2014-01-03      6.820        6.82        3410.0   000983.XSHE     500.0   西山煤电
  2014-01-03     19.060       19.06        1906.0   002001.XSHE     100.0    新和成
  2014-01-03      9.640        9.64        2892.0   002299.XSHE     300.0   圣农发展
  2014-01-03     24.600       24.60        2460.0   002375.XSHE     100.0   亚厦股份
  2014-01-03      9.140        9.14        3656.0   600000.XSHG     400.0   浦发银行
  2014-01-03      4.980        4.98        3486.0   600011.XSHG     700.0   华能国际
  2014-01-03      7.610        7.61        3044.0   600016.XSHG     400.0   民生银行
  2014-01-03      4.010        4.01        3609.0   600019.XSHG     900.0   宝钢股份
  2014-01-03      2.990        2.99        3588.0   600027.XSHG    1200.0   华电国际
  2014-01-03      4.380        4.38        3504.0   600028.XSHG     800.0   中国石化
  2014-01-03      2.700        2.70        3510.0   600029.XSHG    1300.0   南方航空
  2014-01-03      8.110        8.11        3244.0   600048.XSHG     400.0   保利地产
  2014-01-03     17.650       17.65        3530.0   600066.XSHG     200.0   宇通客车
  2014-01-03      3.940        3.94        3546.0   600068.XSHG     900.0    葛洲坝
  ...               ...         ...           ...           ...       ...    ...
  2018-01-22     18.690       19.79       57391.0   600362.XSHG    2900.0   江西铜业
  2018-01-22      7.375        6.88      129344.0   601919.XSHG   18800.0   中远海控
  2018-01-22     58.643       60.74      218664.0   601336.XSHG    3600.0   新华保险
  2018-01-22      7.653        6.64       14608.0   601727.XSHG    2200.0   上海电气
  2018-01-22     21.661       20.59       78242.0   601688.XSHG    3800.0   华泰证券
  2018-01-22      6.402        6.65      202825.0   600688.XSHG   30500.0   上海石化
  2018-01-22     18.511       18.22       34618.0   600739.XSHG    1900.0   辽宁成大
  2018-01-22      5.875        5.06       23782.0   600369.XSHG    4700.0   西南证券
  2018-01-22     13.940       13.40        8040.0   002736.XSHE     600.0   国信证券
  2018-01-22     17.568       21.20       19080.0   600030.XSHG     900.0   中信证券
  2018-01-22     15.532       14.59       23344.0   601788.XSHG    1600.0   光大证券
  2018-01-22      6.455        5.98       25714.0   601118.XSHG    4300.0   海南橡胶
  2018-01-22      3.720        3.36       25536.0   600157.XSHG    7600.0   永泰能源
  2018-01-22      5.982        5.38       48420.0   601018.XSHG    9000.0    宁波港
  2018-01-22      3.413        3.21      149265.0   600795.XSHG   46500.0   国电电力
  2018-01-22      7.766        8.09       33978.0   601600.XSHG    4200.0   中国铝业
  2018-01-22      7.296        6.17       15425.0   000959.XSHE    2500.0   首钢股份
  2018-01-22     19.104       18.89       24557.0   000776.XSHE    1300.0   广发证券
  2018-01-22     11.978       13.84       53976.0   002024.XSHE    3900.0   苏宁云商
  2018-01-22      9.861       12.06       20502.0   601766.XSHG    1700.0   中国中车
  2018-01-22     10.324       12.89       41248.0   600352.XSHG    3200.0   浙江龙盛
  2018-01-22      8.500        9.35        8415.0   601225.XSHG     900.0   陕西煤业
  2018-01-22     27.897       30.94       64974.0   601628.XSHG    2100.0   中国人寿
  2018-01-22     15.446       14.63       16093.0   002299.XSHE    1100.0   圣农发展
  2018-01-22      7.346        6.85       71925.0   600050.XSHG   10500.0   中国联通
  2018-01-22      8.600        6.42        6420.0   601718.XSHG    1000.0   际华集团
  2018-01-22     12.398       10.19       49931.0   600100.XSHG    4900.0   同方股份
  2018-01-22     22.260       23.31        6993.0   000623.XSHE     300.0   吉林敖东
  2018-01-22     14.789       13.97       32131.0   600837.XSHG    2300.0   海通证券
  2018-01-22     31.334       32.19       16095.0   600547.XSHG     500.0   山东黄金
  
  [159399 rows x 6 columns],
  'summary': {'STOCK': 10000000.0,
   'alpha': 0.060999999999999999,
   'annualized_returns': 0.23100000000000001,
   'benchmark': '000300.XSHG',
   'benchmark_annualized_returns': 0.17000000000000001,
   'benchmark_total_returns': 0.89300000000000002,
   'beta': 0.94699999999999995,
   'cash': 2080687.6529999999,
   'downside_risk': 0.035999999999999997,
   'end_date': '2018-01-22',
   'information_ratio': 0.95199999999999996,
   'max_drawdown': 0.40799999999999997,
   'run_type': 'BACKTEST',
   'sharpe': 0.81999999999999995,
   'sortino': 5.4530000000000003,
   'start_date': '2014-01-03',
   'strategy_file': 'strategy.py',
   'strategy_name': 'strategy',
   'total_returns': 1.323,
   'total_value': 23225390.339000002,
   'tracking_error': 0.056000000000000001,
   'unit_net_value': 2.323,
   'units': 10000000.0,
   'volatility': 0.23899999999999999},
  'trades':                      commission     exec_id  last_price  last_quantity  \
  datetime                                                                 
  2014-01-03 15:00:00      5.0000  1516788432       11.93          300.0   
  2014-01-03 15:00:00      5.0000  1516788433        7.84          400.0   
  2014-01-03 15:00:00      5.0000  1516788434       20.19          100.0   
  2014-01-03 15:00:00      5.0000  1516788435       14.07          200.0   
  2014-01-03 15:00:00      5.0000  1516788436        5.15          700.0   
  2014-01-03 15:00:00      5.0000  1516788437        5.33          600.0   
  2014-01-03 15:00:00      5.0000  1516788438       18.33          200.0   
  2014-01-03 15:00:00      5.0000  1516788439        7.50          400.0   
  2014-01-03 15:00:00      5.0000  1516788440        9.74          300.0   
  2014-01-03 15:00:00      5.0000  1516788441       30.99          100.0   
  2014-01-03 15:00:00      5.0000  1516788442        7.92          400.0   
  2014-01-03 15:00:00      5.0000  1516788443       12.10          300.0   
  2014-01-03 15:00:00      5.0000  1516788444        4.68          700.0   
  2014-01-03 15:00:00      5.0000  1516788445        7.15          500.0   
  ...                         ...         ...         ...            ...   
  2017-09-29 15:00:00      6.4160  1516879312        8.02         1000.0   
  2017-09-29 15:00:00      5.0000  1516879313        4.05          200.0   
  2017-09-29 15:00:00      7.0312  1516879314        7.99         1100.0   
  2017-09-29 15:00:00      6.6144  1516879315        6.89         1200.0   
  2017-09-29 15:00:00      5.0000  1516879316        7.99          200.0   
  2017-09-29 15:00:00      7.2512  1516879317        4.12         2200.0   
  2017-09-29 15:00:00      5.0000  1516879318        6.35          700.0   
  
                      order_book_id    order_id position_effect  side symbol  \
  datetime                                                                     
  2014-01-03 15:00:00   000001.XSHE  1516788430            None   BUY   平安银行   
  2014-01-03 15:00:00   000002.XSHE  1516788431            None   BUY    万科A   
  2014-01-03 15:00:00   000024.XSHE  1516788432            None   BUY   招商地产   
  2014-01-03 15:00:00   000063.XSHE  1516788433            None   BUY   中兴通讯   
  2014-01-03 15:00:00   000069.XSHE  1516788434            None   BUY   华侨城A   
  2014-01-03 15:00:00   000157.XSHE  1516788435            None   BUY   中联重科   
  2014-01-03 15:00:00   000338.XSHE  1516788436            None   BUY   潍柴动力   
  2014-01-03 15:00:00   000425.XSHE  1516788437            None   BUY   徐工机械   
  2014-01-03 15:00:00   000630.XSHE  1516788438            None   BUY   铜陵有色   
  2014-01-03 15:00:00   000651.XSHE  1516788439            None   BUY   格力电器   
  2014-01-03 15:00:00   000656.XSHE  1516788440            None   BUY   金科股份   
  ...                           ...         ...             ...   ...    ...   
  2017-09-29 15:00:00   601857.XSHG  1516885076            None  SELL   中国石油   
  2017-09-29 15:00:00   601919.XSHG  1516885077            None   BUY   中远海控   
  2017-09-29 15:00:00   601933.XSHG  1516885078            None  SELL   永辉超市   
  2017-09-29 15:00:00   601988.XSHG  1516885079            None   BUY   中国银行   
  2017-09-29 15:00:00   601992.XSHG  1516885080            None  SELL   金隅集团   
  
                          tax     trading_datetime  transaction_cost  
  datetime                                                            
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00            5.0000  
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00            5.0000  
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00            5.0000  
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00            5.0000  
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00            5.0000  
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00            5.0000  
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00            5.0000  
  ...                     ...                  ...               ...  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00            7.2512  
  2017-09-29 15:00:00   4.445  2017-09-29 15:00:00            9.4450  
  
  [90887 rows x 12 columns]}}

交集组合(不对冲)

import rqalpha
from rqalpha.api import *

#读取文件位置
def init(context):
    context.codes = Intersection_weight
    context.stocks = None
    context.pos_weight = 0.9
    scheduler.run_daily(find_pool)
    

def find_pool(context, bar_dict):
    try:
        codes = context.codes.loc[context.now].dropna()
        context.stocks = codes
    except:
        context.stocks = None

def handle_bar(context, bar_dict):
    buy(context, bar_dict)


def buy(context, bar_dict):
    pool = context.stocks
    if pool is not None:
        for ins, percentage in pool.items():
            order_target_percent(ins, percentage*context.pos_weight)
        for ins in context.portfolio.positions:
            if ins not in context.stocks:
                order_target_percent(ins, 0)

config = {
  "base": {
    "start_date": "2014-01-03",
    "end_date": "2018-01-31",
    "accounts": {'stock':10000000},
    "benchmark": "000300.XSHG"
  },
  "extra": {
    "log_level": "error",
  },
    
  "mod": {
    "sys_analyser": {
      #"enabled": True,
      "plot": True
    },
  }
}

rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)

{'sys_analyser': {'benchmark_portfolio':              cash  market_value  static_unit_net_value  total_value  \
  date                                                                  
  2014-01-03  745.3    9999254.70                  1.000  10000000.00   
  2014-01-06  745.3    9771663.60                  1.000   9772408.90   
  2014-01-07  745.3    9768870.00                  0.977   9769615.30   
  2014-01-08  745.3    9785937.15                  0.977   9786682.45   
  2014-01-09  745.3    9699990.30                  0.979   9700735.60   
  2014-01-10  745.3    9624170.25                  0.970   9624915.55   
  2014-01-13  745.3    9575413.20                  0.962   9576158.50   
  2014-01-14  745.3    9659090.25                  0.958   9659835.55   
  2014-01-15  745.3    9642023.10                  0.966   9642768.40   
  2014-01-16  745.3    9654681.60                  0.964   9655426.90   
  ...           ...           ...                    ...          ...   
  2018-01-09  745.3   18286294.50                  1.816  18287039.80   
  2018-01-10  745.3   18367090.65                  1.829  18367835.95   
  2018-01-11  745.3   18357400.35                  1.837  18358145.65   
  2018-01-12  745.3   18442125.00                  1.836  18442870.30   
  2018-01-15  745.3   18443172.60                  1.844  18443917.90   
  2018-01-16  745.3   18588221.55                  1.844  18588966.85   
  2018-01-17  745.3   18543043.80                  1.859  18543789.10   
  2018-01-18  745.3   18644748.30                  1.854  18645493.60   
  2018-01-19  745.3   18705771.00                  1.865  18706516.30   
  2018-01-22  745.3   18929259.00                  1.871  18930004.30   
  
              unit_net_value       units  
  date                                    
  2014-01-03        1.000000  10000000.0  
  2014-01-06        0.977241  10000000.0  
  2014-01-07        0.976962  10000000.0  
  2014-01-08        0.978668  10000000.0  
  2014-01-09        0.970074  10000000.0  
  2014-01-10        0.962492  10000000.0  
  2014-01-13        0.957616  10000000.0  
  2014-01-14        0.965984  10000000.0  
  2014-01-15        0.964277  10000000.0  
  ...                    ...         ...  
  2018-01-12        1.844287  10000000.0  
  2018-01-15        1.844392  10000000.0  
  2018-01-16        1.858897  10000000.0  
  2018-01-17        1.854379  10000000.0  
  2018-01-18        1.864549  10000000.0  
  2018-01-19        1.870652  10000000.0  
  2018-01-22        1.893000  10000000.0  
  
  [991 rows x 6 columns],
  'portfolio':                    cash  market_value  static_unit_net_value   total_value  \
  date                                                                         
  2014-01-03  9706814.639      292951.0                  1.000  9.999766e+06   
  2014-01-06  9400250.633      588600.0                  1.000  9.988851e+06   
  2014-01-07  9097252.082      888252.0                  0.999  9.985504e+06   
  2014-01-08  8790795.296     1184237.0                  0.999  9.975032e+06   
  2014-01-09  8489710.813     1481302.0                  0.998  9.971013e+06   
  2014-01-10  8182080.859     1779354.0                  0.997  9.961435e+06   
  2014-01-13  7880399.282     2080951.0                  0.996  9.961350e+06   
  2014-01-14  7599941.843     2387894.0                  0.996  9.987836e+06   
  ...                 ...           ...                    ...           ...   
  2018-01-18  2606790.021    25697584.0                  2.799  2.830437e+07   
  2018-01-19  2606790.021    25778644.6                  2.830  2.838543e+07   
  2018-01-22  2606790.021    25703953.4                  2.839  2.831074e+07   
  
              unit_net_value       units  
  date                                    
  2014-01-03        0.999977  10000000.0  
  2014-01-06        0.998885  10000000.0  
  2014-01-07        0.998550  10000000.0  
  2014-01-08        0.997503  10000000.0  
  2014-01-09        0.997101  10000000.0  
  2014-01-10        0.996143  10000000.0  
  2014-01-13        0.996135  10000000.0  
  ...                    ...         ...  
  2018-01-19        2.838543  10000000.0  
  2018-01-22        2.831074  10000000.0  
  
  [991 rows x 6 columns],
  'stock_account':                    cash  dividend_receivable  market_value   total_value  \
  date                                                                       
  2014-01-03  9706814.639                  0.0      292951.0  9.999766e+06   
  2014-01-06  9400250.633                  0.0      588600.0  9.988851e+06   
  2014-01-07  9097252.082                  0.0      888252.0  9.985504e+06   
  2014-01-08  8790795.296                  0.0     1184237.0  9.975032e+06   
  ...                 ...                  ...           ...           ...   
  2018-01-18  2606790.021                  0.0    25697584.0  2.830437e+07   
  2018-01-19  2606790.021                  0.0    25778644.6  2.838543e+07   
  2018-01-22  2606790.021                  0.0    25703953.4  2.831074e+07   
  
              transaction_cost  
  date                          
  2014-01-03           234.361  
  2014-01-06           254.006  
  ...                      ...   
  2018-01-15             0.000  
  2018-01-16             0.000  
  2018-01-17             0.000  
  2018-01-18             0.000  
  2018-01-19             0.000  
  2018-01-22             0.000  
  
  [991 rows x 5 columns],
  'stock_positions':             avg_price  last_price  market_value order_book_id  quantity symbol
  date                                                                          
  2014-01-03     14.070       14.07       12663.0   000063.XSHE     900.0   中兴通讯
  2014-01-03      6.920        6.92       12456.0   000961.XSHE    1800.0   中南建设
  2014-01-03      6.820        6.82       12958.0   000983.XSHE    1900.0   西山煤电
  2014-01-03      4.980        4.98       12948.0   600011.XSHG    2600.0   华能国际
  ...               ...         ...           ...           ...       ...    ...
  2018-01-22     25.885       33.08      258024.0   600036.XSHG    7800.0   招商银行
  2018-01-22      8.089        7.68      205824.0   601669.XSHG   26800.0   中国电建
  2018-01-22      7.894        9.29      140279.0   601009.XSHG   15100.0   南京银行
  2018-01-22      3.320        3.21       68694.0   600795.XSHG   21400.0   国电电力
  
  [63607 rows x 6 columns],
  'summary': {'STOCK': 10000000.0,
   'alpha': 0.112,
   'annualized_returns': 0.29199999999999998,
   'benchmark': '000300.XSHG',
   'benchmark_annualized_returns': 0.17000000000000001,
   'benchmark_total_returns': 0.89300000000000002,
   'beta': 0.88800000000000001,
   'cash': 2606790.0210000002,
   'downside_risk': 0.047,
   'end_date': '2018-01-22',
   'information_ratio': 1.242,
   'max_drawdown': 0.34200000000000003,
   'run_type': 'BACKTEST',
   'sharpe': 1.0369999999999999,
   'sortino': 5.077,
   'start_date': '2014-01-03',
   'strategy_file': 'strategy.py',
   'strategy_name': 'strategy',
   'total_returns': 1.831,
   'total_value': 28310743.421,
   'tracking_error': 0.076999999999999999,
   'unit_net_value': 2.831,
   'units': 10000000.0,
   'volatility': 0.23000000000000001},
  'trades':                      commission     exec_id  last_price  last_quantity  \
  datetime                                                                 
  2014-01-03 15:00:00     10.1304  1516792677       14.07          900.0   
  2014-01-03 15:00:00      9.9648  1516792678        6.92         1800.0   
  2014-01-03 15:00:00     10.3664  1516792679        6.82         1900.0   
  2014-01-03 15:00:00     10.3584  1516792680        4.98         2600.0   
  ...                         ...         ...         ...            ...   
  2017-09-29 15:00:00     15.2984  1516832861       14.71         1300.0   
  2017-09-29 15:00:00      5.0000  1516832862        6.30          100.0   
  2017-09-29 15:00:00     43.4592  1516832863       15.09         3600.0   
  
                      order_book_id    order_id position_effect  side symbol  \
  datetime                                                                     
  2014-01-03 15:00:00   000063.XSHE  1516792676            None   BUY   中兴通讯   
  2014-01-03 15:00:00   000961.XSHE  1516792677            None   BUY   中南建设   
  2014-01-03 15:00:00   000983.XSHE  1516792678            None   BUY   西山煤电   
  2014-01-03 15:00:00   600011.XSHG  1516792679            None   BUY   华能国际   
  2014-01-03 15:00:00   600019.XSHG  1516792680            None   BUY   宝钢股份   
  2014-01-03 15:00:00   600028.XSHG  1516792681            None   BUY   中国石化   
  2014-01-03 15:00:00   600123.XSHG  1516792682            None   BUY   兰花科创   
  2014-01-03 15:00:00   600157.XSHG  1516792683            None   BUY   永泰能源   
  2014-01-03 15:00:00   600188.XSHG  1516792684            None   BUY   兖州煤业   
  2014-01-03 15:00:00   600348.XSHG  1516792685            None   BUY   阳泉煤业   
  ...                           ...         ...             ...   ...    ...   
  2017-09-29 15:00:00   601997.XSHG  1516834906            None   BUY   贵阳银行   
  2017-09-29 15:00:00   601998.XSHG  1516834907            None   BUY   中信银行   
  2017-09-29 15:00:00   600690.XSHG  1516834908            None  SELL   青岛海尔   
  
                          tax     trading_datetime  transaction_cost  
  datetime                                                            
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00           10.1304  
  2014-01-03 15:00:00   0.000  2014-01-03 15:00:00            9.9648  
  ...                     ...                  ...               ...  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00            5.0000  
  2017-09-29 15:00:00   4.748  2017-09-29 15:00:00            9.7480  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00           58.4584  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00           57.6720  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00            5.0000  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00            5.0000  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00           59.9872  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00           15.2984  
  2017-09-29 15:00:00   0.000  2017-09-29 15:00:00            5.0000  
  2017-09-29 15:00:00  54.324  2017-09-29 15:00:00           97.7832  
  
  [40187 rows x 12 columns]}}
Clone this wiki locally