.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/quickstart_backtest.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_quickstart_backtest.py: Your First FX Backtest ====================== This tutorial runs a complete, reproducible FX backtest with TradeTide. It uses the bundled EUR/USD data, so no data-provider account or download is required. The strategy is deliberately small: Bollinger Bands identify potential mean-reversion entries, while fixed take-profit and stop-loss levels make the risk rules explicit. The result is a compact four-panel report showing price and signals, equity, open positions, and drawdown. It is an example workflow—not investment advice or a claim about live-trading performance. .. GENERATED FROM PYTHON SOURCE LINES 17-22 Imports ------- The public API keeps the essential pieces together: market data, an indicator-backed strategy, exit rules, capital management, and a backtester. .. GENERATED FROM PYTHON SOURCE LINES 22-29 .. code-block:: Python from TradeTide import Backtester, BacktestResult, Currency, Market, Strategy from TradeTide import capital_management, exit_strategy from TradeTide import BollingerBands from TradeTide.times import days, minutes .. GENERATED FROM PYTHON SOURCE LINES 30-36 Load bundled market data ------------------------ ``time_span`` can be a ``timedelta`` (as used here) or a compact duration string such as ``"3d 6h"``. TradeTide ships samples for EUR/USD, GBP/USD, CHF/USD, JPY/USD, and CAD/USD. .. GENERATED FROM PYTHON SOURCE LINES 36-45 .. code-block:: Python market = Market() market.load_from_database( currency_0=Currency.EUR, currency_1=Currency.USD, time_span=5 * days, ) .. GENERATED FROM PYTHON SOURCE LINES 46-52 Define the signal rule ---------------------- Bollinger Bands compare price with a rolling average and its volatility band. Adding the indicator to a strategy lets the backtester derive trade signals from it. .. GENERATED FROM PYTHON SOURCE LINES 52-57 .. code-block:: Python strategy = Strategy() strategy.add_indicator(BollingerBands(window=30 * minutes, multiplier=2.0)) .. GENERATED FROM PYTHON SOURCE LINES 58-64 Set risk and sizing rules ------------------------- Keep the example's assumptions visible. ``Static`` exits each position at a four-pip stop loss or take profit; ``FixedLot`` caps both individual size and simultaneous exposure. .. GENERATED FROM PYTHON SOURCE LINES 64-74 .. code-block:: Python risk_rules = exit_strategy.Static(stop_loss=4, take_profit=4) position_sizing = capital_management.FixedLot( capital=100_000, fixed_lot_size=10_000, max_capital_at_risk=10_000, max_concurrent_positions=1, ) .. GENERATED FROM PYTHON SOURCE LINES 75-81 Run and inspect the backtest ---------------------------- ``run`` performs signal evaluation, position handling, and portfolio simulation. The chart is a useful first sanity check; evaluate strategies on appropriate out-of-sample data before making any trading decision. .. GENERATED FROM PYTHON SOURCE LINES 81-91 .. code-block:: Python backtester = Backtester( strategy=strategy, market=market, exit_strategy=risk_rules, capital_management=position_sizing, ) backtester.run() result = BacktestResult.from_portfolio(backtester.portfolio) result.plot_equity_drawdown() .. image-sg:: /gallery/images/sphx_glr_quickstart_backtest_001.png :alt: Equity candles, Maximum drawdown: 0.05% :srcset: /gallery/images/sphx_glr_quickstart_backtest_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.639 seconds) .. _sphx_glr_download_gallery_quickstart_backtest.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: quickstart_backtest.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: quickstart_backtest.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: quickstart_backtest.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_