-
Notifications
You must be signed in to change notification settings - Fork 13
/
RealizedVolatility.java
66 lines (53 loc) · 1.89 KB
/
RealizedVolatility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ievents;
import market.Price;
/**
* Created by author.
*
* The class RealizedVolatility is a practical realization of the theoretical work presented in the working paper
* "Bridging the gap between physical and intrinsic time". This Intrinsic Event volatility Estimator is based on the
* eq. 13. The right hand side of the equation is used to compute squared returns. It is the only crucial component of
* traditional volatility estimators.
*/
public class RealizedVolatility {
private double totalVolatility;
private double normalizedVolatility;
private DcOS dcOS;
private long timeFirstPrice, timeLastPrice;
private double sqrtOsDeviation;
private final long MLSEC_IN_YEAR = 31536000000L;
public RealizedVolatility(double threshold){
timeFirstPrice = timeLastPrice = 0L;
dcOS = new DcOS(threshold, threshold, -1, threshold, threshold, true);
}
public void run(Price aPrice){
int event = dcOS.run(aPrice);
if (event == 1 || event == -1){
sqrtOsDeviation += dcOS.computeSqrtOsDeviation();
}
if (timeFirstPrice == 0){
timeFirstPrice = aPrice.getTime();
}
timeLastPrice = aPrice.getTime();
}
public double finish(){
totalVolatility = Math.sqrt(sqrtOsDeviation);
return totalVolatility;
}
public double normalizeVolatility(double totalVolatility){
double coef = (double) MLSEC_IN_YEAR / (timeLastPrice - timeFirstPrice);
normalizedVolatility = totalVolatility * Math.sqrt(coef);
return normalizedVolatility;
}
public double getTotalVolatility() {
return totalVolatility;
}
public double getNormalizedVolatility() {
return normalizedVolatility;
}
public long getTimeFirstPrice() {
return timeFirstPrice;
}
public long getTimeLastPrice() {
return timeLastPrice;
}
}