-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnalyzeValues.java
130 lines (108 loc) · 5.5 KB
/
AnalyzeValues.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
//Author: Matthew Lee
//In collaboration with Timothy Poehlman and Griffin Nicolino for WWU CS Hackathon 2019
//TODO: Add Documentation
import java.util.Set;
import com.google.gson.*;
//Today's High
//Today's low
//Yesterday ending
//
public class AnalyzeValues{
static double estimateTomorrowClosing(JsonObject input, int time) {
double difference = 0;
String timePeriod;
if(time == 0) {
timePeriod = "Time Series (Daily)";
} else if (time == 1) {
timePeriod = "Weekly Time Series";
} else {
timePeriod = "Monthly Time Series";
}
JsonObject dailySeries = input.getAsJsonObject(timePeriod);
//from what I understand, due to the way the keySet() function is constructed, using the map interface, the resulting set preserves the order of entry, and it preserves it when
//converting to another ordered data structure as well.
Set<String> keys = dailySeries.keySet();
String[] keysArray = keys.toArray(new String[keys.size()]);
JsonObject today = dailySeries.getAsJsonObject(keysArray[0]);
//alternatively, simply retrieve this as a double. today.get("4. close").getAsDouble();
//I'm not sure if this .get(int).toString() business works, but I think it should, as Eclipse tends to warn me if something is out of place.
double todayNum = Double.valueOf(today.get("4. close").toString().replaceAll("^\"|\"$", ""));
if(time == 0) {
for(int i = 0; i < 7; i++){
JsonObject day = dailySeries.getAsJsonObject(keysArray[i]);
JsonObject dayBefore = dailySeries.getAsJsonObject(keysArray[i+1]);
double dayNum = Double.valueOf(day.get("4. close").toString().replaceAll("^\"|\"$", ""));
double afterNum = Double.valueOf(dayBefore.get("4. close").toString().replaceAll("^\"|\"$", ""));
difference += (dayNum - afterNum);
}
difference /=7;
} else if(time == 2){
for(int i = 0; i < 12; i++){
JsonObject day = dailySeries.getAsJsonObject(keysArray[i]);
JsonObject dayBefore = dailySeries.getAsJsonObject(keysArray[i+1]);
double dayNum = Double.valueOf(day.get("4. close").toString().replaceAll("^\"|\"$", ""));
double afterNum = Double.valueOf(dayBefore.get("4. close").toString().replaceAll("^\"|\"$", ""));
difference += (dayNum - afterNum);
}
difference /=12;
} else {
for(int i = 0; i < 4; i++){
JsonObject day = dailySeries.getAsJsonObject(keysArray[i]);
JsonObject dayBefore = dailySeries.getAsJsonObject(keysArray[i+1]);
double dayNum = Double.valueOf(day.get("4. close").toString().replaceAll("^\"|\"$", ""));
double afterNum = Double.valueOf(dayBefore.get("4. close").toString().replaceAll("^\"|\"$", ""));
difference += (dayNum - afterNum);
}
difference /= 4;
}
//returning today's closing value added to the average difference in closing over the past week
return (todayNum + difference);
}
static String lastTradingDayStats(JsonObject input) {
//getting today's opening and closing values - same as the first part of the estimateTomorrowClosing() function
JsonObject dailySeries = input.getAsJsonObject("Time Series (Daily)");
Set<String> keys = dailySeries.keySet();
String[] keysArray = keys.toArray(new String[keys.size()]);
JsonObject today = dailySeries.getAsJsonObject(keysArray[0]);
String open = today.get("1. open").toString().replaceAll("^\"|\"$", "");
String close = today.get("4. close").toString().replaceAll("^\"|\"$", "");
String high = today.get("2. high").toString().replaceAll("^\"|\"$", "");
String low = today.get("3. low").toString().replaceAll("^\"|\"$", "");
return "Opening Price: " + open + " Closing Price: " + close + " High: " + high + " Low: " + low;
}
static String intradayStats(JsonObject input) {
JsonObject currentSeries = input.getAsJsonObject("Time Series (1min)");
Set<String> keys = currentSeries.keySet();
String[] keysArray = keys.toArray(new String[keys.size()]);
JsonObject currenttime = currentSeries.getAsJsonObject(keysArray[0]);
String open = currenttime.get("1. open").toString().replaceAll("^\"|\"$", "");
String close = currenttime.get("4. close").toString().replaceAll("^\"|\"$", "");
String high = currenttime.get("2. high").toString().replaceAll("^\"|\"$", "");
String low = currenttime.get("3. low").toString().replaceAll("^\"|\"$", "");
return "Opening Price: " + open + " Closing Price: " + close + " High: " + high + " Low: " + low;
}
static String foreignExchange(JsonObject input) {
JsonObject currentExchange = input.getAsJsonObject("Realtime Currency Exchange Rate");
String from = currentExchange.get("2. From_Currency Name").toString().replaceAll("^\"|\"$", "");
String to = currentExchange.get("4. To_Currency Name").toString().replaceAll("^\"|\"$", "");
String value = currentExchange.get("5. Exchange Rate").toString().replaceAll("^\"|\"$", "");
return "A single " + from + " is equal to " + value + " " + to;
}
/*
static void generateGraph(JsonObject input) {
int[] values = new int[365];
int[] indices = new int[365];
JsonObject dailySeries = input.getAsJsonObject("Time Series (Daily)");
JsonArray arrayVersion = dailySeries.getAsJsonArray();
for(int i = 0; i < 365; i++){
JsonObject day = dailySeries.getAsJsonObject(arrayVersion.get(i).toString());
double dayNum = Double.valueOf(day.get("4. close").toString());
//the graph library being used can't handle
dayNum*=100;
values[i] = (int) dayNum;
}
for(int i = 0; i<365; i++) {
indices[i] = i;
}
}*/
}