@@ -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 ();
0 commit comments