Looping intervals for a dataset

2 visualizaciones (últimos 30 días)
Mikkel Mølbak Christiansen el 13 de Abr. de 2022
Respondida: Lokesh el 21 de Sept. de 2023
I am having problems making a loop that seperates a dataset into intervals. I have a dataset with stock prices, volume and timestamps and i want to calculate the variance of prices for each interval of timestamps. Let's say intervals of 9:30-10:00, 10:00-11:30,... 3:30-4:00 and then i want for example the variance of prices inside these intervals. Another problem regarding the timestamps is that my dataset has date and time in the same column and i don't know how to only use time.
I also have trouble making a loop for volumes. If let's say i want intervals of 100-499, 500-999, 1000-1999, 2000-4999, 5000-max and then again want to calculate variance for prices or some other metric. It's primearly the looping of intervals and being able to calculate something for each intervals that are causing me problems.
Hope someone can help me with this puzzle. Thanks.
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Respuestas (1)

Lokesh el 21 de Sept. de 2023
Hi Christiansen,
I understand that you want to calculate variance of prices for each interval of timestamps and volumes from the dataset containing stock prices, volume and timestamps.
• To create intervals based on time, you can use “discretize” function by applying it to the timestamps and specifying the desired intervals.
• To extract the time component from a column that contains both date and time, you can use the "timeofday" function.
• For calculating the variance, you can use the “var” function.
• And for creating intervals for the volume, you can follow the same steps used for creating time intervals.
Below is an example code to calculate the variance of prices for each time interval.
% Sample timestamps, prices, and volume
timestamps = [datetime('2021-09-15 09:35:00'), datetime('2021-09-15 09:45:00'), datetime('2021-09-15 10:05:00'), datetime('2021-09-15 10:20:00'), datetime('2021-09-15 11:00:00'), datetime('2021-09-15 09:15:00'), datetime('2021-09-15 10:10:00')];
prices = [50, 55, 58, 52, 60, 57, 62];
volume = [1000, 1500, 2000, 800, 1200, 900, 1100];
% Extract the time component from timestamps
time = timeofday(timestamps);
% Define time intervals
intervals = [timeofday(datetime('09:00:00')), timeofday(datetime('09:30:00')), timeofday(datetime('10:00:00')), timeofday(datetime('10:30:00')), timeofday(datetime('11:00:00'))];
% Discretize time into intervals
intervalIndex = discretize(time, intervals);
% Initialize a cell array to store prices within each interval
intervalPrices = cell(numel(intervals)-1, 1);
% Group prices within each interval
for ind = 1:numel(time)
interval = intervalIndex(ind);
intervalPrices{interval} = [intervalPrices{interval}, prices(ind)];
end
% Calculate the variances for each interval
priceVariances = cellfun(@var, intervalPrices);
% Display the calculated variance along with time interval as a table
intervalData = table(intervals(1:end-1)', intervals(2:end)', intervalPrices, priceVariances, 'VariableNames', {'IntervalStart', 'IntervalEnd', 'Prices', 'PriceVariance'});
disp(intervalData);
Please note that this code uses a sample dataset with the variables "timestamps", "prices", and "volume". You can replace these with your actual data. For calculating the variance for volume intervals, the code can be modified accordingly.
Please go through the below links to know more about the functions used above:
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

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