[关闭]
@Channelchan 2018-01-06T15:17:04.000000Z 字数 1025 阅读 125144

4. 数据获取

history_bars

数据获取代码

上证股票: .XSHG

深证股票: .XSHE

当前可交易股指期货: get_future_contracts("IF")[0]

  1. import rqalpha
  2. from rqalpha.api import *
  3. def init(context):
  4. context.sh_stock = '600036.XSHG'
  5. context.sz_stock = '000001.XSHE'
  6. context.future = get_future_contracts("IF")[0]
  7. # bar_dirt可读取bar信息
  8. def handle_bar(context, bar_dict):
  9. # history_bars用来读取历史数据
  10. sh_stock = history_bars(context.sh_stock, 1, '1d')
  11. print ('sh_stock:', sh_stock)
  12. print (bar_dict[context.sh_stock].last)
  13. sz_stock = history_bars(context.sz_stock, 1, '1d')
  14. print ('sz_stock:', sz_stock)
  15. future = history_bars(context.future, 1, '1d')
  16. print ('future:', future)
  17. config = {
  18. "base": {
  19. "start_date": "2017-04-03",
  20. "end_date": "2017-04-05",
  21. "accounts": {'stock':1000000, 'future':1000000},
  22. "benchmark": "000300.XSHG"
  23. },
  24. "extra": {
  25. "log_level": "error",
  26. },
  27. "mod": {
  28. "sys_analyser": {
  29. "enabled": True,
  30. "plot": True
  31. }
  32. }
  33. }
  34. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)
sh_stock: [(20170405000000,  19.22,  19.29,  19.01,  19.07,  48602592.)]
19.07
sz_stock: [(20170405000000,  9.16,  9.22,  9.15,  9.21,  49915009.)]
future: [(20170405000000,  3485.,  3511.2,  3471.8,  3507.2,  16966.)]
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注