Skip to content

Commit

Permalink
Agregada llamadas a API
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 21 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/mx/itam/DataHistorica.java
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();
}
}
87 changes: 83 additions & 4 deletions src/main/java/mx/itam/DataServices.java
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;
}
}
43 changes: 43 additions & 0 deletions src/main/java/mx/itam/Interval.java
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;
}
}
28 changes: 11 additions & 17 deletions src/main/java/mx/itam/MainUI.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package mx.itam;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.*;

/**
* This UI is the application entry point. A UI may either represent a browser window
Expand All @@ -25,17 +20,16 @@ public class MainUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();

final TextField name = new TextField();
name.setCaption("Type your name here:");

Button button = new Button("Click Me");
button.addClickListener(e -> {
layout.addComponent(new Label("Thanks " + name.getValue()
+ ", it works!"));
});

layout.addComponents(name, button);
DataServices data = new DataServices();
Grid<DataHistorica> grid = new Grid<>();
grid.setItems(data.getData_interval());
grid.addColumn(DataHistorica::getDate).setCaption("Fecha");
grid.addColumn(DataHistorica::getOpenPrice).setCaption("Apertura");
grid.addColumn(DataHistorica::getClosePrice).setCaption("Cierre");
grid.addColumn(DataHistorica::getMaxPrice).setCaption("Maximo");
grid.addColumn(DataHistorica::getMinPrice).setCaption("Minimo");
grid.setWidth("100%");
layout.addComponents(grid);

setContent(layout);
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/mx/itam/Symbol.java
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());
}
}

0 comments on commit c1c65cb

Please sign in to comment.