Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/asvvvad1/exchange
Go library for getting current and historical exchange rates, Forex and Crypto currency conversion with no authentication using Free foreign exchange rates API
https://github.com/asvvvad1/exchange
api bitcoin cryptocurrency currency currency-converter exchange-rates fluctuations forex free go go-library golang timeseries
Last synced: about 1 month ago
JSON representation
Go library for getting current and historical exchange rates, Forex and Crypto currency conversion with no authentication using Free foreign exchange rates API
- Host: GitHub
- URL: https://github.com/asvvvad1/exchange
- Owner: asvvvad1
- License: mit
- Created: 2020-04-30T00:01:54.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-22T14:52:24.000Z (over 4 years ago)
- Last Synced: 2024-08-10T05:09:22.983Z (5 months ago)
- Topics: api, bitcoin, cryptocurrency, currency, currency-converter, exchange-rates, fluctuations, forex, free, go, go-library, golang, timeseries
- Language: Go
- Homepage: https://pkg.go.dev/github.com/asvvvad/exchange?tab=doc
- Size: 57.6 KB
- Stars: 16
- Watchers: 1
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# exchange
[![Go Report Card](https://goreportcard.com/badge/github.com/asvvvad/exchange)](https://goreportcard.com/report/github.com/asvvvad/exchange)Go library for current & historical exchange rates, forex & crypto currency conversion, fluctuation, and timeseries using the new [Free foreign exchange rates API](https://exchangerate.host/#/) by [arzzen](https://github.com/arzzen/) ([github](https://github.com/arzzen/exchangerate.host))
## Features:
- Currency conversion, historical & current exchange rates, timeseries and fluctuations
- No authentication/token needed!
- Select any base currency
- 171 forex currency and 6000+ cryptocurrency!
- Caching (optional, default) using [go-cache](https://github.com/patrickmn/go-cache)
- Easy to use:## Usage:
> #### `go get -u github.com/asvvvad/exchange`
```go
package mainimport (
"fmt""github.com/asvvvad/exchange"
)func main() {
// Create a new Exchange instance and set USD as the base currency for the exchange rates and conversion
ex := exchange.New("USD")
// convert 10 USD to EUR
fmt.Println(ex.ConvertTo("EUR", 10))
// You can convert between 171 fiat and +6000 cryptocurrency aswelL!
// convert 10 USD to BTC
fmt.Println(ex.ConvertTo("BTC", 10))
// convert 10 USD to EUR at 2012-12-12 (date must be in the format YYYY-MM-DD)
fmt.Println(ex.ConvertAt("2012-12-12", "EUR", 10))// Get the available forex/fiat codes ([]string)
forexCodes, _ := ex.ForexCodes()
// Get the available crypto codes ([]string)
// Warning: +6000
cryptoCodes, _ := ex.CryptoCodes()// Get the forex codes data, includes code and description.
forexData, _ := ex.ForexData()// Get the crypto codes data, includes code and description.
// Warning: +6000
cryptoData, _ := ex.CryptoData()// loop through the forex cpdes
for _, code := range forexCodes {
// print the forex codes data in the format: USD: US Dollar
fmt.Println(code+":", forexData[code]["description"])
}// Change the base currency to euro
ex.SetBase("EUR")
// Get the latest exchange rates with all currencies (Base is EUR)
fmt.Println(ex.LatestRatesAll())// Get the latest rates again, this time it will be loaded from in-memory cache
// Cache last till midnight GMT because it's the time exchangerate.host update the rates
fmt.Println(ex.LatestRatesAll())
// disable caching
ex.SetCache(false)// Get the latest rates with multiple currencies, not all (USD and JPY only)
fmt.Println(ex.LatestRatesMultiple([]string{"USD", "JPY"}))// Get the exchange rates at 2012-12-12 but only with USD
fmt.Println(ex.HistoricalRatesSingle("2012-12-12", "USD"))// Get historical rates between 2012 12 10 and 2012 12 12 for JPY and GBP
fmt.Println(ex.TimeseriesMultiple("2012-12-10", "2012-12-12", []string{"USD", "JPY"}))// Get the fluctuation between 2012 12 10 and 2012 12 12 with USD
fluctuation, _ := ex.FluctuationSingle("2012-12-10", "2012-12-12", "USD")
// Print the change
fmt.Println(fluctuation["change"])
}```
### Results returned by each method:
- ConvertTo, ConvertAt, HistoricalRatesSingle, LatestRatesSingle
- - `big.Float`, error
- LatestRatesAll, LatestRatesMultiple, HistoricalRatesAll, HistoricalRatesMultiple:
- - `map[symbol(string)]rate(big.Float)`
- ForexCodes
- - `[]string{codes}`, error
- ForexData
- - `map[symbol]map[
code
description
]string`, error
- CryptoCodes
- - `[]string{codes}`, error
- CryptoData
- - `map[symbol]map[
symbol
name
]string`, error
- FluctuationAll, FluctuationMultiple,
- - `map[symbol]map[
start_rate
end_rate
change
change_pct
]*big.Float`, error
- FluctuationSingle
- - `map[
start_rate
end_rate
change
change_pct
]*big.Float`, error- TimeseriesAll, TimeseriesMultiple
- - `map[date]map[symbols]*big.Float`, error
- TimeseriesSingle
- - `map[date]map[symbol]*big.Float`, error> ## Notes:
- You can use All, Multiple, Single with all of LatestRates, HistoricalRates, Timeseries and Fluctuation.
- Oldest date for historical rates and conversion is 1999-01-04
- Maximum allowed timeframe for Timeseries is 365 days#### Input validation with the appropriate errors for all methods is provided to help debug
#### Any help and contribution is welcome!
This is my first Go library and I had trouble with JSON parsing (and I still do, didn't use bitly/simplejson to reduce dependencies) Theres a lot of room for improvement