流通市值最小股票(新筛选器版)

来源:https://uqer.io/community/share/56ee3c99228e5b887fe50dc2

策略思路

总是持有流通市值最小的10只股票

  1. import numpy as np
  2. from CAL.PyCAL import *
  3. start = '2013-01-05'
  4. end = '2015-12-07'
  5. benchmark = 'HS300'
  6. universe = StockScreener(Factor.LFLO.nsmall(20)) # LFLO 为流通市值对数,LCAP 为总市值对数
  7. capital_base = 1000000
  8. refresh_rate = 1
  9. stk_num = 10 # 持仓股票数量
  10. def initialize(account):
  11. pass
  12. def handle_data(account):
  13. open_price = account.get_attribute_history('openPrice', 1) # 前一日开盘价
  14. close_price = account.referencePrice # 前一日收盘价
  15. # 获取回测当日的前一天日期
  16. dt = Date.fromDateTime(account.current_date)
  17. cal = Calendar('China.SSE')
  18. last_day = cal.advanceDate(dt,'-1B',BizDayConvention.Preceding) #计算出倒数第一个交易日
  19. last_day_str = last_day.strftime("%Y%m%d")
  20. # 市值排序
  21. mkt_value = DataAPI.MktEqudGet(secID=account.universe,tradeDate=last_day_str,field="secID,negMarketValue",pandas="1")
  22. univ_sorted = mkt_value.sort('negMarketValue').secID
  23. buylist = {}
  24. for s in univ_sorted:
  25. if not (np.isnan(close_price[s]) or close_price[s] == 0 or np.isnan(open_price[s]) or open_price[s] == 0):
  26. buylist[s] = 0
  27. if len(buylist) >= stk_num : # 只持有stk_num数目的股票
  28. break
  29. for s in account.valid_secpos:
  30. if s not in buylist:
  31. order_to(s, 0)
  32. v = account.referencePortfolioValue / len(buylist)
  33. for s in buylist:
  34. buylist[s] = v / close_price[s] - account.valid_secpos.get(s, 0)
  35. for s in sorted(buylist, key=buylist.get):
  36. order(s, buylist[s])

流通市值最小股票(新筛选器版) - 图1

回测详情或者bt打印出来,都可以看到我们维护了一个总是持有10只最小流通市值股票的持仓;偶尔持仓股票数目会有11或者12只

  1. bt
tradeDatecashsecurity_positionportfolio_valuebenchmark_returnblotter
02013-01-083352.2479{u'300237.XSHE': {u'amount': 56400, u'cost': 1…1017415.8479-0.004200[Order(order_time: 2013-01-08 09:30, symbol: 0…
12013-01-0911778.5117{u'300237.XSHE': {u'amount': 56900, u'cost': 1…1028379.71170.000313[Order(order_time: 2013-01-09 09:30, symbol: 3…
22013-01-103589.4794{u'300237.XSHE': {u'amount': 59000, u'cost': 1…1031625.07940.001758[Order(order_time: 2013-01-10 09:30, symbol: 3…
32013-01-113536.7632{u'300237.XSHE': {u'amount': 59100, u'cost': 1…1033944.0632-0.018703[Order(order_time: 2013-01-11 09:30, symbol: 3…
42013-01-14220.7088{u'300280.XSHE': {u'amount': 12700, u'cost': 8…1071566.80880.038051[Order(order_time: 2013-01-14 09:30, symbol: 3…
52013-01-152933.8669{u'300280.XSHE': {u'amount': 12800, u'cost': 8…1084159.16690.007033[Order(order_time: 2013-01-15 09:30, symbol: 3…
62013-01-161784.7565{u'300280.XSHE': {u'amount': 12700, u'cost': 8…1072728.6565-0.007227[Order(order_time: 2013-01-16 09:30, symbol: 3…
72013-01-1766.0136{u'300280.XSHE': {u'amount': 12700, u'cost': 8…1058278.1136-0.009445[Order(order_time: 2013-01-17 09:30, symbol: 3…
82013-01-1880.2938{u'300237.XSHE': {u'amount': 58600, u'cost': 1…1068224.09380.016719[Order(order_time: 2013-01-18 09:30, symbol: 3…
92013-01-213050.0950{u'300237.XSHE': {u'amount': 58600, u'cost': 1…1068048.89500.005957[Order(order_time: 2013-01-21 09:30, symbol: 3…
102013-01-222609.2459{u'300237.XSHE': {u'amount': 61900, u'cost': 1…1036051.4459-0.005362[Order(order_time: 2013-01-22 09:30, symbol: 3…
112013-01-232045.4473{u'300237.XSHE': {u'amount': 61000, u'cost': 1…1026266.24730.004066[Order(order_time: 2013-01-23 09:30, symbol: 3…
122013-01-242926.7991{u'300237.XSHE': {u'amount': 60700, u'cost': 1…1000829.7991-0.009473[Order(order_time: 2013-01-24 09:30, symbol: 3…
132013-01-25328.4654{u'300237.XSHE': {u'amount': 60600, u'cost': 1…999702.1654-0.004290[Order(order_time: 2013-01-25 09:30, symbol: 3…
142013-01-28286.7192{u'300237.XSHE': {u'amount': 60500, u'cost': 1…1019742.91920.031182[Order(order_time: 2013-01-28 09:30, symbol: 3…
152013-01-2917.7471{u'300237.XSHE': {u'amount': 60700, u'cost': 1…1028465.74710.009050[Order(order_time: 2013-01-29 09:30, symbol: 3…
162013-01-30171.7371{u'300237.XSHE': {u'amount': 61100, u'cost': 1…1012637.13710.004802[Order(order_time: 2013-01-30 09:30, symbol: 3…
172013-01-31386.9216{u'300237.XSHE': {u'amount': 60700, u'cost': 1…996104.6216-0.000681[Order(order_time: 2013-01-31 09:30, symbol: 3…
182013-02-01668.2563{u'300237.XSHE': {u'amount': 60500, u'cost': 1…1016779.95630.021006[Order(order_time: 2013-02-01 09:30, symbol: 3…
192013-02-042638.0595{u'300237.XSHE': {u'amount': 60500, u'cost': 1…1027883.25950.001713[Order(order_time: 2013-02-04 09:30, symbol: 3…
202013-02-05422.8302{u'300237.XSHE': {u'amount': 61100, u'cost': 1…1037631.53020.008606[Order(order_time: 2013-02-05 09:30, symbol: 3…
212013-02-06102645.2421{u'300237.XSHE': {u'amount': 61100, u'cost': 1…1036665.54210.001505[Order(order_time: 2013-02-06 09:30, symbol: 3…
222013-02-07350.1216{u'300237.XSHE': {u'amount': 61000, u'cost': 1…1040688.9216-0.005753[Order(order_time: 2013-02-07 09:30, symbol: 3…
232013-02-0866.3084{u'300237.XSHE': {u'amount': 61600, u'cost': 1…1052094.30840.004294[Order(order_time: 2013-02-08 09:30, symbol: 3…
242013-02-18356.0140{u'300237.XSHE': {u'amount': 60600, u'cost': 1…1059799.6140-0.012357[Order(order_time: 2013-02-18 09:30, symbol: 3…
252013-02-19347.5874{u'300237.XSHE': {u'amount': 60900, u'cost': 1…1043617.1874-0.018948[Order(order_time: 2013-02-19 09:30, symbol: 3…
262013-02-201524.6353{u'300237.XSHE': {u'amount': 60900, u'cost': 1…1064335.03530.006341[Order(order_time: 2013-02-20 09:30, symbol: 3…
272013-02-215.6970{u'300237.XSHE': {u'amount': 61100, u'cost': 1…1055551.1970-0.034074[Order(order_time: 2013-02-21 09:30, symbol: 3…
282013-02-22427.2956{u'300237.XSHE': {u'amount': 60600, u'cost': 1…1065969.2956-0.005340[Order(order_time: 2013-02-22 09:30, symbol: 3…
292013-02-25128.1500{u'300237.XSHE': {u'amount': 61100, u'cost': 1…1080711.15000.003220[Order(order_time: 2013-02-25 09:30, symbol: 3…
6772015-10-272335.2216{u'300405.XSHE': {u'amount': 21700, u'cost': 3…9409754.22160.001008[Order(order_time: 2015-10-27 09:30, symbol: 3…
6782015-10-283639.3976{u'300405.XSHE': {u'amount': 21900, u'cost': 3…9225569.3976-0.018915[Order(order_time: 2015-10-28 09:30, symbol: 3…
6792015-10-292647.5406{u'300405.XSHE': {u'amount': 21900, u'cost': 3…9463491.54060.002379[Order(order_time: 2015-10-29 09:30, symbol: 3…
6802015-10-303303.6906{u'300405.XSHE': {u'amount': 21900, u'cost': 3…9688757.69060.000218[Order(order_time: 2015-10-30 09:30, symbol: 3…
6812015-11-0211034.8026{u'300405.XSHE': {u'amount': 23100, u'cost': 3…9996076.8026-0.016445[Order(order_time: 2015-11-02 09:30, symbol: 3…
6822015-11-0329889.7366{u'002735.XSHE': {u'amount': 26300, u'cost': 3…10474014.7366-0.003012[Order(order_time: 2015-11-03 09:30, symbol: 3…
6832015-11-043009.8936{u'002735.XSHE': {u'amount': 26700, u'cost': 3…10880432.89360.047048[Order(order_time: 2015-11-04 09:30, symbol: 3…
6842015-11-051079.4096{u'002735.XSHE': {u'amount': 26800, u'cost': 3…10870617.40960.021340[Order(order_time: 2015-11-05 09:30, symbol: 3…
6852015-11-06104.6466{u'002735.XSHE': {u'amount': 27600, u'cost': 3…11390740.64660.023585[Order(order_time: 2015-11-06 09:30, symbol: 0…
6862015-11-098962.8616{u'002735.XSHE': {u'amount': 27500, u'cost': 3…12093281.86160.012385[Order(order_time: 2015-11-09 09:30, symbol: 3…
6872015-11-1022992.6176{u'002735.XSHE': {u'amount': 27400, u'cost': 3…12110893.6176-0.001853[Order(order_time: 2015-11-10 09:30, symbol: 3…
6882015-11-111201562.4166{u'002735.XSHE': {u'amount': 28000, u'cost': 3…13030169.41660.000106[Order(order_time: 2015-11-11 09:30, symbol: 6…
6892015-11-12113031.2706{u'002735.XSHE': {u'amount': 28200, u'cost': 3…14037309.2706-0.009996[Order(order_time: 2015-11-12 09:30, symbol: 3…
6902015-11-135102.3686{u'002735.XSHE': {u'amount': 29400, u'cost': 3…12922427.3686-0.012932[Order(order_time: 2015-11-13 09:30, symbol: 0…
6912015-11-1621313.6426{u'002735.XSHE': {u'amount': 29100, u'cost': 3…13183437.64260.004774[Order(order_time: 2015-11-16 09:30, symbol: 0…
6922015-11-171401.0406{u'002735.XSHE': {u'amount': 29600, u'cost': 3…12937004.0406-0.001525[Order(order_time: 2015-11-17 09:30, symbol: 6…
6932015-11-18173.1346{u'002735.XSHE': {u'amount': 29700, u'cost': 3…12168156.1346-0.011390[Order(order_time: 2015-11-18 09:30, symbol: 0…
6942015-11-192455.0746{u'002735.XSHE': {u'amount': 29400, u'cost': 3…12936911.07460.015984[Order(order_time: 2015-11-19 09:30, symbol: 0…
6952015-11-207478.6776{u'002735.XSHE': {u'amount': 29700, u'cost': 3…13013394.6776-0.000155[Order(order_time: 2015-11-20 09:30, symbol: 3…
6962015-11-233168.3376{u'002735.XSHE': {u'amount': 29700, u'cost': 3…12619590.3376-0.005577[Order(order_time: 2015-11-23 09:30, symbol: 3…
6972015-11-2415418.3816{u'002735.XSHE': {u'amount': 29400, u'cost': 3…13665856.38160.000148[Order(order_time: 2015-11-24 09:30, symbol: 0…
6982015-11-2570214.3646{u'002735.XSHE': {u'amount': 29400, u'cost': 3…13704250.36460.007384[Order(order_time: 2015-11-25 09:30, symbol: 6…
6992015-11-2616885.0396{u'002735.XSHE': {u'amount': 29600, u'cost': 3…13902736.0396-0.005865[Order(order_time: 2015-11-26 09:30, symbol: 6…
7002015-11-2747391.8976{u'002735.XSHE': {u'amount': 29100, u'cost': 3…13367121.8976-0.053848[Order(order_time: 2015-11-27 09:30, symbol: 0…
7012015-11-3031493.4806{u'002735.XSHE': {u'amount': 30100, u'cost': 3…14149036.48060.002648[Order(order_time: 2015-11-30 09:30, symbol: 3…
7022015-12-012339.8786{u'002761.XSHE': {u'amount': 40700, u'cost': 3…13996263.87860.007089[Order(order_time: 2015-12-01 09:30, symbol: 6…
7032015-12-021396517.7026{u'002761.XSHE': {u'amount': 40200, u'cost': 3…13965267.70260.036267[Order(order_time: 2015-12-02 09:30, symbol: 0…
7042015-12-031439379.3926{u'002735.XSHE': {u'amount': 30900, u'cost': 3…14861814.39260.007347[Order(order_time: 2015-12-03 09:30, symbol: 0…
7052015-12-041479225.7226{u'002761.XSHE': {u'amount': 41600, u'cost': 3…15134121.7226-0.019125[Order(order_time: 2015-12-04 09:30, symbol: 3…
7062015-12-071552999.2546{u'002735.XSHE': {u'amount': 31600, u'cost': 3…15369296.25460.002723[Order(order_time: 2015-12-07 09:30, symbol: 0…
  1. 707 rows × 6 columns