Skip to content
All library documents

Dual Moving Average Crossover Strategy with Stop Losses

Code Quant course library

Summary

This strategy uses a fast and a slow moving average to trade long and short. It detects a bullish crossover when the fast average moves above the slow average, and a bearish crossover when it moves below. When a signal opposes an open position, the code closes that position and opens one in the new direction; when flat, it opens the indicated side. Trade size is calculated from stored account capital, the latest price, and the contract value.

Separate stop thresholds close long or short positions when price moves adversely relative to the recorded entry price. Realized profit or loss is added to the capital value saved between runs. The example shows a 15-minute timeframe and includes crossover and stop settings, but offers no backtest or evidence of profitability. Fees, slippage, partial fills, sizing constraints, and exception details are not addressed, so the example does not establish live-trading robustness.

Key ideas

  • A bullish crossover opens a long position or reverses a short position.
  • A bearish crossover opens a short position or reverses a long position.
  • Position size is based on stored capital, current price, and contract value.
  • Separate price thresholds trigger exits for long and short positions.
  • Realized trading results update the capital value persisted between runs.
  • The example provides no performance evidence and omits several execution costs and failure cases.

Tags

From a private course collection; the original is not published.