嘉庆离散指标测试

来源:https://uqer.io/community/share/55595229f9f06c6c7104f8b3

  1. import numpy as np
  2. start = datetime(2011, 1, 1)
  3. end = datetime(2015, 4, 27)
  4. benchmark = 'HS300'
  5. universe = set_universe('HS300')
  6. capital_base = 100000
  7. short_history = 30
  8. longest_history = 60
  9. pos_pieces = 10
  10. enter_window = 20
  11. exit_window = 10
  12. N = 4
  13. def initialize(account):
  14. account.postion_size_hold = {}
  15. for stk in universe:
  16. account.postion_size_hold[stk] = 0
  17. def handle_data(account):
  18. for stock in account.universe:
  19. cnt_price = account.referencePrice[stock]
  20. a1 = account.get_attribute_history('closePrice', longest_history)[stock]-account.get_attribute_history('lowPrice', longest_history)[stock]
  21. b1 = account.get_attribute_history('closePrice', longest_history)[stock]-account.get_attribute_history('highPrice',longest_history)[stock]
  22. c1 = account.get_attribute_history('highPrice',longest_history)[stock]-account.get_attribute_history('lowPrice',longest_history)[stock]
  23. d1 = account.get_attribute_history('turnoverVol', longest_history)[stock]
  24. adl = ((((a1)-(b1))/(c1)))*d1
  25. a2 = account.get_attribute_history('closePrice', short_history)[stock]-account.get_attribute_history('lowPrice', short_history)[stock]
  26. b2 = account.get_attribute_history('closePrice', short_history)[stock]-account.get_attribute_history('highPrice',short_history)[stock]
  27. c2 = account.get_attribute_history('highPrice',short_history)[stock]-account.get_attribute_history('lowPrice',short_history)[stock]
  28. d2 = account.get_attribute_history('turnoverVol', short_history)[stock]
  29. ads = ((((a2)-(b2))/(c2)))*d2
  30. mean_cp1 = adl.mean()
  31. mean_cp2 = ads.mean()
  32. flag = mean_cp1 - mean_cp2
  33. if flag > 0 and account.postion_size_hold[stock]<N:
  34. order_to(stock, capital_base/pos_pieces/cnt_price/N)
  35. account.postion_size_hold[stock] += 1
  36. elif flag < 0 :
  37. order_to(stock, 0)
  38. account.postion_size_hold[stock] = 0