# ALMA Envelope Reversion

> ALMA Envelope Reversion is a strategy that trades USDCAD on 15-minute bars. It holds at most 1 open trade.

Source: https://algobarsx.com/docs/ex-55-alma-envelope-reversion/

```algobarsx
strategy "ALMA Envelope Reversion"
    market: USDCAD
    bars: 15m
    max_open: 1

input envelope_width = 0.4%

env = envelope(alma(close, 21), 21, percent: envelope_width)

when crosses_below(close, env.lower) and cci(hlc3, 20) < -150:
    buy limit: env.lower, risk: 0.5%, stop: env.lower - 1.5 * atr(14), target: env.middle, expires: 4 bars

when crosses_above(close, env.upper) and cci(hlc3, 20) > 150:
    sell limit: env.upper, risk: 0.5%, stop: env.upper + 1.5 * atr(14), target: env.middle, expires: 4 bars

channel upper: env.upper, lower: env.lower
```
What this script says

ALMA Envelope Reversion is a strategy that trades USDCAD on 15-minute bars. It holds at most 1 open trade.

You can change one input: `envelope_width` (default 0.4%).

It calculates `env` as the moving average envelope (source the 21-bar ALMA of the close, length 21, percent `envelope_width`).

When the close crosses below `env.lower` and the 20-bar CCI of the typical price is below -150, it buys with a limit order at `env.lower`, risking 0.5% of the balance, with a stop at `env.lower` minus 1.5 × the 14-bar ATR, with a target at `env.middle` and expiring after 4 bars.

When the close crosses above `env.upper` and the 20-bar CCI of the typical price is above 150, it sells with a limit order at `env.upper`, risking 0.5% of the balance, with a stop at `env.upper` plus 1.5 × the 14-bar ATR, with a target at `env.middle` and expiring after 4 bars.

On the chart, it draws a channel.
