Skip to content

crazzyghost/alphavantage-java

Repository files navigation

CircleCI codecov

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.

Installation (Gradle)

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    ...
    implementation 'com.github.crazzyghost:alphavantage-java:1.1.0'
}

Usage

  1. configure the wrapper
  2. Select a category
  3. Set the parameters for the selected category
  4. Add response callbacks
  5. fetch results

1. Configure the wrapper

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.

2. Select a category

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() 
    ...

3. Set the parameters for the selected category

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()
    ...

4. Add response callbacks

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))
    ...

5. fetch results

When you are okay with setting the parameters call the fetch() method. Simple!

Examples

Fetching Stock Time Series Data

...
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());
}

Fetching Forex Rate Data

AlphaVantage.api()
    .forex()
    .daily()
    .fromSymbol("USD")
    .toSymbol("GHS")
    .onSuccess(e->handleSuccess(e.getForexUnits()))
    .forSymbol("MSFT")
    .fetch();

Fetching Currency Exchange Rate Data

AlphaVantage.api()
    .exchangeRate()
    .fromCurrency("USD")
    .toCurrency("GHS")
    .onSuccess((ExchangeRateResponse e) -> handleSuccess(e))
    .fetch();

Fetching Crypto Currency Data

AlphaVantage.api()
    .crypto()
    .daily()
    .symbol("BTC")
    .market("CNY")
    .onSuccess(response -> handleSuccess(response.getCryptoUnits()))
    .fetch();

Fetching Technical Indicator Data

AlphaVantage.api()
    .indicator()
    .sma()
    .forSymbol("IBM")
    .interval(Interval.THIRTY_MIN)
    .seriesType(SeriesType.HIGH)
    .timePeriod(200)
    .onSuccess(e -> handleSuccess(e))
    .fetch();