MACD quantization trade

来源:https://uqer.io/community/share/56265ed3f9f06c4ca62fb618

  1. import talib
  2. import copy
  3. from numpy import arange, array, isnan
  4. #本策略的目的在于对常见的MACD指标策略进行验证
  5. start = '2014-01-01' #start time
  6. end = '2015-10-19' #end time
  7. benchmark = 'HS300'
  8. #universe = ['000001.XSHE', '600000.XSHG']
  9. universe = set_universe('HS300') # 证券池,支持股票和基金
  10. capital_base = 1000000
  11. freq = 'd'
  12. refresh_rate = 1
  13. def dea_deviate_from_k_line(k_line, dea):
  14. #判断k线和dea走势是否背离,主要思路是
  15. #从最新一天开始往回寻找几个极值点,然后
  16. #进行极值比较
  17. #然后是从当前点到第一个极值点进行比对
  18. #最后按照权重返回对应数值
  19. #对数组进行翻转
  20. k_line_re = k_line[::-1]
  21. dea_re = dea[::-1]
  22. k_line_extremum = []
  23. #获取极值点的索引,只统计近似区间的情况
  24. k_line_extremum_inx = {}
  25. dea_extremum = []
  26. dea_extremum_inx = {}
  27. for c in xrange(1, len(k_line_re) - 1):
  28. if not isnan(k_line_re[c - 1]) and not isnan(k_line_re[c]) and not isnan(k_line_re[c + 1]):
  29. if k_line_re[c - 1] < k_line_re[c] and k_line_re[c + 1] < k_line_re[c]:
  30. #获取到极值点
  31. k_line_extremum.append(k_line_re[c])
  32. k_line_extremum_inx[k_line_re[c]] = c
  33. for c in xrange(1, len(dea_re) - 1):
  34. if not isnan(dea_re[c - 1]) and not isnan(dea_re[c]) and not isnan(dea_re[c + 1]):
  35. if dea_re[c - 1] < dea_re[c] and dea_re[c + 1] < dea_re[c]:
  36. dea_extremum.append(dea_re[c])
  37. dea_extremum_inx[dea_re[c]] = c
  38. #对极值点进行筛选判断趋势
  39. sig_extremum = 0
  40. if len(k_line_extremum) >= 2 and len(dea_extremum) >= 2:
  41. k_line_first_deviate = k_line_extremum[0] - k_line_extremum[1]
  42. dea_first_deviate = dea_extremum[0] - dea_extremum[1]
  43. k_and_dea_start_deviate = k_line_extremum_inx[k_line_extremum[0]] - dea_extremum_inx[dea_extremum[0]]
  44. k_and_dea_end_deviate = k_line_extremum_inx[k_line_extremum[1]] - dea_extremum_inx[dea_extremum[1]]
  45. #k线和dea的同时启动差距要小于2天
  46. if abs(k_and_dea_start_deviate) < 2:
  47. #超过3天的背离趋势被确定为趋势
  48. k_deviate_dates = abs(k_line_extremum_inx[k_line_extremum[0]]) - abs(k_line_extremum_inx[k_line_extremum[1]])
  49. dea_deviate_date = abs(dea_extremum_inx[dea_extremum[0]]) - abs(dea_extremum_inx[dea_extremum[1]])
  50. #最好是能同时停止,如果k线和dea线持续时间差值过大,说明应该以大的线为主要趋势
  51. dea_k_deviate = abs(k_deviate_dates) - abs(dea_deviate_date)
  52. if abs(k_deviate_dates) >= 3 and abs(dea_deviate_date) >= 3 and dea_k_deviate < 3:
  53. if k_line_first_deviate < 0 and dea_first_deviate > 0 :
  54. #k线下降,dea上升说明是变盘上涨信号
  55. sig_extremum = 1;
  56. elif k_line_first_deviate > 0 and dea_first_deviate < 0:
  57. #k线上升,dea下降说明是变盘下跌信号
  58. sig_extremum = -1
  59. else:
  60. #需要看是以那个线为准
  61. if abs(k_deviate_dates) > abs(dea_deviate_date):
  62. if k_line_first_deviate < 0:
  63. sig_extremum = -1
  64. else:
  65. sig_extremum = 1
  66. else:
  67. if dea_first_deviate < 0:
  68. sig_extremum = -1
  69. else:
  70. sig_extremum = 1
  71. return sig_extremum
  72. def initialize(account):
  73. pass
  74. def handle_data(account):
  75. fibonacci_sequence_date1 = 144
  76. fibonacci_sequence_date2 = 143
  77. hist = account.get_attribute_history('closePrice', fibonacci_sequence_date1)
  78. for stock in account.universe:
  79. s_num = 0
  80. for c in hist[stock]:
  81. if isnan(c):
  82. s_num += 1
  83. if s_num >= len(hist[stock]) - 10:
  84. #说明数据太少直接放过
  85. continue
  86. macd, macdsignal, macdhist = talib.MACD(hist[stock])
  87. trade_signal = 0
  88. for c in range(fibonacci_sequence_date2, len(macd) - 1):
  89. #1.DIFF、DEA均为正,DIFF向上突破DEA,买入信号
  90. if not isnan(macd[c]) and not isnan(macdsignal[c]) and macd[c] > 0 and macdsignal[c] > 0:
  91. if not isnan(macd[c - 1]) and not isnan(macdsignal[c - 1]) and macd[c - 1] < macdsignal[c - 1]:
  92. if not isnan(macd[c + 1]) and not isnan(macdsignal[c + 1]) and macd[c + 1] > macdsignal[c + 1]:
  93. trade_signal += 1
  94. #2.DIFF、DEA均为负,DIFF向下跌破DEA,卖出信号
  95. if not isnan(macd[c]) and not isnan(macdsignal[c]) and macd[c] < 0 and macdsignal[c] < 0:
  96. if not isnan(macd[c - 1]) and not isnan(macdsignal[c - 1]) and macd[c - 1] > macdsignal[c - 1]:
  97. if not isnan(macd[c + 1]) and not isnan(macdsignal[c + 1]) and macd[c + 1] < macdsignal[c + 1]:
  98. trade_signal -= 1
  99. #4.分析MACD柱状线,由负变正,买入信号
  100. if not isnan(macdhist[c - 1]) and not isnan(macdhist[c]) and not isnan(macdhist[c + 1]):
  101. if macdhist[c] > macdhist[c - 1] and macdhist[c + 1] > macdhist[c] and macdhist[c] > 0 and macdhist[c - 1] < 0:
  102. trade_signal += 1
  103. #5.分析MACD柱状线,由正变负,卖出信号
  104. if not isnan(macdhist[c - 1]) and not isnan(macdhist[c]) and not isnan(macdhist[c + 1]):
  105. if macdhist[c] < macdhist[c - 1] and macdhist[c + 1] < macdhist[c] and macdhist[c] < 0 and macdhist[c - 1] > 0:
  106. trade_signal -= 1
  107. #3.DEA线与K线发生背离,行情反转信号
  108. #如果返回0则代表不对信号进行叠加
  109. trade_signal += dea_deviate_from_k_line(hist[stock], macdsignal)
  110. if trade_signal > 0 and trade_signal <= 1:
  111. order(stock, 100)
  112. elif trade_signal > 1:
  113. order(stock, 200)
  114. elif trade_signal < 0 and trade_signal >= -1:
  115. order(stock, -100)
  116. elif trade_signal < -1:
  117. order(stock, -200)

MACD quantization trade - 图1