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.1.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.1-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gfil_calculators-1.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 e13c509e6465da26abbdaf1a646b0dfbbfed8c0bbb895ad32f87d1cd59e70c7e
MD5 9b5b90dbcb948fb98c4e73d33d168b76
BLAKE2b-256 734884e7c787dde1a7db898a87db881de54b9d3bad4ed13f4daec5db31c11018

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gfil_calculators-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 00943dbbd5f8f9842279848a1f2e1d8dc64ec119bc2daaf518289fd05d80e61a
MD5 0a5f6afcd4f8410582b8c3dd5fd4f3e7
BLAKE2b-256 74711d702227cc5717e45dd2ccc2b8ddf0815041447ba2239c8dc8daeb37269b

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