Get Trade History

This commit is contained in:
Henry Varona 2016-12-18 20:40:03 -04:00
parent 0bb457ac4d
commit 6e2b886fd2
4 changed files with 151 additions and 2 deletions

View file

@ -0,0 +1,12 @@
package de.bitsharesmunich.graphenej;
/**
*
* @author henry
*/
public class MarketTrade {
public String date;
public double price;
public double amount;
public double value;
}

View file

@ -896,7 +896,7 @@ public class Test {
WebSocket mWebSocket = factory.createSocket(BLOCK_PAY_DE);
mWebSocket.addListener(new GetLimitOrders("1.3.0", "1.3.562", 100, new WitnessResponseListener() {
mWebSocket.addListener(new GetLimitOrders("1.3.0", "1.3.120", 100, new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
List<LimitOrder> orders = (List<LimitOrder>) response.result;
@ -927,4 +927,45 @@ public class Test {
System.out.println("IOException. Msg: " + e.getMessage());
}
}
void testGetTradeHistory() {
SSLContext context = null;
try {
context = NaiveSSLContext.getInstance("TLS");
WebSocketFactory factory = new WebSocketFactory();
// Set the custom SSL context.
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(BLOCK_PAY_DE);
Calendar from = Calendar.getInstance();
from.roll(Calendar.DAY_OF_MONTH, false);
from.roll(Calendar.DAY_OF_MONTH, false);
Calendar to = Calendar.getInstance();
to.roll(Calendar.DAY_OF_MONTH, false);
mWebSocket.addListener(new GetTradeHistory("BTS", "EUR", "20161215T0130000", "20161212T233000",100, new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
List<MarketTrade> orders = (List<MarketTrade>) response.result;
for(MarketTrade markeTrade : orders){
System.out.println("At " + markeTrade.date + " amount " + markeTrade.amount + " value " + markeTrade.value + " price " + markeTrade.price);
}
}
@Override
public void onError(BaseResponse.Error error) {
}
}));
mWebSocket.connect();
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());
}
}
}

View file

@ -6,7 +6,6 @@ import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFrame;
import com.sun.tools.internal.ws.processor.model.Block;
import de.bitsharesmunich.graphenej.RPC;
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
import de.bitsharesmunich.graphenej.models.*;

View file

@ -0,0 +1,97 @@
package de.bitsharesmunich.graphenej.api;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFrame;
import de.bitsharesmunich.graphenej.MarketTrade;
import de.bitsharesmunich.graphenej.RPC;
import de.bitsharesmunich.graphenej.interfaces.WitnessResponseListener;
import de.bitsharesmunich.graphenej.models.ApiCall;
import de.bitsharesmunich.graphenej.models.BaseResponse;
import de.bitsharesmunich.graphenej.models.WitnessResponse;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by nelson on 11/15/16.
*/
public class GetTradeHistory extends WebSocketAdapter {
private String a;
private String b;
private String toTime;
private String fromTime;
private int limit;
private WitnessResponseListener mListener;
public GetTradeHistory(String a, String b, String toTime, String fromTime,int limit, WitnessResponseListener mListener) {
this.a = a;
this.b = b;
this.toTime = toTime;
this.fromTime = fromTime;
this.limit = limit;
this.mListener = mListener;
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList<>();
accountParams.add(this.a);
accountParams.add(this.b);
accountParams.add(this.toTime);
accountParams.add(this.fromTime);
accountParams.add(this.limit);
ApiCall getAccountByName = new ApiCall(0, "get_trade_history", accountParams, RPC.VERSION, 1);
websocket.sendText(getAccountByName.toJsonString());
}
@Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
if (frame.isTextFrame()) {
System.out.println("<<< " + frame.getPayloadText());
}
try {
String response = frame.getPayloadText();
GsonBuilder builder = new GsonBuilder();
Type GetTradeHistoryResponse = new TypeToken<WitnessResponse<List<MarketTrade>>>() {
}.getType();
WitnessResponse<List<MarketTrade>> witnessResponse = builder.create().fromJson(response, GetTradeHistoryResponse);
if (witnessResponse.error != null) {
this.mListener.onError(witnessResponse.error);
} else {
this.mListener.onSuccess(witnessResponse);
}
} catch (Exception e) {
e.printStackTrace();
}
websocket.disconnect();
}
@Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
if (frame.isTextFrame()) {
System.out.println(">>> " + frame.getPayloadText());
}
}
@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
mListener.onError(new BaseResponse.Error(cause.getMessage()));
websocket.disconnect();
}
@Override
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
mListener.onError(new BaseResponse.Error(cause.getMessage()));
websocket.disconnect();
}
}