# Pine traps

## History references

`close[1]` in Pine is the previous bar. In Forge an array is ordered oldest first, so the previous bar is index `-2`.

```python
import forge as fg

FORGE_API = 1


def previous_close(close):
    # Pine's close[1] is the bar before the latest closed one.
    return close[-2]
```

> **Trap.** A port that writes `close[-1]` for "one bar back" reads the latest bar, which is the wrong bar.

## Repainting on the forming bar

A Pine script recalculates on every tick of the bar that is still forming, so its plot can move and then settle. A backtest only sees the settled value. Forge computes on closed bars, so what a chart shows is what a test used.

> **Trap.** A condition that flickers on the forming bar in Pine never fires in a Forge test, and one that fires only after the bar closes never flickers. Compare them on closed bars.

## Look-ahead in higher timeframes

Pine's security call can be told to look ahead, which shows a higher-timeframe value before its bar has closed. Forge has no such setting: a slower series is visible on the first bar whose close is at or after its own close.

> **Trap.** A strategy that looked good in Pine only because of look-ahead will look worse in Forge. The Forge result is the one that could have been traded.

## Persistent state

Pine's `var` keeps a value across bars. A Forge `compute` has no state, because it works on arrays. Express the state as an array operation, or use the bar-by-bar form with declared state fields.

## na

Pine's `na` is Forge's NaN. A comparison with it is false in both.

```python
import forge as fg
from forge import ta

FORGE_API = 1


def clean(value):
    # Pine's nz(value): a missing value becomes zero.
    return ta.nz(value)
```

> **Trap.** `nz` turns a missing value into a number. Use it on purpose, because a zero is a real value to everything that reads it.

## Order fills

A Pine strategy fills a market order at the next bar's open by default. A Forge order decided at a close fills at the first real price after it, which is usually close to the same place and is always a price that traded.
