npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

temple-trading-utils

v1.0.0

Published

Trading calculations — indicators (EMA, RSI, SMA, MACD), position sizing, risk/reward, pips, lot sizes, and profit/loss

Readme

trading-utils

Trading calculations — indicators, position sizing, risk/reward, pips, lot sizes, profit/loss, and Fibonacci levels.

Install

npm install trading-utils

Quick 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.5

Use 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 build

License

MIT