# Grid Accumulator

> Grid Accumulator is a strategy that trades EURUSD on 15-minute bars. It holds at most 5 open trades, takes long trades only and adds up to 5 entries i

Source: https://algobarsx.com/docs/ex-17-grid-accumulator/

```algobarsx
strategy "Grid Accumulator"
    market: EURUSD
    bars: 15m
    direction: long
    pyramiding: 5
    max_open: 5

input levels = 5
input spacing = 15 pips
input grid_size = 0.1 lots

state anchor: price = na

when trades.open(tag: "grid").len == 0 and orders.pending(tag: "grid").len == 0 and close > ema(close, 200):
    anchor = close
    for step in 1..levels:
        buy limit: close - spacing * step, size: grid_size, stop: close - spacing * (levels + 2), target: close + spacing, tag: "grid"

when close < anchor - spacing * (levels + 2):
    close_all tag: "grid"
    cancel orders.pending(tag: "grid")
```
What this script says

Grid Accumulator is a strategy that trades EURUSD on 15-minute bars. It holds at most 5 open trades, takes long trades only and adds up to 5 entries in the same direction.

You can change 3 inputs: `levels` (default 5), `spacing` (default 15 pips) and `grid_size` (default 0.1 lots).

It remembers `anchor` from one bar to the next.

When the number of open trades tagged "grid" is 0 and the number of pending orders tagged "grid" is 0 and the close is above the 200-bar EMA of the close, it sets `anchor` to the close; it also goes through each `step` in 1 to `levels` and buys with a limit order at the close minus `spacing` × `step`, with a size of `grid_size`, with a stop at the close minus `spacing` × (`levels` plus 2), with a target at the close plus `spacing` and tagged "grid".

When the close is below `anchor` minus `spacing` × (`levels` plus 2), it closes all trades tagged "grid" and cancels pending orders tagged "grid".
