-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Se agregan clases para manejo de datos: [x] DataHistorica: Guarda los datos de una moneda en un intervalo. [x] Interval: Guarda los intervalos validos para el API de Binance. [x]Symbol: Guarda las conversiones de las criptomonedas validas para la API. Se agrega la clase DataServices que funciona como puente para recoleccion de datos. Funciones parametrizadas para obtener los datos de una cripto moneda en los ultimos 10 periodos (definidos por el intervalo) [se puso 10 arbitrario porque version de prueba]. MainUI: Agrega una Grid para ver la información, se agregará un histograma para hacerla más amigable.
- Loading branch information
Alex
committed
May 10, 2018
1 parent
9136e92
commit c1c65cb
Showing
6 changed files
with
262 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package mx.itam; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
public class DataHistorica { | ||
private long openTime; | ||
private double openPrice; | ||
private double maxPrice; | ||
private double minPrice; | ||
private double closePrice; | ||
|
||
public DataHistorica(long openTime, double openPrice, double maxPrice, double minPrice, double closePrice) { | ||
this.openTime = openTime; | ||
this.openPrice = openPrice; | ||
this.maxPrice = maxPrice; | ||
this.minPrice = minPrice; | ||
this.closePrice = closePrice; | ||
} | ||
|
||
public static String getDateFromEpoch(long time){ | ||
Timestamp stamp = new Timestamp(time); | ||
Date date = new Date(stamp.getTime()); | ||
return date.toString(); | ||
} | ||
|
||
public long getOpenTime() { | ||
return openTime; | ||
} | ||
public String getDate(){ | ||
return getDateFromEpoch(openTime); | ||
} | ||
|
||
public double getOpenPrice() { | ||
return openPrice; | ||
} | ||
|
||
public double getMaxPrice() { | ||
return maxPrice; | ||
} | ||
|
||
public double getMinPrice() { | ||
return minPrice; | ||
} | ||
|
||
public double getClosePrice() { | ||
return closePrice; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("Open Time: ").append(getDateFromEpoch(openTime)).append("\tOpen Price: ").append(openPrice); | ||
sb.append("\tMax Price: ").append(maxPrice).append("\tMin Price: ").append(minPrice); | ||
sb.append("\tClose Price: ").append(closePrice); | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,92 @@ | ||
package mx.itam; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import javax.xml.crypto.Data; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
public class DataServices { | ||
private final static String Endpoint = "https://api.binance.com/"; | ||
private ArrayList<String> Symbol_List; | ||
|
||
private final static String ENDPOINT = "https://api.binance.com/"; | ||
private ArrayList<Symbol> symbol_List; | ||
private ArrayList<Interval> intervals; | ||
private ArrayList<DataHistorica> data_interval; | ||
public DataServices(){ | ||
Symbol_List = new ArrayList<>(); | ||
symbol_List = new ArrayList<>(); | ||
try{ | ||
getSymbols(); | ||
getInterval(); | ||
Collections.sort(symbol_List); | ||
int index = symbol_List.indexOf(new Symbol("BTCUSDT")); | ||
getDataInterval(symbol_List.get(index),intervals.get(10),10); | ||
}catch(java.io.IOException ex){ | ||
//Error de conexion en API: Loggear este error | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
private void getSymbols() throws java.io.IOException{ | ||
OkHttpClient client = new OkHttpClient(); | ||
Request request = new Request.Builder() | ||
.url(ENDPOINT+"api/v1/exchangeInfo") | ||
.get() | ||
.build(); | ||
Response response = client.newCall(request).execute(); | ||
if(response.isSuccessful()) { | ||
JSONObject jsonResponse = new JSONObject(response.body().string()); | ||
JSONArray symbolArray=jsonResponse.getJSONArray("symbols"); | ||
for(int i=0; i< symbolArray.length();i++){ | ||
JSONObject symbolObj = symbolArray.getJSONObject(i); | ||
symbol_List.add(new Symbol(symbolObj.getString("symbol"),symbolObj.getString("baseAsset"),symbolObj.getString("quoteAsset"))); | ||
} | ||
}else{ | ||
//Error de que no conecto con la API | ||
System.out.println("Error getSymbols"); | ||
} | ||
} | ||
private void getInterval(){ | ||
intervals = new ArrayList<>(); | ||
intervals.add(new Interval("1m",1,0,0)); | ||
intervals.add(new Interval("3m",3,0,0)); | ||
intervals.add(new Interval("5m",5,0,0)); | ||
intervals.add(new Interval("15m",15,0,0)); | ||
intervals.add(new Interval("30m",30,0,0)); | ||
intervals.add(new Interval("1h",0,1,0)); | ||
intervals.add(new Interval("4h",0,4,0)); | ||
intervals.add(new Interval("12h",0,12,0)); | ||
intervals.add(new Interval("1d",0,0,1)); | ||
intervals.add(new Interval("3d",0,0,3)); | ||
intervals.add(new Interval("1w",0,0,7)); | ||
intervals.add(new Interval("1M",0,0,30)); | ||
} | ||
private long getEpochInMilis(Interval interval, int limit){ | ||
return System.currentTimeMillis() - interval.getTimeInMilis()*limit; | ||
} | ||
private void getDataInterval(Symbol symbol, Interval interval, int limit)throws java.io.IOException{ | ||
OkHttpClient client = new OkHttpClient(); | ||
Request request = new Request.Builder() | ||
.url(ENDPOINT+"api/v1/klines?symbol="+symbol.getSymbol()+"&interval="+interval.getTimeCode()+"&startTime="+getEpochInMilis(interval,limit)+"&limit="+limit) | ||
.get() | ||
.build(); | ||
System.out.println(ENDPOINT+"api/v1/klines?symbol="+symbol.getSymbol()+"&interval="+interval.getTimeCode()+"&startTime="+getEpochInMilis(interval,limit)+"&limit="+limit); | ||
Response response = client.newCall(request).execute(); | ||
if(response.isSuccessful()){ | ||
data_interval = new ArrayList<>(); | ||
JSONArray jsonArray = new JSONArray(response.body().string()); | ||
for(int i=0;i<jsonArray.length();i++){ | ||
JSONArray dataJson = jsonArray.getJSONArray(i); | ||
data_interval.add(new DataHistorica(dataJson.getLong(0), dataJson.getDouble(1),dataJson.getDouble(2),dataJson.getDouble(3),dataJson.getDouble(4))); | ||
} | ||
}else{ | ||
//Error de conexion con la API | ||
System.out.println("Error getDataInterval"); | ||
} | ||
} | ||
|
||
public ArrayList<DataHistorica> getData_interval() { | ||
return data_interval; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package mx.itam; | ||
|
||
public class Interval { | ||
private String timeCode; | ||
private String fullName; | ||
private long timeInMilis; | ||
|
||
public long getTimeInMilis() { | ||
return timeInMilis; | ||
} | ||
|
||
public Interval(String timeCode, int minutes, int hour, int days) { | ||
this.timeCode = timeCode; | ||
this.fullName = beautyInterval(timeCode); | ||
this.timeInMilis = 1000*60*minutes + 1000*60*60*hour + 1000*60*60*24*days; | ||
|
||
} | ||
|
||
@Override | ||
public String toString() { | ||
return fullName; | ||
} | ||
|
||
public String beautyInterval(String st){ | ||
if(st.contains("m")){ | ||
return st.replace("m","- Minutos"); | ||
} | ||
if(st.contains("h")){ | ||
return st.replace("h","- Horas"); | ||
} | ||
if(st.contains("d")){ | ||
return st.replace("d","- Dias"); | ||
} | ||
if(st.contains("M")){ | ||
return st.replace("M","- Mes"); | ||
} | ||
return st; | ||
} | ||
|
||
public String getTimeCode() { | ||
return timeCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package mx.itam; | ||
|
||
import java.util.Objects; | ||
|
||
public class Symbol implements Comparable { | ||
private String symbol; | ||
private String baseAsset; | ||
private String quoteAsset; | ||
|
||
public Symbol(String symbol, String baseAsset, String quoteAsset) { | ||
this.symbol = symbol; | ||
this.baseAsset = baseAsset; | ||
this.quoteAsset = quoteAsset; | ||
} | ||
|
||
public String getSymbol() { | ||
return symbol; | ||
} | ||
|
||
public String getBaseAsset() { | ||
return baseAsset; | ||
} | ||
|
||
public String getQuoteAsset() { | ||
return quoteAsset; | ||
} | ||
|
||
public Symbol(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return baseAsset+"\\"+quoteAsset; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Symbol)) return false; | ||
Symbol symbol1 = (Symbol) o; | ||
return Objects.equals(symbol, symbol1.symbol); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(symbol); | ||
} | ||
public int compareTo(Object obj){ | ||
if (!(obj instanceof Symbol)) { | ||
return -1; | ||
} | ||
Symbol symbol1 = (Symbol) obj; | ||
return this.symbol.compareTo(symbol1.getSymbol()); | ||
} | ||
} |