Categorygithub.com/mitsutoshi/bitflyergo
modulepackage
1.3.14
Repository: https://github.com/mitsutoshi/bitflyergo.git
Documentation: pkg.go.dev

# README

bitflyergo

.github/workflows/test.yml CircleCI Go Report Card

bitflyergo is golang library to trade cryptocurrency on bitFlyer.

Support following functions.

  • REST-API Client
  • Realtime API (WebSocket JSON-RPC)
  • Utility functions

How to use

Here are some ways to use the library. If you want to know API specification, See tha public documents.

Initialize

You need to call bitflyergo.NewBitflyer with arguments. First argument is your api key, second that is your api secret. If you don't use private api, you may specify blank.

apiKey := "<Your API Key>"
apiSecret := "<Your API Secret>"
bf := bitflyergo.NewBitflyer(apiKey, apiSecret)

Call Public API

/v1/getexecutions

Get the last five execution histories of FX_BTC_JPY.

  • product_code: FX_BTC_JPY
  • count: 5
params := map[string]string{
    "product_code": "FX_BTC_JPY",
    "count":        "5",
}
executions, err := bf.GetExecutions(params)

Return value is []bitflyergo.Execution.

for _, e := range *executions {
    fmt.Println(e)
}
{800001572 2019-02-09T10:49:55.58 398759 0.01 BUY JRF20190209-XXXXX-XXXXX1 JRF20190209-YYYYYY-YYYYY1}
{800001572 2019-02-09T10:49:55.57 398758 0.01 BUY JRF20190209-XXXXX-XXXXX2 JRF20190209-YYYYYY-YYYYY2}
...

/v1/getboard

board, err := bf.GetBoard()

Call Private API

/v1/me/getexecutions

/v1/me/getchildorders

params := map[string]string{
    "": "",
}
childOrders, err := api.GetChildOrders(params)

/v1/me/getpositions

productCode := "FX_BTC_JPY"
positions, err := api.GetPositions(productCode)

/v1/me/getcollateral

collateral, err := api.GetCollateral()

/v1/me/getbalance

balance, err := api.GetBalance()

/v1/me/sendchildorder

Place the limit order. SendChildOrder returns childOrderAcceptanceId string. childOrderAcceptanceId is when order is accepted ID.

params := map[string]string{
    "price": "400000",
}
childOrderAcceptanceId, err := api.SendChildOrder("FX_BTC_JPY", "LIMIT", "BUY", 0.01, params)

Place the market order. market order does't need to specify price of argument.

childOrderAcceptanceId, err := api.SendChildOrder("FX_BTC_JPY", "MARKET", "BUY", 0.01, nil)

Receive streaming data from websocket

bitflyergo provides the APIs to use bitFlyer Lightning Realtime API.

First, you need to implement Callback interface's methods.

// OnReceiveBoard is the callbck when board is received from websocket.
OnReceiveBoard(channelName string, board *Board)

// OnReceiveBoardSnapshot is the callbck when board snapshot is received from websocket.
OnReceiveBoardSnapshot(channelName string, board *Board)

// OnReceiveExecutions is the callbck when executions is received from websocket.
OnReceiveExecutions(channelName string, executions []Execution)

// OnReceiveTicker is the callbck when ticker is received from websocket.
OnReceiveTicker(channelName string, ticker *Ticker)

// OnReceiveChildOrderEvents is the callbck when child order event is received from websocket.
OnReceiveChildOrderEvents(channelName string, event []ChildOrderEvent)

// OnReceiveParentOrderEvents is the callbck when board is received from websocket.
OnReceiveParentOrderEvents(channelName string, event []ParentOrderEvent)

// OnErrorOccur is the callbck when error is occurred during receiving stream data.
OnErrorOccur(channelName string, err error)

Then, Write code for receiving Realtime API data from websocket.

// Create WebSocketClient with Callback interface implement.
ws := WebSocketClient{
    Debug: false,
	Cb:    &YourCallbackImplement{},
}

// connect Realtime API.
err := ws.Connect()
if err != nil {
	log.Fatal(err)
}

// start receiving data. must to use goroutine.
go ws.Receive()

// subscribe channel
ws.SubscribeExecutions("FX_BTC_JPY")

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
LOOP:
	for {
		select {
		case _ = <-interrupt:
			break LOOP
		}
	}

How to test

Tests using private api require following environment variables.

export APIKEY=<value>
export APISECRET=<value>

# Functions

CreateOHLC converts executions to OHLC.
NewBitflyer creates Bitflyer instance.

# Constants

order type: LIMIT.
order type: MARKET.
health: BUSY.
health: NO_ORDER.
health: NORMAL.
health: STOP.
health: SUPER_BUSY.
health: VERY_BUSY.
minimum orderable size.
PathCancelAllChildOrders is path of api to cancel all child orders.
PathCancelChildOrder is path of api to cancel child order.
PathGetBalance is path of api to get balance.
PathGetBoard is path of '/getboard'.
PathGetBoardState is path of '/getboardstate'.
PathGetChildOrders is path of api to get own child orders.
PathGetCollateral is path of api to get collateral.
PathGetExecutions is path of '/getexecutions'.
PathGetHealth is path of '/gethealth'.
PathGetMarkets is path of '/getmarkets'.
PathGetMyExecutions is path of api to get own executions.
PathGetPositions is path of api to get positions.
PathGetTicker is path of '/getticker'.
PathSendChildOrder is path of api to send child order.
ProductCodeBtcJpy is product code of BTC_JPY.
ProductCodeEthBtc is product code of ETH_BTC.
ProductCodeFxBtcJpy is product code of FX_BTC_JPY.
side: BUY.
side: SELL.
state: AWAITING_SQ.
state: CIRCUIT_BREAK.
state: CLOSED.
state: MATURED.
state: PREOPEN.
state: RUNNING.
state: STARTING.

# Variables

Logger is logger.

# Structs

ApiError is lightning api error.
Balance is the balance of account.
Bitflyer is bitFlyer api client.
Board is board.
BoardState is board's state.
ChildOrder is own child orders.
ChildOrderEvent is type of child order event receiving from websocket.
Collateral is the collateral of account.
No description provided by the author
Execution is one of the execution history.
Health is market health state.
Market is the return value of '/getmarkets' API.
MyExecution is executed own history.
OHLC is four value candle(open, high, low, close).
OpenDate is the date created position.
Event of parent order happened.
Position is own positions.
Ticker is the return value of '/getticker' API.
TickerTime is time of ticker.
TimeWithSecond is time with second as time.Time.
No description provided by the author

# Interfaces

Callback is the callback functions when receiving data from websocket.