Skip to content

Latest commit

 

History

History
142 lines (105 loc) · 3.3 KB

README.md

File metadata and controls

142 lines (105 loc) · 3.3 KB

go-phemex

A Golang SDK for phemex API.

Build Status GoDoc Go Report Card codecov

Most REST APIs listed in phemex API document are implemented, as well as the AOP websocket APIs.

For best compatibility, please use Go >= 1.8.

Make sure you have read phemex API document before continuing.

API List

Name Description Status
rest-api.md Details on the Rest API

Installation

go get github.com/Krisa/go-phemex

Importing

import (
    "github.com/Krisa/go-phemex"
)

Documentation

GoDoc

REST API

Setup

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

var (
    apiKey = "your api key"
    secretKey = "your secret key"
)
client := phemex.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.

Create Order

order, err := client.NewCreateOrderService().Symbol("BTCUSD").
        Side(phemex.SideTypeBuy).Type(phemex.OrderTypeLimit).
        TimeInForce(phemex.TimeInForceTypeGTC).Quantity("5").
        Price("1000").Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(order)

Cancel Order

_, err := client.NewCancelOrderService().Symbol("BTCUSD").
    OrderID(4432844).Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}

List Open Orders

openOrders, err := client.NewListOpenOrdersService().Symbol("BTCUSD").
    Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
for _, o := range openOrders {
    fmt.Println(o)
}

Get Account

res, err := client.NewGetAccountPositionService().Currency("BTC").Do(context.Background())
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(res)

Websocket

You initiate PhemexClient the same way

User Data

wsHandler := func(message interface{}) {
    switch message := message.(type) {
    case *phemex.WsAOP:
        // snapshots / increments
    case *phemex.WsPositionInfo:
        // when a position is active
    case *phemex.WsError:
        // on connection
    }
}

errHandler := func(err error) {
    // initiate reconnection with `once.Do...`
}

auth := PhemexClient.NewWsAuthService()

if test {
    auth = auth.URL("wss://testnet.phemex.com/ws")
}

c, err := auth.Do(context.Background())
// err handling

err = PhemexClient.NewStartWsAOPService().SetID(1).Do(c, wsHandler, errHandler)
// err handling