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
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.
Add swift-alpha-vantage
to the dependencies within your application's
Package.swift
file. Substitute "x.x.x" with the latest
swift-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"])
import Foundation
import AlphaVantage
let fetcher = Stock(
apiKey: "alpha_vantage_api_key",
export: (path: URL(fileURLWithPath: "."), dataType: .json)
)
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
}
}
# Substitute "x.x.x" with the latest release
curl https://github.com/iamWing/swift-alpha-vantage/releases/download/x.x.x/sav -o sav
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"
}
sav stock <function> <symbol> [options]
# e.g.
sav stock intraday -i 5 -o /home/ MSFT
# Available API functions:
# intraday, daily, daily-adjusted
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 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" }
}
swift-alpha-vantage is licensed under the MIT License.