Skip to content

Strategy Gallery

Five complete, runnable strategies demonstrating the Meridian SDK. Each is a standalone Python script — no infrastructure needed.

Quick Start

pip install open-meridian
git clone https://github.com/Societal-Lab-Inc/meridian-python.git
cd meridian-python/examples/strategies
python momentum/strategy.py

Strategies

1. Momentum (Beginner)

Buy winners, avoid losers. Compute the N-day return for each symbol. Buy when return exceeds a threshold, sell when it drops below.

# Core logic (simplified)
ret = (bars[-1].close - bars[0].close) / bars[0].close
if ret > 0.05:
    self.submit_order(symbol, "BUY", 50)

File: momentum/strategy.py What you'll learn: N-day returns, position tracking, buy/sell signals


2. Mean Reversion (Beginner)

Fade extremes, bet on normalization. Compute a rolling z-score. Buy when price is unusually low, sell when unusually high, close when it returns to the mean.

# Core logic (simplified)
z_score = (close - mean) / std
if z_score < -2.0:
    self.submit_order(symbol, "BUY", 50)
elif z_score > 2.0:
    self.submit_order(symbol, "SELL", 50)

File: mean-reversion/strategy.py What you'll learn: Z-scores, shorting, mean-reversion exits


3. Pairs Trading (Intermediate)

Trade the spread between two correlated stocks. Track the AAPL/MSFT price ratio. When it deviates from its mean, go long the cheap one and short the expensive one. Close when the ratio reverts.

# Core logic (simplified)
ratio = aapl_close / msft_close
z_score = (ratio - mean_ratio) / std_ratio
if z_score > 2.0:   # AAPL expensive vs MSFT
    sell AAPL, buy MSFT
elif z_score < -2.0: # AAPL cheap vs MSFT
    buy AAPL, sell MSFT

File: pairs-trading/strategy.py What you'll learn: Price ratios, market-neutral hedging, spread trading


4. Moving Average Crossover (Intermediate)

The classic trend-following strategy. Compute fast (10-day) and slow (50-day) moving averages. Buy on golden cross (fast crosses above slow), sell on death cross.

# Core logic (simplified)
fast_ma = mean(closes[-10:])
slow_ma = mean(closes[-50:])
if fast_ma > slow_ma and prev_fast <= prev_slow:  # golden cross
    self.submit_order(symbol, "BUY", 100)

File: ma-crossover/strategy.py What you'll learn: Moving averages, trend detection, crossover signals


5. Risk Parity Rebalancer (Intermediate)

Equal risk contribution across asset classes. Allocate capital inversely proportional to each asset's volatility. Lower-vol assets (bonds) get larger weights. Rebalance every 20 days.

# Core logic (simplified)
weights = {s: (1/vol[s]) / sum(1/vol[j] for j in symbols) for s in symbols}
# SPY (higher vol) → smaller weight, TLT (lower vol) → larger weight

File: risk-parity/strategy.py What you'll learn: Portfolio weights, volatility targeting, periodic rebalancing


Sample Data

All strategies use CSV files in examples/strategies/data/:

File Description
aapl_2023.csv Apple-like equity, moderate uptrend (252 trading days)
msft_2023.csv Microsoft-like equity, similar trend
spy_2023.csv S&P 500 ETF proxy, broad market
tlt_2023.csv Treasury bond ETF proxy, different risk profile

How Every Strategy Works

from meridian import LocalClient, SignalEngine

client = LocalClient(starting_cash=500_000)
client.load_bars("AAPL", "data/aapl_2023.csv")

class MyStrategy(SignalEngine):
    def on_data(self, window):
        # Your logic here — called once per bar date
        bars = window.bars("AAPL", lookback=20)
        if some_condition(bars):
            self.submit_order("AAPL", "BUY", 100)

MyStrategy(client).run(warmup=20)

print(client.positions())   # What you hold
print(client.cash())         # Cash remaining
print(client.fills())        # Trade history
print(client.journal())      # Double-entry accounting

Next Steps

  • Modify a strategy's parameters and re-run
  • Combine data from multiple symbols
  • Add your own CSV data
  • Build your first plugin with the scaffold CLI

Want to build something original?

These examples are starting points. Modify parameters, combine strategies, or use your own data.