Developing Bitcoin Trading Algorithms with Quantiacs
-
This article was published on gitconnected on Medium: check it out here.
In this article we describe the implementation of a simple quantitative strategy on the Bitcoin Futures contract with Quantiacs.
This month we released a new version of the Quantiacs platform with several major improvements. In addition to providing a new open-source backtesting tool and the possibility to download for free financial data, we created a cloud environment for quants. Here it is possible to code trading algorithms in Python using Jupyter Notebook or JupyterLab and to run them directly online using our resources for free.
The competition model has also changed: the 15th edition of the Futures contest, based on 75 liquid Futures contracts (including stock indices, bonds, forex and commodities), is running together with the 1st edition of the Bitcoin Futures contest.
We are going to allocate 4M USD to the best systems. 7 quants will get allocations for their Futures systems, and 7 quants for their Bitcoin Futures systems, provided they are submitted before May 31st, 2021, and they have an in-sample Sharpe ratio larger than 1.
In this article we describe the implementation of a simple trading algorithm on the Bitcoin Futures contract.
After registering to Quantiacs, you can open your personal development area, Clone one example and edit it with Jupyter Notebook or JupyterLab.
After opening the notebook we import the needed libraries:
import xarray as xr import qnt.backtester as qnbt import qnt.data as qndata import qnt.ta as qnta
We based Quantiacs on xarray as it makes working with multi-dimensional arrays very efficient. Our qnt library can be accessed at our github repository and the documentation can be accessed at our page.
Next we define two functions: one for loading the Bitcoin Futures data, the second one for defining an elementary trend-following strategy based on the crossing of two simple moving averages:
def load_data(period): crypto= qndata.cryptofutures.load_data(tail=period) return crypto def strategy(data): close_crypto= data.sel(field=”close”) sma100= qnta.sma(close_crypto, 100).isel(time=-1) sma20 = qnta.sma(close_crypto, 20).isel(time=-1) return xr.where(sma100 < sma20, 1, 0)
The strategy is investing in Bitcoin Futures when the 20-day moving average is larger than the 100-day one, otherwise it is not allocating anything.
Finally we call the Quantiacs backtesting function:
qnbt.backtest( competition_type= “cryptofutures”, load_data= load_data, lookback_period= 365, start_date= “2013-06-01”, strategy= strategy )
and run the notebook. Among the output messages, you will see that the Sharpe ratio in the in-sample period relevant for the competition is larger than 1:
Check the sharpe ratio... Period: 2014-01-01 - 2021-02-17 Sharpe Ratio = 1.4853014048450253 Ok.
This means that this system can be submitted and will be eligible for the contest. Of course this idea represents only a starting point, so you should improve it.
Once you have completed your research, you can select this strategy from your Development area and click on the Submit button. The strategy will be processed every day on our servers with new market data, and you will be able to monitor the progress in your personal Competition area.
The first submissions are already arriving and they can be checked out at the Leaderboard page:
The full strategy
import xarray as xr import qnt.backtester as qnbt import qnt.data as qndata import qnt.ta as qnta def load_data(period): crypto= qndata.cryptofutures.load_data(tail=period) return crypto def strategy(data): close_crypto= data.sel(field=”close”) sma100= qnta.sma(close_crypto, 100).isel(time=-1) sma20 = qnta.sma(close_crypto, 20).isel(time=-1) return xr.where(sma100 < sma20, 1, 0) qnbt.backtest( competition_type= “cryptofutures”, load_data= load_data, lookback_period= 365, start_date= “2013-06-01”, strategy= strategy )