PDF下载 下载
  1. # coding=utf-8
  2. from __future__ import print_function, absolute_import, unicode_literals
  3. from gm.api import *
  4. '''
  5. 本策略通过获取SHSE.000300沪深300的成份股数据并统计其30天内
  6. 开盘价大于前收盘价的天数,并在该天数大于阈值10的时候加入股票池
  7. 随后对不在股票池的股票平仓并等权配置股票池的标的,每次交易间隔1个月.
  8. 回测数据为:SHSE.000300在2015-01-15的成份股
  9. 回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00
  10. '''
  11. def init(context):
  12. # 每月第一个交易日的09:40 定时执行algo任务
  13. schedule(schedule_func=algo, date_rule='1m', time_rule='09:40:00')
  14. # context.count_bench累计天数阙值
  15. context.count_bench = 10
  16. # 用于对比的天数
  17. context.count = 30
  18. # 最大交易资金比例
  19. context.ratio = 0.8
  20. def algo(context):
  21. # 获取当前时间
  22. now = context.now
  23. # 获取上一个交易日
  24. last_day = get_previous_trading_date(exchange='SHSE', date=now)
  25. # 获取沪深300成份股
  26. context.stock300 = get_history_constituents(index='SHSE.000300', start_date=last_day,
  27. end_date=last_day)[0]['constituents'].keys()
  28. # 获取当天有交易的股票
  29. not_suspended_info = get_history_instruments(symbols=context.stock300, start_date=now, end_date=now)
  30. not_suspended_symbols = [item['symbol'] for item in not_suspended_info if not item['is_suspended']]
  31. trade_symbols = []
  32. if not not_suspended_symbols:
  33. print('没有当日交易的待选股票')
  34. return
  35. for stock in not_suspended_symbols:
  36. recent_data = history_n(symbol=stock, frequency='1d', count=context.count, fields='pre_close,open',
  37. fill_missing='Last', adjust=ADJUST_PREV, end_time=now, df=True)
  38. diff = recent_data['open'] - recent_data['pre_close']
  39. # 获取累计天数超过阙值的标的池.并剔除当天没有交易的股票
  40. if len(diff[diff > 0]) >= context.count_bench:
  41. trade_symbols.append(stock)
  42. print('本次股票池有股票数目: ', len(trade_symbols))
  43. # 计算权重
  44. percent = 1.0 / len(trade_symbols) * context.ratio
  45. # 获取当前所有仓位
  46. positions = context.account().positions()
  47. # 如标的池有仓位,平不在标的池的仓位
  48. for position in positions:
  49. symbol = position['symbol']
  50. if symbol not in trade_symbols:
  51. order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market,
  52. position_side=PositionSide_Long)
  53. print('市价单平不在标的池的', symbol)
  54. # 对标的池进行操作
  55. for symbol in trade_symbols:
  56. order_target_percent(symbol=symbol, percent=percent, order_type=OrderType_Market,
  57. position_side=PositionSide_Long)
  58. print(symbol, '以市价单调整至权重', percent)
  59. if __name__ == '__main__':
  60. '''
  61. strategy_id策略ID,由系统生成
  62. filename文件名,请与本文件名保持一致
  63. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST
  64. token绑定计算机的ID,可在系统设置-密钥管理中生成
  65. backtest_start_time回测开始时间
  66. backtest_end_time回测结束时间
  67. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST
  68. backtest_initial_cash回测初始资金
  69. backtest_commission_ratio回测佣金比例
  70. backtest_slippage_ratio回测滑点比例
  71. '''
  72. run(strategy_id='strategy_id',
  73. filename='main.py',
  74. mode=MODE_BACKTEST,
  75. token='token_id',
  76. backtest_start_time='2017-07-01 08:00:00',
  77. backtest_end_time='2017-10-01 16:00:00',
  78. backtest_adjust=ADJUST_PREV,
  79. backtest_initial_cash=10000000,
  80. backtest_commission_ratio=0.0001,
  81. backtest_slippage_ratio=0.0001)

[集合竞价选股(股票)]

0 篇笔记