Documentation
¶
Overview ¶
Package positionsizing loads the position sizing reference tables that ship with this module and repeats the arithmetic behind them.
The data files are embedded, so importing this package needs no network access and no credentials. Every calculation here mirrors the ones documented in docs/position-sizing-formulas.md, and every number in data/ comes with the formula that produces it.
Interactive calculator: https://positionsizetool.com/ Citable archive: https://doi.org/10.5281/zenodo.22840538
Index ¶
- Constants
- Variables
- func FloorToStep(v, step float64) float64
- func PipValue(units, pipSize, quoteToAccountRate float64) float64
- func PositionSize(balance, riskPercent, entry, stop, unitStep float64) (float64, error)
- func RecoveryGain(drawdown float64) (float64, error)
- func RemainingEquity(riskPercent float64, losses int) float64
- func Table(name string) ([][]string, error)
- type Contract
Constants ¶
const ( ContractSpecificationsTable = "contract-specifications.csv" PipValueByLotSizeTable = "pip-value-by-lot-size.csv" LosingStreakEquityTable = "losing-streak-equity.csv" DrawdownRecoveryTable = "drawdown-recovery.csv" )
Table names embedded in this module.
Variables ¶
var Data embed.FS
var ErrNoSuchTable = errors.New("positionsizing: unknown table")
ErrNoSuchTable is returned when an unknown table name is requested.
Functions ¶
func FloorToStep ¶
FloorToStep rounds v down to a whole multiple of step.
A naive floor turns 49999.999999999956 into 49 units of 1000, i.e. 49000, losing 999 units of size that the risk budget actually pays for. Anything within a rounding hair of the next step is treated as that step; everything else still rounds down, because rounding up raises risk without saying so.
func PositionSize ¶
PositionSize returns the largest tradable size that keeps the loss inside the risk budget, given a balance, a risk percentage, an entry price and a stop price. The result is floored to unitStep. It never rounds up.
func RecoveryGain ¶
RecoveryGain returns the gain a reduced balance needs to get back to its original value: gain = 1/(1 - drawdown) - 1.
func RemainingEquity ¶
RemainingEquity returns the fraction of equity left after n consecutive losses of riskPercent each: remaining = (1 - risk%)^n.