Back

Bitfinex Brokerage

Having a little time on my hands (and sufficient naivety) I've decided to implement a Bitfinex brokerage. I'm taking this as a learning experience having been developing in C# for a long time but never being involved in trading systems. I have toyed with simple algorithms through LEAN in backtesting and paper trading with a simulated broker, so am in the process of integrating with the Bitfinex api as the next logical step. I'd guess at this stage I'm about halfway towards an end-to-end trade.

I'm open minded about how I go about this so would be interested in any advice. I'm also already aware that this whole effort is likely a fool's errand, so I don't mind hearing exactly how foolish it is and how unlikely it is that this will ever result in something that turns a profit.
Update Backtest








Epic @James! That is awesome to hear! :) For the sake of efficiency it might be more efficient to work through any questions on this in the Gitter chat room -- https://gitter.im/QuantConnect/Lean

Brokerages are a challenging undertaking but certainly not a fools errand! :) I had a quick look at the Bitfinex API and you'll need to setup a polling system since they do not have a push-order-fill event system. The Tradier API uses a similar system and its an absolute mess to code. It works something like this:
1> Poll to get order book state,
2> Wait 500ms
3> Poll again, if there's a change from (1) then send a fill event to algorithm.
https://github.com/QuantConnect/Lean/blob/master/Brokerages/Tradier/TradierBrokerage.cs

Ideally bitfinex would open up a web socket order API and this would be a piece of cake.

If you commit the work to Github we can make it available on the website so the whole community can use this brokerage!
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


@James You might want to take a look at https://github.com/twobeeb/BitfinexAPI It is about a year old, but it might give you some ideas.

Nick
1

Thanks. There are already projects that did the actual API integration including:

https://github.com/workingobrien/TradingApi.Bitfinex

I have no problem with reusing existing code in my personal fork and don't expect things will get much further in the short term. If I were starting from scratch I may avoid REST and use the Websocket API (http://docs.bitfinex.com/#websocket) where possible.

At this stage, my main concerns are about extending LEAN. So far I've found that the SecurityType.Forex, currency pairs and FeeModel are more tightly coupled to the existing brokerage than I'd like. Ideally, I would like to extend the Forex objects, set the brokerage model and then add conditions based on currency pairs and keep things simple. I have looked at adding a new security type and market from the ground up but think (and hope) this is unnecessary.
0

I continue to be impressed with the LEAN engine: after taking a closer look I've been able to integrate with very minimal changes. This has meant taking a work around approach. The calculator for buying quantities expects trades to be in whole number units, but Bitfinex allow trading at 0.01 BTC. The solution is to convert in brokerage class and quote all prices at this unit level. This might be more acceptable if the symbol could be mBTCUSD, but the currency pairs expect 6 characters. At this stage I am more conscious of making minimal impact than enhancing extensibility so can live with this.
1

Great news James! Happy to hear it integrated with minimal changes. I agree the quantity should be a decimal not an int. We'll make this change soon. Please submit a PR with the brokerage implementation for review to github and we'll see about including it in master.
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Second planned stage of Bitfinex websockets integration is now complete. I've been live trading and Alpha testing for a few weeks now, My fork passes all brokerage unit tests. We're probably not ready for a PR until 3rd party library is refactored. Regardless, can now trade Bitcoin with Lean.

https://github.com/RicksonByArmbar/Lean

3

@James that is epic! Creating a brokerage is a big endeavor -- nicely done! Please feel free to do a PR when you think its ready. We'll put it through our own testing and add the option to live trade with BTC in the QuantConnect.com GUI!

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


It's great you're so enthusiastic. The project has been a lot of fun. I'm getting near the point a public beta would be beneficial. In terms of refactoring the 3rd party library, the code is clean and efficient, but there are two issues:

1. Log4Net is used for logging which is superfluous with custom logging stack.
2. It relies on app.config whereas LEAN uses config.json. I have leveraged xml transforms for app.config very effectively in the past so admit I miss having this capability.

Apart from that I need to comment and perform some QC, but it's not that far away from being presentable.

On another note: I'd really like to hear your thoughts on DI/IOC. For instance, it would be useful to mock out the exchange when testing brokerage classes, and I expect this is something you've already looked into.

0

My last comment is outdated already. Migration is done. Merged with latest upstream. For the time being, referencing Moq for some unit tests. Alpha testing and live trading has started with a potential release build.

0

Awesome James! Thanks for the updates. Please keep us in the loop! If possible please submit a PR for the crypto.io toolbox project. I'm sure lots of users will be interested in backtesting on bitcoin data.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


I should be able to cherry pick the tick data downloader sometime soon. I'm tempted to just PR the whole Bitfinex branch as it's now been quite extensively tested. The only noteworthy thing missing relates to the margin call behavior, which is completely different to the SecurityPortfolioManager. You can just ignore the margin call requests at algorithm level, but this is a less than ideal solution. I'm undecided whether to hold back the PR until I have a complete and highly polished version or instead run with the current build that is functional but with known omissions.

0

It may make sense to submit the PR now and allow for some review/feedback on the code. While the review is in progress, maybe we can implement an IBrokerageModel for bitfinex would could serve up the proper custom margin model.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Agreed. I'm really keen to get started on ironing out the wrinkles and I'm open minded about the most appropriate way to extend Lean.

In terms of the margin call behavior I was expecting to need to override SecurityPortfolioManager.ScanForMarginCall and generate warnings/requests when appropriate. The real problem here would be injecting the derived class without overriding several other areas. As I said I'm open minded on this issue.

0


1

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed