Example Strategies ================== These four walkthroughs show how to build complete, working strategies in Quantlens from scratch. Each one introduces a different trading style — read them in order or jump to whichever interests you. .. tip:: You can clone any of these strategies from the **Strategy Gallery** and run them immediately without building anything. ---- .. contents:: On this page :local: :depth: 1 ---- Strategy 1 — RSI Mean Reversion --------------------------------- **Style:** Mean reversion **Universe:** NASDAQ stocks **Timeframe:** Daily rebalance **Idea:** Buy stocks that have been oversold (RSI < 30) and sell when they recover (RSI > 70). Mean reversion assumes that extreme moves tend to snap back toward average. RSI below 30 means a stock has fallen sharply and *may* be due for a bounce. Step 1 — Config ~~~~~~~~~~~~~~~~ Place a **Config** block and set: .. list-table:: :header-rows: 1 :widths: 35 65 * - Setting - Value * - Start Date - ``2010-01-01`` * - End Date - ``2022-12-31`` * - Starting Cash - ``100,000`` * - Slots - ``10`` * - Stop Loss - ``0.07`` * - Take Profit - ``0.15`` * - Primary Interval - ``1d`` * - Secondary Interval - ``1d`` Step 2 — Block layout ~~~~~~~~~~~~~~~~~~~~~~ Inside **Trading Loop**, add a **On Interval → Primary** block. Inside that, connect: .. code-block:: text [IF Block] Condition: RSI(14) < Threshold(30) Do: [Rebalance: Enter] Selected: (connected symbol list from Momentum block) Then add a **On Interval → Secondary** block with: .. code-block:: text [Check Stop Loss (eod)] [Check Take Profit (eod)] Step 3 — Full block diagram ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: text ┌─ Config ──────────────────────────────────────────┐ │ Date: 2010–2022 | Slots: 10 | SL: 7% | TP: 15% │ └───────────────────────────────────────────────────┘ ┌─ Trading Loop ────────────────────────────┐ │ │ │ ┌─ Primary Interval (1d) ─────────────────────┐ │ │ │ IF RSI(14) < 30 │ │ │ │ → Rebalance: Enter │ │ │ │ IF RSI(14) > 70 │ │ │ │ → Rebalance: Exit │ │ │ └─────────────────────────────────────────────┘ │ │ │ │ ┌─ Secondary Interval (1d) ───────────────────┐ │ │ │ Check Stop Loss (eod) │ │ │ │ Check Take Profit (eod) │ │ │ └─────────────────────────────────────────────┘ │ └───────────────────────────────────────────────────┘ Step 4 — What to expect ~~~~~~~~~~~~~~~~~~~~~~~~ - **Win Rate:** Typically 45–55% — many small wins and losses. - **Best in:** Range-bound or mildly volatile markets. - **Weakest in:** Strong trending markets (RSI stays above 70 for months without reverting). - **Max Drawdown:** Watch for -20% to -35% in bear markets. .. warning:: Pure RSI mean-reversion without a market filter tends to perform poorly during prolonged downtrends. Consider adding ``Index Value > SMA(200)`` as a condition to avoid buying in bear markets. ---- Strategy 2 — MACD Trend Following ------------------------------------ **Style:** Trend following **Universe:** NASDAQ stocks **Timeframe:** Weekly rebalance **Idea:** Enter when MACD crosses above its Signal line; exit on the reverse cross. MACD captures momentum shifts. A bullish crossover means short-term momentum is accelerating above the longer trend. Step 1 — Config ~~~~~~~~~~~~~~~~ .. list-table:: :header-rows: 1 :widths: 35 65 * - Setting - Value * - Slots - ``15`` * - Stop Loss - ``0.10`` * - Primary Interval - ``1w`` * - Secondary Interval - ``1d`` Step 2 — Core condition ~~~~~~~~~~~~~~~~~~~~~~~~ The MACD block outputs both a **MACD line** and a **Signal line**. To detect a bullish crossover: .. code-block:: text Condition: (MACD(12,26,9) line > Threshold(0)) AND (MACD(12,26,9) histogram > Threshold(0)) This captures the state *after* a crossover has occurred — MACD line positive, histogram positive. Step 3 — Block diagram ~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: text ┌─ Trading Loop ────────────────────────────┐ │ │ │ ┌─ Primary Interval (1w) ─────────────────────┐ │ │ │ IF MACD > 0 AND Histogram > 0 │ │ │ │ → Rank by Momentum(189) → Enter (Top 15) │ │ │ │ IF MACD < 0 │ │ │ │ → Rebalance: Exit │ │ │ └─────────────────────────────────────────────┘ │ │ │ │ ┌─ Secondary Interval (1d) ───────────────────┐ │ │ │ Check Stop Loss (eod) │ │ │ └─────────────────────────────────────────────┘ │ └───────────────────────────────────────────────────┘ Step 4 — What to expect ~~~~~~~~~~~~~~~~~~~~~~~~ - **Win Rate:** Lower (~40%) but average winning trade is larger than average losing trade. - **Profit Factor:** Aim for > 1.5. - **Best in:** Trending bull markets — MACD stays positive for long periods. - **Weakest in:** Choppy, sideways markets — frequent false crossovers. ---- Strategy 3 — SMA Crossover (Classic) -------------------------------------- **Style:** Trend following (moving average) **Universe:** Single asset (e.g. BTC/USD or SPY) **Timeframe:** Daily **Idea:** Buy when the 50-day SMA crosses above the 200-day SMA ("Golden Cross"); sell on the reverse ("Death Cross"). This is one of the oldest and most widely followed technical setups. Step 1 — Config ~~~~~~~~~~~~~~~~ .. list-table:: :header-rows: 1 :widths: 35 65 * - Setting - Value * - Slots - ``1`` * - Stop Loss - ``0.15`` * - Primary Interval - ``1d`` Step 2 — Block diagram ~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: text ┌─ Trading Loop ────────────────────────────┐ │ │ │ ┌─ Primary Interval (1d) ─────────────────────┐ │ │ │ │ │ │ │ IF SMA(50) > SMA(200) │ │ │ │ → Rebalance: Enter │ │ │ │ │ │ │ │ IF SMA(50) < SMA(200) │ │ │ │ → Rebalance: Exit │ │ │ │ │ │ │ └─────────────────────────────────────────────┘ │ └───────────────────────────────────────────────────┘ Step 3 — What to expect ~~~~~~~~~~~~~~~~~~~~~~~~ - **Number of trades:** Very few — crossovers happen infrequently (once every few months). - **Best in:** Long bull markets with clear trends (e.g. S&P 500 2010–2021). - **Weakest in:** Sideways or choppy markets — the two SMAs intertwine and generate many false signals. - **Lag:** The 200-day SMA is very slow; the strategy may enter late and exit late. .. tip:: Try optimising the periods: ``50, 100, 150`` for fast and ``150, 200, 250`` for slow. Use the Optimizer to find the best pair for your chosen market and period. ---- Strategy 4 — Momentum Portfolio --------------------------------- **Style:** Momentum / systematic rotation **Universe:** NASDAQ stocks **Timeframe:** Bi-weekly rebalance **Idea:** Every two weeks, rank all stocks by their 189-day momentum (rate of change) and hold the top performers. Exit positions that drop out of the top. Add a market filter to avoid holding during bear markets. This is the most realistic of the four strategies and is closest to professional quantitative investing. Step 1 — Config ~~~~~~~~~~~~~~~~ .. list-table:: :header-rows: 1 :widths: 35 65 * - Setting - Value * - Slots - ``10`` * - Stop Loss - ``0.05`` * - Primary Interval - ``2w`` * - Secondary Interval - ``1d`` Step 2 — Market filter ~~~~~~~~~~~~~~~~~~~~~~~ Before entering positions, check whether the broad market (NASDAQ index) is in an uptrend. This avoids buying stocks during a market-wide crash. .. code-block:: text Condition: Index Value > SMA(200) Only rebalance into new positions when this is true. Exit all positions when it turns false. Step 3 — Full block diagram ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: text ┌─ Trading Loop ────────────────────────────┐ │ │ │ ┌─ Primary Interval (2w) ─────────────────────┐ │ │ │ │ │ │ │ IF Index Value > SMA(200) │ │ │ │ → Rank by Momentum(189) │ │ │ │ → Cooldown Filter(20d) │ │ │ │ → Rebalance: Enter │ │ │ │ → Rebalance: Exit (dropped) │ │ │ │ │ │ │ │ IF Index Value < SMA(200) │ │ │ │ → Rebalance: Exit (all) │ │ │ │ │ │ │ └─────────────────────────────────────────────┘ │ │ │ │ ┌─ Secondary Interval (1d) ───────────────────┐ │ │ │ Check Stop Loss (eod) │ │ │ └─────────────────────────────────────────────┘ │ └───────────────────────────────────────────────────┘ Step 4 — Why the Cooldown Filter? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Without it, a stock that was stopped out this week could immediately re-enter the portfolio on the next rebalance. The 20-day cooldown prevents chasing a damaged stock while it's still in a downtrend. Step 5 — What to expect ~~~~~~~~~~~~~~~~~~~~~~~~ .. list-table:: :header-rows: 1 :widths: 35 65 * - Metric - Typical range * - Annualised Return - 15–25% (varies by period) * - Sharpe Ratio - 0.8 – 1.5 * - Max Drawdown - -20% to -40% * - Number of Trades - 200 – 600 over 10 years .. warning:: These numbers are from historical backtesting — past performance does not guarantee future results. Always validate on an out-of-sample period before trading live. ---- Next steps ---------- - Read :doc:`understanding_results` to interpret Sharpe Ratio, Max Drawdown, and Profit Factor. - Read :doc:`blocks_indicators` for full parameter details on RSI, MACD, SMA, and Momentum. - Use the **Optimizer** (Silver/Gold) to tune parameters — see :doc:`running_backtest`.