[关闭]
@Channelchan 2018-11-12T14:16:17.000000Z 字数 4222 阅读 37324

仓位控制


等仓三次进场

1/3,1/3,1/3

  1. import rqalpha
  2. from rqalpha.api import *
  3. import talib
  4. def init(context):
  5. context.s1 = "000001.XSHE"
  6. context.SHORTPERIOD = 10
  7. context.LONGPERIOD = 30
  8. context.time = 0
  9. def handle_bar(context, bar_dict):
  10. prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')
  11. short_avg = talib.SMA(prices, context.SHORTPERIOD)
  12. long_avg = talib.SMA(prices, context.LONGPERIOD)
  13. cur_position = context.portfolio.positions[context.s1].quantity
  14. shares = context.portfolio.cash/bar_dict[context.s1].close
  15. if short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:
  16. order_target_value(context.s1, 0)
  17. context.time=0
  18. if short_avg[-1] - long_avg[-1] > 0 and short_avg[-2] - long_avg[-2] < 0 and context.time==0:
  19. order_shares(context.s1, shares/3)
  20. context.time=1
  21. elif context.time == 1:
  22. order_shares(context.s1, shares/3)
  23. context.time=2
  24. elif context.time == 2:
  25. order_shares(context.s1, shares/3)
  26. context.time=3
  27. config = {
  28. "base": {
  29. "start_date": "2000-06-01",
  30. "end_date": "2016-12-01",
  31. "accounts": {'stock': 1000000},
  32. "benchmark": "000300.XSHG"
  33. # "strategy_file_path": os.path.abspath(__file__)
  34. },
  35. "extra": {
  36. "log_level": "verbose",
  37. },
  38. "mod": {
  39. "sys_analyser": {
  40. "report_save_path": '.',
  41. "enabled": True,
  42. "plot": True
  43. }
  44. }
  45. }
  46. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)

5/3/2三次进场

  1. import rqalpha
  2. from rqalpha.api import *
  3. import talib
  4. def init(context):
  5. context.s1 = "000001.XSHE"
  6. context.SHORTPERIOD = 10
  7. context.LONGPERIOD = 30
  8. context.time = 0
  9. def handle_bar(context, bar_dict):
  10. prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')
  11. short_avg = talib.SMA(prices, context.SHORTPERIOD)
  12. long_avg = talib.SMA(prices, context.LONGPERIOD)
  13. cur_position = context.portfolio.positions[context.s1].quantity
  14. shares = context.portfolio.cash/bar_dict[context.s1].close
  15. if short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:
  16. order_target_value(context.s1, 0)
  17. context.time=0
  18. if short_avg[-1] - long_avg[-1] > 0 and short_avg[-2] - long_avg[-2] < 0 and context.time==0:
  19. order_shares(context.s1, shares*0.5)
  20. context.time=1
  21. elif context.time == 1:
  22. order_shares(context.s1, shares*0.3)
  23. context.time=2
  24. elif context.time == 2:
  25. order_shares(context.s1, shares*0.2)
  26. context.time=3
  27. config = {
  28. "base": {
  29. "start_date": "2000-06-01",
  30. "end_date": "2016-12-01",
  31. "accounts": {'stock': 1000000},
  32. "benchmark": "000300.XSHG"
  33. # "strategy_file_path": os.path.abspath(__file__)
  34. },
  35. "extra": {
  36. "log_level": "verbose",
  37. },
  38. "mod": {
  39. "sys_analyser": {
  40. "report_save_path": '.',
  41. "enabled": True,
  42. "plot": True
  43. }
  44. }
  45. }
  46. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)

三进三出

50/30/20/-50/-30/-20

  1. import rqalpha
  2. from rqalpha.api import *
  3. from queue import Queue
  4. import talib
  5. def init(context):
  6. context.s1 = "000001.XSHE"
  7. context.SHORTPERIOD = 10
  8. context.LONGPERIOD = 30
  9. context.pos = [5000, 3000, 2000]
  10. context.queue = Queue()
  11. context.buy=0
  12. context.sell=0
  13. def buy(context):
  14. prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')
  15. short_avg = talib.SMA(prices, context.SHORTPERIOD)
  16. long_avg = talib.SMA(prices, context.LONGPERIOD)
  17. if short_avg[-1] - long_avg[-1] > 0 and short_avg[-2] - long_avg[-2] < 0:
  18. return True
  19. def sell(context):
  20. prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')
  21. short_avg = talib.SMA(prices, context.SHORTPERIOD)
  22. long_avg = talib.SMA(prices, context.LONGPERIOD)
  23. cur_position = context.portfolio.positions[context.s1].quantity
  24. if short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:
  25. return True
  26. def handle_bar(context, bar_dict):
  27. size = context.queue.qsize()
  28. if size == 0:
  29. if buy(context):
  30. context.buy=1
  31. volume = context.pos[size]
  32. ## order volume
  33. order_shares(context.s1, volume)
  34. context.queue.put(volume)
  35. return
  36. if context.buy:
  37. if sell(context):
  38. context.sell=1
  39. context.buy=0
  40. elif size < 3:
  41. volume = context.pos[size]
  42. ## order volume
  43. order_shares(context.s1, volume)
  44. context.queue.put(volume)
  45. if context.sell:
  46. if size > 0:
  47. volume = context.queue.get()
  48. ## sell volume
  49. order_shares(context.s1, -volume)
  50. else:
  51. context.sell=0
  52. config = {
  53. "base": {
  54. "start_date": "2000-06-01",
  55. "end_date": "2016-12-01",
  56. "accounts": {'stock': 1000000},
  57. "benchmark": "000300.XSHG"
  58. # "strategy_file_path": os.path.abspath(__file__)
  59. },
  60. "extra": {
  61. "log_level": "verbose",
  62. },
  63. "mod": {
  64. "sys_analyser": {
  65. "report_save_path": '.',
  66. "enabled": True,
  67. "plot": True
  68. }
  69. }
  70. }
  71. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)

获利加仓

  1. 在新的趋势信号中输入1个仓位。
  2. 在n天之后只有在新高利润的情况下,才能进入另一个仓位。
  3. 在添加了5次加仓之后,停止加仓。

平均成本 Averaging Down

亏损加仓来平均持有成本,这种策略胜率需要很高。


最优f: Investing and Reinvesting: Optimal f


r_w:赢赚比例
r_l: 输赔比例
p: 赢概率p
f: 投注比例

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