Back

Crypto Trading on QuantConnect

Hey QC community,

We are excited to announce the launch of cryptocurreny backtesting and live trading on QuantConnect! We’ve partnered with GDAX to support their cryptocurrency exchange.  Using your GDAX account, your algorithms can now trade BTC, LTC and ETH currency pairs. Huge thanks to our community members who helped make Lean ready for crypto. In this post, I’ll demonstrate backtesting a cryptocurrency trading algorithm using python and describe how you can get started live trading cryptocurrencies with GDAX.

Attached is a simple backtest that demonstrates subscribing to BTCUSD hourly data and placing an BTCUSD market order. Initially, QuantConnect is offering a limited set of GDAX quote data for BTCUSD for backtesting, however, users can expect our crypto data library to grow soon. Clone the backtest below to start building your crypto trading algorithms.

In order to get your algorithm live trading, sign up for a GDAX account here: https://www.gdax.com/ . To live trade, you’ll need an api key, api secret and api passcode.  To get this information, login to your GDAX account and click API from the menu in the top right corner. Create a new API key that has permissions to ‘view’ and ‘trade’ and be sure to record the api key, api secret and api passcode. You now have all the information needed to launch your live crypto-trading algorithm! Simply click ‘Go Live’ from your algorithm and follow the wizard. Be sure to select Gdax as your brokerage.

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.


Jared Broad, I still can't get the benchmark working for crypto. I've attached a backtest reproducing the problem.

Also, love the PostOnly option! You should release the Time in Force Policy options too, Immedaite or Cancel being the one I'm most interested in.

1


Also because I thought it'd be handy for others, here's my override of SetHoldings that uses limit orders so you don't have to pay fees! Just paste it into your class for new powers. Made to be tweakable.

# Override SetHoldings to use limit orders (ratio is of totalPortfolioValue.)
def SetHoldings(self, symbol, ratio):

security = self.Securities[symbol]
if not security.IsTradable:
self.Debug("{} is not tradable.".format(symbol))
return # passive fail
ratio = d.Decimal(ratio)
price, quantity = security.Price, security.Holdings.Quantity

# Keep 2% Cash (for the limit order, rounding errors, and safety)
totalPortfolioValue = self.Portfolio.TotalPortfolioValue * d.Decimal(0.98)

# +0.1% Limit Order
# (to make sure it executes quickly and without much loss)
# (if you set the limit large it will act like a market order)
limit = 1.001
desiredQuantity = totalPortfolioValue * ratio / price
orderQuantity = desiredQuantity - quantity
# limit needs to be inverse when selling
limitPrice = price * d.Decimal(limit if orderQuantity >= 0 else 1/limit)
self.Log("Limit Order: {} coins @ ${} per coin".format(orderQuantity, limitPrice))
self.LimitOrder(self.crypto, orderQuantity, limitPrice)

Use it like regular SetHoldings:

self.SetHoldings("BTCUSD", 1) # all in

Although order quantities are rounded for now, until someone fixes LimitOrder :).

2


Sorry there was a typo in the last line, but I can't edit so here's it all again:

# Override SetHoldings to use limit orders (ratio is of totalPortfolioValue.)
def SetHoldings(self, symbol, ratio):

security = self.Securities[symbol]
if not security.IsTradable:
self.Debug("{} is not tradable.".format(symbol))
return # passive fail
ratio = d.Decimal(ratio)
price, quantity = security.Price, security.Holdings.Quantity

# Keep 3% Cash (for the limit order, rounding errors, and safety)
keep = .03
usablePortfolioValue = self.Portfolio.TotalPortfolioValue * d.Decimal(1 - keep)

# +0.1% Limit Order
# (to make sure it executes quickly and without much loss)
# (if you set the limit large it will act like a market order)
limit = 1.001
desiredQuantity = usablePortfolioValue * ratio / price
orderQuantity = desiredQuantity - quantity
# limit needs to be inverse when selling
limitPrice = price * d.Decimal(limit if orderQuantity >= 0 else 1/limit)
self.Log("Limit Order: {} coins @ ${} per coin".format(orderQuantity, limitPrice))
self.LimitOrder(symbol, orderQuantity, limitPrice)
3

Hi

Something that would be very useful woudl be StopLimitOrder for crypto, as far as I can't tell it does not work. 

Since the exchange is very unreliable at the moment, it would be good to be able to protect yourself with StopLimitOrder stop losses. Based on recent incidents most of the time they would be executed when the exchange API crashes during big sell offs.

Is this something in the pipeline? (or something I did not test properly!) 

thanks,

0

@AlphAngel, sadly the GDAX API does not support StopLimit orders, very strange since they are actually supported in their trading platform (Stop -> Advanced -> Limit price). 

If enough people requested this feature (omission) to them, maybe they will add them to the API and we will quickly add them to the QC GDAXBrokerage implementation.

2

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 see these currencies have historical quote data (like forex) so the data has accurate Ask/Bid prices. I can't remember, but does this mean that backtests buy at the ask and sell at the bid? I'm wondering if slippage is something to factor in or not with backtest results.

2

Crypto minute data has spread yes; sub-minute only has spread at the minute boundaries. This will buy and sell at the ask and bid price. 

You still need to worry about slippage as the depth of the market is shallow at the top of book. At most you'll normally only have 0.1 BTC etc available to trade. We have the depth data but LEAN doesn't support it yet so its on the todo.

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.


Yes I'd absolutely love depth simulation, I'm working on a low-liquidity strategy but it's making unrealistically large sums of money in backtests because the backtester doesn't understand when the market is illiquid. I'll be trying live soon but it'd be great to see realistic performance historically.

0

.Plot() seems to be broken now in my example above? Throws fatal error that doesn't make sense to me.

0

This is so exciting! Thank you so much for all your hard work getting crypto added to QC. You guys rock! 

2

Haumed Rahmani, there are two bugs in the Plot method that we will fix ASAP.
Meanwhile, do not use self.Plot(string, value). Instead use self.Plot(string, string, float(value)):

self.Plot("Leverage?", "Leverage?", float(portfolio.TotalHoldingsValue / portfolio.TotalPortfolioValue)) # inaccurate
self.Plot("Price", "Price", float(security.Price))
self.Plot("Quantity", "Quantity", float(quantity))
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.


I just stumbled about the QuantConnect website and really like it. I'm more or less only trading in Crypto thus that's the important part for me. So a question would for sure be if there are plans to add other Crypto-Exchanges, cause honestly spoken, GDAX is one of the worst of them and only offers an extremly limited set of Trading-Pairs. The, in my opinion, interesting Crypto Exchanges are more Poloniex, Bittrex, HitBTC, COSS in the future (they are in Beta and don't have an API ready at the moment). The point is simply that trading Altcoins is the more interesting and rewarding part than trading Bitcoin/Ethereum/Litecoin.

3

Absolutely thomas_brawand! We're working on Bitfinex now. LEAN requires a reliable API and ideally streaming order messages so it does limit us to the major exchanges.

5

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.


Bitfinex is a step into the right direction ;) 

Thanks for the lightning fast answer

2

Another crypto exchange would be great! 

1

Hi, my experience live with gdax so far as been pretty poor, during the big selloff the API usually gets disconnected meaning your sell orders are not executed and you can place stop loss order so it is very risky... still works in mild market but just be aware that the connectivity is nto as realibale as with the big equity/Fx brokers... so you need to be on top of it !!

2

@AlphAngel have you had any luck with LimitOrders on GDAX? How did you do that?

I'm using the code by @Haumed_Rahmani but it doesn't work for Liquidate it seems, just SetHoldings.

0

Crypto is working fine for all supported order types - inc liquidate and set holdings. GDAX doesn't support virtual positions; I've given a detailed explaination at the link below. Lots of users seem to be stubling with this so we'll try and write something to help understanding/using crypto.

https://www.quantconnect.com/forum/discussion/3178/quotbtcusdquot---what-are-virtual-positions/p1
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.


@Drew baker, I don't have any issues with Limit orders they work fine (even Post Only), I'm using c# not sure if it makes a difference..

1

Seconded on adding more exchanges--GDAX is only 9th of the Crypto exchanges by Daily Volume.

I'd love to see the top 8, including Binance, Bithumb, and Bitfinex (which you mentioned is indev) get prioritized and added ASAP!

3

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