Giter Club home page Giter Club logo

bitflyergo's Introduction

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>

bitflyergo's People

Contributors

mitsutoshi avatar kajeagentspi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.