# StratForge Strategy Examples Catalog

> Production-ready C++23 strategy examples demonstrating the `nonabt::` API patterns and performance.

StratForge strategies are implemented as C++ classes inheriting from `nonabt::Strategy`. They provide high-performance execution with sub-microsecond decision loops, ideal for high-frequency or large-scale backtesting.

---

## Core Strategy Examples

### 1. Bollinger Bands Breakout
**File**: `bollinger_bands.cpp`
**Logic**: Buy on lower band touch (oversold), sell on upper band touch (overbought). Includes 5% stop-loss management.

**Code Pattern**:
```cpp
class BollingerBandsStrategy : public nonabt::Strategy {
public:
    void init() override {
        bbands_ = std::make_unique<nonabt::BollingerBands>(data().close(), 20, 2.0);
    }

    void next() override {
        const double price = data().close()[0];
        const double upper = bbands_->top()[0];
        const double lower = bbands_->bottom()[0];

        if (!position().size && price <= lower) {
            buy(100.0);
            stop_order_id_ = sell(100.0, price * 0.95, nonabt::OrderType::Stop);
        } else if (position().size > 0 && price >= upper) {
            cancel(stop_order_id_);
            close();
        }
    }
private:
    std::unique_ptr<nonabt::BollingerBands> bbands_;
    std::size_t stop_order_id_ = 0;
};
```

---

### 2. MACD Trend Following
**File**: `macd_trend.cpp`
**Logic**: Classic momentum strategy using MACD/Signal crossovers.

**Code Pattern**:
```cpp
void next() override {
    const double macd_line = macd_->macd()[0];
    const double signal_line = macd_->signal()[0];

    if (!position().size && macd_line > signal_line) {
        buy(50.0);
    } else if (position().size > 0 && macd_line < signal_line) {
        close();
    }
}
```

---

### 3. RSI Mean Reversion
**File**: `rsi_mean_reversion.cpp`
**Logic**: Buy when RSI < 30 (oversold), sell when RSI > 70 (overbought). Demonstrates parameter mapping and custom sizers.

**Code Pattern**:
```cpp
void init() override {
    rsi_ = std::make_unique<nonabt::RSI>(data().close(), 14);
    setsizer(std::make_unique<nonabt::PercentSizer>(95.0));
}
```

---

### 4. SMA Crossover
**File**: `sma_crossover.cpp`
**Logic**: Golden Cross / Death Cross pattern. Demonstrates basic `next()` signal logic.

**Code Pattern**:
```cpp
void next() override {
    const double fast = sma_fast_->line()[0];
    const double slow = sma_slow_->line()[0];

    if (!position().size && fast > slow) {
        buy(100.0);
    } else if (position().size > 0 && fast < slow) {
        close();
    }
}
```

---

### 5. Multi-Timeframe Analysis
**File**: `multi_timeframe.cpp`
**Logic**: Trades on Daily bars while using a Weekly EMA trend filter.

**Code Pattern**:
```cpp
void next() override {
    const double daily_price = data(0).close()[0];
    const double weekly_ema = weekly_ema_->line()[0];

    if (!position().size && daily_price > weekly_ema) {
        buy(50.0);
    }
}
```

---

### 6. Pairs Trading (Statistical Arbitrage)
**File**: `pairs_trading.cpp`
**Logic**: Market-neutral strategy trading the Z-score of the spread between two correlated assets.

**Code Pattern**:
```cpp
void next() override {
    const double spread = data(0).close()[0] - data(1).close()[0];
    // ... calculate z_score ...
    if (z_score > 2.0) {
        sell(10.0, 0, nonabt::OrderType::Market, 0); // Short asset A
        buy(10.0, 0, nonabt::OrderType::Market, 1);  // Long asset B
    }
}
```

---

## LLM-Generated Strategy Examples

These strategies were generated by the StratForge LLM pipeline (using Claude 4.5 Sonnet) to demonstrate complex order management and risk control.

### 7. MACD with Trailing Stop
**File**: `llm_generated/macd_trailing_stop.cpp`
**Logic**: Bullish MACD crossover entry with a 1.5% trailing stop-loss that adjusts upward with price.

**Code Pattern**:
```cpp
void next() override {
    if (position().size > 0) {
        highest_since_entry_ = std::max(highest_since_entry_, current_price);
        const double trailing_stop = highest_since_entry_ * 0.985;
        if (current_price <= trailing_stop) {
            close();
        }
    }
}
```

---

### 8. RSI with Stop-Loss and Take-Profit
**File**: `llm_generated/rsi_with_stop.cpp`
**Logic**: Mean-reversion entry (RSI < 30) with bracket orders: 3% stop-loss and 5% take-profit.

**Code Pattern**:
```cpp
if (!position().size && rsi_value < 30.0) {
    buy(100.0);
    // Place bracket orders
    stop_id = sell(100.0, price * 0.97, OrderType::Stop);
    limit_id = sell(100.0, price * 1.05, OrderType::Limit);
}
```

---

### 9. SMA Crossover with Stop-Loss
**File**: `llm_generated/sma_crossover_with_stop.cpp`
**Logic**: Classic SMA(10/30) crossover with a fixed 2% stop-loss protection on entry.

**Code Pattern**:
```cpp
if (!position().size && fast > slow) {
    buy(50.0);
    sell(50.0, entry_price * 0.98, OrderType::Stop);
}
```

---

## Performance Comparison: C++ vs Python

| Strategy | C++23 (StratForge) | Python (backtrader) | Speedup |
|----------|------------------------|---------------------|---------|
| SMA Crossover | 45ms | 4,200ms | 93x |
| Bollinger Bands | 58ms | 6,100ms | 105x |
| Pairs Trading | 110ms | 15,400ms | 140x |

*Benchmark: 1,000,000 bars on a single thread (Ryzen 9 5950X).*

---
[Back to Index](./index.md)
