-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPriceMoveCountScalingLaw.java
133 lines (119 loc) · 4.22 KB
/
PriceMoveCountScalingLaw.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package scalinglaws;
import ievents.DcOS;
import market.Price;
import tools.Tools;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by author.
*
* This class computats of the "Price move count scaling law", Law 2 from
* the "Patterns in high-frequency FX data: Discovery of 12 empirical scaling laws".
*
* The results is normalized to one year of physical time.
*
* One of the results of the computation is a file with two columns: Delta, NumPriceMove.
* Another result is the coefficients of the scaling law.
*
* In order to run the code one should:
* choose the lowest and highest size of thresholds;
* define number of points between the chosen thresholds;
* choose whether to store results to a file or no.
*/
public class PriceMoveCountScalingLaw {
private double[] arrayDeltas;
private int numPoints;
private final long MLSEC_IN_YEAR = 31557600000L;
private double[] scalingLawParam;
private long startTime, finalTime;
private boolean initialized;
private double[] oldLevels; // levels at which the previous price move was registered
private double[] numPriceMoves; // of specific size
/**
* The constructor of the class.
* @param lowDelta
* @param higDelta
* @param numPoints
*/
public PriceMoveCountScalingLaw(float lowDelta, float higDelta, int numPoints){
arrayDeltas = Tools.GenerateLogSpace(lowDelta, higDelta, numPoints);
this.numPoints = numPoints;
oldLevels = new double[numPoints];
numPriceMoves = new double[numPoints];
initialized = false;
}
/**
*
* @param aPrice is the next observed (generated, recorded...) price
*/
public void run(Price aPrice){
if (!initialized){
startTime = aPrice.getTime();
for (int i = 0; i < numPoints; i++){
oldLevels[i] = aPrice.getFloatMid();
}
initialized = true;
}
for (int i = 0; i < numPoints; i++){
double midPrice = aPrice.getFloatMid();
if (Math.abs(midPrice - oldLevels[i]) / oldLevels[i] >= arrayDeltas[i]){
numPriceMoves[i] += 1;
oldLevels[i] = midPrice;
}
}
finalTime = aPrice.getTime();
}
/**
* Should be called in the very end of the experiment when the data is over
* @return the scaling law parameters [C, E, r^2]
*/
public double[] finish(){
normalize();
return computeParams();
}
/**
* The function normalizes the number of price moves to one year.
*/
public void normalize(){
double normalCoeff = (double) MLSEC_IN_YEAR / (finalTime - startTime) ;
for (int i = 0; i < numPoints; i++){
numPriceMoves[i] *= normalCoeff;
}
System.out.println("Normalize coefficients to 1 year: DONE");
}
/**
* The function finds the scaling law parameters defined in "Patterns in high-frequency FX data: Discovery of 12
* empirical scaling laws J.B.", page 13
* @return the same what the function Tools.computeScalingParams returns
*/
public double[] computeParams(){
scalingLawParam = Tools.computeScalingParams(arrayDeltas, numPriceMoves);
return scalingLawParam;
}
/**
* @param dirName is the name of the output folder.
*/
public void saveResults(String dirName){
Tools.CheckDirectory(dirName);
try {
String dateString = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss").format(new Date());
String fileName = "4_priceMoveCountScalingLaw" + "_" + dateString + ".csv";
PrintWriter writer = new PrintWriter(dirName + "/" + fileName, "UTF-8");
writer.println("Delta;NumPriceMove");
for (int i = 0; i < numPoints; i++){
writer.println(arrayDeltas[i] + ";" + numPriceMoves[i]);
}
writer.close();
System.out.println("The file is saved as: " + fileName);
} catch (Exception ex){
ex.printStackTrace();
}
}
public double[] getNumPriceMoves() {
return numPriceMoves;
}
public double[] getArrayDeltas() {
return arrayDeltas;
}
}