-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockInput.java
294 lines (253 loc) · 8.31 KB
/
StockInput.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package Prototype_003;
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class StockInput {
private List<String> dateStringList;
private List<Double> openPriceList;
private List<Double> highPriceList;
private List<Double> lowPriceList;
private List<Double> closePriceList;
private List<Double> adjClosePriceList;
private List<BigInteger> volumeList;
private List<Date> xDateList;
private Date[] xDates;
private double[] openPrices;
private double[] highPrices;
private double[] lowPrices;
private double[] closePrices;
private double[] adjClosePrices;
private BigInteger[] volumes;
private SimpleDateFormat sdf;
public StockInput() {
dateStringList = new ArrayList<>();
openPriceList = new ArrayList<>();
highPriceList = new ArrayList<>();
lowPriceList = new ArrayList<>();
closePriceList = new ArrayList<>();
adjClosePriceList = new ArrayList<>();
volumeList = new ArrayList<>();
xDateList = new ArrayList<>();
xDates = new Date[xDateList.size()];
openPrices = new double[openPriceList.size()];
highPrices = new double[highPriceList.size()];
lowPrices = new double[lowPriceList.size()];
closePrices = new double[closePriceList.size()];
adjClosePrices = new double[adjClosePriceList.size()];
volumes = new BigInteger[volumeList.size()];
sdf = new SimpleDateFormat("yyy-MM-dd");
}
/**
* This method takes in a .csv file and parses the data into Lists and Arrays.
* Reads the data with a Scanner and adds the data to ArrayLists.
* Then copies the data from ArrayLists to Arrays for method/class use.
* @param stockData
*/
public void insertStockData(File stockData) {
dateStringList = new ArrayList<>();
openPriceList = new ArrayList<>();
highPriceList = new ArrayList<>();
lowPriceList = new ArrayList<>();
closePriceList = new ArrayList<>();
adjClosePriceList = new ArrayList<>();
volumeList = new ArrayList<>();
try {
Scanner input = new Scanner(stockData);
//Separates data from .csv file if there is a comma afterwards or a new line.
input.useDelimiter(",|\n");
input.useLocale(Locale.getDefault());
//Skips the first row that describes the type of data
input.skip("Date,Open,High,Low,Close,Adj Close,Volume");
while(input.hasNext()) {
//System.out.println(input.next());
dateStringList.add(input.next());
openPriceList.add(Double.valueOf(input.next()));
highPriceList.add(Double.valueOf(input.next()));
lowPriceList.add(Double.valueOf(input.next()));
closePriceList.add(Double.valueOf(input.next()));
adjClosePriceList.add(Double.valueOf(input.next()));
volumeList.add(new BigInteger(input.next()));
}
//System.out.println(volumes.toString());
populateData(
dateStringList,
openPriceList, highPriceList, lowPriceList,
closePriceList, adjClosePriceList, volumeList
);
input.close();
} catch(FileNotFoundException e) {
System.out.println(e.getMessage());
} catch(NoSuchElementException e) {
System.out.println(e.getMessage());
} catch(NumberFormatException e) {
System.out.println(e.getMessage());
} catch(ParseException e) {
System.out.println(e.getMessage());
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
/**
* Gets stock data in the form of Lists.
* Creates a new Lists for the x axis of xChart OHLC Charts called xDateList.
* Copies parameter list data to arrays via a regular for loop.
* @param dateStringList
* @param openPriceList
* @param highPriceList
* @param lowPriceList
* @param closePriceList
* @param adjClosePriceList
* @param volumeList
* @throws ParseException
*/
protected void populateData(
List<String> dateStringList, List<Double> openPriceList, List<Double> highPriceList,
List<Double> lowPriceList, List<Double> closePriceList, List<Double> adjClosePriceList,
List<BigInteger> volumeList
) throws ParseException {
this.xDateList = new ArrayList<>();
this.xDates = new Date[dateStringList.size()];
this.openPrices = new double[openPriceList.size()];
this.highPrices = new double[highPriceList.size()];
this.lowPrices = new double[lowPriceList.size()];
this.closePrices = new double[closePriceList.size()];
this.adjClosePrices = new double[adjClosePriceList.size()];
this.volumes = new BigInteger[volumeList.size()];
/*
* Thanks to xChart and their developers/collaborators for their example code,
* particularly OHLCChart01's populateData() method for showing how to fill in
* array data for xChart OHLC Chart and line chart.
*
* OHLCChart01.java:
* https://github.com/knowm/XChart/blob/9abbb92d37c69cd305f00fe10ff090cabda81ee5/
* xchart-demo/src/main/java/org/knowm/xchart/demo/charts/ohlc/OHLCChart01.java#L22
*
* Modified method to get additional lists and list types from parameters.
* Replaced Calendar with just Date object type.
* Copies ArrayList elements to arrays instead of adding data to new Lists.
*/
this.sdf = new SimpleDateFormat("yyy-MM-dd");
for(int i = 0; i < dateStringList.size(); i++) {
Date date = sdf.parse(dateStringList.get(i));
xDateList.add(date);
xDates[i] = date;
openPrices[i] = openPriceList.get(i);
highPrices[i] = highPriceList.get(i);
lowPrices[i] = lowPriceList.get(i);
closePrices[i] = closePriceList.get(i);
adjClosePrices[i] = adjClosePriceList.get(i);
volumes[i] = volumeList.get(i);
}
}
/*
* Getters for Lists and Arrays that contain stock data.
*/
/**
* Getter for List containing stock dates as String data types.
* @return dateStringList
*/
public List<String> getDates() {
return dateStringList;
}
/**
* Getter for List containing open prices of a stock as Double data types.
* @return openPriceList
*/
public List<Double> getOpenPriceList() {
return openPriceList;
}
/**
* Getter for List containing high prices of a stock as Double data types.
* @return highPriceList
*/
public List<Double> getHighPriceList() {
return highPriceList;
}
/**
* Getter for List containing low prices of a stock as Double data types.
* @return lowPriceList
*/
public List<Double> getLowPriceList() {
return lowPriceList;
}
/**
* Getter for List containing close prices of a stock as Double data types.
* @return closePriceList
*/
public List<Double> getClosePriceList() {
return closePriceList;
}
/**
* Getter for List containing adjusted close prices of a stock as Double data types.
* @return adjClosePriceList
*/
public List<Double> getAdjClosePriceList() {
return adjClosePriceList;
}
/**
* Getter for List containing volumes of a stock as BigInteger data types.
* @return volumeList
*/
public List<BigInteger> getVolumeList() {
return volumeList;
}
/**
* Getter for List containing stock dates as Date data types.
* Primarily for xChart OHLC Charts.
* @return xDateList
*/
public List<Date> getXDateList() {
return xDateList;
}
/**
* Getter for array containing stock dates as Date data types.
* @return xDates.
*/
public Date[] getXDates() {
return xDates;
}
/**
* Getter for an array of type double containing open prices.
* @return openPrices
*/
public double[] getOpenPrices() {
return openPrices;
}
/**
* Getter for an array of type double containing high prices.
* @return highPries
*/
public double[] getHighPrices() {
return highPrices;
}
/**
* Getter for an array of type double containing low prices.
* @return lowPrices
*/
public double[] getLowPrices() {
return lowPrices;
}
/**
* Getter for an array of type double containing close prices.
* @return closePrices
*/
public double[] getClosePrices() {
return closePrices;
}
/**
* Getter for an array of type double containing adjusted close prices.
* @return adjClosePrices
*/
public double[] getAdjClosePrices() {
return adjClosePrices;
}
/**
* Getter for an array of type BigInteger containing volumes of a stock.
* @return volumes
*/
public BigInteger[] getVolumes() {
return volumes;
}
}