---
title: "Examples: The Five Perps Scenarios"
description: The five canonical perps strategies behind the engine's acceptance battery, covering liquidation on both sides, fee classification, funding erosion, and bankruptcy accounting.
---

These are reproduction scripts: each one re-derives its pinned numbers on the recorded fixture bars committed beside its probe, which is what makes them verifiable. Run on a live chart they simply enter at that chart's prices (their thresholds are fixture values near 100), so for live experimentation use the chart-adapted `Perps:` templates in the editor's template picker instead.

These five scripts are the perps module's ground truth. They ship in three places at once: as editor templates (the `Perps:` entries in the template picker), as executed examples in this documentation, and as an acceptance battery inside the engine's test suite where every expected number is derived by hand from the [formulas](perps-and-leverage) and the engine cannot ship unless all five reproduce to the last digit.

Each section names the exact behavior the scenario pins down. The headline numbers live in the [canonical scenarios table](perps-and-leverage#the-five-canonical-scenarios).

## 1. Long liquidation

A 10x long entered at 100 with 0.5% maintenance margin must liquidate at exactly `(100 - 10) / 0.995 = 90.45226130653266`.

```kscript
//@version=2
strategy(title="Perps Liquidation Anchor", position="onchart", axis=true, initialCapital=10000, instrument="perps", leverage=10, maintenanceMarginPercent=0.5, qtyType="fixed", qtyValue=1, slippageBps=0, makerFeePercent=0, takerFeePercent=0, funding="off")

timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)

if (barIndex == 0) {
  strategy.entry("L", "long")
}

plotLine(value=bars.close, width=1, colors=["#dc2626"], label=["Close"], desc=["Close price"])
```

## 2. Short liquidation with taker fees

The short side of the same formula, `(100 + 10) / 1.005 = 109.45273631840797`, with a market entry so both the entry fill and the liquidation fill pay `takerFeePercent`.

```kscript
//@version=2
strategy(title="Perps 2: Short Liquidation with Taker Fees", position="onchart", axis=true, initialCapital=10000, qtyType="fixed", qtyValue=1, instrument="perps", leverage=10, maintenanceMarginPercent=0.5, takerFeePercent=0.05, funding="off")

timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries armed = 100.5

if (strategy.positionSize() == 0 && bars.close < armed) {
  strategy.entry("S", "short")
}

plotLine(value=bars.close, width=1, colors=["#94a3b8"], label=["Close"], desc=["Close price"])
```

## 3. Maker and taker split

Two trades, four fills, four fee classifications: a limit entry and its take-profit limit pay maker; a market entry and its protective stop pay taker. Also a guard-design lesson: the script phase runs at bar close, after fills, so entry zones stay disjoint from exit prices or the flat-position guard re-arms on the very bar an exit filled.

```kscript
//@version=2
strategy(title="Perps 3: Maker and Taker Split", position="onchart", axis=true, initialCapital=10000, qtyType="fixed", qtyValue=1, instrument="perps", leverage=5, makerFeePercent=0.01, takerFeePercent=0.05, funding="off")

timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries limitZoneLow = 100.5
timeseries limitZoneHigh = 103
timeseries marketZone = 110.5

if (strategy.positionSize() == 0 && bars.close > limitZoneLow && bars.close < limitZoneHigh) {
  strategy.entry("LimitIn", "long", limit=95)
}
if (strategy.positionSize() == 0 && bars.close > marketZone) {
  strategy.entry("MarketIn", "long")
}
if (strategy.positionSize() > 0) {
  strategy.exit("TP", fromEntry="LimitIn", limit=105)
  strategy.exit("SL", fromEntry="MarketIn", stop=92)
}

plotLine(value=bars.close, width=1, colors=["#94a3b8"], label=["Close"], desc=["Close price"])
```

## 4. Funding erosion

Funding settles against the isolated margin: each settlement debits cash and committed margin together, the liquidation price tightens as margin erodes, and a settlement that depletes margin liquidates the position at that bar's open with zero price pnl.

```kscript
//@version=2
strategy(title="Perps 4: Funding Erosion", position="onchart", axis=true, initialCapital=10000, qtyType="fixed", qtyValue=1, instrument="perps", leverage=10, maintenanceMarginPercent=0.5, funding="data")

timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries armed = 99.5

if (strategy.positionSize() == 0 && bars.close > armed) {
  strategy.entry("L", "long")
}

plotLine(value=bars.close, width=1, colors=["#94a3b8"], label=["Close"], desc=["Close price"])
```

## 5. Bankruptcy gap

Price gaps straight through the liquidation level: the fill is the bar price because it is worse, the loss beyond committed margin is recorded as `bankruptcyDeficit`, and equity floors at exactly zero.

```kscript
//@version=2
strategy(title="Perps Bankruptcy Gap", position="onchart", axis=true, initialCapital=10, instrument="perps", leverage=10, maintenanceMarginPercent=0.5, qtyType="fixed", qtyValue=1, slippageBps=0, makerFeePercent=0, takerFeePercent=0, funding="off")

timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)

if (barIndex == 0) {
  strategy.entry("L", "long")
}

plotLine(value=strategy.equity(), width=1, colors=["#7c2d12"], label=["Equity"], desc=["Strategy equity"])
```
