按时间段查找 quantmod 中的第二大值 [英] Finding the second highest value in quantmod by time period

查看:49
本文介绍了按时间段查找 quantmod 中的第二大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到 6 个月间隔内的最大值和第二大值.我正在使用 runMax 来查找第一个值,但我不知道如何处理第二个值.到目前为止,这是我的代码:

 库(quantmod)图书馆(TTR)getSymbols("GOOGL")GOOGL_mo<-to.monthly(GOOGL)#获取月度数据GOOGL_mo$Max_6mo<-runMax(GOOGL.High, 6)#添加6个月内最大值的列

我想添加另一个具有第二大值的列以具有如下内容:

<前>日期 开盘高低收盘量 Max6mo 2ndMax6m02016 年 10 月 802.55 839.00 796.23 809.90 35391730 839.00 819.062016 年 11 月 810.87 816.04 743.59 775.88 48353316 839.00 819.062016 年 12 月 778.55 824.30 753.36 792.45 34356689 839.00 824.002017 年 1 月 800.62 867.00 796.89 820.19 36840255 867.00 839.002017 年 2 月 824.00 853.79 812.05 844.93 26492197 867.00 853.792017 年 3 月 851.38 874.42 824.30 847.80 34553208 874.42 867.002017 年 4 月 848.75 935.90 834.60 924.52 28721553 935.90 874.422017 年 5 月 924.15 965.90 920.80 942.17 21302485 965.90 935.90

有什么想法吗?

解决方案

library(quantmod)图书馆(TTR)getSymbols("GOOG",src="google")GOOG <- to.monthly(GOOG)事情 <- 数据.frame(max1 = rep(NA, 5), max2 = rep(NA, 5))for (x in 1:(nrow(GOOG)-5)) {max1 <- max(GOOG[x:(x+5), "GOOG.High"])max2 <- max(GOOG$GOOG.High[x:(x+5)][GOOG$GOOG.High[x:(x+5)] != max1])东西<- rbind(东西,data.frame(max1,max2))}GOOG <- cbind(data.frame(GOOG), thing)

I am trying to find the maximal and the second largest value during a 6 months interval. I am using runMax to find the first value but I can't figure out how to do for the second one. Here's my code so far:

    library(quantmod)
    library(TTR)
    getSymbols("GOOGL")
    GOOGL_mo<-to.monthly(GOOGL)#to get monthly data
    GOOGL_mo$Max_6mo<-runMax(GOOGL.High, 6)#add a column with the max value during a 6 months period

I would like to add another column with the second largest value to have something like this:

Date        Open      High     Low        Close     Volume   Max6mo  2ndMax6m0
Oct 2016    802.55    839.00   796.23     809.90    35391730 839.00  819.06
Nov 2016    810.87    816.04   743.59     775.88    48353316 839.00  819.06
Dec 2016    778.55    824.30   753.36     792.45    34356689 839.00  824.00
Jan 2017    800.62    867.00   796.89     820.19    36840255 867.00  839.00
Feb 2017    824.00    853.79   812.05     844.93    26492197 867.00  853.79
Mar 2017    851.38    874.42   824.30     847.80    34553208 874.42  867.00
Apr 2017    848.75    935.90   834.60     924.52    28721553 935.90  874.42
May 2017    924.15    965.90   920.80     942.17    21302485 965.90  935.90

Any idea?

解决方案

library(quantmod)
library(TTR)
getSymbols("GOOG",src="google")
GOOG <- to.monthly(GOOG)

thing <- data.frame(max1 = rep(NA, 5), max2 = rep(NA, 5))
for (x in 1:(nrow(GOOG)-5)) {
  max1 <- max(GOOG[x:(x+5), "GOOG.High"])
  max2 <- max(GOOG$GOOG.High[x:(x+5)][GOOG$GOOG.High[x:(x+5)] != max1])
  thing <- rbind(thing, data.frame(max1, max2))
}
GOOG <- cbind(data.frame(GOOG), thing)

这篇关于按时间段查找 quantmod 中的第二大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆