positionsizing

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 6 Imported by: 0

README

position-sizing-data

DOI PyPI npm Go Reference

Reference data and arithmetic for position sizing across forex, stocks and crypto futures.

This repository exists because the numbers behind "how many units should I trade" are usually scattered across broker pages, forum posts and screenshots. Here they are in plain CSV and Markdown, with the formula written out, so anyone can check the arithmetic instead of trusting it.

An interactive implementation of the same calculation lives at https://positionsizetool.com/.

An archival copy of this dataset is deposited at Zenodo: doi:10.5281/zenodo.22840538. Use that DOI to cite the data in anything that outlives the repository.

The same tables and formulas are published as installable packages, so the arithmetic can be checked from a REPL instead of a spreadsheet: position-sizing-data on PyPI (source in pypi/), position-sizing-data on npm (source in npm/), and the Go module in this repository, which embeds the CSV files so go get github.com/tk25719/position-sizing-data needs nothing else.

The four data files are also mirrored on data platforms, each with the same provenance note attached: the canonical landing page is positionsizetool.com/reference-data.html, and the mirrors are Kaggle and Hugging Face.

Contents

File What is in it
data/contract-specifications.csv Default contract size, pip size and unit step per instrument, for 131 symbols across forex, metals, energies, indices and crypto.
data/pip-value-by-lot-size.csv Pip value per standard, mini, micro and nano lot, on a USD-denominated account, plus the cost of a 20-pip move and of a 1-pip spread.
data/losing-streak-equity.csv Equity remaining after 1 to 20 consecutive losses at 0.5%, 1% and 2% risk per trade.
data/drawdown-recovery.csv Gain required on the reduced balance to return to the original balance, for drawdowns from 5% to 90%.
checklists/pre-trade-risk-checklist.md What to confirm before an order goes in.
docs/position-sizing-formulas.md The formulas, why each term sits where it does, and a worked example.

The formula in one line

units = (risk_budget - commission) / ((|entry - stop| + spread) x quote_to_account_rate)

The tradable size is that number rounded down to the broker's unit step. Rounding up increases risk without saying so.

The full derivation, including why the spread is added to the stop distance while the commission is subtracted from the budget, is in docs/position-sizing-formulas.md.

Where the numbers come from

  • Instrument rows are the built-in defaults of the calculator linked above. They are typical retail contract specifications, not exchange-published official values, and several are editable by design. Verify the contract size and unit step of your own instrument against your broker before placing an order.
  • Everything else in data/ is computed, not copied. Each table has its generating formula stated at the top of this table's entry below:
    • losing-streak-equity.csv: remaining = (1 - risk%)^n
    • drawdown-recovery.csv: gain_required = 1 / (1 - drawdown) - 1
    • pip-value-by-lot-size.csv: pip_value = units x pip_size, converted to USD at the stated rate

Cite

YI, JUN (2026). Position sizing reference data and worked arithmetic for forex, stocks and crypto futures [Data set]. Zenodo. https://doi.org/10.5281/zenodo.22840538

Licence

Code and documentation: MIT. Data files in data/: CC0 1.0 Universal, to the extent applicable.

The Zenodo deposit of this dataset carries its own licence, CC BY 4.0, chosen at deposit time; refer to the record for the terms that apply to the archived copy.

This is not investment advice. Nothing here recommends a trade, a size or a broker.

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

View Source
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

View Source
var Data embed.FS
View Source
var ErrNoSuchTable = errors.New("positionsizing: unknown table")

ErrNoSuchTable is returned when an unknown table name is requested.

Functions

func FloorToStep

func FloorToStep(v, step float64) float64

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 PipValue

func PipValue(units, pipSize, quoteToAccountRate float64) float64

PipValue returns the pip value of a position size: pip value = units x pip size.

func PositionSize

func PositionSize(balance, riskPercent, entry, stop, unitStep float64) (float64, error)

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

func RecoveryGain(drawdown float64) (float64, error)

RecoveryGain returns the gain a reduced balance needs to get back to its original value: gain = 1/(1 - drawdown) - 1.

func RemainingEquity

func RemainingEquity(riskPercent float64, losses int) float64

RemainingEquity returns the fraction of equity left after n consecutive losses of riskPercent each: remaining = (1 - risk%)^n.

func Table

func Table(name string) ([][]string, error)

Table returns every row of one embedded CSV file, including its header row.

Types

type Contract

type Contract struct {
	Symbol        string
	AssetClass    string
	QuoteCurrency string
	PipSize       float64
	ContractSize  float64
	UnitStep      float64
}

Contract is one row of contract-specifications.csv.

func ContractFor

func ContractFor(symbol string) (Contract, bool)

ContractFor returns the row for one symbol, or false when it is not listed.

func Contracts

func Contracts() ([]Contract, error)

Contracts returns every instrument row. These are typical retail contract specifications, not exchange-published official values: verify them against your own broker before placing an order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL