[关闭]
@Channelchan 2018-01-06T14:03:25.000000Z 字数 2846 阅读 85594

相对强弱市场

强者越强,弱者越弱

目录

  1. 什么是相对强弱?
  2. 怎么计算相对强弱?
  3. 如何用图形展示相对强弱?
  4. 如何用相对强弱来编写策略?

什么是相对强弱?

一段时间内某股票和本行业的股票或整个市场的比较,即对该股票市场表现的计量。

怎么计算相对强弱?

RS = Stock/Index

MOM_RS = Momentum(RS)

MOM_MOM = Momentum(MOM_RS)

如何用图形展示相对强弱?

  1. from jaqs.data import DataView
  2. from jaqs.data import RemoteDataService
  3. import os
  4. import numpy as np
  5. import talib as ta
  6. import pandas as pd
  7. from datetime import datetime
  8. import matplotlib.pyplot as plt
  9. import warnings
  10. warnings.filterwarnings("ignore")
  11. dataview_folder = 'JAQS_Data/hs300'
  12. dv = DataView()
  13. dv.load_dataview(dataview_folder)
Dataview loaded successfully.
  1. def change_index(df):
  2. df.index = pd.Index(map(lambda x: datetime.strptime(str(x),"%Y%m%d") , df.index))
  3. return df
  4. stock = change_index(dv.get_ts('close_adj').loc[20170105:])
  5. hs300 = change_index(dv.data_benchmark.loc[20170105:])
  1. RS = stock['600036.SH']/hs300.close
  2. RS = RS.dropna()
  3. print (RS.tail())
2017-12-18    0.009375
2017-12-19    0.009595
2017-12-20    0.009642
2017-12-21    0.009609
2017-12-22    0.009527
dtype: float64
  1. #Momentum_RS
  2. import talib as ta
  3. MOM_RS = ta.ROCR100(RS.values, 20)
  4. MOM_MOM = ta.ROCR100(MOM_RS, 20)
  5. data_s = stock['600036.SH']
  6. data1 = pd.Series(MOM_RS, index=RS.index)
  7. data2 = pd.Series(MOM_MOM, index=RS.index)
  8. data = pd.concat([data_s, RS, data1, data2], axis=1)
  9. data.columns = ['close', 'RS', 'MOM_RS', 'MOM_MOM']
  10. print (data.tail())
                close        RS      MOM_RS    MOM_MOM
2017-12-18  37.360561  0.009375   96.485483  88.288186
2017-12-19  38.718887  0.009595  101.076718  94.407043
2017-12-20  38.863951  0.009642  100.035532  92.229752
2017-12-21  39.088141  0.009609   99.639105  90.583452
2017-12-22  38.626574  0.009527   98.532208  93.897203
  1. import matplotlib.pyplot as plt
  2. plt.figure(figsize=(15,7))
  3. plt.plot(data.MOM_RS.tail(20).values, data.MOM_MOM.tail(20).values)
  4. plt.axhline(100,alpha=0.3)
  5. plt.axvline(100,alpha=0.3)
  6. X=data['MOM_RS'].iloc[-1]
  7. Y=data['MOM_MOM'].iloc[-1]
  8. plt.scatter(X,Y,color='r', s=100)
  9. plt.show()

output_9_0.png-28.9kB

如何用相对强弱来编写策略?

买入时机:

第一象限:(MOM_RS>100, MOM_MOM>100)

第四象限:(MOM_RS< 100, MOM_MOM >100)

卖出时机

第二象限:(MOM_RS > 100, MOM_MOM < 100)

第三象限:(MOM_RS< 100, MOM_MOM < 100)

  1. #Relative_Strength
  2. import rqalpha
  3. from rqalpha.api import *
  4. import talib
  5. def init(context):
  6. context.s1 = "000001.XSHE"
  7. context.index = "000300.XSHG"
  8. context.PERIOD = 50
  9. def handle_bar(context, bar_dict):
  10. price = history_bars(context.s1, context.PERIOD*3, '1d', 'close')
  11. index = history_bars(context.index, context.PERIOD*3, '1d', 'close')
  12. if len(price)==len(index):
  13. RS = price/index
  14. MOM = talib.ROCR100(RS, context.PERIOD)
  15. if len(MOM)>context.PERIOD:
  16. MOM_MOM = talib.ROCR100(MOM, context.PERIOD)
  17. cur_position = context.portfolio.positions[context.s1].quantity
  18. shares = context.portfolio.cash/bar_dict[context.s1].close
  19. if (MOM_MOM[-1]<100) and (MOM_MOM[-2]>100):
  20. order_target_value(context.s1, 0)
  21. if (MOM_MOM[-1]>100) and (MOM_MOM[-2]<100) and (cur_position==0):
  22. order_shares(context.s1, shares)
  23. config = {
  24. "base": {
  25. "start_date": "2010-06-01",
  26. "end_date": "2017-12-30",
  27. "accounts": {'stock':1000000},
  28. "benchmark": "000001.XSHE"
  29. },
  30. "extra": {
  31. "log_level": "error",
  32. },
  33. "mod": {
  34. "sys_analyser": {
  35. "enabled": True,
  36. "plot": True
  37. }
  38. }
  39. }
  40. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)

output_11_0.png-71.1kB

作业

根据以上代码做出其他象限进场的策略

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