This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/io/github/f4pl0/historicaldata/HistoricalData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/io/github/f4pl0/historicaldata/data/IEXHistoricalEquityPrice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/io/github/f4pl0/historicaldata/enums/DateRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |