golang_forex_quotes

package module
v0.0.0-...-2ce07d8 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2020 License: MIT Imports: 10 Imported by: 0

README

golang-forex-quotes

golang-forex-quotes is a Golang library for fetching realtime forex quotes. Any contributions or issues opened are greatly appreciated.

1Forge Data

Table of Contents

Requirements

Known Issues

Please see the list of known issues here: Issues

Installation

go get github.com/1Forge/golang-forex-quotes

Usage

Initialize the client
import (
	Forex "github.com/1Forge/golang-forex-quotes"
)
WebSocket API
func main() {
    client := Forex.CreateForgeClient("YOUR_API_KEY")

    symbols := []string{"BTC/JPY", "AUD/JPY", "GBP/CHF"}

    // Specify the update handler
    client.OnUpdate(func(q Forex.Quote) {
        fmt.Println(q)
    })

    // Specify the message handler
    client.OnMessage(func(m string) {
        fmt.Println(m)
    })

    // Specify the disconnection handler
    client.OnDisconnection(func() {
        fmt.Println("Disconnected")
    })

    // Specify the login success handler
    client.OnLoginSuccess(func() {
        fmt.Println("Successfully logged in")

        // Subscribe to some symbols
        client.SubscribeTo(symbols)

        // Subscribe to all symbols
        client.SubscribeToAll()
    })

    // Specify the connection handler
    client.OnConnection(func() {
        fmt.Println("Connected")
    })

    // Connect to the socket server
    client.Connect()

    // Wait 25 seconds
    time.Sleep(25 * time.Second)

    // Unsubscribe from some symbols
    client.UnsubscribeFrom(symbols)

    // Unsubscribe from all symbols
    client.UnsubscribeFromAll()

    // Disconnect
    client.Disconnect()
}
RESTful API
func main() {
    client := Forex.CreateClient("YOUR_API_KEY")

    // Get the list of symbols
    symbols, e := client.GetSymbols()

    if e != nil {
        log.Fatal(e)
    }

    // Gets quotes
    quotes, e := client.GetQuotes(symbols)

    if e != nil {
        log.Fatal(e)
    }

    fmt.Println(quotes)

    // Convert currencies
    conversion, e := client.Convert("EUR", "USD", 100)
    if e != nil {
        log.Fatal(e)
    }

    fmt.Println(conversion.Value)
    fmt.Println(conversion.Text)
    fmt.Println(conversion.Timestamp)

    // Get the market status
    marketStatus, e := client.GetMarketStatus()

    if e != nil {
        log.Fatal(e)
    }

    fmt.Println("Is the market open?", marketStatus.MarketIsOpen)

    // Get current quota
    quota, e := client.GetQuota()

    if e != nil {
        log.Fatal(e)
    }

    fmt.Println("Quota used", quota.QuotaUsed)
    fmt.Println("Quota limit", quota.QuotaLimit)
    fmt.Println("Quota remaining", quota.QuotaRemaining)
    fmt.Println("Hours until reset", quota.HoursUntilReset)
}

Support and Contact

Please contact me at contact@1forge.com if you have any questions or requests.

License and Terms

This library is provided without warranty under the MIT license.

Documentation

Index

Constants

View Source
const (
	LOGIN                = "login"
	SUBSCRIBE_TO         = "subscribe_to"
	UNSUBSCRIBE_FROM     = "unsubscribe_from"
	SUBSCRIBE_TO_ALL     = "subscribe_to_all"
	UNSUBSCRIBE_FROM_ALL = "unsubscribe_from_all"
	MESSAGE              = "message"
	FORCE_CLOSE          = "force_close"
	POST_LOGIN_SUCCESS   = "post_login_success"
	UPDATE               = "update"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConversionResult

type ConversionResult struct {
	Value     float32
	Text      string
	Timestamp int
}

type ForgeClient

type ForgeClient struct {
	// contains filtered or unexported fields
}

func CreateForgeClient

func CreateForgeClient(apiKey string) *ForgeClient

func (*ForgeClient) Connect

func (c *ForgeClient) Connect()

SOCKET

func (*ForgeClient) Convert

func (c *ForgeClient) Convert(from string, to string, quantity int) (ConversionResult, error)

func (*ForgeClient) Disconnect

func (c *ForgeClient) Disconnect()

func (*ForgeClient) GetMarketStatus

func (c *ForgeClient) GetMarketStatus() (MarketStatus, error)

func (*ForgeClient) GetQuota

func (c *ForgeClient) GetQuota() (Quota, error)

func (*ForgeClient) GetQuotes

func (c *ForgeClient) GetQuotes(symbols []string) ([]Quote, error)

REST

func (*ForgeClient) GetSymbols

func (c *ForgeClient) GetSymbols() ([]string, error)

func (*ForgeClient) OnConnection

func (c *ForgeClient) OnConnection(callback func())

func (*ForgeClient) OnDisconnection

func (c *ForgeClient) OnDisconnection(callback func())

func (*ForgeClient) OnLoginSuccess

func (c *ForgeClient) OnLoginSuccess(callback func())

func (*ForgeClient) OnMessage

func (c *ForgeClient) OnMessage(callback func(string))

func (*ForgeClient) OnUpdate

func (c *ForgeClient) OnUpdate(callback func(Quote))

func (*ForgeClient) SubscribeTo

func (c *ForgeClient) SubscribeTo(symbols []string)

func (*ForgeClient) SubscribeToAll

func (c *ForgeClient) SubscribeToAll()

func (*ForgeClient) UnsubscribeFrom

func (c *ForgeClient) UnsubscribeFrom(symbols []string)

func (*ForgeClient) UnsubscribeFromAll

func (c *ForgeClient) UnsubscribeFromAll()

type MarketStatus

type MarketStatus struct {
	MarketIsOpen bool `json:"market_is_open"`
}

type Quota

type Quota struct {
	QuotaUsed       int `json:"quota_used"`
	QuotaLimit      int `json:"quota_limit"` //0 = Unlimited
	QuotaRemaining  int `json:"quota_remaining"`
	HoursUntilReset int `json:"hours_until_reset"`
}

type Quote

type Quote struct {
	Symbol string  `json:"s"`
	Bid    float32 `json:"b"`
	Ask    float32 `json:"a"`
	Price  float32 `json:"p"`
	Time   int     `json:"t"`
}

type RestClient

type RestClient struct {
	ApiKey string
}

func CreateRestClient

func CreateRestClient(apiKey string) RestClient

func (RestClient) Convert

func (c RestClient) Convert(from string, to string, quantity int) (ConversionResult, error)

func (RestClient) GetMarketStatus

func (c RestClient) GetMarketStatus() (MarketStatus, error)

func (RestClient) GetQuota

func (c RestClient) GetQuota() (Quota, error)

func (RestClient) GetQuotes

func (c RestClient) GetQuotes(symbols []string) ([]Quote, error)

func (RestClient) GetSymbols

func (c RestClient) GetSymbols() ([]string, error)

type RestError

type RestError struct {
	Error   bool
	Message string
}

type SocketClient

type SocketClient struct {
	ApiKey string
	// contains filtered or unexported fields
}

func CreateSocketClient

func CreateSocketClient(apiKey string) *SocketClient

func (*SocketClient) Connect

func (c *SocketClient) Connect()

func (*SocketClient) Disconnect

func (c *SocketClient) Disconnect()

func (*SocketClient) OnConnection

func (c *SocketClient) OnConnection(callback func())

func (*SocketClient) OnDisconnection

func (c *SocketClient) OnDisconnection(callback func())

func (*SocketClient) OnLoginSuccess

func (c *SocketClient) OnLoginSuccess(callback func())

func (*SocketClient) OnMessage

func (c *SocketClient) OnMessage(callback func(string))

func (*SocketClient) OnUpdate

func (c *SocketClient) OnUpdate(callback func(Quote))

func (*SocketClient) SubscribeTo

func (c *SocketClient) SubscribeTo(symbols []string)

func (*SocketClient) SubscribeToAll

func (c *SocketClient) SubscribeToAll()

func (*SocketClient) UnsubscribeFrom

func (c *SocketClient) UnsubscribeFrom(symbols []string)

func (*SocketClient) UnsubscribeFromAll

func (c *SocketClient) UnsubscribeFromAll()

type UnlimitedQuota

type UnlimitedQuota struct {
	QuotaUsed       int    `json:"quota_used"`
	QuotaLimit      string `json:"quota_limit"`
	QuotaRemaining  string `json:"quota_remaining"`
	HoursUntilReset int    `json:"hours_until_reset"`
}

Jump to

Keyboard shortcuts

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