Trading on Bitfinex using QuickFIX

Jacob Plaster
Bitfinex
Published in
4 min readMay 7, 2019

At Bitfinex we are constantly working to improve our service performance and have recently announced that we are moving to self-hosted servers in order to provide access to cross-connectivity & co-location services.

To take full advantage of these improvements, we also started work on the development of a FIX gateway which will allow traders to use their existing FIX infrastructure to interact with the Bitfinex platform.

How does it work? Our gateway is written in the speedy Golang and uses the official QuickFIX/go to handle all of the FIX protocol complexities. The incoming requests are then transformed on to our Golang WebSocket candles implementation and sent to the Bitfinex platform for processing. The response then follows the same path in reverse.

Getting Started

1. Building the gateway

First, you’ll need to make sure that you have Go installed on your machine. Once you have done this you can head over to github.com/bitfinexcom/bfxfixgw where you will be able to build the binaries.

$ make build
$ make install

Running the above commands will compile the gateway and place the binary in your go bin directory (which can be found here cd $GOPATH/bin).

2. Create a config

The gateway spins up two separate servers when it begins, one for the market data and one for executing orders.

We can adjust the settings of these two processes using config files which describe the clients that will be connecting and setting runtime variables such as server port and log output directory.

Let’s start by creating a new file in the current directory marketdata.cfg:

[DEFAULT]
SenderCompID=BFXFIX
ResetOnLogon=Y
ReconnectInterval=10
FileLogPath=tmp/md_service/log
SocketAcceptPort=5001
StartTime=00:05:00
StartDay=Sun
EndTime=00:00:00
EndDay=Sun
[SESSION]
TargetCompID=EXORG_MD
BeginString=FIX.4.2
DefaultApplVerID=FIX.4.2
HeartBtInt=30

This config tells the gateway to accept a client with the ID EXORG_MD which will be using Fix version FIX.4.2 and begin the market data fix server on port 5001.

Next, lets create the orders.cfg which is almost identical to the previous:

[DEFAULT]
SenderCompID=BFXFIX
ReconnectInterval=10
FileLogPath=tmp/ord_service/log
FileStorePath=tmp/ord_service/data
SocketAcceptPort=5002
StartTime=00:05:00
StartDay=Sun
EndTime=00:00:00
EndDay=Sun
[SESSION]
TargetCompID=EXORG_ORD
BeginString=FIX.4.2
DefaultApplVerID=FIX.4.2
HeartBtInt=30

This tells the gateway to accept a client with the ID EXORG_ORD and begin the order fix server on port 5002. We can accept any number of sessions by simply adding more session descriptions.

3. Running the gateway

We are now ready to begin the gateway and start receiving fix sessions from clients. We just have to start the binary that we compiled earlier whilst passing in our configs as flags:

FIX_SETTINGS_DIRECTORY=$GOPATH/bin ~$GOPATH/bin/bfxfixgw -v -orders -ordcfg "orders.cfg" -md -mdcfg "marketdata.cfg" -rest "https://api.bitfinex.com/v2/" -ws "wss://api.bitfinex.com/ws/2"

The above command tells the gateway to locate the config files in the $GOPATH/bin directory and begin both a market data server with the config name marketdata.cfg and a orders server with the config orders.cfg.

The gateway will now be running on ports 5001 and 5002 ready for you to begin creating sessions.

Client authentication

Once a client session is established, it will need to authenticate itself using the the login function 35=A and specifying a Bitfinex api key 20000=<API_KEY> a Bitfinex secret 20001=<API_SECRET> and finally a Bitfinex username 20002=<USER>.

When sent in the form of a fix message it will look something like this:

8=FIX.4.2|9=186|35=A|34=1|49=EXORG_ORD|52=20180416-18:27:47.541|56=BFXFIX|20000=U83q9jkML2GVj1fVxFJOAXQeDGaXIzeZ6PwNPQLEXt4|20001=77SWIRggvw0rCOJUgk9GVcxbldjTxOJP5WLCjWBFIVc|20002=connamara|98=0|108=30|10=117|

For more information on fix messages please head to the repo page: github.com/bitfinexcom/bfxfixgw. Here you will find advanced examples on creating orders and subscribing to data.

Create an order & testing client

In order to help develop around the gateway we have included a test client script which automatically generates fix protocol requests, prints the output into the console and then sends the request to the gateway. This feature can be really useful to help visualize the parameters needed for each request.

To use the client we need to create another config file, this file will tell the test client which Bitfinex api key, secret and username to use when authenticating with the gateway.

Create new config file named client_order.cfg in the go bin path:

[DEFAULT]
SenderCompID=EXORG_ORD
ReconnectInterval=10
FileLogPath=tmp/ord_client/log
FileStorePath=tmp/ord_client/data
StartTime=00:05:00
StartDay=Sun
EndTime=00:00:00
EndDay=Sun
ApiKey=U83q9jkML2GVj1fVxFJOAXQeDGaXIzeZ6PwNPQLEXt4
ApiSecret=77SWIRggvw0rCOJUgk9GVcxbldjTxOJP5WLCjWBFIVc
BfxUserID=jacobplaster
[SESSION]
TargetCompID=BFXFIX
BeginString=FIX.4.2
DefaultApplVerID=FIX.4.2
SocketConnectHost=localhost
SocketConnectPort=5002
HeartBtInt=30

Notice how we have set the SenderCompID to be the same as the ID that we specified earlier in the gateway order config. If the ID does not match with any session in the gateway config then the client connection will be rejected.

Now we can run the client using the following command:

$GOPATH/bin/fix_client -cfg $GOPATH/bin/orders_fix42.cfg

The client will then proceed to ask you a few questions about the command you want to execute. Since we want to create a new order we can start by using the nos command:

Enter command:
nos
Enter ClOrdID (integer):
1
Enter symbol:
tBTCUSD
Enter order type:
market
Enter qty:
0.05
Enter side:
buy

The script will then display and send the fix payload to the gateway! You can also use this client to subscribe to market data and more so please play around with this useful little script.

Thanks for reading!

As mentioned, before please make sure you head over to the repo main page at github.com/bitfinexcom/bfxfixgw which includes a lot more advanced examples on how to use the gateway.

The entire project is open-source so please feel free to open PR to help us improve the gateway.

Start trading on Bitfinex today.

The Bitfinex APIs are designed to allow complete access to the features provided by Bitfinex. Learn more about our API documentation here.

We’ve recently open-sourced a number of the development libraries most essential to Bitfinex. If you are interested in learning more, visit our Github.

--

--