XChange icon indicating copy to clipboard operation
XChange copied to clipboard

GDAX implementing GDAX Historic Rates

Open madwicket opened this issue 9 years ago • 7 comments

I was wondering if I could get some help with an error I am seeing with implementing GDAX Historic Rates. Comments are in Bold

I have added this to GDAX.java @get @path("products/{baseCurrency}-{targetCurrency}/candles?start={startTime}&end={endTime}&granularity={granularityInSecs}") GDAXHistoricRates[] getHistoricRates(@pathparam("baseCurrency") String baseCurrency, @pathparam("targetCurrency") String targetCurrency, @pathparam("startTime") String startTime, @pathparam("endTime") String endTime, @pathparam("granularityInSecs") String granularityInSecs) throws IOException;

Added a class of GDAXHistoricRates:

package org.knowm.xchange.gdax.dto.marketdata;

import java.math.BigDecimal;

/**

Created by MadWicket on 7/1/2017. */ public class GDAXHistoricRates { private final Long time; private final BigDecimal low; private final BigDecimal high; private final BigDecimal open; private final BigDecimal close; private final BigDecimal volume;

/* public GDAXHistoricRates(@jsonproperty("time") String time, @jsonproperty("low") BigDecimal low, @jsonproperty("high") BigDecimal high, @jsonproperty("open") BigDecimal open, @jsonproperty("close") BigDecimal close, @jsonproperty("volume") BigDecimal volume) {*/ public GDAXHistoricRates(Long time,BigDecimal low, BigDecimal high, BigDecimal open, BigDecimal close, BigDecimal volume) { this.time = time; this.low = low; this.high = high; this.open = open; this.close = close; this.volume = volume; }

public Long getTime() { return time; }

public BigDecimal getLow() { return low; }

public BigDecimal getHigh() { return high; }

public BigDecimal getOpen() { return open; }

public BigDecimal getClose() { return close; }

public BigDecimal getVolume() { return volume; }

@override public String toString() { return "GDAXHistoricRates [time=" + time + ", low=" + low + ", high=" + high + ", open=" + open + ", close=" + close + ", volume=" + volume + "]"; } }

Also have added this function to GDAXMarketServiceRaw

public GDAXHistoricRates[] getHistoricRates(CurrencyPair currencyPair, String startTime, String endTime, String granularityInSecs) throws IOException { return this.coinbaseEx.getHistoricRates(currencyPair.base.getCurrencyCode(), currencyPair.counter.getCurrencyCode(), startTime, endTime, granularityInSecs); }

When I test the code with: CurrencyPair btcUSDPair = new CurrencyPair(Currency.BTC, Currency.USD); String startTime = "2017-07-01T10:00:00.000000-0500"; String endTime = "2017-07-01T11:00:00.000000-0500"; String granularity = "60"; GDAXHistoricRates[] rates = gdaxMarketDataServiceRaw.getHistoricRates(btcUSDPair, startTime, endTime, granularity);

I am getting an error: Exception in thread "main" si.mazi.rescu.HttpStatusIOException: Can not deserialize instance of org.knowm.xchange.gdax.dto.marketdata.GDAXHistoricRates out of START_ARRAY token at [Source: [[1498924740,2465.98,2468.06,2465.98,2467.83,1.51111634],[1498924680,2465,2470.49,2468.6,2465,22.22688688000002],[1498924620,2467.58,2472.3,2472.3,2467.58,3.8412693099999995],[1498924560,2471.01,2473.81,2473.81,2471.04,2.601899999999999],[1498924500,2472.8,2474.02,2472.8,2473.76,4.348945669999999],[1498924440,2471.17,2474.99,2474.98,2471.33,6.413041009999999],[1498924380,2472.06,2475,2472.06,2475,0.95145589],[1498924320,2469.99,2472.06,2469.99,2472.06,2.777323000000001],[1498924260,2469.98,2469.99,2469.99,2469.98,3.27080923],[1498924200,2469.99,2469.99,2469.99,2469.99,3.73715842],[1498924140,2469.98,2469.98,2469.98,2469.98,0.4433263],[1498924080,2467.52,2469.98,2467.78,2469.98,6.6533103],[1498924020,2467.52,2469.98,2469.44,2467.52,4.722014950000001],[1498923960,2467.77,2469.99,2469.02,2467.83,18.09031563],[1498923900,2468.95,2469.99,2469.99,2468.95,20.984654690000003],[1498923840,2469.23,2469.99,2469.23,2469.95,0.80937389],[1498923780,2468.61,2469.97,2468.61,2468.95,2.7652663399999997],[1498923720,2468.61,2468.61,2468.61,2468.61,0.8389],[1498923660,2468.61,2469,2469,2468.61,2.6861],[1498923600,2468.85,2469.01,2468.86,2469,0.6688000000000001],[1498923540,2469.01,2469.98,2469.98,2469.01,1.16738877],[1498923480,2469.29,2469.99,2469.98,2469.98,1.82293339],[1498923420,2467.99,2469.99,2469.97,2469.99,2.3856853399999993],[1498923360,2468.74,2469.99,2469.44,2469.99,1.83640219],[1498923300,2468.12,2469.99,2468.99,2468.12,9.15815478],[1498923240,2465.32,2469,2468.89,2468.99,1.97067207],[1498923180,2468.97,2469.11,2469.11,2468.99,0.75413867],[1498923120,2465.21,2469.11,2466.58,2469.07,13.527611269999996],[1498923060,2466.57,2466.58,2466.57,2466.58,6.374597879999998],[1498923000,2465.01,2466.57,2465.39,2466.57,3.5704083599999996],[1498922940,2465.32,2465.47,2465.46,2465.39,5.396296830000001],[1498922880,2464.89,2465.47,2465.24,2465.47,8.66986411],[1498922820,2463.98,2465.96,2465.56,2465.24,4.514513849999999],[1498922760,2463.99,2466.45,2465.24,2465.73,2.73812368],[1498922700,2463.18,2465.22,2463.18,2465.22,1.7727281],[1498922640,2460,2464.28,2463.09,2464.05,4.078915359999999],[1498922580,2461.99,2464.28,2464.24,2464.28,3.3786699499999986],[1498922520,2464.25,2464.27,2464.27,2464.25,2.26559554],[1498922460,2464.07,2465.45,2464.07,2464.28,2.09086203],[1498922400,2461.93,2464.19,2463.54,2464.1,2.84171432],[1498922340,2461.16,2464.42,2464.42,2463.55,4.548074169999997],[1498922280,2458.24,2466.64,2458.24,2464.44,6.322822399999999],[1498922220,2455.34,2458.24,2455.4,2458.19,4.9823014799999985],[1498922160,2455.28,2455.92,2455.9,2455.28,1.33220846],[1498922100,2455.16,2456.99,2455.88,2455.28,2.08494906],[1498922040,2454.65,2455.87,2455.58,2455.85,3.1533982600000003],[1498921980,2454.65,2455.88,2455.88,2454.65,8.257270729999998],[1498921920,2454.65,2455.88,2454.65,2455.88,2.8405079300000002],[1498921860,2454.32,2454.66,2454.32,2454.66,1.5852210500000001],[1498921800,2453.36,2454.62,2453.37,2453.36,2.6746598600000002],[1498921740,2452.93,2454.59,2454.57,2453.45,2.8144810200000006],[1498921680,2454.87,2455.88,2454.87,2455.46,0.7601],[1498921620,2454.47,2454.71,2454.47,2454.68,3.2206431899999997],[1498921560,2454.4,2454.68,2454.4,2454.52,0.94935581],[1498921500,2454.55,2455.8,2455.8,2454.55,0.47332071000000003],[1498921440,2451.51,2455.77,2454.32,2455.77,3.41248868],[1498921380,2454.66,2455.85,2454.66,2455.85,1.72702815],[1498921320,2454.11,2454.66,2454.11,2454.64,2.8339],[1498921260,2450,2454.1,2452.64,2454.1,9.362184749999996],[1498921200,2450.76,2459.35,2459.35,2450.76,22.831026350000005]]; line: 1, column: 2] (through reference chain: Object[][0]) at si.mazi.rescu.ResponseReader.read(ResponseReader.java:104) at si.mazi.rescu.RestInvocationHandler.mapInvocationResult(RestInvocationHandler.java:169) at si.mazi.rescu.RestInvocationHandler.receiveAndMap(RestInvocationHandler.java:157) at si.mazi.rescu.RestInvocationHandler.invoke(RestInvocationHandler.java:120) at com.sun.proxy.$Proxy11.getHistoricRates(Unknown Source) at org.knowm.xchange.gdax.service.GDAXMarketDataServiceRaw.getHistoricRates(GDAXMarketDataServiceRaw.java:83) at org.knowm.xchange.gdax.GDAXMain.main(GDAXMain.java:72)

So I can see that it is reaching out and getting the data but for some reason it is not deserializing the data. I have tried returning the data into a two dimensional array, but that doesn't seem to do the trick.

madwicket avatar Jul 02 '17 15:07 madwicket

It's failing on the JSON deserialization. What's the JSON look like from GDAX?

You should take a look at the Wiki page here: https://github.com/timmolter/XChange/wiki/New-Implementation-Best-Practices

Starting on step 3, get the JSON, create a unit test class, and keep fixing your DTO until it all works out. With the unit test you'll be able to see the jackson exceptions and be able to google the specific deserialization problems you're encountering...

timmolter avatar Jul 02 '17 16:07 timmolter

It is weird the output of json is literally this:

[[1498924740,2465.98,2468.06,2465.98,2467.83,1.51111634],[1498924680,2465,2470.49,2468.6,2465,22.22688688000002]]

There are no json properties, and from their api reads:

RESPONSE ITEMS

Each bucket is an array of the following information:

time bucket start time low lowest price during the bucket interval high highest price during the bucket interval open opening price (first trade) in the bucket interval close closing price (last trade) in the bucket interval volume volume of trading activity during the bucket interval

I am still fairly new to java so I feel I am missing something simple.

madwicket avatar Jul 03 '17 01:07 madwicket

GOT THE DTO TO WORK!!!!!!!!!!!!!!!!!!!!!!!!

package org.knowm.xchange.gdax.dto.marketdata;

import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.io.IOException; import java.math.BigDecimal;

/**

  • Created by MadWicket on 7/1/2017. */ @JsonDeserialize(using = GDAXHistoricRates.GDAXOrderDeserializer.class) public class GDAXHistoricRates {

private final long time; private final BigDecimal low; private final BigDecimal high; private final BigDecimal open; private final BigDecimal close; private final BigDecimal volume;

public GDAXHistoricRates(long time,BigDecimal low, BigDecimal high, BigDecimal open, BigDecimal close, BigDecimal volume) {
this.time = time;
this.low = low;
this.high = high;
this.open = open;
this.close = close;
this.volume = volume;

}

public long getTime() { return time; }

public BigDecimal getLow() { return low; }

public BigDecimal getHigh() { return high; }

public BigDecimal getOpen() { return open; }

public BigDecimal getClose() { return close; }

public BigDecimal getVolume() { return volume; }

@Override public String toString() { return "GDAXHistoricRates [time=" + time + ", low=" + low + ", high=" + high + ", open=" + open + ", close=" + close + ", volume=" + volume + "]"; }

static class GDAXOrderDeserializer extends JsonDeserializer {

@Override
public GDAXHistoricRates deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {

  ObjectCodec oc = jsonParser.getCodec();
  JsonNode node = oc.readTree(jsonParser);
  if (node.isArray()) {
    long time = node.path(0).asLong();
    BigDecimal low = new BigDecimal(node.path(1).asText());
    BigDecimal high = new BigDecimal(node.path(2).asText());
    BigDecimal open = new BigDecimal(node.path(3).asText());
    BigDecimal close = new BigDecimal(node.path(4).asText());
    BigDecimal volume = new BigDecimal(node.path(5).asText());

    return new GDAXHistoricRates(time,low,high,open,close,volume);
  }
  return null;
}

} }

Implementation of code in main to call:

CurrencyPair btcUSDPair = new CurrencyPair(Currency.BTC, Currency.USD); String startTime = "2017-07-01T10:00:00.000000-0500"; String endTime = "2017-07-01T11:00:00.000000-0500"; String granularity = "60"; GDAXHistoricRates[] rates = gdaxMarketDataServiceRaw.getHistoricRates(btcUSDPair, startTime, endTime, granularity); System.out.println(rates[0]);

madwicket avatar Jul 03 '17 23:07 madwicket

Good work! Looks like the correct way to do it at first glance.

timmolter avatar Jul 04 '17 07:07 timmolter

Was this ever implemented? I can't find a way to get the historic rates anywhere..?

jpsgamboa avatar Jan 16 '18 10:01 jpsgamboa

in which version we will get this feature? I found it very useful...

kuceraf avatar Feb 01 '18 19:02 kuceraf

This would be very useful for me as well.

subnetmarco avatar Feb 12 '18 03:02 subnetmarco