Compute variable within time interval

13 Ansichten (letzte 30 Tage)
Dung Nguyen
Dung Nguyen am 10 Nov. 2019
Beantwortet: Anmol Dhiman am 15 Nov. 2019
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 Kommentar
Oren B
Oren B am 10 Nov. 2019
you can use timer object to calculet every 1 minute interval.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Anmol Dhiman
Anmol Dhiman am 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.

Kategorien

Mehr zu Shifting and Sorting Matrices 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