Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Commit

Permalink
add: historical data endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
F4pl0 committed Nov 4, 2023
1 parent 83cd4c6 commit f6a7081
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/io/github/f4pl0/IEXCloudClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.f4pl0.config.ConfigInjector;
import io.github.f4pl0.config.IEXCloudConfig;
import io.github.f4pl0.equitiesmarketdata.EquitiesMarketData;
import io.github.f4pl0.historicaldata.HistoricalData;
import io.github.f4pl0.reference.Reference;

/**
Expand All @@ -14,6 +15,7 @@ public class IEXCloudClient {
public final EquitiesMarketData equitiesMarketData;
public final Reference reference;
public final CompanyData companyData;
public final HistoricalData historicalData;

/**
* Create a new IEXCloudClient.
Expand All @@ -24,6 +26,7 @@ private IEXCloudClient(IEXCloudConfig config) {
equitiesMarketData = new EquitiesMarketData();
reference = new Reference();
companyData = new CompanyData();
historicalData = new HistoricalData();
}

/**
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/io/github/f4pl0/historicaldata/HistoricalData.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
package io.github.f4pl0.historicaldata;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.f4pl0.IEXHttpClient;
import io.github.f4pl0.historicaldata.data.IEXHistoricalEquityPrice;
import io.github.f4pl0.historicaldata.enums.DateRange;
import lombok.NonNull;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class HistoricalData {
private final IEXHttpClient httpClient = IEXHttpClient.getInstance();
private final ObjectMapper mapper = new ObjectMapper();

/**
* Historical Equity Prices
*
* <p>
* Returns daily, end of day split adjusted, dividend + split adjusted, and unadjusted equity prices since 2005
* for the U.S. and over 100 international exchanges.
* </p>
*
* @see <a href="https://iexcloud.io/docs/core/HISTORICAL_PRICES#historical-equity-prices">IEX Cloud API</a>
* @param symbol The stock symbol.
* @param range The date range.
* @param limit The number of results to return. Defaults to 1. Ignored if range is {@link DateRange#YEAR_TO_DATE}.
* @throws IOException If the request fails.
* @return A list of {@link IEXHistoricalEquityPrice} objects.
*/
public List<IEXHistoricalEquityPrice> historicalEquityPrices(
@NonNull String symbol,
@NonNull DateRange range,
int limit
) throws IOException {
String encodedSymbol = URLEncoder.encode(symbol, StandardCharsets.UTF_8);
String requestUri = "/data/core/historical_prices/" + encodedSymbol;

if (range == DateRange.YEAR_TO_DATE) {
requestUri += "?range=" + range.toString();
} else if (limit < 1) {
requestUri += "?range=1" + range.toString();
} else {
requestUri += "?range=" + limit + range.toString();
}

CloseableHttpResponse response = httpClient.execute(requestUri);

return mapper.readValue(
EntityUtils.toString(response.getEntity()),
mapper.getTypeFactory().constructCollectionType(List.class, IEXHistoricalEquityPrice.class));
}

// TODO: Add technical indicators endpoint
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.f4pl0.historicaldata.data;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class IEXHistoricalEquityPrice {
private BigDecimal close;
private BigDecimal fclose;
private BigDecimal fhigh;
private BigDecimal flow;
private BigDecimal fopen;
private long fvolume;
private BigDecimal high;
private BigDecimal low;
private BigDecimal open;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date priceDate;
private String symbol;
private BigDecimal uclose;
private BigDecimal uhigh;
private BigDecimal ulow;
private BigDecimal uopen;
private long uvolume;
private long volume;
private String id;
private String key;
private String subkey;
private long date;
private long updated;
}
19 changes: 19 additions & 0 deletions src/main/java/io/github/f4pl0/historicaldata/enums/DateRange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.f4pl0.historicaldata.enums;

public enum DateRange {
YEAR("y"),
YEAR_TO_DATE("ytd"),
MONTH("m"),
DAY("d");

private final String value;

DateRange(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}

0 comments on commit f6a7081

Please sign in to comment.