From c1c65cbab63eac17bed217f7c57bab8e0072c671 Mon Sep 17 00:00:00 2001 From: Alex <> Date: Wed, 9 May 2018 19:51:09 -0500 Subject: [PATCH] Agregada llamadas a API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- pom.xml | 10 +++ src/main/java/mx/itam/DataHistorica.java | 59 ++++++++++++++++ src/main/java/mx/itam/DataServices.java | 87 ++++++++++++++++++++++-- src/main/java/mx/itam/Interval.java | 43 ++++++++++++ src/main/java/mx/itam/MainUI.java | 28 +++----- src/main/java/mx/itam/Symbol.java | 56 +++++++++++++++ 6 files changed, 262 insertions(+), 21 deletions(-) create mode 100644 src/main/java/mx/itam/DataHistorica.java create mode 100644 src/main/java/mx/itam/Interval.java create mode 100644 src/main/java/mx/itam/Symbol.java diff --git a/pom.xml b/pom.xml index c63aada..32a806b 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,16 @@ + + com.squareup.okhttp3 + okhttp + 3.10.0 + + + org.json + json + 20180130 + javax.servlet javax.servlet-api diff --git a/src/main/java/mx/itam/DataHistorica.java b/src/main/java/mx/itam/DataHistorica.java new file mode 100644 index 0000000..fb59183 --- /dev/null +++ b/src/main/java/mx/itam/DataHistorica.java @@ -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(); + } +} diff --git a/src/main/java/mx/itam/DataServices.java b/src/main/java/mx/itam/DataServices.java index f64c558..5ec4d65 100644 --- a/src/main/java/mx/itam/DataServices.java +++ b/src/main/java/mx/itam/DataServices.java @@ -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 Symbol_List; - + private final static String ENDPOINT = "https://api.binance.com/"; + private ArrayList symbol_List; + private ArrayList intervals; + private ArrayList 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 getData_interval() { + return data_interval; + } } diff --git a/src/main/java/mx/itam/Interval.java b/src/main/java/mx/itam/Interval.java new file mode 100644 index 0000000..42622df --- /dev/null +++ b/src/main/java/mx/itam/Interval.java @@ -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; + } +} diff --git a/src/main/java/mx/itam/MainUI.java b/src/main/java/mx/itam/MainUI.java index 97d1b26..85afa6c 100644 --- a/src/main/java/mx/itam/MainUI.java +++ b/src/main/java/mx/itam/MainUI.java @@ -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 @@ -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 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); } diff --git a/src/main/java/mx/itam/Symbol.java b/src/main/java/mx/itam/Symbol.java new file mode 100644 index 0000000..09cba44 --- /dev/null +++ b/src/main/java/mx/itam/Symbol.java @@ -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()); + } +}