-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathInstantaneousVolatility.java
71 lines (61 loc) · 2.25 KB
/
InstantaneousVolatility.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
67
68
69
70
71
package ievents;
import market.Price;
/**
* The class InstantaneousVolatility is a practical realization of the theoretical work presented in the paper
* "Waiting Times and Number of Directional Changes in an Intrinsic Time Framework". This Intrinsic Event volatility
* Estimator uses the number of directional-change intrinsic events as the indicator of the realized volatility.
* The used equation is \sigma = \delta \sqrt( N_{DC} / T )
*/
public class InstantaneousVolatility {
private DcOS dcOS; // the instance used to register intrinsic events
private long timeFirstPrice, timeLastPrice; // to actualize the computed volat
private int numDCs; // in the whole sample
private final long MLSEC_IN_YEAR = 31536000000L;
private double threshold; // used to register intrinsic events
private double totalVolat; // is the volatility of the whole sample
private double annualVolat; // annualized volatility
/**
* Constructor
* @param threshold size in percentage (1% = 0.01)
*/
public InstantaneousVolatility(double threshold){
timeFirstPrice = timeLastPrice = 0L;
dcOS = new DcOS(threshold, threshold, 1, threshold, threshold, true);
this.threshold = threshold;
}
/**
* Should be run for each price
* @param aPrice
*/
public void run(Price aPrice){
int event = dcOS.run(aPrice);
if (event == 1 || event == -1){
numDCs += 1;
}
if (timeFirstPrice == 0){
timeFirstPrice = aPrice.getTime();
}
timeLastPrice = aPrice.getTime();
}
/**
* Run it in the very end of the analysis
* @return the annualized volatility
*/
public void finish(){
numDCtoVolatility();
}
/**
* Computes volatility using the formula \sigma = \delta \sqrt( N_{DC} / T )
*/
private void numDCtoVolatility(){
double numYearsInWholeSample = (double) (timeLastPrice - timeFirstPrice) / MLSEC_IN_YEAR;
annualVolat = threshold * Math.sqrt(numDCs / numYearsInWholeSample);
totalVolat = annualVolat * Math.sqrt(numYearsInWholeSample);
}
public double getTotalVolat() {
return totalVolat;
}
public double getAnnualVolat() {
return annualVolat;
}
}