在 R 中同时对多个时间序列进行滚动均值回报和锐化

如何解决在 R 中同时对多个时间序列进行滚动均值回报和锐化

我使用这个脚本来计算一次单个 ETF 的平均滚动回报和平均滚动夏普比率:

library(quantmod)
library(httr)
library(jsonlite)
library(tidyverse)
library(hrbrthemes)
library(tidyquant)
library(timetk)
library(dplyr)
library(roll)
###
tickers = c('afk'); forex = c('EURUSD')   
tiingo_api_key("XXXXX") #wts = c(0.1,0.2,0.25,0.2) 
price_data <- tq_get(tickers,from = '2019-10-01',to = '2021-01-29',get = "tiingo")
forex_data <- tq_get(forex,get = "tiingo")
forex_data <- forex_data[,c(2,8)]

price_data <- price_data %>%
  # join datasets using date column.
  # non key columns from rhs that have the same names as columns in 
  # the lhs get assigned the suffix "_forex_data"
  left_join(forex_data,by = c("date"),suffix = c("","_forex_data")) %>%
  # calculate new value of adjusted by dividing by forex value
  mutate(adjusted = adjusted / adjusted_forex_data) %>%
  # exclude adjusted_forex_data column from result
  select(-adjusted_forex_data)

price_data <- na.omit(price_data) 
price_data <- price_data[,8)]
##
xdf <- data.frame()
xdf <- price_data
colnames(xdf)[2] <- "adjClose"
xdf_02 <- xdf %>%
  mutate(lagx = lag(adjClose)) %>%
  mutate(pct_change = (adjClose - lagx)/adjClose)%>%
  mutate(rollmeanx = roll_mean(pct_change,width = 68),rollsdx = roll_sd(pct_change,width=68)) %>%
  mutate(roll_sharpe = rollmeanx / rollsdx)

xdf_02 <- xdf_02[,c(1,4,5,7)]

#write.csv(xdf_02,'afk_r.csv')

我需要对几个 etf 重复这个操作。 为此,我尝试了这个脚本:

library(quantmod)
library(httr)
library(jsonlite)
library(tidyverse)
library(hrbrthemes)
library(tidyquant)
library(timetk)
library(dplyr)
library(roll)

###
tickers = c('xly','xlv','xli'); forex = c('EURUSD')   
tiingo_api_key("XXXX") #wts = c(0.1,from = '2019-08-19',to = '2019-10-19',2,8)]

colnames(price_data)[3] <- "adjClose"
price_data_02 <- price_data %>%
  mutate(lagx = lag(adjClose)) %>%
  mutate(pct_change = (adjClose - lagx)/adjClose)%>%
  mutate(rollmeanx = roll_mean(pct_change,width = 10),width=10)) %>%
  mutate(roll_sharpe = rollmeanx / rollsdx^5)

但它不能正常工作。这是 price_data02 的 dput。

structure(list(symbol = c("XLY","XLY","XLV","XLI","XLI"),date = structure(c(1566172800,1566259200,1566345600,1566432000,1566518400,1566864000,1566950400,1567036800,1567123200,1567468800,1567555200,1567641600,1567728000,1567987200,1568073600,1568160000,1568246400,1568332800,1568592000,1568678400,1568764800,1568851200,1568937600,1569196800,1569283200,1569369600,1569456000,1569542400,1569801600,1569888000,1569974400,1570060800,1570147200,1570406400,1570492800,1570579200,1570665600,1570752000,1571011200,1571097600,1571184000,1571270400,1571356800,1566172800,1571356800),tzone = "UTC",class = c("POSIXct","POSIXt")),adjClose = c(103.88979190684,103.985536180942,105.940631520236,106.096510680224,103.014000529557,103.656054210621,105.123520157882,106.773230698533,106.651057131133,106.752183731718,107.124677149076,109.040842730252,109.247378023503,109.568338069556,109.302004838183,109.941910107034,110.128764894984,109.409045218438,108.834273053662,108.989530382645,108.827860236458,108.570749369651,107.757863416592,108.300501919417,107.120516287642,108.465962815583,108.199545227096,107.750358633043,109.129846724491,108.075096442047,105.832515699236,105.961569473432,107.027904160657,106.408985827586,105.658867955606,106.324973528198,106.787293972671,107.68727179356,107.752492580939,108.604244285425,108.882370462176,108.528286758828,108.098752156478,79.7690744415815,79.1411361629132,79.6907748306897,79.3948067613073,77.3621365050958,77.8143629822358,78.5120834734553,79.1591091551872,79.6413048138808,79.5765650643146,79.1234362097159,79.8078108166086,79.9828303406139,79.1766667827908,79.5272422185871,80.6357079172772,80.3610764188852,79.9539261939731,80.3891963088317,80.1405701177907,80.0521265098073,80.455259611697,81.2234506709137,80.9154011793777,80.1550223863104,80.0379887611256,79.7427685476557,79.3351813102774,80.3786481095186,79.4897334845071,78.1095846438923,78.5264905136837,79.8090839437686,79.4299037039426,78.1920270560489,78.5873352799982,78.7947931577411,79.2211265351062,79.2732953079122,80.6482408791092,80.4180877363704,80.541688641759,80.0837798356084,65.5316291319365,65.1905505248625,65.5636513034318,65.7399118984554,63.8180764075688,64.0429801055418,64.9374155819368,66.0930691100842,66.6878168783335,66.1144476016407,66.529346485538,67.6427423430565,67.7458012282476,67.8875770062091,68.6161415417236,69.5324528883564,69.3622868620804,69.4247838104348,69.893912323993,69.5359752503656,69.3616314471594,69.033862777913,68.8724115050268,68.8456157054001,68.4405021069962,69.1184940526835,69.345285107955,69.0237442710668,69.4092222987107,67.6575658763924,66.1922748854133,66.3878761442095,67.1240973679064,66.6914199010372,65.8941095924985,66.3089478833794,66.7582685555905,67.8212228997667,67.7634712310145,68.2513378525342,68.1106187139345,68.0448169006413,67.3224024690253),lagx = c(NA,103.88979190684,68.0448169006413),pct_change = c(NA,0.000920746073145609,0.0184546317238109,0.00146922042005707,-0.0299232156291454,0.00619407796248412,0.0139594445187539,0.01545060058461,-0.00114554483271719,0.000947302406849019,0.0034771952389569,0.017572916103711,0.0018905286056997,0.00292931381188991,-0.00243667288415593,0.00582039431758291,0.00169669375777951,-0.00657824657101311,-0.00528116877752874,0.00142451599193398,-0.00148555843913384,-0.00236814121943363,-0.00754363465723802,0.00501048927020529,-0.0110154961221968,0.0124043201481387,-0.00246228011335603,-0.00416877122036123,0.0126407956471404,-0.00975942022878049,-0.0211899030084918,0.0012179299989351,0.00996314648583695,-0.00581641041174378,-0.00709943127816679,0.00626480826177106,0.00432935817804974,0.00835732771292667,0.000605283328636722,0.00784271102929999,0.00255437290326066,-0.0032625936879903,-0.00397353895194581,-0.355146125402815,-0.00793441071373612,0.00689714297475791,-0.00372780136958125,-0.0262747430208002,0.00581160674981317,0.0088867911836202,0.00817373627163204,0.00605459264913445,-0.000813552953860299,-0.00572686016059428,0.0085752835454333,0.00218821368611202,-0.01018183248399,0.00440824333921507,0.0137465860636734,-0.00341746913593323,-0.00509231058802971,0.0054145349729139,-0.0031023761208035,-0.00110482521626189,0.00501064944461538,0.00945774961382844,-0.00380705634583806,-0.00948635245091245,-0.00146222596289983,-0.00370215655722352,-0.00513753457982663,0.0129818903873515,-0.0111827601634213,-0.0176693916234096,0.00530911119374079,0.0160707699763676,-0.00477377186858107,-0.0158312387400629,0.00503017722309765,0.00263288815705768,0.00538156166178961,0.000658087601926903,0.0170486740468151,-0.00286195741800309,0.00153462023795305,-0.00571787204713022,-0.222063008297471,-0.00523202526022366,0.00569066504308259,0.00268118088286884,-0.0301142810794384,0.0035117619074307,0.0137738077251694,0.0174852453321922,0.00891838713710508,-0.00867237491187286,0.00623632886560044,0.0164599455751195,0.00152125863629342,0.00208839060419683,0.0106179758748389,0.0131781823964128,-0.0024532931939566,0.000900210917833641,0.00671200821301252,-0.00514750921862546,-0.0025135481903864,-0.00474794044628248,-0.00234420821571471,-0.000389215774340291,-0.00591920845014461,0.00980912496690811,0.0032704610691041,-0.00465840907768502,0.00555370042881158,-0.0258900301781256,-0.0221368882322849,0.0029463400572015,0.0109680614349516,-0.00648775310993824,-0.0120998722567077,0.00625614346363184,0.00673056210013877,0.0156728867267276,-0.0008522536951417,0.00714808876822038,-0.0020660381781391,-0.000967036378234412,-0.0107306692144323
),rollmeanx = c(NA,NA,0.00298044584668048,0.00464566284973703,0.00298925253792591,0.00313526187710919,0.00588391615160814,0.00584654778711802,0.00462027271102059,0.00241738799545827,0.00200382560097712,0.00205154695948561,0.00155527159167654,-0.000438834140637924,-0.0013822504669317,-0.00117413292110016,-0.00203201524490424,-0.00137362266184867,-0.00178952004896222,-0.00154857251389704,0.000243623928569883,-0.000874769693501565,-0.00284520415043736,-0.00248659702860049,-0.000735918914292995,-0.0018186088824879,-0.0014270023980849,-0.00204095358672166,-0.00136178975758109,-0.000109179864252296,-0.00131273109610267,0.00044748202970538,0.00282190962088063,0.00237385725218809,0.000980188708409815,-0.0339527827906973,-0.0340362807342542,-0.0339730472629555,-0.0347787632177186,-0.0382419702910913,-0.0377213379489737,-0.0376169299335417,-0.0370549935967045,-0.036123274962992,-0.0358072763631835,-0.000865349838961441,0.0007856195869555,0.000314726658090912,-0.00033067645334996,0.00273762218265157,0.00353112011403759,0.00230069408208225,0.000974089396116077,0.000910083628494023,0.000681201311799703,0.00114340480623294,0.000786941396151151,0.00151389498892279,0.00215137260273798,0.000761913023725232,-0.000758968178932093,-0.000787436921061122,-0.000791959320240814,-3.52237787970495e-05,-0.000843262183058831,-0.0024997188237736,-0.00246987264886106,-0.00180857061260715,-0.00190524216488145,-0.0025397307937965,-0.00189049047519675,-0.00125698600376863,-0.000205076379607008,-0.00143745665814947,0.00138568676287417,0.00286643018341482,0.00248898108783604,0.000310116885486266,-0.0214188067574027,-0.0203588854094188,-0.0202928366274203,-0.0202880073548392,-0.023837591628962,-0.0235522241984116,-0.0238797108305762,-0.0218449905555566,-0.0211066138656414,-0.0214020641521157,0.00142786956419144,0.00359706664772575,0.00318012600704683,0.00312084697917963,0.00719407267460736,0.00816071472350556,0.00653800463159296,0.00487950119015711,0.00465886329774785,0.00501134986707259,0.00413636216147391,0.00201557355933371,0.0016290268741329,0.00138126623627919,-0.000272452196219162,-0.000609357939169628,-3.69825128635584e-05,-0.000592844512415424,-0.000708675290835518,-0.00278292738678553,-0.00474526139097538,-0.00397583334062698,-0.00264460637556035,-0.00325446010912014,-0.00387252648977645,-0.00422782464010407,-0.00388181453700061,-0.00184868495655934,-0.00248928036895467,0.000814531525679931,0.00282161653109451,0.00243027888755092,0.000260405822612528),rollsdx = c(NA,0.0134841568195999,0.0142102103925022,0.0133617900776779,0.0133513079444992,0.00720300348777936,0.00720218488402019,0.0066933457287114,0.00634903067804303,0.00673015322975725,0.00672351799074568,0.00678942802961725,0.00385761371806388,0.00434719025872052,0.0046179237036809,0.00557599792443561,0.00684948020241074,0.00676812001759355,0.00661996487436154,0.00781523887100851,0.00840542133855674,0.0105901706529699,0.0106685462381951,0.0111710670679376,0.0110765150259844,0.0107805043170853,0.0100557477605009,0.0102515759659759,0.0106288134181471,0.00966211558692595,0.00955510543729772,0.00578872326420001,0.00609211488458235,0.00574740309766703,0.112976854387956,0.112955109139078,0.112980351066087,0.112705243098847,0.111760609431317,0.111973609162597,0.112021291860913,0.112259036897868,0.112608787611361,0.112713414274796,0.0108370376131496,0.0108978560310096,0.0107044567314341,0.0111601414489364,0.00646492261696063,0.00731519373885582,0.00734898883967546,0.00736835445412688,0.00732195255143012,0.00741697708347617,0.00711097945850556,0.00677861308008291,0.00731422581505028,0.00640260188748834,0.00730279244956732,0.00570760955999991,0.00572303229458311,0.00572682863211845,0.00699715000072969,0.00781004948429501,0.00945506088599968,0.00948183569894266,0.0105738720243143,0.010598563559126,0.0112714010510982,0.0115245038249566,0.0115877964390116,0.0116735206347549,0.0107398588037852,0.011571832681135,0.00965048221251629,0.00961808834911209,0.00861511313052298,0.0710011590048217,0.0711727459280912,0.0711992266027793,0.0712009559938597,0.0706618069135388,0.0707773921050657,0.0705759483304227,0.0715359995763646,0.071841585236276,0.0717773132977845,0.0135599568444965,0.0141004602638644,0.0140933169605556,0.0140968950343196,0.00798765419203688,0.00807692022915802,0.00844556579314001,0.00764769054788447,0.00754941651863466,0.00691330255424837,0.00728477294878403,0.00632186623256007,0.00647184586794521,0.00649967123494101,0.00597070454265456,0.00516857370156232,0.00525784228032607,0.00543847992799762,0.00527550470494566,0.00955597962169792,0.0113423807347606,0.0116002247919026,0.0125344993925396,0.0125609042910873,0.0128552074244012,0.0124786360633152,0.0127545719186988,0.0141600338472121,0.0139309924988578,0.0114638806205139,0.00832687176592357,0.0084118832879582,0.0087564030983619),roll_sharpe = c(NA,6685936.72065089,8017614.08588343,7018420.49412074,7390175.58759154,303457831.38321,301701990.16466,343915160.124637,234319932.365587,145122636.528603,149313338.457622,107805345.140361,-513696634.595997,-890314621.582531,-559092284.016373,-376976210.725815,-91112795.8598865,-126007475.754857,-121801635.87812,8356196.78334217,-20849530.8685462,-21359871.763186,-17991989.5949062,-4230158.78094462,-10907459.3475125,-9800073.06957493,-19850034.8662731,-12026988.4026027,-804856.828796049,-15588869.806185,5618212.74691359,434139357.655666,282887772.972616,156297275.131569,-1844.70949236578,-1851.02676904941,-1845.52487756475,-1912.46516501926,-2193.29199296906,-2142.93355275844,-2132.4578924304,-2078.45287781833,-1994.92100566292,-1968.30892082855,-5789470.07687758,5111012.83362099,2239290.17721102,-1910094.15459264,242413530.602272,168571180.195937,107329952.244829,44848329.2430647,43246080.3576327,30348748.7220495,62885843.8430733,54984232.2037732,72319252.6797467,199955402.250585,36682642.0431574,-125300350.018944,-128258096.728735,-128567718.189978,-2100051.88283564,-29019751.9921323,-33080305.4832977,-32226447.4516634,-13682492.5573323,-14246728.2791774,-13960409.9188887,-9299579.50478395,-6016253.88136516,-946033.172647721,-10060090.7869726,6678121.87804663,34244930.9891507,30239721.4485301,6534622.28640464,-11870.4745751393,-11147.7029903439,-11090.8894943484,-11086.9035534244,-13531.2737991098,-13260.476946954,-13637.835605511,-11660.8071126149,-11029.073503579,-11233.6184909524,3114562.13693613,6453302.74222076,5719765.60187294,5606026.48211451,221247589.049459,237410120.344228,152160342.169442,186519350.922088,189982252.319422,317341525.476637,201622249.257587,199605333.614001,143478695.887835,119074960.747121,-35905618.217015,-165203371.531602,-9203619.27340462,-124610074.081346,-173431195.233419,-34924146.3939243,-25277786.0464064,-18927625.9791942,-8547243.41782084,-10408171.0343201,-11030595.382811,-13972733.3601454,-11500198.7808806,-3247442.34902245,-4744205.82362176,4113861.09463618,70483686.0815983,57701874.5526117,5058502.40572489
)),row.names = c(NA,-129L),class = c("tbl_df","tbl","data.frame"
),na.action = structure(c(`6` = 6L,`50` = 50L,`94` = 94L),class = "omit"))

这是 price_data 的 dput:

structure(list(symbol = c("XLY",

问题出现在 pct_change 计算中,因为脚本在两个不同的 etfs 时间序列之间进行 pct_change 计算,这错误地夸大(缩小)了具有最高(最低)价格的 etf 的尖锐值。 感谢您的每一份贡献

解决方法

使用 group_by 解决了这个问题。 Tnx Phiver

library(quantmod)
library(httr)
library(jsonlite)
library(tidyverse)
library(hrbrthemes)
library(tidyquant)
library(timetk)
library(dplyr)
library(roll)

###
tickers = c('xly','xlv','xli'); forex = c('EURUSD')   
tiingo_api_key("XXXX") #wts = c(0.1,0.2,0.25,0.2) 
price_data <- tq_get(tickers,from = '2019-08-19',to = '2019-10-19',get = "tiingo")
forex_data <- tq_get(forex,get = "tiingo")
forex_data <- forex_data[,c(2,8)]

price_data <- price_data %>%
  # join datasets using date column.
  # non key columns from rhs that have the same names as columns in 
  # the lhs get assigned the suffix "_forex_data"
  left_join(forex_data,by = c("date"),suffix = c("","_forex_data")) %>%
  # calculate new value of adjusted by dividing by forex value
  mutate(adjusted = adjusted / adjusted_forex_data) %>%
  # exclude adjusted_forex_data column from result
  select(-adjusted_forex_data)

price_data <- na.omit(price_data) 
price_data <- price_data[,c(1,2,8)]

colnames(price_data)[3] <- "adjClose"

by_etf <- price_data %>% group_by(symbol)

price_data_02 <- by_etf %>%
  mutate(lagx = lag(adjClose)) %>%
  mutate(pct_change = (adjClose - lagx)/adjClose)%>%
  mutate(rollmeanx = roll_mean(pct_change,width = 10),rollsdx = roll_sd(pct_change,width=10)) %>%
  mutate(roll_sharpe = rollmeanx / rollsdx^5)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res