动量策略(momentum driven)

来源:https://uqer.io/community/share/555ed9eef9f06c6c7304f9a9

  1. import pandas as pd
  2. start = '2011-11-01'
  3. end = '2015-03-01'
  4. benchmark = 'HS300'
  5. universe = set_universe('HS300') # 股票池为沪深300
  6. capital_base = 10000000
  7. refresh_rate = 10
  8. def initialize(account):
  9. pass
  10. def handle_data(account):
  11. history = account.get_attribute_history('closePrice', 20)
  12. momentum = {'symbol':[], 'c_ret':[]}
  13. for stk in account.universe:
  14. momentum['symbol'].append(stk)
  15. momentum['c_ret'].append(history[stk][-1]/history[stk][0])
  16. # 按照过去20日收益率排序,并且选择前20%的股票作为买入候选
  17. momentum = pd.DataFrame(momentum).sort(columns='c_ret').reset_index()
  18. momentum = momentum[len(momentum)*4/5:len(momentum)] # 选择
  19. buylist = momentum['symbol'].tolist()
  20. for stk in account.valid_secpos:
  21. if stk not in buylist:
  22. order_to(stk, 0)
  23. # 等权重买入所选股票
  24. portfolio_value = account.referencePortfolioValue
  25. for stk in buylist:
  26. if stk not in account.valid_secpos:
  27. order_to(stk, int(portfolio_value / account.referencePrice[stk] / 100.0 / len(buylist))*100)

动量策略(momentum driven) - 图1

  1. bt
tradeDatecashsecurity_positionportfolio_valuebenchmark_returnblotter
02011-11-291.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …10057034.0296360.013697[Order(order time: datetime.datetime(2011, 11,…
12011-11-301.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9744501.409636-0.033370[]
22011-12-011.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9865655.5006360.024625[]
32011-12-021.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9708455.312636-0.010180[]
42011-12-051.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9406180.945636-0.014048[]
52011-12-061.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9415313.949636-0.002000[]
62011-12-071.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9454383.5546360.004724[]
72011-12-081.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9492264.755636-0.001276[]
82011-12-091.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9410892.851636-0.008531[]
92011-12-121.627636{u'600809.XSHG': 5700, u'600597.XSHG': 21100, …9280515.559636-0.010293[]
102011-12-136.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9116051.541010-0.022506[Order(order time: datetime.datetime(2011, 12,…
112011-12-146.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9042660.489010-0.010094[]
122011-12-156.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …8973191.009010-0.023646[]
132011-12-166.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9203671.2070100.021079[]
142011-12-196.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9272914.609010-0.002394[]
152011-12-206.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9245375.546010-0.003076[]
162011-12-216.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9134328.126010-0.015971[]
172011-12-226.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9174823.7520100.000952[]
182011-12-236.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9185929.7020100.007613[]
192011-12-266.144010{u'000423.XSHE': 4200, u'600036.XSHG': 16300, …9054507.546010-0.009943[]
202011-12-279.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…8855733.701263-0.013130[Order(order time: datetime.datetime(2011, 12,…
212011-12-289.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…8820469.7802630.001255[]
222011-12-299.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…8836530.5332630.001487[]
232011-12-309.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…8976704.5262630.014875[]
242012-01-049.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…8858664.267263-0.020032[]
252012-01-059.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…8785873.316263-0.009730[]
262012-01-069.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…8790219.6262630.006245[]
272012-01-099.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…9032347.1252630.034039[]
282012-01-109.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…9247829.1182630.033260[]
292012-01-119.172263{u'601328.XSHG': 41600, u'600036.XSHG': 16300,…9186427.159263-0.004797[]
7552015-01-121.942737{u'600036.XSHG': 23600, u'000776.XSHE': 22100,…20280828.092737-0.009344[]
7562015-01-131.942737{u'600036.XSHG': 23600, u'000776.XSHE': 22100,…20204218.4327370.000131[]
7572015-01-141.942737{u'600036.XSHG': 23600, u'000776.XSHE': 22100,…20223885.602737-0.003307[]
7582015-01-151.942737{u'600036.XSHG': 23600, u'000776.XSHE': 22100,…20962393.2127370.029036[]
7592015-01-161.942737{u'600036.XSHG': 23600, u'000776.XSHE': 22100,…21076614.7227370.008608[]
7602015-01-193.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…19298994.131317-0.077023[Order(order time: datetime.datetime(2015, 1, …
7612015-01-203.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…19641436.9213170.012240[]
7622015-01-213.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…20548813.1313170.044951[]
7632015-01-223.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…20654164.3213170.005277[]
7642015-01-233.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…20704527.4013170.001155[]
7652015-01-263.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…20741863.4113170.010150[]
7662015-01-273.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…20501439.751317-0.009161[]
7672015-01-283.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…20072450.291317-0.013877[]
7682015-01-293.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…19879912.781317-0.012346[]
7692015-01-303.731317{u'601328.XSHG': 49500, u'600066.XSHG': 21600,…19612742.771317-0.013616[]
7702015-02-026.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …19457712.474377-0.023419[Order(order time: datetime.datetime(2015, 2, …
7712015-02-036.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …19862476.7143770.024891[]
7722015-02-046.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …19714886.724377-0.010379[]
7732015-02-056.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …19679598.144377-0.010236[]
7742015-02-066.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …19297569.294377-0.016194[]
7752015-02-096.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …19131675.2443770.010114[]
7762015-02-106.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …19621103.0943770.018238[]
7772015-02-116.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …20007845.2343770.007978[]
7782015-02-126.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …20139049.8043770.002548[]
7792015-02-136.734377{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …20416511.1843770.007829[]
7802015-02-165.417277{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …21015638.0572770.008547[Order(order time: datetime.datetime(2015, 2, …
7812015-02-175.417277{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …20991534.3872770.006526[]
7822015-02-255.417277{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …20681973.077277-0.012376[]
7832015-02-265.417277{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …20936634.3472770.025172[]
7842015-02-275.417277{u'002153.XSHE': 4400, u'600498.XSHG': 19400, …21204596.7572770.001836[]
  1. 785 rows × 6 columns