1. # coding=utf-8
      2. from __future__ import print_function, absolute_import, unicode_literals
      3. from datetime import datetime
      4. import numpy as np
      5. from gm.api import *
      6. import sys
      7. try:
      8. from sklearn import svm
      9. except:
      10. print('请安装scikit-learn库和带mkl的numpy')
      11. sys.exit(-1)
      12. '''
      13. 本策略选取了七个特征变量组成了滑动窗口长度为15天的训练集,随后训练了一个二分类(上涨/下跌)的支持向量机模型.
      14. 若没有仓位则在每个星期一的时候输入标的股票近15个交易日的特征变量进行预测,并在预测结果为上涨的时候购买标的.
      15. 若已经持有仓位则在盈利大于10%的时候止盈,在星期五损失大于2%的时候止损.
      16. 特征变量为:1.收盘价/均值2.现量/均量3.最高价/均价4.最低价/均价5.现量6.区间收益率7.区间标准差
      17. 训练数据为:SHSE.600000浦发银行,时间从2016-03-01到2017-06-30
      18. 回测时间为:2017-07-01 09:00:00到2017-10-01 09:00:00
      19. '''
      20. def init(context):
      21. # 订阅浦发银行的分钟bar行情
      22. context.symbol = 'SHSE.600000'
      23. subscribe(symbols=context.symbol, frequency='60s')
      24. start_date = '2016-03-01' # SVM训练起始时间
      25. end_date = '2017-06-30' # SVM训练终止时间
      26. # 用于记录工作日
      27. # 获取目标股票的daily历史行情
      28. recent_data = history(context.symbol, frequency='1d', start_time=start_date, end_time=end_date, fill_missing='last',
      29. df=True)
      30. days_value = recent_data['bob'].values
      31. days_close = recent_data['close'].values
      32. days = []
      33. # 获取行情日期列表
      34. print('准备数据训练SVM')
      35. for i in range(len(days_value)):
      36. days.append(str(days_value[i])[0:10])
      37. x_all = []
      38. y_all = []
      39. for index in range(15, (len(days) - 5)):
      40. # 计算三星期共15个交易日相关数据
      41. start_day = days[index - 15]
      42. end_day = days[index]
      43. data = history(context.symbol, frequency='1d', start_time=start_day, end_time=end_day, fill_missing='last',
      44. df=True)
      45. close = data['close'].values
      46. max_x = data['high'].values
      47. min_n = data['low'].values
      48. amount = data['amount'].values
      49. volume = []
      50. for i in range(len(close)):
      51. volume_temp = amount[i] / close[i]
      52. volume.append(volume_temp)
      53. close_mean = close[-1] / np.mean(close) # 收盘价/均值
      54. volume_mean = volume[-1] / np.mean(volume) # 现量/均量
      55. max_mean = max_x[-1] / np.mean(max_x) # 最高价/均价
      56. min_mean = min_n[-1] / np.mean(min_n) # 最低价/均价
      57. vol = volume[-1] # 现量
      58. return_now = close[-1] / close[0] # 区间收益率
      59. std = np.std(np.array(close), axis=0) # 区间标准差
      60. # 将计算出的指标添加到训练集X
      61. # features用于存放因子
      62. features = [close_mean, volume_mean, max_mean, min_mean, vol, return_now, std]
      63. x_all.append(features)
      64. # 准备算法需要用到的数据
      65. for i in range(len(days_close) - 20):
      66. if days_close[i + 20] > days_close[i + 15]:
      67. label = 1
      68. else:
      69. label = 0
      70. y_all.append(label)
      71. x_train = x_all[: -1]
      72. y_train = y_all[: -1]
      73. # 训练SVM
      74. context.clf = svm.SVC(C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability=False,
      75. tol=0.001, cache_size=200, verbose=False, max_iter=-1,
      76. decision_function_shape='ovr', random_state=None)
      77. context.clf.fit(x_train, y_train)
      78. print('训练完成!')
      79. def on_bar(context, bars):
      80. bar = bars[0]
      81. # 获取当前年月日
      82. today = bar.bob.strftime('%Y-%m-%d')
      83. # 获取数据并计算相应的因子
      84. # 于星期一的09:31:00进行操作
      85. # 当前bar的工作日
      86. weekday = datetime.strptime(today, '%Y-%m-%d').isoweekday()
      87. # 获取模型相关的数据
      88. # 获取持仓
      89. position = context.account().position(symbol=context.symbol, side=PositionSide_Long)
      90. # 如果bar是新的星期一且没有仓位则开始预测
      91. if not position and weekday == 1:
      92. # 获取预测用的历史数据
      93. data = history_n(symbol=context.symbol, frequency='1d', end_time=today, count=15,
      94. fill_missing='last', df=True)
      95. close = data['close'].values
      96. train_max_x = data['high'].values
      97. train_min_n = data['low'].values
      98. train_amount = data['amount'].values
      99. volume = []
      100. for i in range(len(close)):
      101. volume_temp = train_amount[i] / close[i]
      102. volume.append(volume_temp)
      103. close_mean = close[-1] / np.mean(close)
      104. volume_mean = volume[-1] / np.mean(volume)
      105. max_mean = train_max_x[-1] / np.mean(train_max_x)
      106. min_mean = train_min_n[-1] / np.mean(train_min_n)
      107. vol = volume[-1]
      108. return_now = close[-1] / close[0]
      109. std = np.std(np.array(close), axis=0)
      110. # 得到本次输入模型的因子
      111. features = [close_mean, volume_mean, max_mean, min_mean, vol, return_now, std]
      112. features = np.array(features).reshape(1, -1)
      113. prediction = context.clf.predict(features)[0]
      114. # 若预测值为上涨则开仓
      115. if prediction == 1:
      116. # 获取昨收盘价
      117. context.price = close[-1]
      118. # 把浦发银行的仓位调至95%
      119. order_target_percent(symbol=context.symbol, percent=0.95, order_type=OrderType_Market,
      120. position_side=PositionSide_Long)
      121. print('SHSE.600000以市价单开多仓到仓位0.95')
      122. # 当涨幅大于10%,平掉所有仓位止盈
      123. elif position and bar.close / context.price >= 1.10:
      124. order_close_all()
      125. print('SHSE.600000以市价单全平多仓止盈')
      126. # 当时间为周五并且跌幅大于2%时,平掉所有仓位止损
      127. elif position and bar.close / context.price < 1.02 and weekday == 5:
      128. order_close_all()
      129. print('SHSE.600000以市价单全平多仓止损')
      130. if __name__ == '__main__':
      131. '''
      132. strategy_id策略ID,由系统生成
      133. filename文件名,请与本文件名保持一致
      134. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST
      135. token绑定计算机的ID,可在系统设置-密钥管理中生成
      136. backtest_start_time回测开始时间
      137. backtest_end_time回测结束时间
      138. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST
      139. backtest_initial_cash回测初始资金
      140. backtest_commission_ratio回测佣金比例
      141. backtest_slippage_ratio回测滑点比例
      142. '''
      143. run(strategy_id='strategy_id',
      144. filename='main.py',
      145. mode=MODE_BACKTEST,
      146. token='token_id',
      147. backtest_start_time='2017-07-01 09:00:00',
      148. backtest_end_time='2017-10-01 09:00:00',
      149. backtest_adjust=ADJUST_PREV,
      150. backtest_initial_cash=10000000,
      151. backtest_commission_ratio=0.0001,
      152. backtest_slippage_ratio=0.0001)

    机器学习(股票)

    原文: https://www.myquant.cn/docs/python_strategyies/112