RSI指标策略

来源:https://uqer.io/community/share/549ccfd2f9f06c4bb886323d

策略思路

  • 使用talib中的RSI函数计算每只股票过去20天的rsi
  • 当rsi低于30是买入,高于70时卖出
  • 每只股票仓位最多不超过总资金的10%
  1. import talib as ta
  2. start = '2011-12-01'
  3. end = '2015-04-01'
  4. benchmark = 'SH50'
  5. universe = set_universe('SH50')
  6. capital_base = 5000000
  7. longest_history = 21
  8. def initialize(account):
  9. account.lower_rsi = 30
  10. account.upper_rsi = 70
  11. def handle_data(account):
  12. all_close_prices = account.get_attribute_history('closePrice', longest_history)
  13. rsi, c_price, c_amount = {}, {}, {}
  14. for stock in account.universe:
  15. rsi[stock] = ta.RSI(all_close_prices[stock], longest_history-1)[-1]
  16. c_amount[stock] = account.secpos.get(stock, 0)
  17. for stock in account.universe:
  18. max_amount = int(0.1 * account.referencePortfolioValue / account.referencePrice[stock])
  19. amount = min(int(25000./account.referencePrice[stock]), max_amount - c_amount[stock])
  20. if (rsi[stock] < account.lower_rsi) and (c_amount[stock] < max_amount):
  21. order(stock, amount)
  22. elif (rsi[stock] > account.upper_rsi) and (c_amount[stock] > 0):
  23. order_to(stock, 0)

RSI指标策略 - 图1