I have intraday data for a given stock spanning over 3 months sorted by date and time.
My Data Looks Like:
DTM1 Tradeprice
03JUN2013:09:15:00 164.6
03JUN2013:09:15:00 167
03JUN2013:09:15:00 166
03JUN2013:09:15:01 164.15
03JUN2013:09:15:01 176.1
03JUN2013:09:15:01 169.3
03JUN2013:09:15:01 165
................................................
03JUN2013:15:29:00 180.7
03JUN2013:15:29:00 170
................................................
03JUN2013:15:29:59 180.3
04JUN2013:09:15:00 176
04JUN2013:09:15:00 175.5
................................................
04JUN2013:09:27:00 165.1
...............................................
04JUN2013:09:28:00 164.3
..............................................
05JUN2013:09:29:00 170
...............................................
Please Note:
1. For every Second there are multiple Tradeprices
2. Data Moves Second by Second to next minute
3. Data Changes next Date as reprented by Variable DTM1
I want to Compute 1. Price Volatility (Standard Deviation) for Every Minute
2. Price Volatility for Every 5 Minutes
3. Return [(Last Price - First Price)/First Price *100 ]Volatility for Every Minute
2. Return Volatility for Every 5 Minutes
3. HI (Max Price / Return) & Low (Min Price / Return) for Every Minute and 5 minute and Day interval.
Attaching the Dataset for your reference.
Please Help.
Thanks in Advance
Ritesh
For the 5-minute return volatility you want the std of 300 1-second returns, where you define 1-second return as
[(last price within the second) - (first price within the second)] / (first price within the second).
Two points:
I would take option 2, which is equivalent to dropping all 1-second intervals in which there is no trading, as per the program below.
OK, back to the operational problem.
Price volatility for 1-minute intervals (up to 60 prices) and 5-minute intervals (up to 300 prices). If it's a liquid stock, using average price per second, vs closing price, is just not going to have much impact on your price volatilty measure. Unless you intend to start studying high frequency trading as it exists today, which makes whole-second time stamps archaic (trades and quotes are now time-stamped to the nanosecond). So let's simplify your operational needs and use closing price for price volatility measures, which is also consistent with any likely definition of returns.
Return volatility. I'm going to use the closing-price to closing-price measure for return.
data need / view=need;
set have end=end_of_have1;
by stock dtm1;
date=datepart(dtm1); format date yymmddn8.;
time=timepart(dtm1); format time time8.;
if last.dtm1;
single_minute= time-mod(time,60);
five_minute= time-mod(time,300);
format single_minute five_minute time8. ;
holding_period=dif(dtm1); /* holding period in secs, almost always=1*/
return= dif(price)/lag(price);
if dif(date)^=0 then do;
holding_period=.;
return=.;
end;
run;
proc summary data=need;
by stock date five_minute;
class single_minute;
output out=want std(price)=price_vol std(return)=return_vol;
run;
Notes:
You can ask proc summary for other stats as well.
"Price volatility for every minute" has a clear meaning, i.e. the std dev of all prices within the minute (or within 5 minutes).
But what do you mean by "return volatility for every minute"?. According to your definition, you calculate only one return per minute (based on last price - first price). There is no return volatility. Or do you mean you want
please clarify
HI @mkeintz, Good Afternoon!!
The authors of the research article that I am following have used the following equation for computing return volatility:
RVOL5min = SQRT[Sigma i = 1 to 300 (Ret i - Avg(Ret))^2/N-1] {300 denotes Seconds for 5 minutes}
But I believe what you suggested sounds more apt to me i.e.; 1. Within minute (and within 5 minute) volatility of trade-by-trade returns.
Thanks
Ritesh
How do you want to handle the multiple prices per second.
HI @Reeza,
Either the first Price of that second or the average price of that second can be taken.
Thanks
Ritesh
For the 5-minute return volatility you want the std of 300 1-second returns, where you define 1-second return as
[(last price within the second) - (first price within the second)] / (first price within the second).
Two points:
I would take option 2, which is equivalent to dropping all 1-second intervals in which there is no trading, as per the program below.
OK, back to the operational problem.
Price volatility for 1-minute intervals (up to 60 prices) and 5-minute intervals (up to 300 prices). If it's a liquid stock, using average price per second, vs closing price, is just not going to have much impact on your price volatilty measure. Unless you intend to start studying high frequency trading as it exists today, which makes whole-second time stamps archaic (trades and quotes are now time-stamped to the nanosecond). So let's simplify your operational needs and use closing price for price volatility measures, which is also consistent with any likely definition of returns.
Return volatility. I'm going to use the closing-price to closing-price measure for return.
data need / view=need;
set have end=end_of_have1;
by stock dtm1;
date=datepart(dtm1); format date yymmddn8.;
time=timepart(dtm1); format time time8.;
if last.dtm1;
single_minute= time-mod(time,60);
five_minute= time-mod(time,300);
format single_minute five_minute time8. ;
holding_period=dif(dtm1); /* holding period in secs, almost always=1*/
return= dif(price)/lag(price);
if dif(date)^=0 then do;
holding_period=.;
return=.;
end;
run;
proc summary data=need;
by stock date five_minute;
class single_minute;
output out=want std(price)=price_vol std(return)=return_vol;
run;
Notes:
You can ask proc summary for other stats as well.
Did the solutions from your previous questions not work? I think we've seen several variations of the same question here, hopefully you've learned the methods and can expand on them.
No and no. Proc expand is more efficient. Wasn't one of the solutions previously using PROC Expand?
If you have a big table try Hash Table, not SQL .
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.