持有市值最小的10只股票

来源:https://uqer.io/community/share/5666456ef9f06c6c8a91b52d

策略是一直持有沪深当中流通市值最少的10只股票。分割线前部分是第一次买入10只市值最少的股票。 分割线后部分是每次换股票的策略。1.因为持仓中可能有停牌,所以希望能把停牌的股票先排除在外,形成new持仓,就是需要换的股票,假如这里还有8只。(这里buylist应该如何传递之前的10只信息?)2.获得现在市场上市值最小的8只股票成为target,取8只是因为极端的情况下就是我把自己持仓的全部都卖出了,换成新的8只。3.判断,卖出,如果旧有的持仓中股票不在新的target里面,证明持有的市值大于target里面的,就卖出,获得资金;买入,若此时卖出了3只,则剩余5只,那么用资金平均买入target里面市值最小的3只。形成新的8只,加上原来停牌的2只,则一共10只。 分割线后部分不知道如何写,希望指导,非常感谢。

  1. start = '2015-12-01'
  2. end = '2015-12-06'
  3. benchmark = 'HS300'
  4. universe = StockScreener(Factor.LFLO.nsmall(20))
  5. capital_base = 100000
  6. freq = 'd'
  7. refresh_rate = 1 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  8. n=10
  9. def initialize(account): # 初始化虚拟账户状态
  10. pass
  11. def handle_data(account): # 每个交易日的买入卖出指令
  12. print "current_date = %s"%(account.current_date.strftime('%Y%m%d'))
  13. buy_list=[]
  14. holding_first=DataAPI.MktEqudGet(secID=account.universe,tradeDate=account.current_date,field="tradeDate,secID,negMarketValue,closePrice",pandas="1")#获得市场上市值最少的10只股票
  15. holding_first=holding_first.sort(columns='negMarketValue')[0:n] #选出第一次能交易的十只
  16. print holding_first
  17. for stk in holding_first.secID:
  18. if len(buy_list)<10:
  19. order(stk,capital_base/n)
  20. buy_list.append(stk)
  21. print buy_list # ——————————————————————————————分割线
  22. new_holding=DataAPI.MktEqudGet(secID='buy_list',tradeDate=account.current_date,field="tradeDate,secID,negMarketValue,closePrice",pandas="1")#在下一个交易周期中获得,当前持仓中能交易的个股信息
  23. new_target=DataAPI.MktEqudGet(secID=account.universe,tradeDate=account.current_date,field="tradeDate,secID,negMarketValue,closePrice",pandas="1")#在下一个交易周期中获得,市场上市值最少的股票
  24. new_target=new_target.sort(columns='negMarketValue')[0:len(new_holidng.secID)] #按照市值排序,并且选择与持仓可交易股票数量相等的股票数
  25. for stock in newholding.secID: #卖出需要换掉的股票,获得相应的资金留作买股票用(先卖出)
  26. if stock not in new_target.secID:
  27. order_to(stock,0)
  28. new_holding.remove(stock)
  29. today_cash = account_cash + account.valid_secpos[stock]
  30. for stock in new_target.secID: #买入需要更换的股票,买入的数目与换出的数目相同且市值由小到大(再买入)
  31. if stock not in new_holding.secID and #买入的股票数目直到与换出的数目相等为止
  32. order_to(stock,today_cash/len#买入的数目) #貌似需要再添加一个变量

持有市值最小的10只股票 - 图1

  1. current_date = 20151201
  2. tradeDate secID negMarketValue closePrice
  3. 15 2015-12-01 300466.XSHE 600200000 30.01
  4. 2 2015-12-01 002735.XSHE 929800000 46.49
  5. 17 2015-12-01 300483.XSHE 974175000 62.85
  6. 3 2015-12-01 002743.XSHE 977251400 32.21
  7. 14 2015-12-01 300464.XSHE 985959000 47.70
  8. 16 2015-12-01 300472.XSHE 988197600 59.28
  9. 13 2015-12-01 300461.XSHE 998798400 59.88
  10. 5 2015-12-01 002755.XSHE 1004984400 39.66
  11. 6 2015-12-01 002760.XSHE 1019700000 46.35
  12. 7 2015-12-01 002761.XSHE 1044600000 34.82
  13. ['300466.XSHE', '002735.XSHE', '300483.XSHE', '002743.XSHE', '300464.XSHE', '300472.XSHE', '300461.XSHE', '002755.XSHE', '002760.XSHE', '002761.XSHE']
  14. current_date = 20151202
  15. tradeDate secID negMarketValue closePrice
  16. 13 2015-12-02 300466.XSHE 660200000 33.01
  17. 2 2015-12-02 002735.XSHE 902000000 45.10
  18. 12 2015-12-02 300464.XSHE 939658200 45.46
  19. 3 2015-12-02 002743.XSHE 945091000 31.15
  20. 16 2015-12-02 300483.XSHE 947205000 61.11
  21. 4 2015-12-02 002755.XSHE 964440400 38.06
  22. 14 2015-12-02 300472.XSHE 997032700 59.81
  23. 1 2015-12-02 002734.XSHE 999050000 30.74
  24. 10 2015-12-02 300423.XSHE 1012220000 46.01
  25. 17 2015-12-02 603009.XSHG 1026528300 38.49
  26. ['300466.XSHE', '002735.XSHE', '300464.XSHE', '002743.XSHE', '300483.XSHE', '002755.XSHE', '300472.XSHE', '002734.XSHE', '300423.XSHE', '603009.XSHG']
  27. current_date = 20151203
  28. tradeDate secID negMarketValue closePrice
  29. 13 2015-12-03 300466.XSHE 726200000 36.31
  30. 1 2015-12-03 002735.XSHE 961200000 48.06
  31. 2 2015-12-03 002743.XSHE 979678600 32.29
  32. 4 2015-12-03 002755.XSHE 1010559200 39.88
  33. 12 2015-12-03 300464.XSHE 1020684600 49.38
  34. 0 2015-12-03 002734.XSHE 1031550000 31.74
  35. 15 2015-12-03 300483.XSHE 1041910000 67.22
  36. 5 2015-12-03 002761.XSHE 1070700000 35.69
  37. 8 2015-12-03 300391.XSHE 1071222750 20.73
  38. 16 2015-12-03 603009.XSHG 1082001900 40.57
  39. ['300466.XSHE', '002735.XSHE', '002743.XSHE', '002755.XSHE', '300464.XSHE', '002734.XSHE', '300483.XSHE', '002761.XSHE', '300391.XSHE', '603009.XSHG']
  40. current_date = 20151204
  41. tradeDate secID negMarketValue closePrice
  42. 12 2015-12-04 300466.XSHE 798800000 39.94
  43. 2 2015-12-04 002743.XSHE 994241800 32.77
  44. 3 2015-12-04 002755.XSHE 1028804000 40.60
  45. 0 2015-12-04 002734.XSHE 1031875000 31.75
  46. 11 2015-12-04 300464.XSHE 1051482900 50.87
  47. 14 2015-12-04 300483.XSHE 1059735000 68.37
  48. 15 2015-12-04 603009.XSHG 1064399700 39.91
  49. 1 2015-12-04 002735.XSHE 1070048000 47.77
  50. 16 2015-12-04 603022.XSHG 1088400000 54.42
  51. 10 2015-12-04 300423.XSHE 1095160000 49.78
  52. ['300466.XSHE', '002743.XSHE', '002755.XSHE', '002734.XSHE', '300464.XSHE', '300483.XSHE', '603009.XSHG', '002735.XSHE', '603022.XSHG', '300423.XSHE']