@ebowwa/quant-rust
v0.2.2
Published
High-performance quantitative finance library with Rust FFI bindings for Bun
Maintainers
Readme
@ebowwa/quant-rust
High-performance quantitative finance library with Rust FFI bindings for Bun.
This package provides blazing-fast quantitative finance computations powered by Rust, accessible through a clean TypeScript API using Bun's FFI capabilities.
Features
- Prediction Markets: Kelly criterion, AMM calculations, LMSR pricing, arbitrage detection
- Technical Indicators: SMA, EMA, RSI, MACD, Bollinger Bands, ATR, and more
- Risk Management: VaR, CVaR, Sharpe ratio, Sortino ratio, drawdown analysis
- Statistical Functions: Mean, variance, standard deviation, correlation
Installation
bun add @ebowwa/quant-rustRequirements
- Bun >= 1.0.0
- Prebuilt binaries are included for:
- macOS (Apple Silicon/arm64)
- macOS (Intel/x64)
- Linux (x64)
- Windows (x64)
If no prebuilt binary is available for your platform, the library will fall back to the development build in target/release/.
Usage
import {
kellyCriterion,
detectArbitrage,
convertOdds,
mean,
sma,
ema,
rsi,
macd,
calculateVar,
calculateSharpeRatio,
} from "@ebowwa/quant-rust";
// Kelly criterion for position sizing
const kelly = kellyCriterion(0.6, 0.5, 1000);
console.log(kelly.kelly_fraction); // 0.2
console.log(kelly.full_bet_size); // 200
// Detect arbitrage in prediction markets
const arb = detectArbitrage(0.45, 0.45);
console.log(arb.has_arbitrage); // true (0.45 + 0.45 = 0.9 < 1)
console.log(arb.profit_per_share); // 0.1 (10 cents per share)
// Convert between odds formats
const odds = convertOdds(0.5, "probability");
console.log(odds.decimal_odds); // 2.0
console.log(odds.american_odds); // 100
// Statistical functions
const avg = mean([1, 2, 3, 4, 5]);
console.log(avg); // 3
// Technical indicators
const smaResult = sma([1, 2, 3, 4, 5], 3);
console.log(smaResult); // [2, 3, 4]
const rsiResult = rsi([10, 11, 12, 11, 10, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16], 14);
console.log(rsiResult); // [71.43...]
// Risk metrics
const returns = [0.01, 0.02, -0.01, 0.03, -0.02, 0.01, 0.02, 0.01, -0.01, 0.02];
const varResult = calculateVar(returns, 0.95);
console.log(varResult.var); // Value at Risk at 95% confidence
const sharpe = calculateSharpeRatio(returns, 0.04, 252);
console.log(sharpe.annualized_sharpe); // Annualized Sharpe ratioAPI Reference
Prediction Market Functions
kellyCriterion(yourProbability, marketPrice, bankroll)
Calculate optimal position sizing using the Kelly criterion.
detectArbitrage(yesPrice, noPrice)
Detect arbitrage opportunities in binary prediction markets.
convertOdds(value, fromType)
Convert between probability, decimal odds, and American odds.
ammCalculateCost(poolYes, poolNo, buyYes, shares)
Calculate cost to buy shares from a constant-product AMM.
lmsrPrice(yesShares, noShares, b)
Calculate LMSR prices for a prediction market.
Statistical Functions
mean(data)
Calculate the arithmetic mean.
stdDev(data)
Calculate the standard deviation.
variance(data)
Calculate the variance.
correlation(x, y)
Calculate Pearson correlation coefficient.
Technical Indicators
sma(prices, period)
Simple Moving Average.
ema(prices, period)
Exponential Moving Average.
rsi(prices, period)
Relative Strength Index.
macd(prices, fastPeriod, slowPeriod, signalPeriod)
MACD (Moving Average Convergence Divergence).
Risk Management
calculateVar(returns, confidenceLevel)
Value at Risk calculation.
calculateDrawdown(equityCurve)
Drawdown analysis.
calculateSharpeRatio(returns, riskFreeRate, periodsPerYear)
Sharpe ratio calculation.
calculateSortinoRatio(returns, riskFreeRate, periodsPerYear)
Sortino ratio (downside deviation only).
Building from Source
If you need to build the native library from source:
# Navigate to the package directory
cd node_modules/@ebowwa/quant-rust
# Build the Rust library
cargo build --releaseLicense
MIT
Repository
https://github.com/ebowwa/codespaces/tree/main/packages/src/quant-rust
