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: