2

I have this simple dataset of a stock price, where Col1 is for Dates, Col2 is for Returns (Close Price D / Close Price D-1, the same as the pct_change in Python) and Col3 for the Ewma Volatility. I'm working with projections and were ploting the Returns vs Ewma Volatility, but reading some articles I realized that I need to plot the Daily Volatility x Ewma Volatility so I can see the predictions more clearly

enter image description here

So I wanted to see a graph like these, but in Red I have Returns and instead of it I need the red line to be Volatility calculated by normal methods (simple Vol). In black is the EwmaVol calculated by the MTS::EWMAvol package.

Here's the data:

structure(list(date = structure(c(18515, 18516, 18520, 18521, 
18522, 18523, 18526, 18527, 18528, 18529, 18530, 18533, 18534, 
18535, 18536, 18537, 18540, 18541, 18542, 18543, 18544, 18547, 
18548, 18549, 18550, 18551, 18554, 18555, 18556, 18557, 18558, 
18561, 18562, 18563, 18564, 18565, 18568, 18569, 18570, 18571, 
18572, 18575, 18576, 18577, 18578, 18579, 18582, 18583, 18584, 
18585, 18586, 18589, 18590, 18591, 18592, 18593, 18596, 18597, 
18598, 18599, 18600, 18603, 18604, 18605, 18606, 18607, 18610, 
18611, 18612, 18613, 18614, 18617, 18618, 18619, 18620, 18621, 
18624, 18625, 18626, 18627, 18628, 18631, 18632, 18633, 18634, 
18635, 18638, 18639, 18640, 18641, 18642, 18645, 18646, 18647, 
18648, 18649, 18652, 18653, 18654, 18655, 18656, 18659, 18660, 
18661, 18662, 18663, 18666, 18667, 18668, 18669, 18670, 18673, 
18674, 18675, 18676, 18677, 18680, 18681, 18682, 18683, 18684, 
18687, 18688, 18689, 18690, 18691, 18694, 18695, 18696, 18697, 
18698, 18701, 18702, 18703, 18704, 18705, 18708, 18709, 18710, 
18711, 18712, 18715, 18716, 18717, 18718, 18719, 18722, 18723, 
18724, 18725, 18726, 18729, 18730, 18731, 18732, 18733, 18736, 
18737, 18738, 18739, 18740, 18743, 18744, 18745, 18746, 18747, 
18750, 18751, 18752, 18753, 18754, 18757, 18758, 18759, 18760, 
18761, 18764, 18765, 18766, 18767, 18768, 18771, 18772, 18773, 
18774, 18775, 18778, 18779, 18780, 18781, 18782, 18785, 18786, 
18787, 18788, 18789, 18792, 18793, 18794, 18795, 18796, 18799, 
18800, 18801, 18802, 18803, 18806, 18807, 18808, 18809, 18810, 
18813, 18814, 18815, 18816, 18817, 18820, 18821, 18822, 18823, 
18824, 18827, 18828, 18829, 18830, 18831, 18834, 18835, 18836, 
18837, 18838, 18841, 18842, 18843, 18844, 18845, 18848, 18849, 
18850, 18851, 18852, 18855, 18856, 18857, 18858, 18859, 18862, 
18863, 18864, 18865, 18866, 18869, 18870, 18871, 18872, 18873, 
18876, 18877, 18878, 18879, 18880, 18883, 18884), class = "Date"), 
    Retorno = c(-0.675993561014219, 0.00727122549799243, 0.0200646939724594, 
    -0.309572944244751, -0.240552943912361, -0.22033250472985, 
    -0.361838491629178, 0.425326793664922, -0.467655896347618, 
    0.184647131258419, 0.289993731622618, -0.247917340719569, 
    -0.266792070525439, 0.408121976560422, 0.432634049112388, 
    -0.526936711212769, 0.255443464814248, -0.277205195648874, 
    0.237634551321446, 0.416255267136204, 0.182381045120273, 
    0, 0.559328565933268, 0.22352526750161, 0.0344661931237814, 
    -0.0114765743015762, -0.340659866250595, 0.518121135418651, 
    0.124920612225829, 0.060477879624518, -0.132268000351469, 
    -0.419044186141948, -0.0468465628669677, -1.24962920487573, 
    0.519314480623082, -0.83241356610111, 0, 0.677072797328314, 
    0.770362872443658, 0.779540076578319, -0.128308411797546, 
    0.208939823146876, -0.313737425389519, 0.218322415089412, 
    -0.69034693281341, 0.812758658841942, 0.167317931481811, 
    -0.208730841632264, -0.387374906435024, 0.145599821661092, 
    0.107177528022895, 0.381307385634918, 0.386256536259558, 
    0.160935467100042, 0.0370218344118541, 0.241511701069377, 
    -0.33376949245172, 0.162429122410115, 0.189409487756105, 
    -0.136073192422632, 0.221928418309387, -0.294157383734931, 
    0.293123696475209, -0.308081743475511, -0.0160523988860364, 
    0.200830763685225, 0.215786923768921, 0.467113206724325, 
    0.405886001764272, 0.105544246268935, -0.128010524950925, 
    -0.195222854432975, 0.202533534363647, 0.36037155474833, 
    0.0195551377992495, 0, 0.386574054270276, -0.0520687967679869, 
    0.134723231050616, -0.0105062746232602, 0, -0.0867476700402434, 
    0.0959051831633806, -0.792631919080304, 0.714335671518634, 
    1.00993938236837, -0.129261214344111, 0.124684691358913, 
    -0.183903752322858, -0.0658608900095052, -0.160979643574251, 
    0.19290543725798, 0.251171599859635, 0.230012814573936, 0.00867119071736477, 
    -0.00849157305596641, -0.0662645178280147, -0.325684851940853, 
    -0.63526581976692, 0.987392935674623, -0.71566845639813, 
    0.823448032203283, 0.364849774654681, 0.493861329996562, 
    0.429481174502912, 0.392845558392575, 0.18687659266044, -0.0496962834358631, 
    -0.351476870541759, 0.325056528852312, 0.0540948459475099, 
    0, 0, 0.298660972132666, -0.129176783491061, 0.0936264785031788, 
    -0.717102311834032, 0.0579970685670885, 0.2387869198789, 
    -0.800077283233545, -0.346606771876278, 0.652393650772446, 
    0.019543204729203, -0.186097235985271, -0.401338954104801, 
    0.770468206437961, -1.16143407489768, 0.287520645270323, 
    -0.180982644002469, 0.636164450379635, -0.167036272711984, 
    0.273933259257009, 0.0018679680615165, 0.401511529829268, 
    -1.0714528798559, 0.225507468509173, -0.0998873705026633, 
    -0.357468921258203, -0.453208884721668, 0.584325924664598, 
    0.294490804371817, -0.071870245489955, 0.227731039838296, 
    0.0272554279847483, 0.282891030886757, 0, 0.444572787367717, 
    0.107513540207911, 0.204070787509094, 0.290177231896655, 
    0.314121770459557, 0.294143400791366, 0.0423074542633505, 
    -0.111752005968369, 0.191432961182637, 0.107012720814761, 
    -0.221435538762791, -0.287455618822024, 0, -0.504458868407395, 
    0.576189122256037, 0.0513721502072407, -0.0878347180967137, 
    0.0305926251264597, 0.0409984249091759, -0.0507344439032554, 
    0.127910054528995, -0.33110581208469, 0.244321197674869, 
    -0.153002648530627, 0.550660961815173, -0.39137091346195, 
    0.00655796886117349, -1.07175015625675, 0.47234018769347, 
    0.457477961736975, 0.194987471696463, -0.192289243894243, 
    -0.100966714379365, 0.295535636777222, 0.0497612954361652, 
    0.424646083712068, -0.0235816065166123, 0.148925361065251, 
    0.0833499347084376, 0.162437322657595, 0.216822541199244, 
    -0.0266349635355871, -0.0203718557462801, 0, 0.163246069746882, 
    0.0540313182825264, -0.132667149729679, -0.0106752473427232, 
    0.0966877877911841, -0.0135235065561488, 0.286749500028446, 
    -0.123925251554195, -0.0750149494882748, -0.00131099907860658, 
    0.0788369651503004, 0.310420308737175, 0.0738549668615309, 
    -0.0388124917441666, 0.156992583882095, -0.158341400377075, 
    0.138832514569506, 0.0484161074075555, 0.0644817162954546, 
    -0.075831329755838, 0.594706467883038, 0.0582157771824145, 
    -0.224095914091399, 0.433246293201151, -0.340500528636388, 
    0.0408415756500143, 0.45961172993559, 0.136168616599819, 
    0.0264246421863121, -0.0041223988663561, -0.273325208808114, 
    -0.429764244359346, 0.318952765660581, 0.152571137442228, 
    0.378021240701817, -0.0502434439840156, -0.284587147638002, 
    -0.549313963668678, 0.024940657477001, 0.106379612006064, 
    -0.666213312763818, 0.101187879104207, 0.212900264239358, 
    -0.319050618049279, 0.151814926005171, 0.165719848192862, 
    0.111396078192044, -0.192914594549868, -0.0785555116791173, 
    -0.0633138947667031, -0.100685336741663, -0.70572669007387, 
    -0.636169672803793, -0.0256706550272056, 0.568449636874711, 
    0.431844471002692, -0.0884432149109783, 0.507974104651909, 
    0.253656436799725, -0.551492331609423, 0.473346006073304, 
    -0.230628440542497, -0.36475396210053, 0.0553706780422077, 
    -0.458282184750842, 0.127876503287846, 0.329153343062354, 
    0, -0.640854284349025, 0.314530520275267, -0.547495384943616, 
    0.532252306040703, 0), EwmaVol = c(0.401545880470543, 
    0.37957402999614, 0.357002237552554, 0.336710024448518, 0.318760036517056, 
    0.316732889747995, 0.306462909109581, 0.288335641955465, 
    0.285974951159514, 0.27897153978688, 0.26226816859379, 0.285059883553795, 
    0.272374799205532, 0.268378720050093, 0.262349047774286, 
    0.258978405250914, 0.253148786729867, 0.238963348264941, 
    0.225541112253008, 0.234992747615912, 0.222414110918961, 
    0.209069264263823, 0.206230071488549, 0.20121898142488, 0.189404201517459, 
    0.179625218204264, 0.169480509616124, 0.174732838517365, 
    0.164762013463212, 0.156538802813315, 0.153370969102807, 
    0.145432704491324, 0.14287274912088, 0.21547817272594, 0.214028935828116, 
    0.234147750804581, 0.220098885756306, 0.21209598867207, 0.242287370393438, 
    0.261087909529102, 0.248230680408422, 0.243595317635679, 
    0.230164613514584, 0.217002608357163, 0.22967146650814, 0.248916351205143, 
    0.239617766167996, 0.225423319002878, 0.218347622569303, 
    0.206598225427181, 0.196302279540574, 0.185030606442786, 
    0.188266218750961, 0.181245068143354, 0.17041364058184, 0.161095631643361, 
    0.160389804076746, 0.156316917381599, 0.14852488563309, 0.142019675657794, 
    0.133567051476024, 0.125838895726541, 0.11969455573153, 0.119360323208544, 
    0.11750261880258, 0.112148663686316, 0.108243847740347, 0.115163834443589, 
    0.116212022405643, 0.109409013330819, 0.104496900700937, 
    0.10935285581462, 0.105502302791016, 0.101242444242857, 0.09516847810389, 
    0.0894583694176566, 0.0866069786756536, 0.0817380126900134, 
    0.0773367431654428, 0.0726971569867781, 0.0683353275675715, 
    0.0697812759496627, 0.067691476973121, 0.129966833467446, 
    0.126803399533392, 0.171461998066648, 0.171739783002973, 
    0.197564652471901, 0.189022309519642, 0.179950836609547, 
    0.170183058064679, 0.164266188880656, 0.155101252531341, 
    0.148693223428185, 0.14012494879068, 0.133740625026294, 0.125716772617523, 
    0.119105020311106, 0.117086850187538, 0.145347969102275, 
    0.152463434186314, 0.158687203287356, 0.152449871680205, 
    0.166738452722435, 0.158612506047059, 0.160075907173878, 
    0.151254857380356, 0.142343596709454, 0.142934873378322, 
    0.142665594947683, 0.134145123063853, 0.126096415680022, 
    0.118530630739221, 0.112499802745561, 0.110940338230715, 
    0.104545755177033, 0.135780351022354, 0.133312910743497, 
    0.125726130222031, 0.153200898521969, 0.159065047312098, 
    0.155282128215572, 0.146884947547679, 0.138117117632574, 
    0.130622632663405, 0.152186093616364, 0.253518876239024, 
    0.238323024716563, 0.227569746334944, 0.248057416617638, 
    0.234104106541416, 0.221001436475852, 0.208519869481825, 
    0.209685706242652, 0.239728740398097, 0.234167233383272, 
    0.224220956969507, 0.216318345630569, 0.223196193500169, 
    0.224922152734894, 0.212452796555951, 0.200672435018375, 
    0.197518356826799, 0.188183960822706, 0.177785419155766, 
    0.16711829400642, 0.169717068257208, 0.166026638555189, 0.156957479697871, 
    0.153622766010829, 0.14469286668481, 0.138471748094382, 0.131579951121295, 
    0.124018474660646, 0.120182498675561, 0.113234218642543, 
    0.107149115926987, 0.101721766522229, 0.095618460530895, 
    0.0999442416592745, 0.101574073875603, 0.0957296378477016, 
    0.093118066436934, 0.0914320009168463, 0.0871185787307817, 
    0.0823469513367038, 0.0811192489506884, 0.0825047887866796, 
    0.0860049643565581, 0.0808493443261423, 0.0961929240668569, 
    0.0911638648549433, 0.0857640508042613, 0.104905012906323, 
    0.114721311495374, 0.109831409732472, 0.104612358275864, 
    0.0993594406351853, 0.0946255646854852, 0.0895721816070949, 
    0.0845410520311026, 0.0838606211253721, 0.0789729132909391, 
    0.0747186193653219, 0.0705734402792115, 0.0688594831676326, 
    0.0657400666843101, 0.0634447557051325, 0.0633681690044842, 
    0.0595660788642152, 0.0603886782791013, 0.0587833071039431, 
    0.057511124086288, 0.0543325953300019, 0.0512261329106077, 
    0.0491856952988916, 0.0510463463396217, 0.0487190552131411, 
    0.047891041488347, 0.0467019683194386, 0.0466241053525344, 
    0.0456515495748067, 0.0431468387328283, 0.041317889619408, 
    0.0452423873993231, 0.0486383165176941, 0.0479392218342959, 
    0.0454006830162697, 0.0435713468777161, 0.0419873301594818, 
    0.0536531567181674, 0.0506716342092926, 0.0562970018467919, 
    0.0684016291611351, 0.0674546466335945, 0.0634076483792872, 
    0.067163694596286, 0.0640197920338736, 0.061921707762386, 
    0.0589219002148625, 0.0567269880819515, 0.0678661716944311, 
    0.0660541475889322, 0.062251261441256, 0.0624556318584788, 
    0.0635712086121672, 0.0630732335813346, 0.0691297797235716, 
    0.0653096157302932, 0.0636744930080208, 0.0944386419928364, 
    0.0915130579751466, 0.0863295417576338, 0.0867829530305672, 
    0.0839894020970701, 0.0791825005794653, 0.0766098726649444, 
    0.0753903595328056, 0.0733451437270916, 0.0693926680771279, 
    0.0664728433228, 0.0956887810320563, 0.0954558159033218, 
    0.0922581275391543, 0.09091195032168, 0.0929899977659706, 
    0.091341181932747, 0.131126103751573, 0.129655613187944, 
    0.148447176829185, 0.153225134955549, 0.153634505982304, 
    0.160933771494317, 0.151965634321099, 0.167395816107282, 
    0.160837085230127, 0.167419121889831, 0.157373974576441, 
    0.217309648664878, 0.211280213400328, 0.212032193587911, 
    0.22982849323927, 0.216038783644914)), class = "data.frame", row.names = c(NA, 
-263L))

Now, I need to have a column with the daily volatility of the assets.

What's the simpliest way of doing it in R?

  • What do you mean "daily volatility" you don't have intraday data so maybe you want to calculate it over longer periods, or.... ? – Will Sep 15 '21 at 19:29
  • I didn't get it. What data do I need to calculate the daily volatility then? Since I've been working with stocks, I can extract some additional information from my system – Vinícius Felizatti Sep 15 '21 at 19:41
  • said another way: over which period do you want to calculate your daily vols? – Will Sep 15 '21 at 20:47
  • for the same period as the function EWMAVol calculates the ewma volatility, based on a year's series of returns – Vinícius Felizatti Sep 15 '21 at 20:51
  • I deleted my answer since your question is unclear. If you only provide returns (:= close price today/close price yesterday) how can one calculate daily volatility? If you want us to build a difference, the only one we can calculate is return_diff := close price today - close price yesterday. – Pax Sep 16 '21 at 11:52
  • Sorry, return today - return yesterday of course. – Pax Sep 16 '21 at 12:16

3 Answers3

4

Volatility is the (typically annualized) standard deviation of returns over a given period. The exact definition of volatility depends on some conventions (percentage returns vs log returns, day count conventions) which will depend a lot on your context.

We don't quite know what we're talking about when we talk about volatility

From your data, it is possible to calculate some metric of volatility by using the zoo function rollapply. Please not that the example here does not conform to most conventions (you'd normally multiply with sqrt(daycount/horizon) to annualize)

library(zoo)
library(ggplot2)
#Define a horizon for volatility calculation, e.g. 20 days
horizon = 20

#Note: volatility is not annualized!
data['vols'] = c(rep(NA, horizon-1), rollapply(as.vector(data$Retorno), horizon, FUN = function(x) sd(x)))

#Alternatively, calculate some measure of day-to-day change in return that is not a volatility
data['not_a_vol'] = abs(data$Retorno - lag(data$Retorno))

You can then plot the results as:

ggplot() + 
  geom_bar(aes(x=data$date, y=data$Retorno), stat = 'Identity') + 
  geom_line(aes(x = data$date, y= data$vols, color = 'red'), lwd=1.5)

Example plot

Your analysis seems to be going in the direction of volatility analysis, so you might find GARCH models or exponential moving average of volatility interesting.

  • Thanks! That's what I wanted for sure but somehow wasn't able express myself clearly, but yeah, that's it, the SDev for a horizon (in my case will problably use 5days) – Vinícius Felizatti Sep 22 '21 at 13:56
1
library(tidyverse)
library(MTS)
library(ggpubr)
#> Loading required package: magrittr
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract
#> Loading required package: magrittr
#> 
#> Attaching package: 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract

data <- structure(list(
  date = structure(c(
    18513, 18514, 18516, 18519,
    18520, 18521, 18522, 18523, 18526, 18527, 18528, 18529, 18530,
    18533, 18534, 18535, 18536, 18537, 18540, 18541, 18542, 18543,
    18544, 18547, 18548, 18549, 18550, 18551, 18554, 18555, 18556,
    18557, 18558, 18561, 18562, 18563, 18564, 18565, 18568, 18569,
    18570, 18571, 18572, 18575, 18576, 18577, 18578, 18579, 18582,
    18583, 18584, 18585, 18586, 18589, 18590, 18591, 18592, 18593,
    18596, 18597, 18598, 18599, 18600, 18603, 18604, 18605, 18606,
    18607, 18610, 18611, 18612, 18613, 18614, 18617, 18618, 18619,
    18620, 18621, 18624, 18625, 18626, 18627, 18628, 18631, 18632,
    18633, 18634, 18635, 18638, 18639, 18640, 18641, 18642, 18645,
    18646, 18647, 18648, 18649, 18652, 18653, 18654, 18655, 18656,
    18659, 18660, 18661, 18662, 18663, 18666, 18667, 18668, 18669,
    18670, 18673, 18674, 18675, 18676, 18677, 18680, 18681, 18682,
    18683, 18684, 18687, 18688, 18689, 18690, 18691, 18694, 18695,
    18696, 18697, 18698, 18701, 18702, 18703, 18704, 18705, 18708,
    18709, 18710, 18711, 18712, 18715, 18716, 18717, 18718, 18719,
    18722, 18723, 18724, 18725, 18726, 18729, 18730, 18731, 18732,
    18733, 18736, 18737, 18738, 18739, 18740, 18743, 18744, 18745,
    18746, 18747, 18750, 18751, 18752, 18753, 18754, 18757, 18758,
    18759, 18760, 18761, 18764, 18765, 18766, 18767, 18768, 18771,
    18772, 18773, 18774, 18775, 18778, 18779, 18780, 18781, 18782,
    18785, 18786, 18787, 18788, 18789, 18792, 18793, 18794, 18795,
    18796, 18799, 18800, 18801, 18802, 18803, 18806, 18807, 18808,
    18809, 18810, 18813, 18814, 18815, 18816, 18817, 18820, 18821,
    18822, 18823, 18824, 18827, 18828, 18829, 18830, 18831, 18834,
    18835, 18836, 18837, 18838, 18841, 18842, 18843, 18844, 18845,
    18848, 18849, 18850, 18851, 18852, 18855, 18856, 18857, 18858,
    18859, 18862, 18863, 18864, 18865, 18866, 18869, 18870, 18871,
    18872, 18873, 18876, 18877, 18878, 18879, 18880
  ), class = "Date"),
  return = c(
    -0.344318028823296, 0.269214666620058, 0.126435486075415,
    0.190402598580308, 0.118204959190486, -0.411914395032645,
    -0.280554281566071, 0.0134834445697551, -0.209400032450252,
    0.440220250108175, -0.299538435031037, 0.0790056559320964,
    0.368012578536158, 0.213210937684974, -0.119491426933381,
    0.324232635217204, 0.33565827603383, -0.284052393900706,
    0.0174981257069227, -0.293140728783266, 0.262857810110247,
    0.25815207221046, 0.234032193845141, 0, 0.47229978173055,
    0.16344672539841, 0.0254415401713886, 0.0782307185721609,
    -0.502295230104942, 0.322732032393595, 0.125213641008163,
    0.0260011812318895, -0.0119807619653632, -0.442636109202831,
    0.119360281355763, -0.935912609987246, 0.503025377994561,
    -0.515851084169158, 0, 0.566675653173945, 0.54537601383754,
    0.288514339206156, -0.384940437925295, 0.423464532950096,
    -0.360198194766125, 0.34916380193169, -0.488427517439975,
    0.751803563456712, 0.0407270958491847, -0.308511236722092,
    -0.356669697629545, -0.00433655513272652, 0.25103546278182,
    0.330904413577973, 0.215960242799815, 0.00310717943959164,
    -0.0202688039084646, 0.148748985331507, -0.35100173325186,
    0.114510581854206, 0.148599263370308, -0.24952519697232,
    0.0901961472235016, -0.184463114050532, 0.293294243386703,
    -0.167917218613252, -0.264913772913978, 0.21209802725542,
    0.257358936163117, 0.218459709282958, 0.320686532500207,
    -0.042363642590243, -0.157974967460515, 0.0326488873406457,
    0.155724946337242, 0.308194493705213, -0.00373113226282772,
    0, 0.388011313498459, -0.160799203187683, -0.0364451647751474,
    -0.00787981636365025, 0, 0.00347476577235568, 0.0958786936178273,
    -0.494900147494504, 0.698924415442, 0.573739010391536, -0.0244258685250395,
    -0.118236771455138, -0.0497255705517377, -0.194646421800621,
    0.0626373850623438, 0.161831284061245, 0.349877415042077,
    0.244794751129892, 0.106084615995957, 0.0496011476778498,
    -0.0155409324898941, -0.248181207363339, -0.565019988759,
    0.713402006899163, -0.492682269522014, 0.560742579750566,
    0.0933300580868543, 0.406010324025276, 0.498072877122919,
    0.183140575863927, 0.211708797400874, 0.0467600239312594,
    -0.26047696162337, 0.156025603919635, 0.0579917193297143,
    0, 0, 0.279910939036699, 0.0361670915156291, -0.0308432601603755,
    -0.303210761690193, -0.0332159735063931, 0.239412585331418,
    -0.440324459780256, -0.0677518990798903, 0.693820654046051,
    0.106956183681477, -0.0265983858612053, -0.566418194735492,
    0.758734670629567, -0.461816077286941, 0.220311418745246,
    -0.212421535066087, 0.11649266375866, -0.0816306818701426,
    0.14720395503046, 0.0998096394235824, 0.125421489047231,
    -0.80404235252332, -0.0571527697596768, -0.0198909048934907,
    -0.384393817169225, -0.00512169253872485, 0.39762002446679,
    0.376723339854912, -0.00691542822410588, -0.0596391805140118,
    0.0496648004967207, 0.365778349002057, 0, 0.290193208620667,
    -0.0756981954953341, 0.303622329534577, 0.132066617966757,
    0.483747928454163, 0.206842677360989, 0.000809787937924961,
    -0.0932400960462393, 0.153919143128314, -0.0367657873301875,
    -0.279532278711733, -0.201551916340671, 0, -0.434262146566655,
    0.516522716400583, -0.085490199662681, 0.00110354252617679,
    -0.0718299839560837, 0.0607082870466307, -0.0272938411585472,
    0.18260643173571, -0.137250457849561, -0.0490415885207028,
    -0.00981171552565897, 0.226374538622723, -0.125646625601663,
    -0.114097545274073, -0.662064537293731, 0.441816451091909,
    0.272870771417137, 0.0287418778864461, -0.153691128743901,
    -0.0535034017089503, 0.183233994720022, 0.0485946620325522,
    0.178109740301375, 0.0246408691518447, 0.105609201872649,
    -0.0371879979512152, 0.074575753280852, 0.193587247420517,
    -0.143096141476954, 0.0120547889261328, 0, 0.0789650925019282,
    -0.00102809637148435, -0.0231060990016233, -0.00602180433261802,
    0.0252638098895634, 0.133415121207804, 0.129518349212272,
    -0.24291680503609, -0.0358833918191526, 0.00388487206410979,
    0.140270275560446, 0.209234313518833, 0.0691442382855439,
    -0.0656206987662583, 0.0382006909145182, 0.0527534442678976,
    0.0712377999932313, 0.105814832605434, 0.199474140948777,
    0.0367215633770196, 0.471059947866816, 0.182962161591784,
    0.00686636091130052, 0.331666913038535, -0.211401586120729,
    0.0150297692120097, 0.294266409263838, 0.0305328433574382,
    -0.107694099922229, 0.0348020405913332, -0.28173634642339,
    -0.200294650252061, 0.27169010217408, 0.085643621458738,
    0.22240270432515, 0.0986854063924764, -0.306275163317938,
    -0.342494037770066, -0.0816072482913978, 0.0236860357074207,
    -0.249044731611009, 0.0228069475130009, 0.275178781456765,
    -0.339227965049474, 0.105235656491434, 0.0344585485449368,
    0.102195427143629, -0.255540645170535, 0.00219929895942133,
    0.0611859110733352, -0.162852193753338, -0.413395221687142,
    -0.245350759376831, 0.0139098467565878, 0.384223744149518,
    0.229978347693158, 0.0890188167461016, 0.17247352519955,
    0.107792570447737, -0.312370635130228, 0.29430590173766,
    -0.167286040723373, -0.275329695257916, -0.127324776340247,
    -0.219446501196637, 0.152123649809292, 0.183579633672417,
    0, -0.170514041270507, 0.0995481496508168, 0
  )
), class = "data.frame", row.names = c(
  NA,
  -263L
))

sigma <-
  data %>%
  column_to_rownames("date") %>%
  EWMAvol() %>%
  pluck("Sigma.t") %>%
  as.numeric()
data["sigma"] <- sigma

data %>% as_tibble()
#> # A tibble: 263 x 3
#>    date        return  sigma
#>    <date>       <dbl>  <dbl>
#>  1 2020-09-08 -0.344  0.0710
#>  2 2020-09-09  0.269  0.0739
#>  3 2020-09-11  0.126  0.0732
#>  4 2020-09-14  0.190  0.0706
#>  5 2020-09-15  0.118  0.0687
#>  6 2020-09-16 -0.412  0.0663
#>  7 2020-09-17 -0.281  0.0716
#>  8 2020-09-18  0.0135 0.0726
#>  9 2020-09-21 -0.209  0.0697
#> 10 2020-09-22  0.440  0.0693
#> # … with 253 more rows

data %>%
  ggplot(aes(return, sigma)) +
  geom_point() +
  stat_smooth(method = "lm") +
  stat_cor()
#> `geom_smooth()` using formula 'y ~ x'

Created on 2021-09-13 by the reprex package (v2.0.1)

danlooo
  • 10,067
  • 2
  • 8
  • 22
1

Now, new assumption: Vola := return_today - return_yesterday.

  # assuming name of dataframe is df
  df$vola <- NA # creates new variable "vola"
for (i in 2:nrow(df)){ # starting in day 2, 
  # since in day 1 is not possible; simple: the difference
  df[i, "vola"] <- df[i, "return"] - df[i-1, "return"]
}
# now you have a column "vola" representing the volatility 
# attached to your dataframe

# to add sigma like danlooo calculated it: 
library(tidyverse)
library(MTS)

sigma <-
  df %>%
  column_to_rownames("date") %>%
  EWMAvol() %>%
  pluck("Sigma.t") %>%
  as.numeric()
df["sigma"] <- sigma[1:263]

# plot 
plot(df$vola, type="l", main="Daily Volatility",
     xlab="Days", ylab="Volatility [€ or $]")
lines(df$sigma, col="red", lwd=2)
  

Created on 2021-09-16 by the reprex package (v2.0.1)

Plot:

enter image description here

Pax
  • 664
  • 4
  • 23
  • So Daily Volatility = Return D - Return D-1? What if, let's say, I have that the 1Year volatility for the initial date is like 15%, is there a way to make like a cumulative Volatility by adding the daily returns to get something more like a positive curve/line for my visualization? – Vinícius Felizatti Sep 16 '21 at 14:11
  • Yes, its how I constructed it. Of course, that doesn't make much sense: Daily volatility should highest price - lowest price, possibly also return, for each day, but you do not have data to compute that. I dont't get what you try to explain with "1Year volatility for the initial date is like 15%..." Cumulative volatility sounds meaningless to me, but, of course, it is possible. – Pax Sep 16 '21 at 14:18