Skip to main content

Backtesting Trading Strategies in Gunbot

Backtesting lets you run your spot strategy on historical data before risking real capital. Use it to compare settings, find weaknesses, and decide what to adjust.

Info

Backtesting simulates trades with historical market data so you can evaluate a strategy and refine its settings.

Preliminary Considerations

To avoid interfering with live trading data and configuration, use a dedicated Gunbot instance (a separate folder) for backtests.

Steps to Create a Second Gunbot Folder:

  1. Copy your entire Gunbot folder to a new directory (e.g., gunbot_backtesting).
  2. Open the config.js file within this new folder using a text editor.
  3. In the gui section, change the PORT number to avoid conflicts with your live instance:
    "gui": {
    "enabled": true,
    "host": "localhost",
    "PORT": 5000, // Default is 5000, change to something like 5050
    "clientport": 3000 // If your primary uses 3000, change this too e.g. 3030
    }
    Ensure the new port is not already in use.

Tips for Effective Backtesting

  • Test One Pair at a Time: Running multiple pairs simultaneously in a single backtesting instance is not supported. If you need to test several pairs in parallel, use separate Gunbot instances for each.
  • Isolate Instances: Keep the backtesting instance distinct from live trading to prevent accidental interference.
  • Clear State Files: Before a new backtest on the same pair, clear relevant JSON state files (for example, in the json folder) to start clean.
  • Custom Strategies: If your custom strategy requires data from multiple timeframes (candles), enable the MULTI_COMP override in your strategy settings for accurate backtesting.
  • AutoConfig Jobs: AutoConfig jobs that rely on internal data, such as manageOverrides jobs, are fully supported in backtesting. Gunbot creates a 'mock' schedule based on your job settings before the backtest begins, ensuring jobs execute relative to candles as they would in a live scenario.
  • Allow Sufficient Time: Backtests run the same strategy code as live trading (without order placement), so long histories can take time.

Setting Up Backtesting in Gunbot

Enable the Backfester Feature

  1. Navigate to http://localhost:YOUR_PORT/setup#advanced in your web browser (replace YOUR_PORT with the port number configured for your backtesting instance, e.g., 5050). Enable the "Backfester" feature. Gunbot interface showing the Backfester feature activation switch in the advanced setup section.(Note: "Backfester" appears to be the term used in the Gunbot interface for its backtesting engine.)

Configure Starting Balances

  1. In the same setup dialog, configure the initial balances for your backtests. Select the exchange, specify the base currency, and enter the starting balance for that currency. Gunbot interface for configuring starting balances for backtesting, including exchange and base currency selection.

Steps to Run Backtests

Select a Trading Pair

  1. On the trading settings page of your backtesting instance, select the spot trading pair you wish to test.

Configure Start and End Times

  1. Set the desired start and end timestamps for the historical data range your backtest will cover. Gunbot interface for setting the start and end dates/times for a backtesting session.

Methodology

Gunbot executes backtests with the same strategy logic and core code as live trading. The main differences are no exchange balance changes and the use of historical data instead of live feeds.

Without Deep Testing

  • Gunbot fetches historical candle data for the selected trading pair and timespan.
  • The strategy logic iterates through each candle of the chosen timeframe, performing one evaluation per candle.

With Deep Testing

  • Gunbot collects the public trading history (tick data) for the selected pair and timespan.
  • It then reconstructs candles from this detailed trade history. This allows the strategy code to run multiple times within a single candle's duration if several trades occurred that led to price changes within that candle, offering a more granular simulation.

Viewing and Interpreting Results

While a backtest runs, the chart and dashboard update in real time. When it completes, each run writes a JSON report in the backtestingReports folder inside your Gunbot directory.

These JSON files are valuable for detailed analysis and can be shared with the Gunbot community, for example, on Gunbot's Backtesting Archive.