Skip to content
All library documents

Simple Moving Average Crossover with Trend-Based Position Reversals

Article Strategy library · Author: FengkieJ

Summary

This example implements a basic crossover system with a 50-period simple moving average and a 200-period simple moving average. When the faster average is above the slower one, the strategy signals a long position; when it is below, it signals short. Existing positions are liquidated when the relationship reverses, allowing the strategy to switch direction as the trend signal changes.

The sample sizes entries using the available account balance, current price, and fee rate, and submits orders at the current price. It is a compact illustration of trend-following logic rather than a complete trading plan: it specifies no stop loss, profit target, market filter, or performance evidence. The crossover can react slowly to turning markets and may repeatedly change direction in sideways conditions. No asset, timeframe, backtest period, or results are supplied, so the example alone does not support conclusions about profitability or suitability.

Key ideas

  • The strategy compares 50-period and 200-period simple moving averages to define direction.
  • A faster average above the slower one signals long exposure, while the reverse signals short exposure.
  • Open positions are liquidated when the moving-average relationship changes.
  • Entry quantity is based on account balance, price, and fees.
  • The example gives no backtest evidence or explicit loss controls.

Tags

Full text
# SMACrossover


# SMACrossover









Simple Moving Average Crossover Strategy
Author: FengkieJ (fengkiejunis@gmail.com)
Simple moving average crossover strategy is the ''hello world'' of algorithmic trading.
This strategy uses two SMAs to determine '''Golden Cross''' to signal for long position, and '''Death Cross''' to signal for short position.

## Source (MIT)

```python
"""
Simple Moving Average Crossover Strategy
Author: FengkieJ (fengkiejunis@gmail.com)
Simple moving average crossover strategy is the ''hello world'' of algorithmic trading.
This strategy uses two SMAs to determine '''Golden Cross''' to signal for long position, and '''Death Cross''' to signal for short position.
"""

from jesse.strategies import Strategy
import jesse.indicators as ta
from jesse import utils

class SMACrossover(Strategy):
    @property
    def slow_sma(self):
        return ta.sma(self.candles, 200)

    @property
    def fast_sma(self):
        return ta.sma(self.candles, 50)

    def should_long(self) -> bool:
        # Golden Cross (reference: https://www.investopedia.com/terms/g/goldencross.asp)
        # Fast SMA above Slow SMA
        return self.fast_sma > self.slow_sma

    def should_short(self) -> bool:
        # Death Cross (reference: https://www.investopedia.com/terms/d/deathcross.asp)
        # Fast SMA below Slow SMA
        return self.fast_sma < self.slow_sma

    def should_cancel_entry(self) -> bool:
        return False

    def go_long(self):
        # Open long position and use entire balance to buy
        qty = utils.size_to_qty(self.balance, self.price, fee_rate=self.fee_rate)

        self.buy = qty, self.price

    def go_short(self):
        # Open short position and use entire balance to sell
        qty = utils.size_to_qty(self.balance, self.price, fee_rate=self.fee_rate)

        self.sell = qty, self.price

    def update_position(self):
        # If there exist long position, but the signal shows Death Cross, then close the position, and vice versa.
        if self.is_long and self.fast_sma < self.slow_sma:
            self.liquidate()
    
        if self.is_short and self.fast_sma > self.slow_sma:
            self.liquidate()

```

Shown in full with attribution under the source's licence. Licence: MIT

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.