Volatility calculation (by analogy of moving average)

11 visualizaciones (últimos 30 días)
Caxap Puc
Caxap Puc el 21 de Oct. de 2016
Comentada: Valeri Disko el 31 de Ag. de 2022
Hi, guys. How can I calculate Volatility for the data set (https://en.wikipedia.org/wiki/Volatility_(finance))? I'm pretty newbie in Matlab and programming, that's why loops are hard for my understanding.
%ticker - is array of data
Z = 252; %Number of trading Days in a year
n = 20; %window of volatility
Volat=zeros(length(ticker)- n, 1);
for i = 1:n
log_change = log(ticker(2:n+1)./ticker(1:n));
stdev = std(log_change);
Vol(i) = (stdev*sqrt(252))';
end
It calculates the only one number, however I'm trying to do 2 things: 1) Create "volatility" array (dataset), which, of course, will contain "ticker minus n" numbers; 2) Create "volatility" as window - from number of start element to number of end element from original dataset.
Will be glad for any assistance.

Respuestas (1)

Adolfo
Adolfo el 24 de Mayo de 2019
You have to calculate a time-window volatily of N ticks.
A simple function for only one series of data, that not considers any possible errors on the data structure, could be implemented as follows:
function r = histvar(serie, N)
r = nan(size(serie));
dailyret = (serie(2:end)-serie(1:end-1))./serie(1:end-1);
for i = N+1:size(serie, 1)-1
window = dailyret(i-N:i, :);
r(i+1) = sqrt(cov(window));
end
end

Categorías

Más información sobre Financial Toolbox en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by