# Sample Trading Strategies

The strategies below illustrate common trading patterns. Each example highlights a specific idea or combination of indicators and is intended to help you understand how strategies are structured and composed.

Some of the sample strategies below can be replicated seen and replicated in the default strategies section and feature some of the most popular trading strategies, including:&#x20;

1. Spot grid trading bot
2. DCA (Dollar Cost Average)
3. Spot Martingale
4. Rebalancing BOT
5. TWAP
6. Infinity Grid
7. Looping
8. Trend follow

### memejob LUA strategies, intro:

All strategies:

* Are stateless
* Script runs **once per bar**
* Use only supported syntax and indicators
* Express intent exclusively via `return` values

**Return values:**

```
return 1   -- Buy / Long / Add (DCA)
return -1  -- Exit or Short (engine-defined)
return 0   -- Hold
```

Each example focuses on a specific concept such as trend detection, momentum confirmation, mean reversion, or volume filtering. You are encouraged to adapt parameters, combine patterns, and test variations rather than using examples verbatim.

### 1. SMA Trend Follower

Uses a Simple Moving Average as a trend baseline. Follows the dominant direction and exits on trend loss.

```lua
local s20 = sma(20)

if close > s20 then
    return 1
elseif close < s20 then
    return -1
end

return 0
```

**Visual intuition:**

```
Price  ──────╮  Buy
SMA    ──────┼──────
Price  ──────╯  Exit
```

### 2. EMA Crossover

Classic fast/slow EMA crossover strategy. Enters on bullish momentum, exits on bearish crossover.

```lua
local e6  = ema(6)
local e21 = ema(21)

if e6 > e21 then
    return 1
elseif e6 < e21 then
    return -1
end

return 0
```

### 3. Triple EMA Trend Stack

Strong trend confirmation using three EMAs aligned in the same direction.

```lua
local e3  = ema(3)
local e6  = ema(6)
local e21 = ema(21)

if e3 > e6 and e6 > e21 then
    return 1
elseif e3 < e6 then
    return -1
end

return 0
```

**Visual:**

```
EMA3   ╭────
EMA6   ├────
EMA21  └────
```

### 4. Pullback to EMA (DCA-Friendly)

Buys pullbacks during an existing uptrend. Suitable for DCA-style accumulation.

```lua
local e6  = ema(6)
local e21 = ema(21)

if close > e21 and close < e6 then
    return 1
elseif close < e21 then
    return -1
end

return 0
```

### 5. EMA Momentum Runner (DCA friendly)

Adds exposure while price remains above a short-term EMA. Exits on momentum loss.

```lua
local e9 = ema(9)

if close > e9 then
    return 1
elseif close < e9 then
    return -1
end

return 0
```

**Visual:**

```
Price ↑  → Add
Price ↓  → Exit
```

### 6. Fast EMA Momentum Proxy

Momentum proxy using fast EMA positioning instead of bar-to-bar price changes (price history not supported).

```lua
local e3 = ema(3)
local e6 = ema(6)

if e3 > e6 then
return 1
elseif e3 < e6 then
return -1
end
```

### 7. SMA Direction Proxy

Approximates SMA slope using price position relative to SMA.

```lua
local s20 = sma(20)

if close > s20 then
return 1
elseif close < s20 then
return -1
end

return 0
```

### 8. EMA Trend Confirmation

Requires both price and EMA to confirm trend direction.

```lua
if close[0] > ema(21) and close[1] > ema(21)[1] then
    return 1
elseif close[0] < ema(21) then
    return -1
end

return 0
```

### 9. Typical Price Trend

Uses the HLC3 (typical price) instead of close to reduce noise.

```lua
local e14 = ema(14)

if hlc3 > e14 then
    return 1
elseif hlc3 < e14 then
    return -1
end

return 0
```

### 10. WMA Trend Follower

Trend-following strategy using a Weighted Moving Average for faster reaction.

```lua
local w20 = wma(20)

if close > w20 then
    return 1
elseif close < w20 then
    return -1
end

return 0
```

### 11. EMA Compression Breakout

Enters when multiple EMAs align, indicating trend expansion.

```lua
local e6  = ema(6)
local e9  = ema(9)
local e21 = ema(21)

if e6 > e9 and e9 > e21 then
    return 1
elseif e6 < e9 then
    return -1
end

return 0
```

### 12. Range Breakout

Trades breakouts beyond the previous bar’s range.

```lua
if close > high[1] then
    return 1
elseif close < low[1] then
    return -1
end

return 0
```

### 13. SMA Mean Reversion (Simple)

Buys below the SMA and exits above it. Simple counter-trend strategy.

```lua
local s50 = sma(50)

if close < s50 then
    return 1
elseif close > s50 then
    return -1
end

return 0
```

### 14. EMA Acceleration

Trades based on the acceleration or deceleration of EMA movement.

```lua
local e6 = ema(6)

if e6 > e6[1] then
    return 1
elseif e6 < e6[1] then
    return -1
end

return 0
```

### 15. Dual-Timeframe Proxy (Fast vs Slow SMA)

Simulates multi-timeframe logic using fast vs slow SMAs.

```lua
local s10 = sma(10)
local s30 = sma(30)

if s10 > s30 then
    return 1
elseif s10 < s30 then
    return -1
end

return 0
```

#### **16. The "Golden Cross" & "Death Cross"**

The "Hello World" of trading bots & classic strategy in trend following.

It captures massive moves by entering when short-term momentum overtakes long-term trends.

* **Logic:**&#x20;
  * Buy when the fast MA (e.g., 50) crosses *above* the slow MA (e.g., 200).&#x20;
  * Sell when it crosses *below*.

```lua
-- Current SMAs
local fast = sma(50, "1d")
local slow = sma(200, "1d")
-- Previous-bar SMAs (window shifted by 1 day)
local fast_prev = sma(51, "1d")
local slow_prev = sma(201, "1d")

-- Golden Cross (Bullish)
if fast_prev <= slow_prev and fast > slow then
    return 1

-- Death Cross (Exit / Bearish)
elseif fast_prev >= slow_prev and fast < slow then
    return -1
end
```

#### **17. The "MACD Zero-Cross" Momentum**

A fast momentum strategy using the MACD line relative to zero.

* Buy when MACD turns positive (bullish momentum)
* Exit when MACD turns negative (bearish momentum)

Detect zero-cross via comparing current vs slightly longer window MACD values

```lua
-- Current MACD
local macd_val = macd(12, 26, "1d")       -- 12/26 daily MACD
-- "Previous" MACD (slightly shifted fast period)
local macd_prev = macd(13, 27, "1d")      -- Shifted by +1 period to approximate prior value

-- MACD Zero-Cross: Bullish
if macd_prev <= 0 and macd_val > 0 then
    return 1
-- MACD Zero-Cross: Bearish / Exit
elseif macd_prev >= 0 and macd_val < 0 then
    return -1
end
```

#### **18. Spot Martingale**

Buys when the price dips below a reference (e.g., EMA) and Continues signaling buys while the dip persists → mimics averaging down. Exits when price recovers above the reference

Logic: Detect zero-cross via comparing current vs slightly longer window MACD values

```lua
-- Reference EMA (trend anchor)
local ema_ref = ema(20, "1h")  -- 20-period EMA, hourly bars

-- Spot Martingale Logic
if close < ema_ref then
    -- price below reference → DCA / Buy
    return 1
elseif close > ema_ref then
    -- price above reference → Exit
    return -1
end
```

#### **19.** WMA Trend Strength

Uses Weighted Moving Average (WMA) to capture strong trends by emphasizing recent bars.

* Buys when trend strength is bullish (price above WMA)
* Exits when trend weakens (price below WMA)

```lua
-- WMA trend reference
local wma_val = wma(20, "1h", 20)  -- 20-period WMA, hourly bars

-- Trend Strength Logic
if close > wma_val then
    return 1   -- Buy / Long
elseif close < wma_val then
    return -1  -- Exit
end
```

#### **20.** Mean Reversion with Bollinger-style Logic

Detects extreme price deviations from a moving average.

* Buy when price drops below the lower band
* Exit when price rises above the upper band

```lua
-- Reference SMA
local sma_val = sma(20, "1h")
-- Approximate band width (e.g., 2% of SMA)
local band_offset = sma_val * 0.02
-- Lower and upper "bands"
local lower_band = sma_val - band_offset
local upper_band = sma_val + band_offset

-- Mean Reversion Logic
if close < lower_band then
    return 1   -- Buy (oversold)
elseif close > upper_band then
    return -1  -- Exit (overbought)
end
```

#### **21.** MACD Histogram Divergence

Looks for momentum divergence between price and MACD histogram.

* Buy when price makes a lower low but MACD histogram shows higher low → bullish divergence
* Exit when divergence fails or turns bearish

```lua
-- MACD Line
local macd_val = macd(12, 26, "1h")
local macd_prev = macd(13, 27, "1h")  -- previous approximation

-- Histogram approximation
local hist = macd_val - macd_prev
local hist_prev = macd_prev - macd(14, 28, "1h")

-- Divergence Logic (bullish)
if close < close[1] and hist > hist_prev then
    return 1   -- Buy
elseif close > close[1] and hist < hist_prev then
    return -1  -- Exit
end
```

**Note:** Composability of supporting data can be queried in the documentation. As more indicators and data sources come live, strategies can evolve and public good repository will evolve.
