---
title: "Build Your First Strategy"
description: A complete walkthrough, from a trading idea to a backtested result you can read critically. Write it, run it, read the disclosures, tune it.
---

> **Since engine 3.0.61; verified against engine 3.0.61.** Every example on this page is an executed probe against that build.

This is the strategy sibling of the indicator primer: one idea taken from a sentence to a backtest you can evaluate honestly. You will write an RSI mean-reversion strategy, run it, read every part of the result, and then tune it like you mean it.

## 1. The idea, in one sentence

"When RSI leaves oversold, buy the dip with a quarter of my equity; take the trade off when momentum recovers; protect it with a stop 4% below."

## 2. Write it

Open a new editor tab and pick the **strategy** template, or paste this:

```javascript title="scripts/probes/strategies/first-strategy.ks"
//@version=2
strategy(title="RSI Reversion", initialCapital=10000, qtyType="percentOfEquity", qtyValue=25, commissionPercent=0.05, slippageBps=2)

timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries r = rsi(source=bars.close, period=14)
timeseries oversold = 30
timeseries recovered = 55
var lastClose = bars.close

if (crossover(r, oversold)) {
  strategy.entry("Dip", "long")
}
if (strategy.positionSize() > 0) {
  strategy.exit("Protect", fromEntry="Dip", stop=lastClose * 0.96)
}
if (crossover(r, recovered)) {
  strategy.closeAll()
}

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

Reading it top to bottom:

- `strategy(...)` replaces `define(...)`. The declaration IS your broker: starting equity, sizing (`25%` of equity per entry), commission, and slippage all live here, so a shared script carries its own assumptions. In the editor, the first-line keyword is clickable to toggle between indicator and strategy.
- `strategy.entry("Dip", "long")` queues a market order. Orders placed while a bar computes fill no earlier than the next bar's open; nothing in a backtest can act on information it did not have.
- `strategy.exit(..., stop=...)` arms a protective bracket against the named entry.
- `strategy.closeAll()` is the signal exit for the recovery case.

## 3. Run a backtest

Hit **Backtest**. The run replays your rules over the granted history for your tier (see [the overview](overview.md#running-a-backtest)), then the Strategy Tester panel opens under the chart.

## 4. Read the result critically

Four places to look, in order:

1. **The overview tiles**: net profit, win rate, profit factor, max drawdown. A strategy that "wins 70% of the time" with a payoff ratio of 0.3 loses money; the tiles together tell you which kind you have. Definitions and formulas for every number are in the [stats reference](stats-reference.md).
2. **The trades tab**: individual entries and exits with per-trade PnL, fees, and exit reason (`signal`, `stop`, `limit`, `trail`, `closeAll`, `endOfData`). Spot-check a few against the chart markers; you should be able to explain every trade from your own rules.
3. **The run details popover** (info icon in the panel header): this is the honesty report. It states the fill precision tier (whether contested intrabar fills were verified on finer data or settled by the fill model), the slippage disclosure for [bookEstimate runs](slippage-and-costs.md#reading-the-slippage-disclosure), and the compute time.
4. **The equity curve**: shape matters more than the end point. A grind punctuated by one lucky spike is a different strategy than a steady slope with shallow drawdowns, at the same net profit.

## 5. Tune it honestly

Things worth changing and rerunning (results are deterministic, so every change you see is yours):

- **Costs first.** Set `commissionPercent` and `slippageBps` to what you actually pay on your venue. Most retail strategies die here, and it is better to learn that in a backtest.
- **Sizing.** Try `qtyType="fixed"` with a small quantity versus `percentOfEquity`. Compounding changes drawdown character, not just the end number.
- **Book-aware slippage.** Add `slippageModel="bookEstimate"` and rerun: fills now pay impact based on recorded order-book depth for your size, where the pair has book history. Crank the size and watch average slippage grow; that is reality refusing to scale with you.
- **The stop.** Tighten `0.96` to `0.99` and watch the exit-reason mix shift from `signal` to `stop`. If stops dominate on your chart interval, read [choosing an interval to trust](fill-simulation.md#choosing-an-interval-to-trust) before tightening further; a stop that is narrow relative to the bar range is asking intrabar questions the interval cannot answer.

## 6. What you have, and what you do not

You now have a deterministic, lookahead-free replay of your rules with disclosed fill quality and costs. You do not have a promise about the future: no backtest survives contact with regime change, and a parameter tuned until the curve looks good is a fit to the past. Prefer fewer parameters, realistic costs, and results that stay acceptable when you nudge every setting.

Next: the full [order API](writing-strategies.md), [how fills are simulated](fill-simulation.md), and [every stat defined](stats-reference.md).
