zoukankan      html  css  js  c++  java
  • 拓端tecdat|R语言基于Garch波动率预测的区制转移交易策略

    原文链接:http://tecdat.cn/?p=17526

    本文提出了一种算法,可以根据市场波动性在均值回归和趋势跟随策略之间进行切换。研究了两种模型:一种使用历史波动率,另一种使用Garch(1,1)波动率预测。均值回归策略使用RSI(2)建模:RSI(2)时为Long,否则为Short。趋势跟踪策略以SMA 50/200交叉建模:当SMA(50)> SMA(200)时为Long,否则为Short。

    以下代码从Yahoo Fiance加载历史价格,并比较买入和持有,均值回归和趋势跟踪策略的效果:

    1.  
       
    2.  
          #*****************************************************************
    3.  
          # 加载历史数据
    4.  
          #******************************************************************
    5.  
          load.packages('quantmod')  
    6.  
          tickers = 'SPY'
    7.  
       
    8.  
          data <- new.env()
    9.  
          getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
    10.  
             
    11.  
          #*****************************************************************
    12.  
          # 代码策略
    13.  
          #******************************************************************
    14.  
          prices = d
    15.  
          # 购买并持有
    16.  
          data$weight[
    17.  
               
    18.  
          # 均值回归(MR)策略
    19.  
          rsi2 = bt.apply.ma
    20.  
               
    21.  
          # 趋势跟踪(TF)策略
    22.  
          sma.short = apply.matrix(prices, SMA, 50)
    23.  
          sma.long = apply.matrix(prices, SMA, 200)
    24.  
          data$weight[] = NA
    25.  
       
    26.  
          #*****************************************************************
    27.  
          # 创建报告
    28.  
          #******************************************************************
    29.  
          plotb
       

    接下来,让我们创建一个基于历史市场波动性在均值回归和趋势跟随策略之间切换的策略。

    1.  
      #*****************************************************************
    2.  
      #根据历史市场波动情况进行区制转移
    3.  
      #使用252天的回溯期按百分比对当前波动率进行分类
    4.  
      #******************************************************************
    5.  
       
    6.  
       
    7.  
      # #区制转移历史
    8.  
      data$weight[] = NA
    9.  
          data$weight[] = iif(vol.rank > 0.5,
    10.  
                      iif(rsi2 <
    11.  
       
    12.  
      l=capital, trade.summary=T)
    13.  
       
    14.  
      #*****************************************************************
    15.  
      #创建报告
    16.  
      #******************************************************************
    17.  
      report(regime, mr)
       

    接下来,我们创建一个GARCH(1,1)波动率预测。

    有一些R包可以适合GARCH模型。我将考虑tseries软件包中的garch函数和fGarch软件包中的garchFit函数。tseries软件包中的garch函数速度很快,但并不总能找到解决方案。fGarch软件包中的garchFit函数速度较慢,但​​收敛得更加一致。为了演示garch函数和garchFit函数之间的速度差异,我创建了一个简单的基准测试:

    1.  
      #*****************************************************************
    2.  
      # 基准化Garch算法
    3.  
      #******************************************************************
    4.  
      temp = garchSim(n=252)
    5.  
       
    6.  
      test1 <- function() {
    7.  
          fit1=garch(temp, order = c(1, 1), control = garch.control(trace = F))
    8.  
      }
    9.  
      test2 <- function() {
    10.  
          fit2=garchFit(~ garch(1,1), data = temp, include.mean=FALSE, trace=F)
    11.  
      }
    12.  
               
    13.  
      benchmark(
    14.  
          test1(),
    15.  
          test2(),
    16.  
          columns=
    17.  
      )

    garchFit函数平均比garch函数慢6倍。因此,要预测波动率,我将尝试在找到解决方案时使用garch函数,否则将尝试使用garchFit函数。

    1.  
      #*****************************************************************
    2.  
      #使用Garch预测波动率
    3.  
      #来自tseries的garch速度很快,但是并不能始终收敛
    4.  
      #fGarch的garchFit速度较慢,但收敛一致
    5.  
      #******************************************************************
    6.  
      load.packages('tseries,fGarch')
    7.  
               
    8.  
      # Sigma[t]^2 = w + a* Sigma[t-1]^2 + b*r[t-1]^2
    9.  
      garch.predict.one.day <- function(fit, r1) {
    10.  
          hl = tail( fitted(fit)[,1] ,1)    
    11.  
       
    12.  
      # 与预测相同( fit, n.ahead=1, doplot=F)[3]
    13.  
      garchFit.predict.one.day <- funct
    14.  
       
    15.  
      garch.vol = NA * hist.vol
    16.  
      for( i in (252+1):nperiods
    17.  
       
    18.  
      err ) FALSE, warning=function( warn ) FALSE )
    19.  
                           
    20.  
          if( !is.logical( fit
    21.  
       
    22.  
      garch(1,1), data = temp, include.mean=FALSE, trace=F),
    23.  
                          error=function( err ) FALSE, warning=function( warn ) FALSE )
    24.  
                           
    25.  
              if( !is.logical(
    26.  
       
    27.  
       
     

    现在,让我们创建一个基于GARCH(1,1)波动率预测在均值回归和趋势跟踪策略之间切换的策略。

    1.  
      #*****************************************************************
    2.  
      # 使用Garch进行区制转移
    3.  
      #******************************************************************        
    4.  
      vol.rank = percent.rank(SMA(percent.rank(garch.v
    5.  
      # 区制转移Garch
    6.  
      data$weight[] = NA
    7.  
          data$weight[] = iif(vol.rank > 0.5,
    8.  
                      iif(rsi2 < 50, 1, -1),
    9.  
                      iif(sma.short > sma.l
    10.  
       
    11.  
       
    12.  
       
    13.  
      #*****************************************************************
    14.  
      #创建报告
    15.  
      #******************************************************************
    16.  
      report(regime.switching)
     

    使用GARCH(1,1)波动率预测的转换策略要比使用历史波动率的策略稍好。 

    您可以采用多种不同的方法将预测合并到模型和交易策略中。R有非常丰富的软件包集,用于建模和预测时间序列。 


    最受欢迎的见解

    1.HAR-RV-J与递归神经网络(RNN)混合模型预测和交易大型股票指数的高频波动率

    2.R语言中基于混合数据抽样(MIDAS)回归的HAR-RV模型预测GDP增长

    3.波动率的实现:ARCH模型与HAR-RV模型

    4.R语言ARMA-EGARCH模型、集成预测算法对SPX实际波动率进行预测

    5.GARCH(1,1),MA以及历史模拟法的VaR比较

    6.R语言多元COPULA GARCH 模型时间序列预测

    7.R语言基于ARMA-GARCH过程的VAR拟合和预测

    8.matlab预测ARMA-GARCH 条件均值和方差模型

    9.R语言对S&P500股票指数进行ARIMA + GARCH交易策略

     

    ▍关注我们 【大数据部落】第三方数据服务提供商,提供全面的统计分析与数据挖掘咨询服务,为客户定制个性化的数据解决方案与行业报告等。 ▍咨询链接:http://y0.cn/teradat ▍联系邮箱:3025393450@qq.com
  • 相关阅读:
    spring boot-17.RabbitMQ
    spring boot-16.使用redis做缓存
    spring boot-15.缓存
    spring boot-14.集成MyBatis
    spring boot-13.数据访问
    docker 安装完mysql 后客户端无法访问
    【python】string functions
    【转】澄清P问题、NP问题、NPC问题
    ubuntu中使用gensim+word2vec[备忘]
    ubuntu熟悉过程中遇到一些小问题记录一下
  • 原文地址:https://www.cnblogs.com/tecdat/p/13891101.html
Copyright © 2011-2022 走看看