# Writing and using a library

> Functions and constants you reuse.

Source: https://algobarsx.com/docs/writing-a-library/

```algobarsx
library "Quant Toolkit"

const TRADING_DAYS = 252

fn kelly_fraction(win_rate: number, payoff: number) -> number:
    return win_rate - (1 - win_rate) / payoff

fn annualized_volatility(source: series<number> = close, length: int = 20) -> number:
    return stdev(returns(source), length) * sqrt(TRADING_DAYS)

fn position_heat(risks: list<percent>) -> percent:
    total = 0%
    for r in risks:
        total += r
    return total
```

A library holds functions, constants and types. It cannot place orders or draw ([AS0405](https://algobarsx.com/docs/diag-where-things-may-go/#AS0405)). Import it with `use library "Quant Toolkit" v2 as qt` and call `qt.kelly_fraction(0.55, 1.8)`.

Examples: [Quant Toolkit](https://algobarsx.com/docs/ex-09-quant-toolkit-library/), [Risk Parity](https://algobarsx.com/docs/ex-19-risk-parity-library/), [Ranking Tools](https://algobarsx.com/docs/ex-59-ranking-tools-library/), and a strategy that uses one: [Kelly Sized Breakout](https://algobarsx.com/docs/ex-28-kelly-sized-breakout/).
