WojciechZankowski / iextrading4j

IEX Cloud open source API wrapper
Apache License 2.0
121 stars 46 forks source link

ChartRequestBuilder() produces Charts containing null values #33

Closed stromilas closed 5 years ago

stromilas commented 5 years ago

Code below produces Charts with public fields containing "null" values, while private fields contain correct values.

IEXTradingClient tradingClient;
List<Chart> chartList = tradingClient.executeRequest(new ChartRequestBuilder()
                .withChartRange(ChartRange.ONE_MONTH)
                .withSymbol("IBM")
                .build());

chartList.get(0).getMarketClose() returns null

Code obtained from your examples.

From debugger: github_issue

WojciechZankowski commented 5 years ago

Hey,

It is because of chart endpoint. If you check chart endpoint for different chart ranges you will see that it returns different values.

Endpoint: https://api.iextrading.com/1.0/stock/aapl/chart/1m

{
    "date": "2018-10-08",
    "open": 222.21,
    "high": 224.8,
    "low": 220.2,
    "close": 223.77,
    "volume": 29663923,
    "unadjustedVolume": 29663923,
    "change": -0.52,
    "changePercent": -0.232,
    "vwap": 222.7821,
    "label": "Oct 8",
    "changeOverTime": 0
}

Endpoint: https://api.iextrading.com/1.0/stock/aapl/chart/1d

{
    "date": "20181108",
    "minute": "09:30",
    "label": "09:30 AM",
    "high": 210.09,
    "low": 209.5,
    "average": 209.761,
    "volume": 7871,
    "notional": 1651026.95,
    "numberOfTrades": 70,
    "marketHigh": 210.12,
    "marketLow": 209.5,
    "marketAverage": 209.961,
    "marketVolume": 1135077,
    "marketNotional": 238321673.2102,
    "marketNumberOfTrades": 2937,
    "open": 209.965,
    "close": 209.68,
    "marketOpen": 209.94,
    "marketClose": 209.62,
    "changeOverTime": 0,
    "marketChangeOverTime": 0
}

I would need to introduce more hierarchy for the chart endpoint and split some chart request to prevent having null values in the Chart object.

I will mark it as Enhancement.

Thanks,

Wojtek

stromilas commented 5 years ago

In my case I'm trying to retrieve few years of stock price history of several specific companies to display as a chart without much knowledge in trading. How would you suggest I should approach this using your library ?

WojciechZankowski commented 5 years ago

So I think you have two options:

  1. Do multiple Chart requests with high Chart Range.

In this case you would need to do separate request for each symbol.

final List<Chart> aapl = iexTradingClient.executeRequest(new ChartRequestBuilder()
        .withChartRange(ChartRange.TWO_YEARS)
        .withSymbol("AAPL")
        .build());

final List<Chart> ibm = iexTradingClient.executeRequest(new ChartRequestBuilder()
        .withChartRange(ChartRange.FIVE_YEARS)
        .withSymbol("IBM")
        .build());
  1. Use Batch endpoint.

IEX gives a possiblity to do batch requests. It means you can get chart data from last 5 years for multiple symbols in one call.

final Map<String, BatchStocks> batchResult = iexTradingClient.executeRequest(
        new BatchMarketStocksRequestBuilder()
                                .addType(BatchStocksType.CHART)
                .withSymbols(Arrays.asList("IBM", "AAPL"))
                .withChartRange(ChartRange.FIVE_YEARS)
                .build());

final BatchStocks ibmBatch = batchResult.get("IBM");
final List<Chart> ibmBatchChart = ibmBatch.getChart();

final BatchStocks aaplBatch = batchResult.get("AAPL");
final List<Chart> aaplBatchChart = ibmBatch.getChart();

Because you need the data from couple years I would use bigger Chart Range. IEX support following ones:

ONE_MONTH,
THREE_MONTHS,
SIX_MONTHS,
YEAR_TO_DATE,
ONE_YEAR,
TWO_YEARS,
FIVE_YEARS

And for listed resolutions above you will only recieve this data:

final Chart point = chartList.get(0);
point.getDate();
point.getOpen();
point.getHigh();
point.getLow();
point.getClose();
point.getVolume();
point.getUnadjustedVolume();
point.getChange();
point.getChangePercent();
point.getVwap();
point.getLabel();
point.getChangeOverTime();

so if you want to plot Candlestick chart you should use getOpen(), getHigh(), getLow(), getClose() methods. If you want to plot simple line chart then you can calculate typical price and draw it or just choose one of values.

Typical Price = ( Close + High + Low + Open ) / 4 or ( High + Low + Close ) / 3

Cheers,

Wojtek

WojciechZankowski commented 5 years ago

IEXCloud is deprecating this endpoint anyway because it became too complicated.

„This endpoint is carried over from the IEX 1.0 API, and has become too complicated. We are working on new endpoints to simplify time series queries.”

Close.