Skip to main content

Backtesting Trading Strategies in Gunbot

Backtesting is a crucial tool for developing and validating trading strategies. It allows you to see how your spot trading strategies would have performed on historical data before risking any real capital. By running backtests, you can identify strengths and weaknesses in your strategies and make performance-based tweaks.

Info

Backtesting involves simulating trades using historical market data to evaluate a strategy's potential profitability. It helps refine strategies by showing how they might have performed in past market conditions.

Preliminary Considerations​

To avoid any interference with your live trading data and configuration, it's best to set up a dedicated Gunbot instance (a separate folder) specifically for running backtests. This ensures your live trading activities remain unaffected by test data.

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: Always keep your backtesting Gunbot instance distinct from your live trading instance to prevent accidental interference.
  • Clear State Files: Before initiating a new backtest for the same trading pair, ensure you clear any relevant JSON state files (e.g., from the json folder) from previous test runs to start with a clean slate.
  • 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: Because backtesting mode executes all Gunbot code just as it would for live trading (excluding actual order placement), comprehensive backtests can be time-consuming, especially over long historical periods.

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 using the exact same strategy logic and core code that it employs for live trading. This ensures that the backtesting results are as representative of potential real-world performance as possible, with the main differences being the absence of actual balance changes on an exchange and the use of historical data instead of live market data 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 is in progress, you can observe the trading chart and dashboard updating in real-time, much like watching a live Gunbot session on fast-forward. Upon completion, each run generates a JSON report file. These reports are stored in the backtestingReports folder within 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.