
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/example_strategy.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_example_strategy.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_example_strategy.py:


Strategy
========

.. GENERATED FROM PYTHON SOURCE LINES 5-57

.. code-block:: Python


    # Import necessary modules and classes from the TradeTide package
    from TradeTide import BackTester, indicators, get_market_data, Strategy
    from TradeTide import capital_management, risk_management


    # Load historical market data for EUR/USD pair for the year 2023 and limit to 4000 data points
    market_data = get_market_data('eur', 'usd', year=2023, time_span='30day', spread=0)

    # Initialize a Moving Average Crossing indicator with specific window settings and minimum period
    indicator_0 = indicators.BB()
    indicator_1 = indicators.RMI()

    strategy = Strategy(
        indicator_0, indicator_1
    )

    # Generate trading signals based on the market data
    strategy.generate_signal(market_data)

    # Plot the indicator signals overlaid on the market data
    strategy.plot()

    # Create the BackTester instance, linking it with the market data and chosen strategy
    backtester = BackTester(market=market_data, strategy=strategy)

    # Set up loss and profit management with specified stop loss and take profit percentages
    risk = risk_management.DirectLossProfit(
        market=market_data,
        stop_loss='10pip',
        take_profit='10pip',
    )

    # Configure capital management strategy with initial capital, spread, and trading constraints
    capital_management = capital_management.LimitedCapital(
        initial_capital=100_000,
        risk_management=risk,
        max_cap_per_trade=10_000,
        limit_of_positions=1,
        micro_lot=1_000
    )

    # Execute the backtest using the configured capital management strategy
    backtester.backtest(capital_management=capital_management)


    # Visualize the backtest results, showing the strategy's performance against the market price
    backtester.plot(show_price=True)

    # Calculate and display key performance metrics for the trading strategy
    metrics = backtester.metrics


.. GENERATED FROM PYTHON SOURCE LINES 58-59

Retrieve and print the final total value of the portfolio after completing the backtest

.. GENERATED FROM PYTHON SOURCE LINES 59-63

.. code-block:: Python

    metrics.print()


    # -


.. _sphx_glr_download_gallery_example_strategy.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: example_strategy.ipynb <example_strategy.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: example_strategy.py <example_strategy.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: example_strategy.zip <example_strategy.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
