Volatility calculation (by analogy of moving average)

20 Ansichten (letzte 30 Tage)
Caxap Puc
Caxap Puc am 21 Okt. 2016
Kommentiert: Valeri Disko am 31 Aug. 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.

Antworten (1)

Adolfo
Adolfo am 24 Mai 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

Kategorien

Mehr zu Financial Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by