exchange

package module
v0.0.0-...-a6da1ed Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2020 License: MIT Imports: 10 Imported by: 3

README

exchange

Go Report Card

Go library for current & historical exchange rates, forex & crypto currency conversion, fluctuation, and timeseries using the new Free foreign exchange rates API by arzzen (github)

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
  • Easy to use:

Usage:

go get -u github.com/asvvvad/exchange
package main

import (
	"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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidAPIResponse = errors.New("Unknown API error")

ErrInvalidAPIResponse is returned when the API return success: false

View Source
var ErrInvalidCode = errors.New("Invalid currency code")

ErrInvalidCode is returned when the currency code is invalid

View Source
var ErrInvalidDate = errors.New("Oldest possible date is 1999-01-04")

ErrInvalidDate is returned when the date is too old

View Source
var ErrInvalidDateFormat = errors.New("Date format must be YYYY-MM-DD")

ErrInvalidDateFormat is returned when the date isn't formatted as YYYY-MM-DD

View Source
var ErrInvalidTimeFrame = errors.New("From date must be older than To date")

ErrInvalidTimeFrame is returned when the to date is older than to date. For example flipped the arguments

View Source
var ErrTimeframeExceeded = errors.New("Maximum allowed timeframe is 365 days")

ErrTimeframeExceeded is returned when the time between start_date and end_date is bigger than 365 days

Functions

func ValidateCode

func ValidateCode(code string) error

ValidateCode validates a single symbol code

func ValidateDate

func ValidateDate(date string) error

ValidateDate validates date string according to YYYY-MM-DD format and if it's

func ValidateSymbols

func ValidateSymbols(symbols []string) error

ValidateSymbols validates all symbols' codes in an array

func ValidateTimeFrame

func ValidateTimeFrame(TimeFrame [2]string) error

ValidateTimeFrame checks if the from and to date are not more than 365 days apart and they're not mixed

Types

type Exchange

type Exchange struct {
	Base         string
	CacheEnabled bool
	// contains filtered or unexported fields
}

Exchange is returned by New() and allows access to the methods

func New

func New(base string) *Exchange

New creates a new instance of Exchange

func (*Exchange) ConvertAt

func (exchange *Exchange) ConvertAt(date string, target string, amount int) (*big.Float, error)

ConvertAt converts the amount from the exchange.Base currency to the target currency at a selected historical date

func (*Exchange) ConvertTo

func (exchange *Exchange) ConvertTo(target string, amount int) (*big.Float, error)

ConvertTo converts the amount from the exchange.Base currency to the target currency

func (*Exchange) CryptoCodes

func (exchange *Exchange) CryptoCodes() ([]string, error)

CryptoCodes returns and array of supported cryptocurrency codes

func (*Exchange) CryptoData

func (exchange *Exchange) CryptoData() (map[string]map[string]string, error)

CryptoData returns a map of supported cryptocurrencies data (name and symbol)

func (*Exchange) FluctuationAll

func (exchange *Exchange) FluctuationAll(start string, end string) (map[string]map[string]*big.Float, error)

FluctuationAll returns the fluctuation for all supported symbols

func (*Exchange) FluctuationMultiple

func (exchange *Exchange) FluctuationMultiple(start string, end string, symbols []string) (map[string]map[string]*big.Float, error)

FluctuationMultiple returns the fluctuation for multiple symbols

func (*Exchange) FluctuationSingle

func (exchange *Exchange) FluctuationSingle(start string, end string, symbol string) (map[string]*big.Float, error)

FluctuationSingle returns the fluctuation for a single symbol

func (*Exchange) ForexCodes

func (exchange *Exchange) ForexCodes() ([]string, error)

ForexCodes returns and array of supported forex/fiat currency codes

func (*Exchange) ForexData

func (exchange *Exchange) ForexData() (map[string]map[string]string, error)

ForexData returns a map of supported forex/fiat currencies data (code & description)

func (*Exchange) HistoricalRatesAll

func (exchange *Exchange) HistoricalRatesAll(date string) (map[string]*big.Float, error)

HistoricalRatesAll returns the historical exchange rates for all supported currencies

func (*Exchange) HistoricalRatesMultiple

func (exchange *Exchange) HistoricalRatesMultiple(date string, symbols []string) (map[string]*big.Float, error)

HistoricalRatesMultiple returns the historical exchange rates for multiple currencies

func (*Exchange) HistoricalRatesSingle

func (exchange *Exchange) HistoricalRatesSingle(date string, symbol string) (*big.Float, error)

HistoricalRatesSingle returns the historical exchange rates for a single currency

func (*Exchange) LatestRatesAll

func (exchange *Exchange) LatestRatesAll() (map[string]*big.Float, error)

LatestRatesAll returns the latest exchange rates for all supportedcurrencies

func (*Exchange) LatestRatesMultiple

func (exchange *Exchange) LatestRatesMultiple(symbols []string) (map[string]*big.Float, error)

LatestRatesMultiple returns the latest exchange rates for multiple currencies

func (*Exchange) LatestRatesSingle

func (exchange *Exchange) LatestRatesSingle(symbol string) (*big.Float, error)

LatestRatesSingle returns the latest exchange rates for a single currencies

func (*Exchange) SetBase

func (exchange *Exchange) SetBase(base string) error

SetBase sets a new base currency for the exchange rates

func (*Exchange) SetCache

func (exchange *Exchange) SetCache(cache bool)

SetCache enables and disable caching (caching last till midnight when the exchange rates are updated)

func (*Exchange) TimeseriesAll

func (exchange *Exchange) TimeseriesAll(start string, end string) (map[string]map[string]*big.Float, error)

TimeseriesAll returns the timeseries for all supported symbols

func (*Exchange) TimeseriesMultiple

func (exchange *Exchange) TimeseriesMultiple(start string, end string, symbols []string) (map[string]map[string]*big.Float, error)

TimeseriesMultiple returns the timeseries for multiple symbols

func (*Exchange) TimeseriesSingle

func (exchange *Exchange) TimeseriesSingle(start string, end string, symbol string) (map[string]map[string]*big.Float, error)

TimeseriesSingle returns the timeseries for a single symbol<

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL