bitfinexcom / bitfinex-api-go

BITFINEX Go trading API - Bitcoin, Litecoin, and Ether exchange
https://www.bitfinex.com/
MIT License
303 stars 222 forks source link

Complete example #247

Open biancheng347 opened 5 months ago

biancheng347 commented 5 months ago

After studying sdk and documents for a long time, in v2 rest, I can't figure out create_order(limt, market), cancel_order, get_order, exchange_info, where rsp contains status. Please give a complete case, thank you

Davi0kProgramsThings commented 5 months ago

Hi @biancheng347.

Setup

package main

import (
    "os"

    "github.com/bitfinexcom/bitfinex-api-go/v2"
)

func main() {
    key := os.Getenv("BFX_API_KEY")
    secret := os.Getenv("BFX_API_SECRET")
    c := bitfinex.NewClient().Credentials(key, secret)
}

Retrieve your active orders

You have 3 ways to retrieve your active orders:

All

data, err := c.Orders.All()

GetBySymbol

data, err := c.Orders.GetBySymbol("tBTCUSD")

GetByOrderId

order, err := c.Orders.GetByOrderId(33950998275)

Note

Remember to check for errors:

if err != nil {
    panic(err)
}

Submit a new order

func (s OrderService) SubmitOrder(onr order.NewRequest) (*notification.Notification, error)

First you need to define a NewRequest object:

request := order.NewRequest{
    Symbol: "tBTCUSD",
    CID:    time.Now().Unix() / 1000,
    Amount: 0.02,
    Type:   "EXCHANGE LIMIT",
    Price:  5000,
})

Then you can submit it:

notification, err := c.Orders.SubmitOrder(&request)

Notification

You can use the notification object to access more information about the operation:

order := notification.NotifyInfo.(*order.Order)

if notification.Status == "SUCCESS" {
    fmt.Printf("Successful new order for %v at %v$.", order.Symbol, order.Price)
}

if notification.Status == "ERROR" {
    fmt.Printf("Something went wrong: %v.", notification.Text)
}

Cancel an active order

func (s OrderService) SubmitCancelOrder(oc order.CancelRequest) error

You can delete an active order by passing its ID:

err := c.Orders.SubmitCancelOrder(&order.CancelRequest{
    ID: 33950998275
})

You can delete an active order via its CID and CIDDate:

err := c.Orders.SubmitCancelOrder(&order.CancelRequest{
    CID: 1573476747887,
    CIDDate: "2014-11-12"
})

API reference

order.Snapshot

order.Order

order.NewRequest

order.CancelRequest

notification.Notification