# Kelly Sized Breakout

> Kelly Sized Breakout is a strategy that trades XAUUSD on 30-minute bars. It holds at most 1 open trade.

Source: https://algobarsx.com/docs/ex-28-kelly-sized-breakout/

```algobarsx
strategy "Kelly Sized Breakout"
    market: XAUUSD
    bars: 30m
    max_open: 1

use library "Quant Toolkit" v2 as qt

input max_risk = 2%

stats = history.last(90d)
edge = qt.kelly_fraction(stats.win_rate / 100%, 2.0)
risk_now = clamp(edge * 50%, 0.25%, max_risk)
breakout = crosses_above(close, highest(high, 20)[1])

when breakout and stats.count >= 20:
    buy risk: risk_now, stop: atr(14) * 1.5, target: 2R

when breakout and stats.count < 20:
    buy risk: 0.25%, stop: atr(14) * 1.5, target: 2R
```
What this script says

Kelly Sized Breakout is a strategy that trades XAUUSD on 30-minute bars. It holds at most 1 open trade.

It uses the library "Quant Toolkit" (version 2) as `qt`.

You can change one input: `max_risk` (default 2%).

It calculates `stats` as closed-trade results over the last 90 days, `edge` as `qt.kelly_fraction(stats.win_rate / 100%, 2.0)`, `risk_now` as `edge` × 50% kept between 0.25% and `max_risk` and `breakout` as whether the close crosses above the previous bar's highest high of the last 20 bars.

When `breakout` and `stats.count` is at or above 20, it buys at market, risking `risk_now` of the balance, with a stop 1.5 × the 14-bar ATR from the entry and with a target 2R from the entry.

When `breakout` and `stats.count` is below 20, it buys at market, risking 0.25% of the balance, with a stop 1.5 × the 14-bar ATR from the entry and with a target 2R from the entry.

This description may be incomplete because the script has errors.
