Skip to content

Backtesting a Built In Strategy

Alex Lopatin edited this page Jan 25, 2019 · 3 revisions

This tutorial shows how to run the "Hello World" of algorithmic trading in Flashbot: Backtesting the Dual Moving Average Crossover strategy.

Contents

  1. Collect market data
  2. Run backtests using FlashbotClient
  3. Run backtests using the dashboard

The source code for this tutorial can be found at:


1. Collect market data

We'll start by backtesting on 1-min candles for the BTC-USD market on Coinbase. To save this data, make sure that you setup the PostgreSQL database as described on the Project Setup page.

Ensure the src/main/resources/application.conf file is configured to

  • Download one year of 1-min candles (as well as 3 months of trades, and an order book, which we'll use later)
  • Use the PostgreSQL DB

Like this:

flashbot {
  ...
  db = "postgres"
  ingest {
    enabled = [
      "coinbase/btc_usd/candles_1m",
      "coinbase/btc_usd/trades", 
      "coinbase/btc_usd/book"
    ]
    backfill = [
      "coinbase/btc_usd/candles_1m", 
      "coinbase/btc_usd/trades"
    ]
    retention = [
      ["*/*/candles_1m", "365d"],
      ["*/*/trades", "90d"],
      ["*/*/book", "1h"]
    ]
  }
  ...
}

And create our main app in src/main/scala/BacktestExample.scala:

todo: source code

This will start a DataServer to collect data and a TradingEngine to run backtests and serve the dashboards.

Run the program by running sbt run in the project root:

$ sbt run
...
todo: logs
...

2. Open the DMAC Backtest dashboard

Ensure Grafana is setup (instructions).

Open Grafana in a web browser ([http://localhost:3000] by default) and select the "DMAC Backtest" dashboard in the left panel. This dashboard is automatically created because Flashbot creates a backtest dashboard for all configured strategies, including the built-in ones like DMAC.

3. Backtest on candles

The DMAC strategy can run on either candle data or trade data. Let's start with 1-min candles. Set the time range (top right of the dashboard) to be one year and update the options at the top of the page to use the Coinbase exchange and "candles_1m" data type. This will immediately show the results of the backtest. It should look something like this:

todo: screenshot

This uses the default strategy parameters of 7 day short SMA and 30 day long SMA but those can also be updated. Try to change those parameters and see how that affects the strategy's performance.

4. Backtest on trades

Change the dashboard time range to 3 months and the data type option to "trades".

You'll see that the performance of the strategy is slightly different than running on candles. This happens because when the strategy runs on trades, it is able to react to the market immediately. When running on 1-min candles, we're only able to react to prices once per minute.

5. Custom strategies

In the next section, we'll learn how to create custom strategies and backtest them in a similar way. When you feel comfortable with that, come back to this section and try to create a custom DMAC strategy that uses exponential moving average instead of simple moving average.