[关闭]
@Channelchan 2017-08-08T15:37:19.000000Z 字数 3351 阅读 30678

引擎设置持有时间的策略


  1. #coding=utf-8
  2. # run_code_demo
  3. from rqalpha import run_code
  4. code = """
  5. import rqalpha
  6. from rqalpha.api import *
  7. import pandas as pd
  8. def init(context):
  9. codes = pd.read_excel('C:/Users/small/Desktop/july_first/lflo.xlsx')
  10. codes.index = codes.pop('date')
  11. context.codes = codes
  12. scheduler.run_weekly(find_pool, tradingday=1)
  13. scheduler.run_weekly(buy, tradingday=1)
  14. #设置持有时间,可以设置为五天,十天等。
  15. context.holdperiod = 10
  16. context.stocks = []
  17. context.time = []
  18. def find_pool(context, bar_dict):
  19. try:
  20. codes = context.codes.loc[context.now]
  21. except KeyError:
  22. return
  23. stocks = codes.index[codes == True]
  24. context.stocks = stocks
  25. def handle_bar(context, bar_dict):
  26. hold_period(context, bar_dict)
  27. def buy(context, bar_dict):
  28. pool = context.stocks
  29. if pool is not None:
  30. stocks_len = len(pool)
  31. cur_value = context.portfolio.market_value
  32. for stock in pool:
  33. if cur_value==0:
  34. order_target_percent(stock, 1.0/stocks_len)
  35. buy_time = context.now.replace(tzinfo=None)
  36. context.time = buy_time
  37. def hold_period( context,bar_dict):
  38. for stock in context.portfolio.positions:
  39. buytime=context.time # 获取买入时间
  40. currenttime=context.now.replace(tzinfo=None) # 获取当前时间
  41. print ('buytime='+str(buytime))
  42. print('currenttime='+str(currenttime))
  43. hold_time=(currenttime-buytime).days # 计算持有天数
  44. if hold_time>context.holdperiod:
  45. order_target_percent(stock, 0)
  46. config = {
  47. "base": {
  48. #设置回测开始时间
  49. "start_date": "2016-05-23",
  50. #设置回测结束时间
  51. "end_date": "2017-06-01",
  52. #设置回测的品种与初始资金
  53. "accounts": {'stock':1000000},
  54. #设置基准收益
  55. "benchmark": "000300.XSHG",
  56. },
  57. "extra": {
  58. #查看最详细的日志,'error'只看错误。
  59. "log_level": "verbose",
  60. },
  61. "mod": {
  62. "sys_analyser": {
  63. #保存report至当下文件
  64. "report_save_path": '.',
  65. #启动策略逐行性能分析
  66. "enabled": True,
  67. #打印图形
  68. "plot": True
  69. },
  70. "sys_simulation": {
  71. #是否限制买入涨停板
  72. "price_limit":False,
  73. "enabled": True,
  74. #设置手续费的倍数,默认是10
  75. "commission_multiplier": 0,
  76. #设置滑点
  77. "slippage": 0
  78. }
  79. }
  80. }
  81. run_code(code, config)
  1. #coding=utf-8
  2. import rqalpha
  3. from rqalpha.api import *
  4. import os
  5. import pandas as pd
  6. def init(context):
  7. codes = pd.read_excel('C:/Users/small/Desktop/july_first/lflo.xlsx')
  8. codes.index = codes.pop('date')
  9. context.codes = codes
  10. scheduler.run_weekly(find_pool, tradingday=1)
  11. scheduler.run_weekly(buy, tradingday=1)
  12. #设置持有时间,可以设置为五天,十天等。
  13. context.holdperiod = 10
  14. context.stocks = []
  15. context.time = []
  16. def find_pool(context, bar_dict):
  17. try:
  18. codes = context.codes.loc[context.now]
  19. except KeyError:
  20. return
  21. stocks = codes.index[codes == True]
  22. context.stocks = stocks
  23. def handle_bar(context, bar_dict):
  24. hold_period(context, bar_dict)
  25. def buy(context, bar_dict):
  26. pool = context.stocks
  27. if pool is not None:
  28. stocks_len = len(pool)
  29. cur_value = context.portfolio.market_value
  30. for stock in pool:
  31. if cur_value==0:
  32. order_target_percent(stock, 1.0/stocks_len)
  33. buy_time = context.now.replace(tzinfo=None)
  34. context.time = buy_time
  35. def hold_period( context,bar_dict):
  36. for stock in context.portfolio.positions:
  37. buytime=context.time # 获取买入时间
  38. currenttime=context.now.replace(tzinfo=None) # 获取当前时间
  39. print ('buytime='+str(buytime))
  40. print('currenttime='+str(currenttime))
  41. hold_time=(currenttime-buytime).days # 计算持有天数
  42. if hold_time>context.holdperiod:
  43. order_target_percent(stock, 0)
  44. config = {
  45. "base": {
  46. #设置回测开始时间
  47. "start_date": "2016-05-23",
  48. #设置回测结束时间
  49. "end_date": "2017-06-01",
  50. #设置回测的品种与初始资金
  51. "accounts": {'stock':1000000},
  52. #设置基准收益
  53. "benchmark": "000300.XSHG",
  54. #运行当下策略文件
  55. "strategy_file_path": os.path.abspath(__file__)
  56. },
  57. "extra": {
  58. #查看最详细的日志,'error'只看错误。
  59. "log_level": "verbose",
  60. },
  61. "mod": {
  62. "sys_analyser": {
  63. #保存report至当下文件
  64. "report_save_path": '.',
  65. #启动策略逐行性能分析
  66. "enabled": True,
  67. #打印图形
  68. "plot": True
  69. },
  70. "sys_simulation": {
  71. #是否限制买入涨停板
  72. "price_limit":False,
  73. "enabled": True,
  74. #设置手续费的倍数,默认是10
  75. "commission_multiplier": 0,
  76. #设置滑点
  77. "slippage": 0
  78. }
  79. }
  80. }
  81. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注