Skip to main content

Trading risk calculators covering Forex, Gold, Crypto, and Indices. The only library with proper pip values for 30+ instruments.

Project description

gfil-calculators

PyPI npm License: MIT

Open-source trading calculator library covering Forex, Gold (XAUUSD), Crypto, Indices, and Energy — with proper pip values and contract sizes for 30+ instruments. Available in Python and JavaScript. Zero dependencies. Pure math. Built by GFIL — Global Financial Intelligence Lab.


Why GFIL Calculators?

  • Multi-asset coverage — Every other calculator library handles one asset class. This one handles all five (Forex, Gold, Crypto, Indices, Energy) with correct pip sizes and contract sizes for each instrument. No other library correctly calculates position size for XAUUSD, BTCUSD, and SPX500 in one API.
  • XAUUSD specialist — Gold trading has unique pip values ($0.01 pip size, 100 oz contract) that most forex calculators get wrong. This library handles it correctly out of the box, along with silver (XAGUSD).
  • Python + JavaScript dual library — Same formulas, same instrument database, same results. Use Python for backtesting and research; use JavaScript for web dashboards and browser extensions. Published on PyPI and npm.
  • MIT licensed, zero dependencies — No numpy, no pandas, no external APIs. Pure math functions you can embed anywhere — even in proprietary commercial products. Attribution appreciated but not required.

Calculators Included (22 total)

Calculator Inputs Output
Position Size account_balance, risk_percent, stop_loss_pips, symbol lots, risk_amount, micro_lots
Position Size (ATR) account_balance, risk_percent, atr_value, atr_multiplier, symbol lots, stop_loss_pips, stop_loss_price
Risk Per Trade account_balance, lots, stop_loss_pips, symbol risk_percent, risk_amount, risk_level
Pip Value symbol, lots, account_currency one_pip, ten_pips, hundred_pips
Pip Value (Cross) symbol, lots, conversion_rate one_pip (with currency conversion)
Pips ↔ Price symbol, price_diff or pips pips or price difference
ATR highs[], lows[], closes[], period ATR series (Wilder's smoothing)
ATR Stop Loss entry_price, atr_value, direction, multiplier stop_loss_price
ATR Take Profit entry_price, atr_value, direction, rrr, multiplier take_profit_price
Fibonacci Retracement high, low, direction 7 levels (0%–100%)
Fibonacci Extension high, low, direction 7 targets (127.2%–423.6%)
Kelly Criterion win_rate, avg_win, avg_loss, fraction kelly_percent, adjusted_percent
Kelly from Trades wins[], losses[], fraction kelly_percent + trade statistics
Drawdown equity_curve[] max_drawdown_percent, recovery_percent
Recovery Needed drawdown_percent gain_percent to recover
Consecutive Losses account_balance, risk_percent, num_losses remaining_balance, total_loss
Margin Required symbol, lots, entry_price, leverage margin, notional_value
Free Margin account_balance, open_positions[] free_margin, margin_level
Risk/Reward entry, stop_loss, take_profit, direction rrr, breakeven_winrate
Risk/Reward (Sized) entry, stop_loss, take_profit, lots, pip_value rrr + risk_dollars, reward_dollars
Profit Factor total_wins, total_losses profit_factor, net_profit, assessment
Compound Interest principal, annual_rate, years, monthly_contribution final_value, total_interest

Quick Start

Python

pip install gfil-calculators
from gfil_calculators.position_size import calculate_position_size
from gfil_calculators.pip_value import calculate_pip_value
from gfil_calculators.instruments import get_instrument

# Position sizing for XAUUSD (Gold)
result = calculate_position_size(10000, 1.0, 50, "XAUUSD")
print(f"Lots: {result['lots']}, Risk: ${result['risk_amount']}")

# Pip value for EURUSD
pip = calculate_pip_value("EURUSD", 1.0, "USD")
print(f"1 pip = ${pip['one_pip']}")

# Get instrument specs
specs = get_instrument("BTCUSD")
print(f"Pip size: {specs['pip_size']}, Contract: {specs['contract_size']}")

JavaScript / Node.js

npm install gfil-trading-calculators
const { calculatePositionSize, calculatePipValue, getInstrument } = require('gfil-trading-calculators');

// Position sizing for XAUUSD (Gold)
const result = calculatePositionSize(10000, 1.0, 50, "XAUUSD");
console.log(`Lots: ${result.lots}, Risk: $${result.risk_amount}`);

// Pip value for EURUSD
const pip = calculatePipValue("EURUSD", 1.0, "USD");
console.log(`1 pip = $${pip.one_pip}`);

Supported Instruments (30+)

Category Instruments
Forex Majors EURUSD, GBPUSD, USDJPY, USDCHF, AUDUSD, NZDUSD, USDCAD
Forex Crosses EURGBP, EURJPY, GBPJPY, AUDJPY, EURNZD, GBPAUD, GBPCAD, GBPJPY
Commodities XAUUSD (Gold), XAGUSD (Silver)
Crypto BTCUSD, ETHUSD, SOLUSD, XRPUSD, DOGEUSD, ADAUSD
Indices SPX500, NAS100, DAX40, FTSE100, DJI30
Energy USOIL (WTI Crude), UKOIL (Brent Crude)

Formula Reference

Position Size

lots = (account_balance × risk_percent / 100) / (stop_loss_pips × pip_value_per_lot)

Pip Value

pip_value = pip_size × contract_size × lots

For XAUUSD: pip_size = 0.01, contract_size = 100 oz1 pip per lot = $1.00

For USDJPY: pip_size = 0.01, contract_size = 100,0001 pip per lot = ¥1,000 (converted to account currency)

ATR (Average True Range)

TR = max(high - low, |high - prev_close|, |low - prev_close|)
ATR = Wilder's smoothing (14-period default)

Kelly Criterion

f* = (p × b - q) / b   where p = win rate, q = 1 - p, b = avg_win / avg_loss

Drawdown

max_drawdown = max(peak - trough) / peak × 100%
recovery_needed = 100 / (100 - drawdown%) × 100% - 100%

Live Demo

Try all 22 calculators free at blog.quant-view.xyz/tools/


Related


License

MIT — Use freely in personal, academic, and commercial projects.


Citation: GFIL — Global Financial Intelligence Lab. gfil-calculators: Open-source trading calculator library. 2026. Available at https://pypi.org/project/gfil-calculators/ and https://www.npmjs.com/package/gfil-trading-calculators/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gfil_calculators-1.1.0.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gfil_calculators-1.1.0-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file gfil_calculators-1.1.0.tar.gz.

File metadata

  • Download URL: gfil_calculators-1.1.0.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for gfil_calculators-1.1.0.tar.gz
Algorithm Hash digest
SHA256 76adb66420bef0d9ea36df9a7cc7b0d9b14dc4a2240fd8dae61c6b5aee12e471
MD5 ee22fd09e20e8e5ed692f0a6ccf08520
BLAKE2b-256 e8c9e76c6fb15162e7723dcbf569103dfc1a5c6913bf0b198aa58c6d89ff3370

See more details on using hashes here.

File details

Details for the file gfil_calculators-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gfil_calculators-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6536c2ff2b54d2dce92bf601bdf9a19af5fc983bcd94425801e5c2bcbb02bfce
MD5 0c38e800a5c8754da805735e6f0470d8
BLAKE2b-256 ce3ae38484e92bce1f9983608c7edd35c198eba7adf59d6f5b78d12f9fe42b50

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page