[关闭]
@Channelchan 2017-08-08T15:34:27.000000Z 字数 1023 阅读 40794

7. 仓位记录


将持仓账户信息用图显示

  1. #coding=utf-8
  2. import rqalpha
  3. from rqalpha.api import *
  4. import os
  5. def init(context):
  6. context.s1 = '000001.XSHE'
  7. context.s2 = '600036.XSHG'
  8. def handle_bar(context, bar_dict):
  9. record(context, bar_dict)
  10. if context.s1 not in context.portfolio.positions:
  11. order_target_percent(context.s1, 0.5)
  12. if context.s2 not in context.portfolio.positions:
  13. order_target_percent(context.s2, 0.2)
  14. #记录当前已占用的资金
  15. def record(context, bar_dict):
  16. pos_s1 = context.portfolio.positions[context.s1].quantity
  17. pos_s2 = context.portfolio.positions[context.s2].quantity
  18. price_s1 = context.portfolio.positions[context.s1].avg_price
  19. price_s2 = context.portfolio.positions[context.s2].avg_price
  20. capital = pos_s1*price_s1+pos_s2*price_s2
  21. plot("capital", capital)
  22. config = {
  23. "base": {
  24. "start_date": "2017-01-03",
  25. "end_date": "2017-06-01",
  26. "accounts": {'stock':1000000},
  27. "benchmark": "000300.XSHG",
  28. "strategy_file_path": os.path.abspath(__file__)
  29. },
  30. "extra": {
  31. "log_level": "verbose",
  32. },
  33. "mod": {
  34. "sys_analyser": {
  35. "report_save_path": '.',
  36. "enabled": True,
  37. "plot": True
  38. }
  39. }
  40. }
  41. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注