海龟交易系统

海龟交易系统也是非常经典的一种策略,我们也放出了范例代码如下,而关于海龟交易系统的介绍也可以参照 这篇帖子

  1. import numpy as np
  2. import talib
  3. import math
  4.  
  5.  
  6. def get_extreme(array_high_price_result, array_low_price_result):
  7. np_array_high_price_result = np.array(array_high_price_result[:-1])
  8. np_array_low_price_result = np.array(array_low_price_result[:-1])
  9. max_result = np_array_high_price_result.max()
  10. min_result = np_array_low_price_result.min()
  11. return [max_result, min_result]
  12.  
  13.  
  14. def get_atr_and_unit( atr_array_result, atr_length_result, portfolio_value_result):
  15. atr = atr_array_result[ atr_length_result-1]
  16. unit = math.floor(portfolio_value_result * .01 / atr)
  17. return [atr, unit]
  18.  
  19.  
  20. def get_stop_price(first_open_price_result, units_hold_result, atr_result):
  21. stop_price = first_open_price_result - 2 * atr_result \
  22. + (units_hold_result - 1) * 0.5 * atr_result
  23. return stop_price
  24.  
  25.  
  26. def init(context):
  27. context.trade_day_num = 0
  28. context.unit = 0
  29. context.atr = 0
  30. context.trading_signal = 'start'
  31. context.pre_trading_signal = ''
  32. context.units_hold_max = 4
  33. context.units_hold = 0
  34. context.quantity = 0
  35. context.max_add = 0
  36. context.first_open_price = 0
  37. context.s = '000300.XSHG'
  38. context.open_observe_time = 55
  39. context.close_observe_time = 20
  40. context.atr_time = 20
  41.  
  42.  
  43. def handle_bar(context, bar_dict):
  44. portfolio_value = context.portfolio.portfolio_value
  45. high_price = history_bars(context.s, context.open_observe_time+1, '1d', 'high')
  46. low_price_for_atr = history_bars(context.s, context.open_observe_time+1, '1d', 'low')
  47. low_price_for_extreme = history_bars(context.s, context.close_observe_time+1, '1d', 'low')
  48. close_price = history_bars(context.s, context.open_observe_time+2, '1d', 'close')
  49. close_price_for_atr = close_price[:-1]
  50.  
  51. atr_array = talib.ATR(high_price, low_price_for_atr, close_price_for_atr, timeperiod=context.atr_time)
  52.  
  53. maxx = get_extreme(high_price, low_price_for_extreme)[0]
  54. minn = get_extreme(high_price, low_price_for_extreme)[1]
  55. atr = atr_array[-2]
  56.  
  57. if context.trading_signal != 'start':
  58. if context.units_hold != 0:
  59. context.max_add += 0.5 * get_atr_and_unit(atr_array, atr_array.size, portfolio_value)[0]
  60. else:
  61. context.max_add = bar_dict[context.s].last
  62.  
  63. cur_position = context.portfolio.positions[context.s].quantity
  64. available_cash = context.portfolio.cash
  65. market_value = context.portfolio.market_value
  66.  
  67. if (cur_position > 0 and
  68. bar_dict[context.s].last < get_stop_price(context.first_open_price, context.units_hold, atr)):
  69. context.trading_signal = 'stop'
  70. else:
  71. if cur_position > 0 and bar_dict[context.s].last < minn:
  72. context.trading_signal = 'exit'
  73. else:
  74. if (bar_dict[context.s].last > context.max_add and context.units_hold != 0 and
  75. context.units_hold < context.units_hold_max and
  76. available_cash > bar_dict[context.s].last*context.unit):
  77. context.trading_signal = 'entry_add'
  78. else:
  79. if bar_dict[context.s].last > maxx and context.units_hold == 0:
  80. context.max_add = bar_dict[context.s].last
  81. context.trading_signal = 'entry'
  82.  
  83. atr = get_atr_and_unit(atr_array, atr_array.size, portfolio_value)[0]
  84. if context.trade_day_num % 5 == 0:
  85. context.unit = get_atr_and_unit(atr_array, atr_array.size, portfolio_value)[1]
  86. context.trade_day_num += 1
  87. context.quantity = context.unit
  88.  
  89. if (context.trading_signal != context.pre_trading_signal or
  90. (context.units_hold < context.units_hold_max and context.units_hold > 1) or
  91. context.trading_signal == 'stop'):
  92. if context.trading_signal == 'entry':
  93. context.quantity = context.unit
  94. if available_cash > bar_dict[context.s].last*context.quantity:
  95. order_shares(context.s, context.quantity)
  96. context.first_open_price = bar_dict[context.s].last
  97. context.units_hold = 1
  98.  
  99. if context.trading_signal == 'entry_add':
  100. context.quantity = context.unit
  101. order_shares(context.s, context.quantity)
  102. context.units_hold += 1
  103.  
  104. if context.trading_signal == 'stop':
  105. if context.units_hold > 0:
  106. order_shares(context.s, -context.quantity)
  107. context.units_hold -= 1
  108.  
  109. if context.trading_signal == 'exit':
  110. if cur_position > 0:
  111. order_shares(context.s, -cur_position)
  112. context.units_hold = 0
  113.  
  114. context.pre_trading_signal = context.trading_signal