1

我(作为 R 和编程的绝对初学者)必须用 R 为我的金融论文做一些分析:(目的是用 GBM 模拟数据(股票价格)并运行结果 2 交易策略。在 GBM 内我将不得不“玩”变量“r”和“sigma”(每个有 3 个不同的值,因此有 9 种组合)。每个组合需要在 T=10、N=250 的时间段内模拟 10000 次。要所有这些模拟都必须应用 2 种交易策略 - MACD 和 RSI(在 TTR 包中)。现在我遇到了编写代码的问题 :(

#Geometrical Brownian Motion
Sim <- GBM(x, r, sigma, T, N)
x <-100
r <-0
sigma <-1
T <- 10
N <- 250

#Additional info for RSI-strategy
retSim <- ROC(Sim)
SimRSI <- RSI(Sim, 14,)
SimRSI[is.na(SimRSI)] <- 0

#Create a vector for the results of the inner loop
portfolio <- rep(0:N)
portfolio[1] <- 100

runs <- 10000

#Creating vectors for final results of portfolio and simulation after 10000 runs (only the last value of each of the 10000 simulations and portfolio results of the strategy required)
resultsSimGBM <- rep(0:runs)
resultsRSIr1sig1 <- rep(0:runs)

#orders
buyRSI<-portfolio[i-1]*exp(retSim[i])
holdRSI<-portfolio[i-1]

#Simulation
portfolio[1]<-100
i <- 1
j <- 1

#Second loop
for(j in 0:runs){

  #Simulation GBM

  x <-100
  r <-0
  sigma <-1
  T <- 10
  N <- 250
  Sim <- GBM(x, r, sigma, T, N)
  retSim <- ROC(Sim)
  SimRSI <- RSI(Sim, 14,)
  SimRSI[is.na(SimRSI)] <- 0
  portfolio[1] <- 100

  #First loop
  for(i in 2:length(Sim)){

    #Apply RSI on GBM
    buyRSI<-portfolio[i-1]*exp(retSim[i])
    holdRSI<-portfolio[i-1]

    if(SimRSI[i-1]<50 && SimRSI[i]>50){portfolio[i]=buyRSI}
    if(SimRSI[i-1]>50 && SimRSI[i]<50){portfolio[i]=holdRSI}
    if(SimRSI[i-1]<50 && SimRSI[i]<50){portfolio[i]=holdRSI}
    if(SimRSI[i-1]>50 && SimRSI[i]>50){portfolio[i]=buyRSI}
    i <- i+1
  }
  resultsRSI[j] <- portfolio[N]
  resultsSimGBM[j] <- Sim[N]
  j <- j+1
}

无论如何,这是我到目前为止所拥有的,它似乎有效。但是,在第一个(内部)循环中,我还需要包含第二个策略(直到现在,它被单独列出)如下所示:

#MACD strategy
portfolioMACD[1]<-100
i <- 1
j <- 1

for(j in 0:runs){
  Sim <- BMSim
  retSim <- ROC(Sim)
  SimMACD <- MACD(Sim, 12, 26, 9, myType="EMA")
  DataSimMACD <- data.frame(SimMACD)
  DataSimMACD$macd[is.na(DataSimMACD$macd)] <- 0
  DataSimMACD$signal[is.na(DataSimMACD$signal)] <- 0

for(i in 2:length(Sim)){
  buyMACD<-portfolioMACD[i-1]*exp(retSim[i])
  sellMACD<-portfolioMACD[i-1]
  holdMACD<-portfolioMACD[i-1]*exp(retSim[i])
  if(DataSimMACD$macd[i-1]<DataSimMACD$signal[i-1] && DataSimMACD$macd[i]>DataSimMACD$signal[i]){portfolioMACD[i]=buyMACD}
  if(DataSimMACD$macd[i-1]>DataSimMACD$signal[i-1] && DataSimMACD$macd[i]<DataSimMACD$signal[i]){portfolioMACD[i]=sellMACD}
  if(DataSimMACD$macd[i-1]>DataSimMACD$signal[i-1] && DataSimMACD$macd[i]>DataSimMACD$signal[i]){portfolioMACD[i]=holdMACD}
  if(DataSimMACD$macd[i-1]<DataSimMACD$signal[i-1] && DataSimMACD$macd[i]<DataSimMACD$signal[i]){portfolioMACD[i]=sellMACD}
  if(DataSimMACD$macd[i]==DataSimMACD$signal[i]){portfolioMACD[i]=sellMACD}
  if(DataSimMACD$macd[i-1]==DataSimMACD$signal[i-1] && DataSimMACD$macd[i]!=DataSimMACD$signal[i]){portfolioMACD[i]=buyMACD}
  i <- i+1
}
  resultsMACD[j] <- portfolioMACD[length(Sim)]
  j <- j+1
}

基本上:1-一个布朗运动必须由 2500 个元素组成,两种交易策略都必须分别应用 2-整个过程必须针对变量 r 和 sigma 的 9 个组合中的每一个重复 10000 次(r1sigma1,r1sigma2 , r1sigma3,.....,r3sigma3) (这我还没有包含在我的代码中 - 不知道如何围绕它构建这两个循环......):( 3-最终结果应该是一个 10000x27 矩阵10000 行(用于运行数量)和 27 列(9x GBM,RSI,MACD)仅填充每个模拟的第 2500 个(最终值)(从第 1 点开始)--> 怎么做?

求救!你们中的某个人可以帮我解决这个烂摊子吗?我完全迷路了,这是我的毕业论文-.-

每一次帮助都将受到高度赞扬和深深的感谢!

在此先感谢,并为长篇大论感到抱歉。

来自柏林的欢呼声,安娜 :)

编辑和另一个简化的例子

    library(sde)

    #Vectors for results
    Returns <- rep(0:N)
    LogReturns <- rep(0:N)
    Simulation <- rep(0:N)
    ResultsSimulation <- rep(0:runs)
    ResultsReturns <- rep(0:runs)
    ResultsLog <- rep(0:runs)

    runs=50   #how ofthen the Simulation of GBM should be repeated
    i <- 1
    j <- 1

    #second loop
    for(j in 2:runs){
      Simulation <- GBM(x, r, sigma, T, N)
      x=100
      r=0
      sigma=1
      T=1
      N=20

    #first loop
      for(i in 2:length(BM)){
        Returns <- ROC(Simulation)
        LogReturns[i+1] <- log(Simulation[i+1]/Simulation[i])

        i <- i+1
      }
      ResultsSimulation[j]<-Simulation[N]
      ResultsReturns[j]<-Returns[N]
      ResultsLog[j]<-LogReturns[N]

      j <- j+1
    }

ResultsMatrix <- as.matrix(data.frame(ResultsSimulation, ResultsReturns, ResultsLog))

这个例子的结构基本上是我的。我需要围绕它构建另外 2 个循环,它们将对 3 个不同的“r”值和“sigma”值(GBM 函数中的变量)进行相同的模拟和计算。结果(每个模拟的最终值和来自第一个循环的计算)应保存在单独的向量或由这些向量组成的矩阵中-> 因此,27 个长度为 50 的投资者(变量 r 和 sigma 的每个组合都有 3 个结果) 例如,如果 sigma=0.1; 0.3;0,6 和 r=0,03; 0,05; 0,08

如何围绕它构建这些循环并相应地保存数据?

对不起,伙计们,但我真的迷路了:(

提前干杯和感谢!至少用于阅读;)

4

1 回答 1

1

这接近你需要的吗?如果是这样,您可以添加新的交易函数来返回投资组合,然后调用它(参见示例):

警告:运行 N_SIMS = 100000 大约需要 1.5 小时!

get.simulation.GBM = function(TIME = 10, N = 250, N_SIMS = 1000, x0 = 100, sigma = c(0.1, 0.3, 0.6), r = c(0.03, 0.05, 0.08))
{
  require(sde)
  params = expand.grid(sigma = sigma, r = r)
  
  # pre-allocate matrix
  results = matrix(0, ncol = N_SIMS * nrow(params), nrow = N)
  
  for (i in 1:nrow(params))
  {
    idx.range =  ((i - 1)*N_SIMS + 1):((i - 1)*N_SIMS + N_SIMS)
    temp.res = replicate(N_SIMS, GBM(x0, r = params[i, 'r'], sigma = params[i, 'sigma'], T = TIME, N = N - 1 ))
    results[, idx.range] = temp.res
  }
  
  return(results)
}

apply.MACD = function(serie, nFast = 12, nSlow = 26, nSig = 9, p0 = 100)
{
  require(TTR)
  roc = ROC(serie)
  sim.MACD = MACD(serie, nFast, nSlow, nSig, maType = "EMA")
  portfolio = rep(0, length = length(serie))
  portfolio[1] = p0
  
  sim.MACD[is.na(sim.MACD)] = 0
  sim.MACD = as.data.frame(sim.MACD)
  
  for (i in 2:length(serie))
  {
    buy = portfolio[i - 1] * exp(roc[i])
    sell = portfolio[i - 1]
    hold = buy
    
    if(sim.MACD$macd[i - 1] < sim.MACD$signal[i - 1] && sim.MACD$macd[i] > sim.MACD$signal[i]){portfolio[i] = buy}
    if(sim.MACD$macd[i - 1] > sim.MACD$signal[i - 1] && sim.MACD$macd[i] < sim.MACD$signal[i]){portfolio[i] = sell}
    if(sim.MACD$macd[i - 1] > sim.MACD$signal[i - 1] && sim.MACD$macd[i] > sim.MACD$signal[i]){portfolio[i] = hold}
    if(sim.MACD$macd[i - 1] < sim.MACD$signal[i - 1] && sim.MACD$macd[i] < sim.MACD$signal[i]){portfolio[i] = sell}
    if(sim.MACD$macd[i] == sim.MACD$signal[i]){portfolio[i] = sell}
    if(sim.MACD$macd[i - 1] == sim.MACD$signal[i - 1] && sim.MACD$macd[i] != sim.MACD$signal[i]){portfolio[i] = buy}
    
  }
  return(portfolio)
}

apply.RSI = function(serie, p0 = 100, n = 14)
{
  require(TTR)
  roc = ROC(serie)
  sim.RSI = RSI(serie, n = n)
  sim.RSI[is.na(sim.RSI)] = 0
  portfolio = rep(0, length = length(serie))
  portfolio[1] = p0
  
  for (i in 2:length(serie))
  {
    buy = portfolio[i - 1] * exp(roc[i])
    hold = portfolio[i - 1]
    
    if(sim.RSI[i - 1] < 50 && sim.RSI[i] > 50){portfolio[i] = buy}
    if(sim.RSI[i - 1] > 50 && sim.RSI[i] < 50){portfolio[i] = hold}
    if(sim.RSI[i - 1] < 50 && sim.RSI[i] < 50){portfolio[i] = hold}
    if(sim.RSI[i - 1] > 50 && sim.RSI[i] > 50){portfolio[i] = buy}
  }
  return(portfolio)
}

# Example (this is SLOW)
simulation.matrix = get.simulation.GBM()
portfolio.RSI = apply(simulation.matrix, 2, apply.RSI)
portfolio.MACD = apply(simulation.matrix, 2, apply.MACD)

# if you need only the last values
portfolio.RSI.last = tail(portfolio.RSI, 1)
portfolio.MACD.last = tail(portfolio.MACD, 1)
于 2013-07-02T15:33:07.043 回答