Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (2024)

Using Binance and Backtrader to analyze historical data, backtest models, automate crypto trading and confirm a few programmatic trading myths.

Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (2)

What we want to achieve in this series is to learn about the Binance crypto platform, learn how to trade with nominal transaction fees, analyze crypto BTC historical data, backtest trading models and understand how we can profit from volume/volatility in these markets.

Part 1: the Binance platform, training and data

-> Part 2: Backtrader for backtesting trading models

Part 3: putting various models to the test

Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (3)

In this part we want to install Backtrader and backtest some trading models against the Binance data we gathered in the prior section.

There are numerous articles and videos on Backtrader and its setup. This popular Python library facilitates the Quant work of backtesting trading strategies with historical data, answering the quintessential question “how profitable would it have been to trade using given BUY/SELL strategies”. This feels like mathematical alchemy at first but one has to remember that historical data is, well, historical! A trading strategy that worked yesterday is unlikely to work today… but we’ll get back to that shortly.

Yesterday’s profitable trading strategy is likely a loser today.

Backtrader (‘bt’) installation instructions are here. Note: there are known issues with mapplotlib versions above 3.2.0 so beware about that.

Quickstart guide is a worthwhile read, find it here.

RSI

What we will be trying with Backtrader here is backtesting an RSI (Relative Strength Indicator) trading strategy on historical crypto data (for BTC) from earlier in the year.

The RSI momentum indicator is explained here. It measures relative oversold and overbought conditions for a given trading asset and a parameter of ‘period’ which is the # of ticks (trading intervals) backwards.

Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (4)

The period parameter defaults to 14, so if the interval is minutes then the formula will include 14 interval ticks of data. As we’ll explore next, each technical indicator has parameters which are our way of ‘tuning’ to market conditions; these parameters have a huge impact on the profitability of any given indicator within a strategy.

Small things often have a huge impact.

Backtest.py

Our backtest setup: backtest.py is shared here. This will provide the backtest structure for our backtest run, to be defined next. This is a fairly standard ‘bt’ setup. Let’s review some of this code, note that there are plenty of examples and video tutorials online on Python backtest to learn from.

Here in the class definition we establish parameters for our RSI strategy.

  • verbose: if we want to output log data during the backtest
  • maperiod: moving average period, the # of ticks to consider
  • quantity: the # of shares to buy/sell
  • upper: the upper threshold of the indicator for overbought
  • lower: the lower threshold of the indicator for oversold
  • stopLoss: the stop loss setting for selling

The next() function in a Backtrader strategy class is what happens after each interval ‘tick’ of data. Here is buy() or sell() according to the data, in this case the RSI indicator and our thresholds.

Here we define the runbacktest() function which will be called by our code. The aforementioned RSI strategy function is added to the cerebro instance.

All fairly standard Backtrader stuff. Let’s see how to run this against our data.

Backtesting our data

Be sure to get data (using the last section’s steps) for Jan 1 to Jan 2 2021, this will be in a file named: BTCUSDT-20210101–20210102–1m.csv with 1440 CSV lines, one for each minute of the day.

Here is the code and output for this one day’s worth of minute-by-minute trading day for Bitcoin (BTC):

Taking a closer look:

The parameters are simple, we want to analyze one day of trading, using the RSI indicator with a period of 12 ticks, no stop-loss and default limits of 70,30 for the overbought and oversold triggers.

Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (5)

The last line of output summarizes the results of this backtest:

/BTCUSDT-20210101-20210102-1m.csv, RSI (Pd 12) (SL 0.0%) (U70 L30) Net $777.78 (0.78%) WL 18/7 SQN 1.76

RSI period 12, 0 (No) stop-loss, (U)pper limit of 70 (L)ower limit of 30, net profit (in one day) of $777.78 with 18 winning trades and 7 losing trades.

The last figure is SQN, a ‘System Quality Number’ (SQN) which is designed to assist traders in determining the strengths, desirability, quality of a trading system. A good quality strategy is seen as one that is both tradeable and efficient.*

The following SQN values suggest the following “qualities” :

  • 1.6–1.9 Below average
  • 2.0–2.4 Average
  • 2.5–2.9 Good
  • 3.0–5.0 Excellent
  • 5.1–6.9 Superb
  • 7.0 — Holy Grail

The SQN formula :

SquareRoot(NumberTrades) * Average(TradesProfit) / StdDev(TradesProfit)

Normally we would insist on at least 30 trades for this metric to be statistically significant but we’ll ignore that for now as we are testing out our backtest with a short period of time.

You can zoom into sections of the plot, for example:

Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (6)

Here we see a buy signal (green up-arrow) as the RSI value dips below 30 and then a profitable sell signal as and profit marker (blue circle) as RSI reaches above 70. See the values for RSI in the right-lower corner.

Profit (in one day) of $777.78 with 18 winning trades and 7 losing trades is quite good, particular for a trading day of relatively shallow action (+1.42%). Imagine what we could achieve on a bullish day with high volume!

Model parameters

You get run get_data for different days and analyze these separately. Notice how different RSI parameters have an impact on profitability from one day to the next.

Axiom of quant trading: try many different things and measure results.

Case in point, the same day of BTC trading but with an RSI period of 20 rather than 12, win-loss of 2/3 and a net profit of -$21.51 (including trading fees). That’s a big difference from the last backtest!

Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (7)

You can also experiment with different RSI limits (other than the default 70/30) and stop-loss parameters. Stop-loss is an automatic sell order once the price goes below some level relative to the executed buy order. As the name implies, this can serve to “stop a loss” after getting into a position in volatility.

Stop-Loss

The way we’ve setup stop-loss here is as follows:

  • 0 : no stop-loss setup, wait for the indicator to trigger a sell order
  • 0.00x : stop-loss at a % value below purchase price, 0.001 is 0.1% under
  • -0.0x : trailing stop-loss will follow the trade as the price goes up, 0.01 is a trailing stop-loss 1% below purchase price

This stop-loss is an important parameter for each trade and can have a significant import, not surprisingly, on performance. For more on stop-loss strategies see here.

Here in our backtest.py is where we set this up using backtrader:

Here is the same run as we just analyzed but with a 0.1% trailing stop-loss

Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (8)

Net profit of $383.67 with 12 wins and 12 losses, much better than the loss we had prior. The you can see in the plot that the trailing stop-loss protected many of the trades from slipping into losses as the indicator waiting for a sell (overbought) signal.

Within a single indicator, in this setup then, we have many different possible permutations:

  • a period range between 10 and 30 intervals (20 variants)
  • a stop-loss setting (let’s imagine 5 different practical variants)
  • a threshold for overbought/oversold (let’s imagine 5 variants for now)

That would be 20x5x5, or 500 different variations for each day. To examine these one-by-one by hand would be ridiculous and yet we want to know which parameters were most profitable and of the highest trading quality and which were not.

Quant alchemy!

This brings us to our next step in this Crypto Quant exploration. We can brute-force determine the most profitable and highest quality trading strategy parameters for a given period of trading and then see how these carry forward.

Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (9)
Crypto Quant: programmatic trading of BTC using Binance and Backtrader — Part 2 of 3 (2024)

FAQs

Is QNT listed on Binance? ›

QNT can be purchased directly from Binance using a debit or credit card. It can also be traded for other cryptocurrencies on the Binance exchange. Live QNT price is updated in real-time on Binance.

What is quant trading in Bitcoin? ›

Crypto Quantitative Trading is an amalgamation of sophisticated mathematical and statistical techniques, machine learning algorithms, and financial acuity applied to the volatile world of cryptocurrencies for making data-driven, strategic investment decisions.

What is 3x on Binance? ›

What does 3x mean in Binance? It means that you are using a leverage of 3x on margin trading. Suppose you open a position of 30 USDT with a capital of 10 USDT. By using leverage, the exchange holds your 10 USDT as collateral and provides you with an additional 20 USDT to open the position.

Can I trade my Bitcoin on Binance? ›

Absolutely, you can buy and sell Bitcoin on Binance! Here's how to make it happen: * Get verified: Binance requires users to verify their identity to prevent fraud and comply with regulations. * Deposit funds: You can deposit fiat currency like USD via bank transfer or debit/credit card.

How much will QNT be worth in 2030? ›

Quant (QNT) Price Prediction 2030
YearPrice
2025$ 102.37
2026$ 107.49
2027$ 112.86
2030$ 130.65
1 more row

How much will Quant be worth in 2025? ›

What is the Quant price prediction for 2025? According to our Quant price prediction, QNT is forecasted to trade within a price range of $ 81.10 and $ 505.36 next year. Quant will increase by 409.97% and reach $ 505.36 if it reaches the higher value target for 2025.

Is quant trading legit? ›

A quant trader's job and associated perks appear very lucrative, but the ones qualifying for this highly competitive field need multifaceted skills, knowledge, and temperament. Quantitative traders usually have a moderate success rate, and many diversify or move out to other streams after a few years due to burnout.

Do quant traders make a lot of money? ›

Yes, quants tend to command high salaries, in part because they are in demand. Hedges funds and other trading firms generally offer the highest compensation. Entry-level positions may earn only $125,000 or $150,000, but there is usually room for future growth in both responsibilities and salary.

What is the difference between trading and quant trading? ›

The biggest benefit of quantitative trading is that it enables you to analyse an immense number of markets across potentially limitless data points. A traditional trader will typically only look at a few factors when assessing a market, and usually stick to the areas that they know best.

What does 3x + 5x + 10x mean on Binance? ›

There are different degrees of leverage in margin trading. You can trade at 2x leverage, or 3x, 5x, 10x, or even up to 100x. In other words, leverage multiplies the amount of money you have to invest. So if you have $10,000 in capital, margin trading at 2x leverage would allow you to purchase $20,000 worth of assets.

What is the max cash out Binance? ›

Here's a quick rundown of Binance withdrawal limits: Regular users: 8,000,000 BUSD. VIP 1: 8,000,000 BUSD. VIP 2: 8,000,000 BUSD.

What is 5x in trading? ›

Leverage is often expressed as a ratio (5:1 or 10:1) or as a multiple (5x or 10x). In the case of a 5x or 5:1 leverage, your purchasing power would be five times that of the amount you deposit as a margin. For instance, you can enter into a position as large as ₹25,000 by depositing a margin of just ₹5,000.

Do I own my Bitcoin on Binance? ›

Binance is a centralized exchange, which means that users deposit their bitcoin into wallets controlled by Binance. When you send bitcoin to Binance from another wallet, the ownership of the bitcoin is transferred from your wallet to the wallet controlled by Binance.

Can I convert my Bitcoin to cash on Binance? ›

Convert Bitcoin into Cash | Binance. Now in Binance P2P Cash Zone, you can sell bitcoin for cash in 10+ fiat currencies: AMD, CLP, DOP, GEL, GHS, GTQ, HNL, KES, NGN, NIO, PEN, PKR, TWD, UYU and ZAR. There are several ways to sell bitcoin for cash on Binance P2P, each with its pros and cons.

How to turn Bitcoin into cash instantly? ›

Bitcoin ATMs are a way to get immediate access to cash using your bitcoins. Bitcoin ATMs do not operate like traditional ATMs. In order to make a cash withdrawal and sell your Bitcoin from the ATM, the machine provides a QR code to which you send your Bitcoin. You simply wait a couple of minutes and receive your cash.

How do I buy Quant on Binance? ›

Where & How to Buy Quant (QNT) Guide
  1. Navigate to buy Quant with USD page on Binance.
  2. Select Quant and USD from the dropdown menu.
  3. Choose "Card" as the payment method, and click "Confirm".
  4. "Add New Card" will appear if you have not purchased crypto in Binance using debit or credit card before.

Which coin will be listed on Binance? ›

Binance New Listings – Analysis of Potential 2024 Coins
  • Dogeverse (DOGEVERSE) – An Upcoming Binance Listing With Over 600,000% Staking APY.
  • Sealana ($SEAL) – Solana-Based Meme Token With a Fixed Presale Price.
  • Mega Dice Token (DICE) – Solana Casino Offering Rewards to Token Holders.
4 days ago

Is Quant listed on Coinbase? ›

Fortunately in United States, you can buy Quant on Coinbase's centralized exchange. Coinbase is the most trusted place for people and businesses to buy, sell, and manage Quant.

How do I know if my coin is listed on Binance? ›

To find the latest cryptocurrencies added to Binance, visit the "Recently Added" section, which is updated regularly with new crypto coins and their trading activity.

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 6378

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.