Author

Topic: Bitcoin Backtesting (Read 67 times)

legendary
Activity: 1596
Merit: 1288
June 08, 2022, 03:07:51 PM
#1

Disclaimer: This article is intended for and only to be used for reference purposes only, Google it again, check sources and DYOR.


Table of contents

      1. What Is Backtesting?
      2. Manual Backtesting
      3. Systematically Backtesting a Strategy
      4. Bitmex Data python code






Introduction

If you are a trader then backtesting is the essential test in developing your trading strategy where you will re-test your trading strategy through historical or past data and thus see what the results will be if you apply it to live data.
However, if backtesting is not done accurately, you will get false results and therefore when you make a trade on the platform it will cause losses.

Therefore, it is a double-edged sword, but doing it correctly will lead you to good results in the cryptocurrency market, especially for those who trade a lot.

Backtesting does not guarantee profits, but it is an ideal way to test whether it is effective or not. However, unreliable data can affect the backtesting process.





Manual Backtesting


It is the way we do it often, where the decision is made based on tracking the latest historical data or certain candles, taking into account some technical indicators, and then building a trading strategy by placing a buy or sell signal in the market.

Mostly, all the user does is use programs like TradingView where he chooses a trading strategy and then presses the restart button and chooses a period in history to start testing.

Based on the results of this test, the trader makes a discretionary decision to buy or sell and may build his or her strategy using an Excel spreadsheet.
Therefore, manual trading is easy and reduces dependence on emotions by studying some market data, but it is not strong enough, as you are doing a non-analytical study of a small part of the historical data, and therefore you may achieve some losses, especially if the recent data is in a certain upward trend.




Systematically Backtesting a Strategy

The main difference here is that the user has more accurate data due to the processing power of computers and the suitability of some programs and thus the possibility of comparing more historical data and using in-depth statistics about their strategy and thus determining whether it is appropriate or not.

The method is data acquisition, historical data requirements, OHLCV, book data request.

Historical data requirements
Trading costs such as commissions and exchange rate differences can lead to inaccuracies in calculating profits and thus deducting more money from profits.

Two different types of market data are used, candlestick and order book data, but order book data is usually more reliable.

OHLCV candlestick data:

OHLCV candles data (Open-High-Low-Close-Volume) OHLCV is basically a spreadsheet of OHLC price data for each time period of the chart timeframe.

The main problem with this method is to ensure that there is sufficient liquidity to support your trading activity, and therefore buy and sell orders may not be executed accurately, and thus may cause losses, but they are easier to obtain.

Book data request

The order book is better than candlestick data. Because it includes market price, size, and depth, and thus a better representation of the orders available at any time.



Bitmex Data python code


The first step is to get and download the data. Bitmex have trading files that are publicly available to download, you can download it from here https://public.bitmex.com/?prefix=data/trade/

Now define the date range for trading data. In our case, we can set it from October 2020.

Code:
import pandas as pd
date_from = "2020-10-01"
date_to = "2020-10-31"
dates = pd.date_range(date_from, date_to).astype(str).str.replace("-", "")


Now define a function that downloads a trading file and saves it to a folder.

Code:
import requests
from os import path
BASE_URL = "https://s3-eu-west-1.amazonaws.com/public.bitmex.com/data/trade/%s"

def download_trade_file(filename, output_folder):
    print(f"Downloading {filename} file")
    url = BASE_URL % filename
    resp = requests.get(url)
    if resp.status_code != 200:
        print(f"Cannot download the {filename} file. Status code: {resp.status_code}")
        return
    with open(path.join(output_folder, filename), "wb") as f:
        f.write(resp.content)
    print(f"{filename} downloaded")

Now iterate through dates and download daily file that end with .csv.gz

Code:
output_folder = "data/bitmex"
for date in dates:
    filename = date + ".csv.gz"
    download_trade_file(filename, output_folder)


import glob
filepaths = glob.glob(path.join(output_folder, "*.csv.gz"))
filepaths = sorted(filepaths)
filepaths

our data will be looks like


As we see above, each trade is in own row. Backtrader doesn't know how to work with this data format so we need to convert it to OHLC format (Open, High, Low, Close).

Code:
df = df.groupby(pd.Grouper(key="Datetime", freq="1Min")).agg(
    {"price": ["first", "max", "min", "last"], "foreignNotional": "sum"}
)
df.columns = ["Open", "High", "Low", "Close", "Volume"]
df.loc[:, "OpenInterest"] = 0.0 # required by backtrader
df = df[df.Close.notnull()]
df.reset_index(inplace=True)

Now you get your data as OHLC format and ready to test it.




Sources
Code:


https://medium.com/swlh/backtesting-a-bitcoin-trading-strategy-96ea854762bc
https://alpaca.markets/learn/backtesting-bitcoin-with-pandas-and-market-data-api/
https://romanorac.medium.com/
https://www.cryptohopper.com/features/backtesting
https://learn.bybit.com/strategies/backtesting-crypto-trading-strategies/

Jump to: