I created this wrapper to make accessing the AlphaVantage API with Java fairly simple and fun. Make sure to get an API key from Alphavantage's website before continuing.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
...
implementation 'com.github.crazzyghost:alphavantage-java:1.1.0'
}
config
ure the wrapper- Select a
category
- Set the
parameters
for the selected category - Add
response callbacks
fetch
results
Access to the API is through the AlphaVantage Singleton which is accessed using the static
.api()
method call on the class. To begin accessing the API, initialize the singleton with a Config
instance.
Config cfg = Config.builder()
.key("#&ALPHA10100DEMOKEY")
.timeout(10)
.build();
The config object is then used to initialize the instance. You will use this object to set your api key as well as set the timeout of the http requests.
AlphaVantage.api().init(cfg);
That's it! We're good to go.
The available API categories to select from currently are: Stock Time Series, Forex, Crypto Currencies, Exchange Rates and Technical Indicators. Each of these map to a method call in the instantiated wrapper.
Category | Method |
---|---|
Stock Time Series Data | .timeSeries() |
Forex Rate Data | .forex() |
Exchange Rate Data | .exchangeRate() |
Crypto Currency Data | .crypto() |
Technical Indicators | .indicator() |
For example
//selecting stock time series category
AlphaVantage.api()
.timeSeries()
...
To set the api request parameters, call the appopriate parameter method. For instance for the function
parameter
function you call daily()
for the TIMESERIES_DAILY
function, intraday()
for the TIMESERIES_INTRADAY
, sma()
for SMA
and so on.
//set stock time series function to intraday
AlphaVantage.api()
.timeSeries()
.intraday()
...
To handle responses add the onSuccess()
or onFailure()
callbacks. Each category has its response. For instance the TimeSeries
category has the TimeSeriesResponse
.
public void handleResponse(TimeSeriesResponse response){/*perform some magic with response*/ }
public void handleError(AlphaVantageException error){/*uh-oh*/ }
AlphaVantage.api()
.timeSeries()
...
.onSuccess(e->handleResponse(e))
.onFailure(e->hanldeError(e))
...
When you are okay with setting the parameters call the fetch()
method. Simple!
...
AlphaVantage.api()
.timeSeries()
.intraday()
.onSuccess(e->handleSuccess(e))
.interval(Interval.ONE_MIN)
.onFailure(e->handleFailure(e))
.forSymbol("MSFT")
.fetch();
...
void handleSuccess(TimeSeriesResponse e){
plotGraph(e.getStockUnits(), e.getMetaData());
}
AlphaVantage.api()
.forex()
.daily()
.fromSymbol("USD")
.toSymbol("GHS")
.onSuccess(e->handleSuccess(e.getForexUnits()))
.forSymbol("MSFT")
.fetch();
AlphaVantage.api()
.exchangeRate()
.fromCurrency("USD")
.toCurrency("GHS")
.onSuccess((ExchangeRateResponse e) -> handleSuccess(e))
.fetch();
AlphaVantage.api()
.crypto()
.daily()
.symbol("BTC")
.market("CNY")
.onSuccess(response -> handleSuccess(response.getCryptoUnits()))
.fetch();
AlphaVantage.api()
.indicator()
.sma()
.forSymbol("IBM")
.interval(Interval.THIRTY_MIN)
.seriesType(SeriesType.HIGH)
.timePeriod(200)
.onSuccess(e -> handleSuccess(e))
.fetch();