From f6a708159e2b86dcea95d23a4e9b835ed831a911 Mon Sep 17 00:00:00 2001 From: f4 Date: Sat, 4 Nov 2023 05:30:38 +0100 Subject: [PATCH] add: historical data endpoint --- .../java/io/github/f4pl0/IEXCloudClient.java | 3 + .../f4pl0/historicaldata/HistoricalData.java | 55 +++++++++++++++++++ .../data/IEXHistoricalEquityPrice.java | 38 +++++++++++++ .../f4pl0/historicaldata/enums/DateRange.java | 19 +++++++ 4 files changed, 115 insertions(+) create mode 100644 src/main/java/io/github/f4pl0/historicaldata/data/IEXHistoricalEquityPrice.java create mode 100644 src/main/java/io/github/f4pl0/historicaldata/enums/DateRange.java diff --git a/src/main/java/io/github/f4pl0/IEXCloudClient.java b/src/main/java/io/github/f4pl0/IEXCloudClient.java index f075802..42f0158 100644 --- a/src/main/java/io/github/f4pl0/IEXCloudClient.java +++ b/src/main/java/io/github/f4pl0/IEXCloudClient.java @@ -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; /** @@ -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. @@ -24,6 +26,7 @@ private IEXCloudClient(IEXCloudConfig config) { equitiesMarketData = new EquitiesMarketData(); reference = new Reference(); companyData = new CompanyData(); + historicalData = new HistoricalData(); } /** diff --git a/src/main/java/io/github/f4pl0/historicaldata/HistoricalData.java b/src/main/java/io/github/f4pl0/historicaldata/HistoricalData.java index 169700e..89dbc73 100644 --- a/src/main/java/io/github/f4pl0/historicaldata/HistoricalData.java +++ b/src/main/java/io/github/f4pl0/historicaldata/HistoricalData.java @@ -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 + * + *

+ * 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. + *

+ * + * @see IEX Cloud API + * @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 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 } diff --git a/src/main/java/io/github/f4pl0/historicaldata/data/IEXHistoricalEquityPrice.java b/src/main/java/io/github/f4pl0/historicaldata/data/IEXHistoricalEquityPrice.java new file mode 100644 index 0000000..1724475 --- /dev/null +++ b/src/main/java/io/github/f4pl0/historicaldata/data/IEXHistoricalEquityPrice.java @@ -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; +} diff --git a/src/main/java/io/github/f4pl0/historicaldata/enums/DateRange.java b/src/main/java/io/github/f4pl0/historicaldata/enums/DateRange.java new file mode 100644 index 0000000..b24ed0f --- /dev/null +++ b/src/main/java/io/github/f4pl0/historicaldata/enums/DateRange.java @@ -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; + } +}