Swiftpack.co - Package - iamWing/swift-alpha-vantage

swift-alpha-vantage

Swift package to get market data from Alpha Vantage APIs

Alpha Vantage provides a set of free APIs for realtime and historical data on stocks, forex (FX), and digital/crypto currencies. This package implements a command line script and a Swift module to the free APIs provided by Alpha Vantage.

This package requires a free API key from Alpha Vantage in order to fetch market data there. The API key can be requested on http://www.alphavantage.co/support/#api-key

Getting Started

Before you get started

This package is written and tested on macOS & Ubuntu, whereas iOS has not been tested and not guaranteed to work. You are welcome to give it a try and post an issue to let me know if it works or not, thanks.

Using as dependency via Swift Package Manager

Add dependencies

Add swift-alpha-vantage to the dependencies within your application's Package.swift file. Substitute "x.x.x" with the latestswift-alpha-vantage` release.

.package(url: "https://github.com/iamWing/swift-alpha-vantage", from: "x.x.x")

Add AlphaVantage to your target's dependencies:

.target(name: "example", dependencies: ["AlphaVantage"])

Initialise AlphaVantage

import Foundation
import AlphaVantage

let fetcher = Stock(
    apiKey: "alpha_vantage_api_key",
    export: (path: URL(fileURLWithPath: "."), dataType: .json)
)

Fetching market data

fetcher.fetchStockIntraday(symbol: "MSFT", interval: .min15) { result, err in
    if let err = err {
        guard let apiErr = err as? ApiResponse.ApiError else {
            self.stderr <<< err.localizedDescription
            return
        }
        
        self.stderr <<< apiErr.errMsg
    }
}

Command line executable

Download Swift Alpha Vantage

# Substitute "x.x.x" with the latest release
curl https://github.com/iamWing/swift-alpha-vantage/releases/download/x.x.x/sav -o sav

Configure API Key

The Alpha Vantage API key can be configured via either environment variable API_KEY or a separate config.json file alongside the executable file sav.

If both environment variable API_KEY and config.json file are both configured, the api key declared in environment variable will be used for the convenience of quick api key modification.

# By environment variable
export API_KEY=your_api_key
# config.json
{
    "API_KEY": "your_api_key"
}

Usage

sav stock <function> <symbol> [options]

# e.g.
sav stock intraday -i 5 -o /home/ MSFT

# Available API functions:
# intraday, daily, daily-adjusted

Remarks

The development of this package is still in early days, therefore only a few API functions provided by Alpha Vantage are implemented atm. Given the limited amount of free time I have recently, the progress on the remaining functions is likely to be slow in the foreseeable future. You're more than welcome to participate the development if you've got the time.

Contributions

Contributions via pull requests are welcome and encouraged :)

In order to run the unit tests, you'll need to create a file PrivateConst.swift under the folder Tests/AlphaVantageTests/ for storing the API key for unit tests, otherwise the test module won't be able to compile.

// PrivateConst.swift
enum PrivateConst {
    static var apiKey: String { return "your-api-key" }
}

Author

Wing Chau @Devtography

License

swift-alpha-vantage is licensed under the MIT License.

Github

link
Stars: 0
Help us keep the lights on

Used By

Total: 0

Releases

0.1.0 - Nov 27, 2019

Added

  • Swift library module AlphaVantage:
    • Partial implementation of Stock Time Series functions including TIME_SERIES_INTRADAY, TIME_SERIES_DAILY, and TIME_SERIES_DAILY_ADJUSTED.
  • Command line executable:
    • Basic command line interface.
    • Command sav stock <intraday/daily/daily-adjusted> <symbol> [options]