Compute variable within time interval
14 views (last 30 days)
Show older comments
Hello everyone, I have problem when compute a variable within time interval. In more detail, my dataset is as below:
Time 04:00:00 04:00:01 ......
Bid Price 100 101...
Ask Price 111 112......
I want to creare a new variable named "Spread" for each 1-minute interval. "Spead" = (Ask-Bid)/(0.5*(Ask+Bid))
Ask is the lowest ask price in 1-minute interval. Bid is the highest bid in 1-minute interval.
Thank you so much. I really appreciate your help. I am a new beginner with Matlab.
1 Comment
Answers (1)
Anmol Dhiman
on 15 Nov 2019
Hi,
You may find the below code useful for solving the problem.
% Changing the time format
datetime.setDefaultFormats('default','hh:mm::ss')
t1 = datetime(0,0,0,4,0,0);
t2 = datetime(0,0,0,4,59,59);
Time = (t1:seconds(1):t2).';
% Enter your BidPrice and AskPrice here
BidPrice = randi([0,200],3600,1);
AskPrice = randi([200,400],3600,1);
TT = timetable(Time,BidPrice,AskPrice);
% Defining Interval for Grouping Time of 1-minute
TT.time_interval = datetime(0,0,0,hour(TT.Time),minute(TT.Time),0);
% Calculating the minimum AskPrice and Maximum BidPrice in 1-minute time interval
Ask_min_timetable = varfun(@min,TT,'GroupingVariables','time_interval','InputVariables','AskPrice');
Bid_max_timetable = varfun(@max,TT,'GroupingVariables','time_interval','InputVariables','BidPrice');
Ask_min = Ask_min_timetable.min_AskPrice;
Bid_max = Bid_max_timetable.max_BidPrice;
% Calculating "Spead"
Spead = 2*(Ask_min - Bid_max)./(Ask_min + Bid_max);
Hope it helps.
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!