[关闭]
@Channelchan 2018-01-06T14:52:03.000000Z 字数 1000 阅读 84932

7. 仓位记录

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

output_1_0.png-80.7kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注