.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/backtester.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_backtester.py: Automated Backtesting with Bollinger Bands Strategy ==================================================== This example demonstrates how to use TradeTide's Backtester class for automated strategy evaluation. The Backtester streamlines the entire workflow by handling position management, portfolio simulation, and performance analysis automatically. We'll test a Bollinger Bands strategy on 100 days of CAD/USD data to evaluate its historical performance and risk characteristics. .. GENERATED FROM PYTHON SOURCE LINES 14-16 Import Libraries and Setup -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 16-26 .. code-block:: Python import matplotlib.pyplot as plt from TradeTide import Backtester, Strategy, Market, Currency, days, hours, minutes from TradeTide.indicators import BollingerBands from TradeTide import capital_management, exit_strategy # Configure plotting plt.style.use("seaborn-v0_8-darkgrid") plt.rcParams["figure.figsize"] = (12, 6) .. GENERATED FROM PYTHON SOURCE LINES 27-30 Load Historical Market Data --------------------------- Load 100 days of CAD/USD forex data for comprehensive backtesting. .. GENERATED FROM PYTHON SOURCE LINES 30-42 .. code-block:: Python market = Market() market.load_from_database( currency_0=Currency.CAD, currency_1=Currency.USD, time_span=2 * days, ) # market.plot() print(f"Loaded {len(market.dates)} data points over {100} days") .. rst-class:: sphx-glr-script-out .. code-block:: none Loaded 2857 data points over 100 days .. GENERATED FROM PYTHON SOURCE LINES 43-47 Configure Trading Strategy -------------------------- Set up a Bollinger Bands strategy with 2.0 standard deviation bands for more conservative signal generation. .. GENERATED FROM PYTHON SOURCE LINES 47-55 .. code-block:: Python indicator = BollingerBands(window=3 * minutes, multiplier=2.0) indicator.run(market) strategy = Strategy() strategy.add_indicator(indicator) .. GENERATED FROM PYTHON SOURCE LINES 56-59 Define Risk Management ---------------------- Configure exit strategy and capital management parameters. .. GENERATED FROM PYTHON SOURCE LINES 59-69 .. code-block:: Python exit_strat = exit_strategy.Static(stop_loss=4, take_profit=4, save_price_data=True) capital_mgmt = capital_management.FixedLot( capital=1_000, fixed_lot_size=10, max_capital_at_risk=100_000, max_concurrent_positions=10, ) .. GENERATED FROM PYTHON SOURCE LINES 70-74 Run Backtest ------------ The Backtester automatically handles the complete workflow: strategy execution, position management, and performance calculation. .. GENERATED FROM PYTHON SOURCE LINES 74-88 .. code-block:: Python backtester = Backtester( strategy=strategy, exit_strategy=exit_strat, market=market, capital_management=capital_mgmt, ) backtester.run() # backtester.plot_summary() # print(backtester.portfolio) .. GENERATED FROM PYTHON SOURCE LINES 89-92 Display Results --------------- View comprehensive performance metrics and analysis. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python backtester.print_performance() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.024 seconds) .. _sphx_glr_download_gallery_backtester.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: backtester.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: backtester.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: backtester.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_