-
Notifications
You must be signed in to change notification settings - Fork 0
/
5Stocks.java
52 lines (49 loc) · 1.66 KB
/
5Stocks.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
public class StockData{
public static void main(String[] args){
FileIO io = new FileIO();
String[] original = io.load("C:\\ stockdata.txt");
int numrows=original.length;
int numcols=original[0].split("\t").length;
double[][] array = new double[numrows][numcols];
for(int i=1;i<numrows;i++){ //load in the data
for(int j=1;j<numcols;j++){
array[i][j]=Double.parseDouble(original[i].split("\t")[j]);
}
}
double drawdown=0;
String startdate="";
String finishdate="";
String company="";
for(int j=1;j<numcols;j++){ //repeat for all companies
double current=100; //start current price at 100%
double peak=100; //start peak is 100%
double trough=0;
String localstartdate="";
String localfinishdate="";
String recorddate="";
for(int i=numrows-1;i>0;i--){
//go through each day - data is backwards
current=current+(current*(array[i][j]/100));
//change the price for today
if(current>peak){ //if it's a record high update
peak=current;
recorddate=original[i].split("\t")[0];
//keep track of the date
}else if(1-current/peak>trough){
//otherwise, are we lower than ever before below the current peak?
trough=1-current/peak;
//keep track of this super low
localstartdate=recorddate;
localfinishdate=original[i].split("\t")[0];
}
}
if(trough>drawdown){
drawdown=trough;
startdate=localstartdate;
finishdate=localfinishdate;
company=original[0].split("\t")[j]; //remember the company
}
} //print out the overall results
System.out.println("The company with the highest drawdown was"+company+" which suffered a drawdown of"+String.format("%.1f",drawdown*100)+"% between the dates of "+startdate+"and "+finishdate);
}
}