Trading on Bitfinex using QuickFIX
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.
1. Building the gateway
$ 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
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
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:
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
The gateway will now be running on ports 5001 and 5002 ready for you to begin creating sessions.
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
When sent in the form of a fix message it will look something like this:
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:
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
Enter ClOrdID (integer):
Enter order type:
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.
Bitfinex FIX Gateway. Contribute to bitfinexcom/bfxfixgw development by creating an account on GitHub.github.com
The entire project is open-source so please feel free to open PR to help us improve the gateway.