Skip to content

Commit ae4c5e8

Browse files
committed
Loading data from file when there already is an up-to-date data in the directory
==> For faster data access
1 parent 3918754 commit ae4c5e8

File tree

7 files changed

+4637
-89
lines changed

7 files changed

+4637
-89
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[[{"location":"D:\\Projects\\Github\\JavaAppCourseProject\\Java_Course_Project\\Statistical_Analysis","type":"PROJECT","hints":{"PROJECT_NAME":"Statistical_Analysis"}},"ABSENT"],[{"location":"C:\\Program Files\\Java\\jre1.8.0_131","type":"JRE","hints":{"EXECUTION_ENVIRONMENT":"JavaSE-1.8"}},"jre:jre:1.8.0"]]
1+
[[{"location":"C:\\Program Files\\Java\\jre1.8.0_144","type":"JRE","hints":{"EXECUTION_ENVIRONMENT":"JavaSE-1.8"}},"jre:jre:1.8.0"]]
Binary file not shown.

Statistical_Analysis/Data/ADBE.csv

Lines changed: 4493 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
GENE
1+
AAPL,2017-11-07,2000-01-03
2+
TSLA,2017-11-07,2010-06-29
3+
GENE,2017-11-07,2005-09-06
4+
ADBE,2017-11-07,2000-01-03

Statistical_Analysis/src/gui/GUIMain.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ public List<Double> getScores() {
153153
}
154154

155155
private static void createAndShowGui() {
156-
Stock S_TSLA = new Stock("TSLA");
157-
S_TSLA.retrieve();
158-
ArrayList TSLA_A = S_TSLA.request("CLOSE", "2011-01-01", "2017-05-16");
156+
Stock S_AAPL = new Stock("AAPL");
157+
S_AAPL.retrieve();
158+
ArrayList AAPL_A = S_AAPL.request("CLOSE", "2015-01-01", "2017-05-16");
159159

160-
GUIMain mainPanel = new GUIMain(TSLA_A);
160+
GUIMain mainPanel = new GUIMain(AAPL_A);
161161
mainPanel.setPreferredSize(new Dimension(800, 600));
162162
JFrame frame = new JFrame("DrawGraph");
163163
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Statistical_Analysis/src/stat_c/Stock.java

Lines changed: 129 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Stock {
2828
public String FirtstDate;
2929

3030
ArrayList Total = new ArrayList();
31-
public ArrayList<Double> Close = new ArrayList<Double>();
31+
public ArrayList<Double> Close = new ArrayList<Double>();
3232
public ArrayList<String> Date = new ArrayList<String>();
3333
public ArrayList<Double> Open = new ArrayList<Double>();
3434
public ArrayList<Double> Volume = new ArrayList<Double>();
@@ -58,74 +58,126 @@ public Stock(String SYMBOL) {
5858
*/
5959
@SuppressWarnings("unchecked")
6060
public void retrieve() {
61-
URL url;
62-
URLConnection urlConn = null;
63-
BufferedReader br = null;
64-
String msg;
65-
StringBuilder bd;
66-
InputStream ins = null;
61+
checkLocal(); //First, check if the data is there and is up-to-date.
6762

68-
String line = "";
69-
String csvSplitBy = ",";
70-
71-
//Using AlphaVantage API to retrieve information
72-
String urlString = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" +
73-
SYMBOL +"&apikey=QX75TG29OOA9H7CJ&datatype=csv&outputsize=full";
74-
75-
//Connecting to AlphaVantage with my apikey
76-
try {
77-
url = new URL(urlString);
78-
urlConn = url.openConnection();
79-
ins = url.openStream();
80-
} catch (IOException e) {
81-
e.printStackTrace();
82-
} finally {
83-
br = new BufferedReader(new InputStreamReader(ins));
84-
bd = new StringBuilder();
85-
msg = null;
63+
if(localAvailability != true) {
64+
URL url;
65+
URLConnection urlConn = null;
66+
BufferedReader br = null;
67+
String msg;
68+
StringBuilder bd;
69+
InputStream ins = null;
70+
71+
String line = "";
72+
String csvSplitBy = ",";
73+
74+
//Using AlphaVantage API to retrieve information
75+
String urlString = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" +
76+
SYMBOL +"&apikey=QX75TG29OOA9H7CJ&datatype=csv&outputsize=full";
77+
78+
//Connecting to AlphaVantage with my apikey
8679
try {
87-
int u = 0; // This variable is just for skipping the first line of the data which all are strings
88-
while((msg = br.readLine()) != null)
89-
{
90-
String tmpList[] = msg.split(csvSplitBy);
91-
92-
bd.append(msg);
93-
Total.add(msg);
94-
Total.add("\n");
95-
bd.append("\n");
96-
97-
list.add(tmpList[0]);
98-
list.add(tmpList[1]);
99-
list.add(tmpList[2]);
100-
list.add(tmpList[3]);
101-
list.add(tmpList[4]);
102-
list.add(tmpList[5]);
103-
if(u != 0) {
104-
Close_M.put(tmpList[0], Double.valueOf(tmpList[4]));
105-
Open_M.put(tmpList[0], Double.valueOf(tmpList[1]));
106-
Volume_M.put(tmpList[0], Double.valueOf(tmpList[5]));
107-
High_M.put(tmpList[0], Double.valueOf(tmpList[2]));
108-
Low_M.put(tmpList[0], Double.valueOf(tmpList[3]));
109-
DATE_M.put(tmpList[0], tmpList[0]);
110-
}
111-
u++;
112-
}
80+
url = new URL(urlString);
81+
urlConn = url.openConnection();
82+
ins = url.openStream();
11383
} catch (IOException e) {
114-
// TODO Auto-generated catch block
11584
e.printStackTrace();
11685
} finally {
117-
System.out.println("Retrieval Done");
86+
br = new BufferedReader(new InputStreamReader(ins));
87+
bd = new StringBuilder();
88+
msg = null;
89+
try {
90+
int u = 0; // This variable is just for skipping the first line of the data which all are strings
91+
while((msg = br.readLine()) != null)
92+
{
93+
String tmpList[] = msg.split(csvSplitBy);
94+
95+
bd.append(msg);
96+
Total.add(msg);
97+
Total.add("\n");
98+
bd.append("\n");
99+
100+
list.add(tmpList[0]);
101+
list.add(tmpList[1]);
102+
list.add(tmpList[2]);
103+
list.add(tmpList[3]);
104+
list.add(tmpList[4]);
105+
list.add(tmpList[5]);
106+
if(u != 0) {
107+
Close_M.put(tmpList[0], Double.valueOf(tmpList[4]));
108+
Open_M.put(tmpList[0], Double.valueOf(tmpList[1]));
109+
Volume_M.put(tmpList[0], Double.valueOf(tmpList[5]));
110+
High_M.put(tmpList[0], Double.valueOf(tmpList[2]));
111+
Low_M.put(tmpList[0], Double.valueOf(tmpList[3]));
112+
DATE_M.put(tmpList[0], tmpList[0]);
113+
}
114+
u++;
115+
}
116+
} catch (IOException e) {
117+
// TODO Auto-generated catch block
118+
e.printStackTrace();
119+
} finally {
120+
System.out.println("Retrieval Done");
121+
}
122+
123+
try {
124+
br.close();
125+
} catch (IOException e) {
126+
e.printStackTrace();
127+
}
118128
}
119-
129+
saveCSV(); //Saving data as a file
130+
Extractor(); //Extracting and saving separated datasets in an instance
131+
recordLocal();
132+
}
133+
134+
else if(localAvailability == true) {
135+
File file = new File("./Data/" + SYMBOL + ".csv");
136+
String msg = null;
137+
BufferedReader br = null;
120138
try {
121-
br.close();
122-
} catch (IOException e) {
123-
e.printStackTrace();
139+
FileReader fr = new FileReader(file);
140+
FileInputStream fis = new FileInputStream(file);
141+
br = new BufferedReader(new InputStreamReader(fis));
142+
} catch (FileNotFoundException e1) {
143+
// TODO Auto-generated catch block
144+
e1.printStackTrace();
145+
} finally {
146+
try {
147+
int u = 0; // This variable is just for skipping the first line of the data which all are strings
148+
while((msg = br.readLine()) != null)
149+
{
150+
String tmpList[] = msg.split(",");
151+
Total.add(msg);
152+
Total.add("\n");
153+
154+
list.add(tmpList[0]);
155+
list.add(tmpList[1]);
156+
list.add(tmpList[2]);
157+
list.add(tmpList[3]);
158+
list.add(tmpList[4]);
159+
list.add(tmpList[5]);
160+
if(u != 0) {
161+
Close_M.put(tmpList[0], Double.valueOf(tmpList[4]));
162+
Open_M.put(tmpList[0], Double.valueOf(tmpList[1]));
163+
Volume_M.put(tmpList[0], Double.valueOf(tmpList[5]));
164+
High_M.put(tmpList[0], Double.valueOf(tmpList[2]));
165+
Low_M.put(tmpList[0], Double.valueOf(tmpList[3]));
166+
DATE_M.put(tmpList[0], tmpList[0]);
167+
}
168+
u++;
169+
}
170+
} catch (IOException e) {
171+
// TODO Auto-generated catch block
172+
e.printStackTrace();
173+
} finally {
174+
System.out.println("Loading " + SYMBOL + " Done");
175+
}
124176
}
125-
}
126-
saveCSV(); //Saving data as a file
127-
Extractor(); //Extracting and saving separated datasets in an instance
128-
recordLocal();
177+
}
178+
179+
180+
129181
}
130182
/*
131183
* Data saving and manipulating methods.
@@ -295,18 +347,18 @@ public void recordLocal() {
295347
File file = new File("./DataMeta");
296348
if(file.exists() != true) {
297349
file.mkdir();
298-
recordLocal(); //Call again
299-
} else if(file.exists()) {
350+
} else if(file.exists() == true) {
300351
file = new File("./DataMeta/Availability.csv"); // 0 : Symbol 1 : fisrt available date 2 : last available date
301352
try {
302-
FileOutputStream fos = new FileOutputStream(file);
303-
FileWriter fw = new FileWriter(file);
304-
if(checkLocal() == false) {
305-
fw.write(SYMBOL);
306-
} else if(checkLocal() == true) {
307-
return;
353+
//FileOutputStream fos = new FileOutputStream(file); //It is this fileoutputstream that forcedly overwrites on a file
354+
//Remember not to make the same mistake
355+
FileWriter fw = new FileWriter(file, true);
356+
BufferedWriter buffw = new BufferedWriter(fw);
357+
if(localAvailability == false) {
358+
fw.append(SYMBOL + "," + this.Date.get(0) + "," + this.Date.get(Date.size() - 1) + "\n");
359+
} else if(localAvailability == true) {
360+
System.out.println("Local File Available");
308361
}
309-
310362
fw.close();
311363
} catch (Exception e) { e.printStackTrace();}
312364
}
@@ -315,25 +367,21 @@ public Boolean checkLocal() {
315367
File file = new File("./DataMeta");
316368
if(file.exists() != true) {
317369
file.mkdir();
318-
recordLocal(); //Call again
319-
} else if(file.exists()) {
370+
} else if(file.exists() == true) {
320371
file = new File("./DataMeta/Availability.csv"); // 0 : Symbol 1 : fisrt available date 2 : last available date
321372
try {
322373
FileReader fr = new FileReader(file);
323374
FileInputStream fis = new FileInputStream(file);
324-
StringBuilder builder = new StringBuilder();
325375
String line = null;
326376
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
327377
while((line = br.readLine()) != null) {
328378
String tmp[] = line.split(",");
329-
for(int i = 0; i < tmp.length; i++) {
330-
if(tmp[0] == SYMBOL) {
331-
localAvailability = true;
332-
return true;
333-
} else {
334-
localAvailability = false;
335-
return false;
336-
}
379+
if(tmp[0].equals(SYMBOL)) { // tmp[0] == SYMBOL did not work. Remember not to make the same mistake
380+
//Also, I have to find out a way to know which date is the latest date for stock information
381+
localAvailability = true;
382+
break;
383+
} else if (tmp[0] != SYMBOL){
384+
continue;
337385
}
338386
}
339387
} catch (Exception e) { e.printStackTrace();

Statistical_Analysis/src/stat_c/Test.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ public class Test {
2525
*/ //These tests are done. Stock class finished.
2626

2727
//SStat class test
28-
28+
2929
Stock S = new Stock("AAPL");
3030
Stock S_TSLA = new Stock("TSLA");
3131
Stock S_GENE = new Stock("GENE");
32+
Stock S_ADBE = new Stock("ADBE");
33+
3234
S.retrieve();
3335
S_TSLA.retrieve();
3436
S_GENE.retrieve();
37+
S_ADBE.retrieve();
3538

3639
SStat St = new SStat(S);
3740
SStat St2 = new SStat(S_TSLA);
3841
SStat St3 = new SStat(S_GENE);
3942

43+
4044
System.out.println("<<<" + S.SYMBOL + ">>>");
4145
System.out.println(St.Geometricmean("CLOSE", "2015-12-25", "2016-01-01"));
4246
ArrayList tmpArray1 = St.PercentageChange("CLOSE", 100, "2012-12-15", "2016-01-01");
@@ -59,7 +63,7 @@ public class Test {
5963
tmpArray2 = St3.PercentageChange("OPEN", 300, "2012-12-15", "2016-01-01");
6064
System.out.println(tmpArray2);
6165

62-
66+
System.out.println("<<<" + S_ADBE.SYMBOL + ">>>");
6367
}
6468

6569
public static void main(String[] ar) {

0 commit comments

Comments
 (0)