temple-trading-utils
v1.0.0
Published
Trading calculations — indicators (EMA, RSI, SMA, MACD), position sizing, risk/reward, pips, lot sizes, and profit/loss
Maintainers
Readme
trading-utils
Trading calculations — indicators, position sizing, risk/reward, pips, lot sizes, profit/loss, and Fibonacci levels.
Install
npm install trading-utilsQuick Start
import { calculateEMA, calculateRSI, calculatePositionSize, calculatePNL } from "trading-utils";
// Technical indicators
const ema = calculateEMA(prices, 20);
const rsi = calculateRSI(prices, 14);
console.log(rsi.signal); // "BUY" | "SELL" | "NEUTRAL"
// Position sizing
const size = calculatePositionSize(10000, 100, 95, 2); // $10K balance, $100 entry, $95 SL, 2% risk
console.log(size.quantity);
// P&L tracking
const pnl = calculatePNL(trades);
console.log(pnl.winRate, pnl.profitFactor);API
Indicators
| Function | Returns | Use |
|----------|---------|-----|
| calculateEMA(prices, period) | { values, last } | Exponential Moving Average |
| calculateSMA(prices, period) | { values, last } | Simple Moving Average |
| calculateRSI(prices, period) | { values, last, signal } | Relative Strength Index with BUY/SELL/NEUTRAL |
| calculateMACD(prices, fast, slow, signalPeriod) | { macd, signal, histogram, last } | MACD with signal line and histogram |
| calculateBollingerBands(prices, period, stdDev) | { upper, middle, lower, last } | Bollinger Bands |
| calculateATR(candles, period) | { values, last } | Average True Range |
Risk Management
| Function | Returns | Use |
|----------|---------|-----|
| calculatePositionSize(balance, entry, stopLoss, riskPercent) | { quantity, totalValue, ... } | Position sizing by risk percentage |
| calculateRiskReward(entry, stopLoss, takeProfit, size, minRatio) | { ratio, acceptable, ... } | Risk/reward check |
Forex
| Function | Returns | Use |
|----------|---------|-----|
| calculatePipValue(lotSize, contractSize, price, precision) | number | Pip value in account currency (precision: 4 for most FX, 5 for JPY pairs) |
| calculateLotSize(balance, riskPercent, pipDistance, pipValuePerLot) | number | Max lot size from risk budget |
| calculatePipDistance(entry, exit, precision) | number | Distance in pips |
| calculatePipProfit(pips, lotSize, pipValue) | number | Profit from pips |
| calculateFullPipResult(entry, exit, lotSize, direction, pipValue, precision) | { pips, profitLoss, ... } | Complete pip calculation |
Profit & Loss
| Function | Returns | Use |
|----------|---------|-----|
| calculatePNL(trades) | PNLResult | Full P&L analytics (win rate, profit factor, avg win/loss) |
Fibonacci
| Function | Returns | Use |
|----------|---------|-----|
| calculateFibonacciLevels(high, low) | FibonacciLevels | Retracement levels (23.6, 38.2, 50, 61.8, 78.6) |
| calculateFibonacciExtension(high, low, swingHigh) | { level100, 127, 161, 200, 261 } | Extension levels |
Examples
RSI Trading Signal
const rsi = calculateRSI(prices, 14);
if (rsi.signal === "BUY" && rsi.last < 30) {
// Oversold — potential buy signal
}Position Sizing with Risk Management
const position = calculatePositionSize(50000, 150, 145, 1); // 1% risk
const rr = calculateRiskReward(150, 145, 165, position.quantity);
if (!rr.acceptable) {
// Risk/reward too low, skip trade
}Tracking Portfolio P&L
const pnl = calculatePNL([
{ entryPrice: 100, exitPrice: 110, quantity: 5, direction: "long" },
{ entryPrice: 200, exitPrice: 180, quantity: 3, direction: "long" },
]);
// pnl.winRate = 50, pnl.profitFactor = 2.5Use Cases
| Your Project | Application | |-------------|-------------| | broker-next | Price analysis, position tracking | | Financial-Freedom-App | Crypto trading indicators | | stock-maket-backend | Forex pip/lot calculations | | new-trading | Strategy indicators (SMA, RSI, MACD) | | financial-freedom-backend | P&L tracking, signal evaluation |
Features
- Zero dependencies — pure TypeScript
- 6 indicator types — EMA, SMA, RSI, MACD, Bollinger Bands, ATR
- Risk management — position sizing, risk/reward ratios
- Forex ready — pips, lot sizes, pip values
- P&L analytics — win rate, profit factor, average win/loss
- Fibonacci — retracement and extension levels
- Fully typed — TypeScript definitions included
Development
npm install
npm run dev # watch mode build
npm run test # run tests
npm run build # production buildLicense
MIT
