Compute variable within time interval

조회 수: 13 (최근 30일)
Dung Nguyen
Dung Nguyen 2019년 11월 10일
답변: Anmol Dhiman 2019년 11월 15일
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
Oren B
Oren B 2019년 11월 10일
you can use timer object to calculet every 1 minute interval.

댓글을 달려면 로그인하십시오.

답변 (1개)

Anmol Dhiman
Anmol Dhiman 2019년 11월 15일
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.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by