.. _MACD_example: Strategy Example ================ This example shows how to write the simple Moving Average Convergence Divergence (MACD) strategy, which is subsequently used in the other examples. The premise is to use two moving averages of the closing price: one averging across a shorter window of the most recent data, thus more reactive to price changes, dubbed the 'fast' signal; the other averaging across a larger window, the 'slow' signal. Bullish behavior is indicated when the fast signal crosses above the slow signal, so the strategy places a buy orders for thes events, and for the bearish cross under event sell orders. .. image:: images/MACD_explanation.png :align: center .. automodule:: src.boatwright :ref:`boatwright.Strategy ` are defined by a trading symbol e.g. "AAPL" or "BTC", a :ref:`boatwright.Broker ` for handling orders, and three required methods which define the trading decision logic and order managment: - :meth:`calculate_signals` for computing technical indicators and other signals - :meth:`step` for completing any actions e.g. check indicators for buy/sell triggers, create and place orders with the broker - :meth:`calculate_prerequisite_data_length` which communicates to :class:`backtest` and :class:`live execution` the minimum amount of data necessary for all technical indicators in the strategy to produce values e.g. a simple moving average with period=10, needs 10 bars of data before the signal can generate. **Python source code:** :download:`../../examples/MACD.py` .. literalinclude:: ../../examples/MACD.py