Skip to content

radovsky1/go-currencycom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-currencycom

A Go client library for the Currency.com API.

GoDoc Currency.com Swagger API Go Report Card

Installation

go get github.com/radovsky1/go-currencycom

Contribution

This project is fully inspired by go-binance. All other materials are taken from the official Currency.com API documentation.

Importing

import (
    currencycom "github.com/radovsky1/go-currencycom"
)

REST API

Setup

Init client for API services. Get APIKey/SecretKey from your account.

var (
    apiKey = "your api key"
    secretKey = "your secret key"
)
currencycom.UseDemo = true // use demo server
client := currencycom.NewClient(apiKey, secretKey)

A service instance stands for a REST API endpoint and is initialized by client.NewXXXService function.

Simply call API in chain style. Call Do() in the end to send HTTP request and get response.

Create Order

order, err := client.NewCreateOrderService().
        Symbol("BTC/USD_LEVERAGE").
        Side(go_currencycom.SideTypeBuy).
        Type(go_currencycom.OrderTypeLimit).
        Quantity(0.03).
        Price(15000).
        Do(context.Background())
if err != nil {
    panic(err)
}
println(order.OrderID)

Fetch Order

order, err := client.NewGetOrderService().
        Symbol("BTC/USD_LEVERAGE").
        OrderID("123456789").
        Do(context.Background())
if err != nil {
    panic(err)
}
println(order.OrderID)

Cancel Order

order, err := client.NewCancelOrderService().
        Symbol("BTC/USD_LEVERAGE").
        OrderID("123456789").
        Do(context.Background())
if err != nil {
    panic(err)
}
println(order.OrderID)

List Open Orders

openOrders, err := client.NewListOpenOrdersService().Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(openOrders)

List Trading Positions

positions, err := client.NewListTradingPositionsService().Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(positions)

Get Account Information

account, err := client.NewGetAccountService().Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(account)

There are more services available, please check the source code.

Websocket API

You don't need Client in websocket API. Just call currencycom.WsXxxServe(args, handler, errHandler).

Market Data

wsMarketDataHandler := func(event *currencycom.WsMarketDataEvent) {
    fmt.Println(event)
}
errHandler := func(err error) {
    fmt.Println(err)
}
doneC, stopC, err := currencycom.WsMarketDataServe("BTC/USD_LEVERAGE", wsMarketDataHandler, errHandler)
if err != nil {
    fmt.Println(err)
    return
}
// use stopC to exit
go func() {
    time.Sleep(5 * time.Second)
    stopC <- struct{}{}
}()
// remove this if you do not want to be blocked here
<-doneC

OHLC Market Data

wsOHLCMarketDataHandler := func(event *currencycom.WsOHLCMarketDataEvent) {
    fmt.Println(event)
}
errHandler := func(err error) {
    fmt.Println(err)
}
doneC, stopC, err := currencycom.WsOHLCMarketDataServe("BTC/USD_LEVERAGE", wsOHLCMarketDataHandler, errHandler)
if err != nil {
    fmt.Println(err)
    return
}
// use stopC to exit
go func() {
    time.Sleep(5 * time.Second)
    stopC <- struct{}{}
}()
// remove this if you do not want to be blocked here
<-doneC

Trades

wsTradesHandler := func(event *currencycom.WsTradesEvent) {
    fmt.Println(event)
}
errHandler := func(err error) {
    fmt.Println(err)
}
doneC, stopC, err := currencycom.WsTradesServe("BTC/USD_LEVERAGE", wsTradesHandler, errHandler)
if err != nil {
    fmt.Println(err)
    return
}
// use stopC to exit
go func() {
    time.Sleep(5 * time.Second)
    stopC <- struct{}{}
}()
// remove this if you do not want to be blocked here
<-doneC

Feedback

If you have any questions/suggestions, please feel free to contact me.

About

A Go SDK for Currency.com

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages