用 Python 实现随机相对强弱指数 StochRSI

开发 后端
随机相对强弱指数简称为StochRSI,是一种技术分析指标,用于确定资产是否处于超买或超卖状态,也用于确定当前市场的态势。

[[424742]]

 Python中文社区(ID:python-china)

随机相对强弱指数简称为StochRSI,是一种技术分析指标,用于确定资产是否处于超买或超卖状态,也用于确定当前市场的态势。顾名思义,StochRSI是标准相对强弱指数(RSI)的衍生,因此被视为是一种能够衡量指数的指数。它是一种振荡器,在中心线的上方和下方波动。

StochRSI最初是在1994年由Stanley Kroll和Tushar Chande撰写的题为《The NewTechnical Trader》的书中描述。它经常被股票交易者使用。

StochRSI如何运作?

通过应用随机振荡器生成公式,从标准RSI生成StochRSI。其生成结果是单个数字评级,围绕中心线(0.5)在0-1的值域范围内上下摆动。但是,StochRSI的修改版本将结果乘以100,因此该值是介于0和100之间而不是0和1之间。通常还会参考3天内的简单移动平均线(SMA)以及StochRSI趋势,作为信号线,旨在降低虚假信号交易的风险。

标准随机震荡指数公式取决于资产的收盘价以及设定周期内的最高价和最低价。但是,当使用公式计算StochRSI时,它直接使用RSI数据(不考虑价格)。

Stoch RSI = (Current RSI - Lowest RSI)/(Highest RSI - Lowest RSI)

与标准RSI一样,StochRSI使用的最常见时间周期为14。StochRSI计算中涉及的14个周期基于图表时间范围。因此,每日图表会显示过去14天(K线图),每小时图表会显示过去14小时生成的StochRSI。

周期可以设置为几天、几小时甚至几分钟,并且它们的使用方式也因交易者而异(根据他们的情况和策略而定)。还可以向上或向下调整周期数,以确定长期或短期趋势。将周期值设置为20,是StochRSI指标一个相当受欢迎的选择。

如上所述,某些StochRSI图表模式指定的范围值为0到100而不是0到1。在这些图表中,中心线为50而不是0.5。因此,通常在0.8处出现的超买信号将表示为80,而超卖信号表示为20而不是0.2。具有0-100设置的图表可能看起来略有不同,但实际原理解释是基本相同的。

如何使用StochRSI?

StochRSI指数如果出现在其范围的上限和下限附近,此时的意义是最重大的。因此,该指标的主要用途是确定潜在的买入和卖出点,以及价格发生的逆转。因此,0.2或以下的数值,会表明资产可能发生超卖,而0.8或以上的数值则表明该资产可能会发生超买。

此外,更接近中心线的数值也可以为交易者提供有关市场趋势的信息。例如,当中心线作为支撑线并且StochRSI线稳定移动到0.5以上时,尤其是数值趋近于0.8,则可能表明其继续看涨或呈上升趋势。同样,当数值始终低于0.5,趋近于0.2时,则表明下跌或呈下降趋势趋势。

我们将通过 Python 中的回测来介绍 RSI 和 StochRSI 这两种方法。

基于均值回归的StochRSI 策略

最常见的 StochRSI 策略基于均值回归。与 RSI 一样,StochRSI 通常使用 80 来表示做空的超买水平,使用 20 来表示要买入的超卖水平。此外,14 天的回顾和平滑期很常见。出于我们的目的,我们将坚持使用这些标准值。

现在编写代码,让我们在 Python 中导入一些标准包。 

  1. import numpy as np  
  2. import pandas as pd  
  3. import matplotlib.pyplot as plt  
  4. import yfinance as yf 

接下来,我们将构建一个函数来计算我们的指标。我们将其称为 calcStochRSI(),它将依靠一些函数来计算 RSI 和随机振荡器,以获得我们选择的指标。 

  1. def calcRSI(data, P=14):  
  2.   # Calculate gains and losses  
  3.   data['diff_close'] = data['Close'] - data['Close'].shift(1)  
  4.   data['gain'] = np.where(data['diff_close']>0,  
  5.     data['diff_close'], 0)  
  6.   data['loss'] = np.where(data['diff_close']<0,   
  7.     np.abs(data['diff_close']), 0)  
  8.   # Get initial values  
  9.   data[['init_avg_gain', 'init_avg_loss']] = data[  
  10.     ['gain', 'loss']].rolling(P)   
  11.   # Calculate smoothed avg gains and losses for all t > P  
  12.   avg_gain = np.zeros(len(data))  
  13.   avg_loss = np.zeros(len(data)) 
  14.   for i, _row in enumerate(data.iterrows()):  
  15.     row = _row[1]  
  16.     if i < P - 1:  
  17.       last_row = row.copy()  
  18.       continue  
  19.     elif i == P-1:  
  20.       avg_gain[i] += row['init_avg_gain']  
  21.       avg_loss[i] += row['init_avg_loss']  
  22.     else:  
  23.       avg_gain[i] += ((P - 1) * avg_gain[i] +  
  24.             row['gain']) / P  
  25.       avg_loss[i] += ((P - 1) * avg_loss[i] +  
  26.             row['loss']) / P          
  27.     last_row = row.copy()  
  28.   data['avg_gain'] = avg_gain  
  29.   data['avg_loss'] = avg_loss  
  30.   # Calculate RS and RSI  
  31.   data['RS'] = data['avg_gain'] / data['avg_loss']  
  32.   data['RSI'] = 100 - 100 / (1 + data['RS'])  
  33.   return data  
  34. def calcStochOscillator(data):  
  35.   data['low_N'] = data['RSI'].rolling(N).min()  
  36.   data['high_N'] = data['RSI'].rolling(N).max()  
  37.   data['StochRSI'] = 100 * (data['RSI'] - data['low_N']) / \  
  38.     (data['high_N'] - data['low_N'])  
  39.   return data  
  40. def calcStochRSI(data, P=14N=14):  
  41.   data = calcRSI(data)  
  42.   data = calcStochOscillator(data)  
  43.   return data  
  44. def calcReturns(df):  
  45.   # Helper function to avoid repeating too much code  
  46.   df['returns'] = df['Close'] / df['Close'].shift(1)  
  47.   df['log_returns'] = np.log(df['returns'])  
  48.   df['strat_returns'] = df['position'].shift(1) * df['returns']  
  49.   df['strat_log_returns'] = df['position'].shift(1) * df['log_returns']  
  50.   df['cum_returns'] = np.exp(df['log_returns'].cumsum()) - 1  
  51.   df['strat_cum_returns'] = np.exp(df['strat_log_returns'].cumsum()) - 1  
  52.   df['peak'] = df['cum_returns'].cummax()  
  53.   df['strat_peak'] = df['strat_cum_returns'].cummax()  
  54.   return df 

有了这些功能,我们只需要为我们的策略构建逻辑就可以了。还要注意,我们有一个名为 calcReturns 的辅助函数,我们可以快速将其应用于回测的结果以从中获取所有返回值。

这意味着回归模型将在 StochRSI 高于 80 时做空或卖出,并在低于 20 时买入。 

  1. def StochRSIReversionStrategy(data, P=14N=14short_level=80,   
  2.   buy_level=20shorts=True):  
  3.   '''Buys when the StochRSI is oversold and sells when it's overbought'''  
  4.   df = calcStochRSI(data, P, N)  
  5.   df['position'] = np  
  6.   df['position'] = np.where(df['StochRSI']<buy_level, 1, df['position'])  
  7.   if shorts:  
  8.     df['position'] = np.where(df['StochRSI']>short_level, -1, df['position'])  
  9.   else:  
  10.     df['position'] = np.where(df['StochRSI']>short_level, 0, df['position'])  
  11.   df['position'] = df['position'].ffill()  
  12.   return calcReturns(df)  
  13. table = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')  
  14. df = table[0]  
  15. syms = df['Symbol']  
  16. # Sample symbols  
  17. ticker = np.random.choice(syms.values)  
  18. ticker = "BSX"  
  19. print(f"Ticker Symbol: {ticker}")  
  20. start = '2000-01-01'  
  21. end = '2020-12-31'  
  22. # Get Data  
  23. yfyfObj = yf.Ticker(ticker)  
  24. data = yfObj.history(startstart=start, endend=end)  
  25. data.drop(['Open', 'High', 'Low', 'Volume', 'Dividends',   
  26.     'Stock Splits'], inplace=Trueaxis=1 
  27. # Run test  
  28. df_rev = StochRSIReversionStrategy(data.copy())  
  29. # Plot results  
  30. colors = plt.rcParams['axes.prop_cycle'].by_key()['color']  
  31. fig, ax = plt.subplots(2, figsize=(12, 8))  
  32. ax[0].plot(df_rev['strat_cum_returns']*100, label='Mean Reversion' 
  33. ax[0].plot(df_rev['cum_returns']*100, label='Buy and Hold' 
  34. ax[0].set_ylabel('Returns (%)')  
  35. ax[0].set_title('Cumulative Returns for Mean Reversion and' +  
  36.                 f' Buy and Hold Strategies for {ticker}')  
  37. ax[0].legend(bbox_to_anchor=[1, 0.6])  
  38. ax[1].plot(df_rev['StochRSI'], label='StochRSI'linewidth=0.5)  
  39. ax[1].plot(df_rev['RSI'], label='RSI'linewidth=1 
  40. ax[1].axhline(80, label='Over Bought'color=colors[1], linestyle=':' 
  41. ax[1].axhline(20, label='Over Sold'color=colors[2], linestyle=':' 
  42. ax[1].axhline(50, label='Centerline'color='k'linestyle=':' 
  43. ax[1].set_ylabel('Stochastic RSI')  
  44. ax[1].set_xlabel('Date')  
  45. ax[1].set_title(f'Stochastic RSI for {ticker}')  
  46. ax[1].legend(bbox_to_anchor=[1, 0.75])  
  47. plt.tight_layout()  
  48. plt.show() 

在我们研究的 21 年期间,均值回归策略击败了Boston Scientific(BSX)的买入和持有策略,回报率为 28 倍,而后者为 2 倍。

在第二个图中显示了 StochRSI 和一些关键指标。我还添加了 RSI 以与更不稳定的 StochRSI 进行比较。这导致交易频繁,如果您的账户较小且交易成本相对较高,这可能会严重影响您的实际回报。我们只是在一个工具上运行它,所以最终进行了 443 笔交易,或者每 12 天交易一次,这看起来并不多。但是,如果我们要使用该指标管理适当的工具组合并频繁进行交易,我们每天可能会进出多笔交易,交易成本会变得很高。 

  1. # Get trades  
  2. diff = df_rev['position'].diff().dropna()  
  3. trade_idx = diff.index[np.where(diff!=0)]  
  4. fig, ax = plt.subplots(figsize=(12, 8))  
  5. ax.plot(df_rev['Close'], linewidth=1label=f'{ticker}' 
  6. ax.scatter(trade_idx, df_rev[trade_idx]['Close'], c=colors[1],   
  7.            marker='^'label='Trade' 
  8. ax.set_ylabel('Price')  
  9. ax.set_title(f'{ticker} Price Chart and Trades for' +  
  10.              'StochRSI Mean Reversion Strategy')  
  11. ax.legend()  
  12. plt.show() 

要查看整体策略的一些关键指标,让我们看看使用以下 getStratStats 函数。 

  1. def getStratStats(log_returns: pd.Series, risk_free_rate: float = 0.02):  
  2.   stats = {}  
  3.   # Total Returns  
  4.   stats['tot_returns'] = np.exp(log_returns.sum()) - 1  
  5.   # Mean Annual Returns  
  6.   stats['annual_returns'] = np.exp(log_returns.mean() * 252) - 1  
  7.   # Annual Volatility  
  8.   stats['annual_volatility'] = log_returns * np.sqrt(252)  
  9.   # Sortino Ratio  
  10.   annualized_downside = log_returns.loc[log_returns<0].std() * np.sqrt(252)  
  11.   stats['sortino_ratio'] = (stats['annual_returns'] - risk_free_rate) \  
  12.     / annualized_downside  
  13.   # Sharpe Ratio  
  14.   stats['sharpe_ratio'] = (stats['annual_returns'] - risk_free_rate) \  
  15.     / stats['annual_volatility']  
  16.   # Max Drawdown  
  17.   cum_returns = log_returns.cumsum() - 1  
  18.   peak = cum_returns.cummax()  
  19.   drawdown = peak - cum_returns  
  20.   stats['max_drawdown'] = drawdown.max()  
  21.   # Max Drawdown Duration  
  22.   strat_dd = drawdown[drawdown==0]  
  23.   strat_ddstrat_dd_diff = strat_dd.index[1:] - strat_dd.index[:-1]  
  24.   strat_dd_days = strat_dd_diff.map(lambda x: x.days)  
  25.   strat_dd_days = np.hstack([strat_dd_days,   
  26.       (drawdown.index[-1] - strat_dd.index[-1]).days])  
  27.   stats['max_drawdown_duration'] = strat_dd_days.max()  
  28.   return stats  
  29. rev_stats = getStratStats(df_rev['strat_log_returns'])  
  30. bh_stats = getStratStats(df_rev['log_returns'])  
  31. pd.concat([pd.DataFrame(rev_stats, index=['Mean Reversion']),  
  32.            pd.DataFrame(bh_stats, index=['Buy and Hold'])]) 

在这里,我们看到该策略的回报率为 28 倍,而基础资产的年度波动率大致相同。此外,根据 Sortino 和 Sharpe Ratios 衡量,我们有更好的风险调整回报。

在 2020 年的新冠疫情中,我们确实看到了均值回归策略的潜在问题之一。该策略的总回报大幅下降,因为该策略的定位是向上回归,但市场继续低迷,该模型只是保持不变 . 它恢复了其中的一部分,但在这次测试中从未达到过疫情之前的高点。正确使用止损有助于限制这些巨大的损失,并有可能增加整体回报。

StochRSI 和动量策略

我们之前提到的另一个基本策略是使用 StochRSI 作为动量指标。当指标穿过中心线时,我们会根据其方向买入或做空股票。 

  1. def StochRSIMomentumStrategy(data, P=14N=14 
  2.   centerline=50shorts=True):  
  3.   '''  
  4.   Buys when the StochRSI moves above the centerline,   
  5.   sells when it moves below  
  6.   '''  
  7.   df = calcStochRSI(data, P) 
  8.   df['position'] = np.nan  
  9.   df['position'] = np.where(df['StochRSI']>50, 1, df['position'])  
  10.   if shorts: 
  11.      df['position'] = np.where(df['StochRSI']<50, -1, df['position'])  
  12.   else:  
  13.     df['position'] = np.where(df['StochRSI']<50, 0, df['position'])  
  14.   df['position'] = df['position'].ffill()  
  15.   return calcReturns(df) 

运行我们的回测: 

  1. # Run test  
  2. df_mom = StochRSIMomentumStrategy(data.copy())  
  3. # Plot results  
  4. colors = plt.rcParams['axes.prop_cycle'].by_key()['color']  
  5. fig, ax = plt.subplots(2, figsize=(12, 8))  
  6. ax[0].plot(df_mom['strat_cum_returns']*100, label='Momentum' 
  7. ax[0].plot(df_mom['cum_returns']*100, label='Buy and Hold' 
  8. ax[0].set_ylabel('Returns (%)')  
  9. ax[0].set_title('Cumulative Returns for Momentum and' +  
  10.                 f' Buy and Hold Strategies for {ticker}')  
  11. ax[0].legend(bbox_to_anchor=[1, 0.6])  
  12. ax[1].plot(df_mom['StochRSI'], label='StochRSI'linewidth=0.5)  
  13. ax[1].plot(df_mom['RSI'], label='RSI'linewidth=1 
  14. ax[1].axhline(50, label='Centerline'color='k'linestyle=':' 
  15. ax[1].set_ylabel('Stochastic RSI')  
  16. ax[1].set_xlabel('Date')  
  17. ax[1].set_title(f'Stochastic RSI for {ticker}')  
  18. ax[1].legend(bbox_to_anchor=[1, 0.75])  
  19. plt.tight_layout()  
  20. plt.show() 

在这种情况下,我们的动量策略表现非常糟糕,在我们假设的时间段内几乎损失了我们所有的初始投资。

查看我们策略的统计数据,该模型的唯一优势是比买入并持有方法的回撤时间略短。 

  1. mom_stats = getStratStats(df_mom['strat_log_returns'])  
  2. bh_stats = getStratStats(df_mom['log_returns'])  
  3. pd.concat([pd.DataFrame(mom_stats, index=['Momentum']),  
  4.            pd.DataFrame(rev_stats, index=['Mean Reversion']),  
  5.            pd.DataFrame(bh_stats, index=['Buy and Hold'])]) 

这并不意味着StochRSI 不适合此类应用。一次糟糕的回测并不意味着该策略毫无价值。相反,一个很好的回测并不意味着你有一些你应该立即开始交易的东西。我们需要与其他指标结合使用以改善结果。 

 

责任编辑:庞桂玉 来源: Python中文社区
相关推荐

2021-09-13 11:59:30

Python股票代码

2017-08-28 18:41:34

PythonLogistic回归随机梯度下降

2016-09-12 14:05:27

PythonPython解释器Web

2009-06-29 17:10:24

什么是JSP

2021-07-29 13:06:29

Python机器学习编程语言

2019-01-24 09:00:00

PythonAutoML机器学习

2021-03-01 08:33:39

插件库弱符号程序

2022-01-26 07:25:09

PythonRSA加解密

2021-11-01 11:15:28

Python资产代码

2017-03-24 08:10:01

微信指数公关媒体

2010-06-02 09:01:43

MySQL随机

2010-11-25 14:52:35

MySQL随机查询

2022-03-03 10:49:46

Python自动追踪代码

2023-03-09 08:12:08

免登录实Python脚本

2022-07-27 10:39:27

Python打包工具

2022-08-11 08:03:43

队列

2010-03-23 09:47:38

Python随机数Python随机字符串

2019-11-06 10:56:59

Python数据分析TGI

2022-07-27 08:24:44

数据库RTOSQL

2020-06-16 16:25:05

C++JavaPython
点赞
收藏

51CTO技术栈公众号