-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathObserver.java
More file actions
131 lines (122 loc) · 6.59 KB
/
Copy pathObserver.java
File metadata and controls
131 lines (122 loc) · 6.59 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
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class Observer {
public float getBid_prize() {
return bid_prize;
}
public float getAsk_prize() {
return ask_prize;
}
public float getBid_quant() {
return bid_quant;
}
public float getAsk_quant() {
return ask_quant;
}
float bid_prize, ask_prize, bid_quant, ask_quant;
public String getName() {
return name;
}
public float getFee() {
return fee;
}
float fee;
String name;
public String getCurrency() {
return currency;
}
String currency;
public Observer(float bidp, float askp, float bidq, float askq, String sname, String currencyname, float takerFee){
bid_prize =bidp;
ask_prize = askp;
bid_quant = bidq;
ask_quant = askq;
name = sname;
currency = currencyname;
fee = takerFee;
}
static Observer getDataBitbay(String currency1, String currency2, String currencyName) throws IOException, ParseException {
URL url = new URL("https://bitbay.net/API/Public/"+ currency1 + currency2 +"/orderbook.json");
URLConnection connection = url.openConnection();
BufferedReader b_reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
JSONObject data = (JSONObject) obj;
JSONArray bids = (JSONArray) data.get("bids");
JSONArray asks = (JSONArray) data.get("asks");
ArrayList bid = (ArrayList) bids.get(0);
ArrayList ask = (ArrayList) asks.get(0);
float bid_price = Float.valueOf(bid.get(0).toString());
float bid_quant = Float.valueOf(bid.get(1).toString());
float ask_price = Float.valueOf(ask.get(0).toString());
float ask_quant = Float.valueOf(ask.get(1).toString());
System.out.println("Bid= " + bid_price + "\t Quantity= " + bid_quant + "\t Ask = " + ask_price + "\t Quantitiy = " + ask_quant);
Observer ob1 = new Observer(bid_price, ask_price, bid_quant, ask_quant, "Bitbay", currencyName, (float) 0.0001);
b_reader.close();
return ob1;
}
static Observer getDataBittrex(String currency1, String currency2, String currencyName) throws IOException, ParseException {
URL url = new URL("https://api.bittrex.com/api/v1.1/public/getorderbook?market="+ currency1 +"-"+ currency2 +"&type=both");
URLConnection connection = url.openConnection();
BufferedReader b_reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
JSONObject data = (JSONObject) obj;
JSONObject results = (JSONObject) data.get("result");
JSONArray bids = (JSONArray) results.get("buy");
JSONArray asks = (JSONArray) results.get("sell");
JSONObject bid = (JSONObject) bids.get(0);
JSONObject ask = (JSONObject) bids.get(0);
float bid_price = Float.valueOf(bid.get("Rate").toString());
float bid_quant = Float.valueOf(bid.get("Quantity").toString());
float ask_price = Float.valueOf(ask.get("Rate").toString());
float ask_quant = Float.valueOf(ask.get("Quantity").toString());
System.out.println("Bid= " + bid_price + "\t Quantity= " + bid_quant + "\t Ask = " + ask_price + "\t Quantitiy = " + ask_quant);
Observer ob1 = new Observer(bid_price, ask_price, bid_quant, ask_quant, "Bittrex", currencyName, (float)0.0002);
b_reader.close();
return ob1;
}
static Observer getDataBitstamp(String currency1, String currency2, String currencyName) throws IOException, ParseException {
URL url = new URL("https://www.bitstamp.net/api/v2/order_book/"+ currency1 + currency2 +"/");
URLConnection connection = url.openConnection();
BufferedReader b_reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
JSONObject data = (JSONObject) obj;
JSONArray bids = (JSONArray) data.get("bids");
JSONArray asks = (JSONArray) data.get("asks");
ArrayList bid = (ArrayList) bids.get(0);
ArrayList ask = (ArrayList) asks.get(0);
float bid_price = Float.valueOf(bid.get(0).toString());
float bid_quant = Float.valueOf(bid.get(1).toString());
float ask_price = Float.valueOf(ask.get(0).toString());
float ask_quant = Float.valueOf(ask.get(1).toString());
System.out.println("Bid= " + bid_price + "\t Quantity= " + bid_quant + "\t Ask = " + ask_price + "\t Quantitiy = " + ask_quant);
Observer ob1 = new Observer(bid_price, ask_price, bid_quant, ask_quant, "Bittstamp", currencyName, (float)0.00025);
b_reader.close();
return ob1;
}
static Observer getDataBitfinex(String currency1, String currency2, String currencyName) throws IOException, ParseException {
URL url = new URL("https://api.bitfinex.com/v1/book/" + currency1 + currency2);
URLConnection connection = url.openConnection();
BufferedReader b_reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
JSONObject data = (JSONObject) obj;
JSONArray bids = (JSONArray) data.get("bids");
JSONArray asks = (JSONArray) data.get("asks");
JSONObject bid = (JSONObject) bids.get(0);
JSONObject ask = (JSONObject) bids.get(0);
float bid_price = Float.valueOf(bid.get("price").toString());
float bid_quant = Float.valueOf(bid.get("amount").toString());
float ask_price = Float.valueOf(ask.get("price").toString());
float ask_quant = Float.valueOf(ask.get("amount").toString());
System.out.println("Bid= " + bid_price + "\t Quantity= " + bid_quant + "\t Ask = " + ask_price + "\t Quantitiy = " + ask_quant);
Observer ob1 = new Observer(bid_price, ask_price, bid_quant, ask_quant, "Bitfinex", currencyName,(float)0.0002);
b_reader.close();
return ob1;
}
}