-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTCData.java
More file actions
172 lines (143 loc) · 5.27 KB
/
BTCData.java
File metadata and controls
172 lines (143 loc) · 5.27 KB
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
package BTC_PriceTracker;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import com.google.gson.stream.JsonReader;
import java.io.InputStreamReader;
import java.net.URL;
public class BTCData {
private final IntegerProperty rawTime;
private final IntegerProperty timeStamp;
private final FloatProperty open;
private final FloatProperty high;
private final FloatProperty low;
private final FloatProperty close;
private final IntegerProperty timeFrom;
private final IntegerProperty timeTo;
public BTCData(int raw_time, int time, Float open, Float high, Float low, Float close, int timeFrom, int timeTo){
this.rawTime = new SimpleIntegerProperty(this, "rawTime");
this.timeStamp = new SimpleIntegerProperty(this, "time");
this.open = new SimpleFloatProperty(this, "open");
this.high = new SimpleFloatProperty(this, "high");
this.low = new SimpleFloatProperty(this, "low");
this.close = new SimpleFloatProperty(this, "close");
this.timeFrom = new SimpleIntegerProperty(this, "timeFrom");
this.timeTo = new SimpleIntegerProperty(this, "timeTo");
this.setRawTime(raw_time);
this.setTimeStamp(time);
this.setOpen(open);
this.setHigh(high);
this.setLow(low);
this.setClose(close);
this.setTimeFrom(timeFrom);
this.setTimeTo(timeTo);
}
// Getters
public Integer getRawTime(){
return rawTime.get();
}
public Integer getTimeStamp(){
return timeStamp.get();
}
public Float getOpen(){
return open.get();
}
public Float getHigh(){ // not used
return high.getValue();
}
public Float getLow(){ // not used
return low.get();
}
public Float getClose(){ // not used
return close.get();
}
public Integer getTimeFrom(){
return timeFrom.get();
}
public Integer getTimeTo(){
return timeTo.get();
}
// Setters
public void setRawTime(int value) {
this.rawTime.set(value);
}
public void setTimeStamp(int value){
this.timeStamp.set(value);
}
public void setOpen(float value) {
this.open.set(value);
}
public void setClose(float value) {
this.close.set(value);
}
public void setLow(float value) {
this.low.set(value);
}
public void setHigh(float value) {
this.high.set(value);
}
public void setTimeFrom(int value){
this.timeFrom.set(value);
}
public void setTimeTo(int value){
this.timeTo.set(value);
}
// Dynamically load API endpoints
public static ObservableList<BTCData> getData(String t){
int interval = 0;
int multiple = 0;
if (t.equals("day")){
interval = 30; // for 30 days
multiple = 86400;
}
if (t.equals("hour")){
interval = 24; // for 24 hours
multiple = 3600;
}
if (t.equals("minute")){
interval = 60; // for 60 minutes
multiple = 60;
}
String URL = String.format("https://min-api.cryptocompare.com/data/histo%s?aggregate=1&e=CCCAGG&extraParams=CryptoCompare&fsym=BTC&limit=%d&tryConversion=false&tsym=USD", t, interval);
System.out.println(URL);
ObservableList<BTCData> values = FXCollections.observableArrayList();
try {
URL address = new URL(URL);
JsonReader reader = new JsonReader(new InputStreamReader(address.openStream()));
Gson gson = new Gson();
JsonObject root = gson.fromJson(reader, JsonObject.class); // get root JSON object
int timeTo = root.get("TimeTo").getAsInt();
int timeFrom = root.get("TimeFrom").getAsInt();
JsonArray data = root.getAsJsonArray("Data"); // get "Data" JSON array
for (JsonElement j: data){
JsonObject jsonObject = j.getAsJsonObject();
// grab time stamp
int raw_time = jsonObject.get("time").getAsInt(); // time from epoch 01/01/1970
int time;
// check whether timestamp is start or end
if (!(raw_time == timeFrom) && !(raw_time == timeTo)){
time = (raw_time - timeFrom)/multiple;
} else {
if (raw_time == timeFrom){ // if timestamp is the start, then set to 0
time = 0;
} else {
time = (raw_time - timeFrom)/multiple;
}
}
Float open = jsonObject.get("open").getAsFloat();
Float high = jsonObject.get("high").getAsFloat(); // not used
Float low = jsonObject.get("low").getAsFloat(); // not used
Float close = jsonObject.get("close").getAsFloat(); // not used
values.add(new BTCData(raw_time, time, open, high, low, close, timeFrom, timeTo));
}
return values;
} catch (Exception e){
e.printStackTrace();
}
return null;
}
}