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 🙏

© 2025 – Pkg Stats / Ryan Hefner

position-calculator

v1.0.0

Published

DCA position calculator with dynamic TP per stage (LONG/SHORT).

Readme

position-calculator

DCA-based position calculator with stage-based dynamic Take Profit for LONG and SHORT. Focused on explicit risk budgeting per level and configurable rounding precision.

Install

npm i position-calculator

Quick start (ESM)

import PositionCalculator, { PositionCalculator as PositionCalculatorNamed } from "position-calculator";

// Both default and named imports are available
const calc = new PositionCalculator({
  riskPercent: 0.10,
  weights: [1, 3, 5],
  targetProfitPercents: [0.0211, 0.0209, 0.0253],
  feePerUnit: 0,
  qtyPrecision: 1,
  pricePrecision: 4
});

const result = calc.calculate({
  side: "LONG",
  limit: 4658,
  tp: 4757,    // Initial TP for the 1st stage
  sl: 4556,
  leverage: 30,
  accountSize: 321000,
  limit1: 4622,
  limit2: 4590
});

console.log(result);

Usage (CommonJS)

const { PositionCalculator } = require("position-calculator");

const calc = new PositionCalculator();
const res = calc.calculate({
  side: "SHORT",
  limit: 4750,
  tp: 4680,
  sl: 4820,
  leverage: 20,
  accountSize: 100000
});

console.log(res);

API

  • Types:

    • Side: "LONG" | "SHORT"
    • CalcInput:
      • side: position side.
      • limit: price for the 1st level (required).
      • tp: Take Profit price for the 1st level (required; subsequent TPs are computed).
      • sl: Stop Loss price (required).
      • leverage: leverage (> 0).
      • accountSize: account size (> 0).
      • limit1, limit2: additional entry levels (optional).
      • riskPercent: total account risk (0–1), default 0.10.
      • weights: per-level weights, default [1, 3, 5].
      • targetProfitPercents: per-stage profit targets (0–1), default [0.02, 0.02, 0.02].
      • feePerUnit: linear fee per unit, default 0.
      • qtyPrecision: decimal places for quantities, default 1.
      • pricePrecision: decimal places for prices, default 4.
    • LegResult (per level):
      • price: level price.
      • qty: quantity at the level.
      • takeProfit: target exit price for that stage.
      • takeProfitAmount: profit target in currency.
      • takeProfitPercent: profit target as % of account.
      • stopLoss: SL price.
      • leverage: used leverage.
  • Class: new PositionCalculator(defaults?)

    • defaults may override: riskPercent, weights, targetProfitPercents, feePerUnit, qtyPrecision, pricePrecision.
  • Method: calculate(input: CalcInput): LegResult[]


Sample output (partial)

[
  {
    "price": 4658,
    "qty": 100.0,
    "takeProfit": 4757.0,
    "takeProfitAmount": 6762.0,
    "takeProfitPercent": 2.11,
    "stopLoss": 4556,
    "leverage": 30
  },
  { "price": 4622, "qty": 300.0, "takeProfit": 4761.3, "takeProfitAmount": 6722.3, ... },
  { "price": 4590, "qty": 500.0, "takeProfit": 4778.4, "takeProfitAmount": 8135.55, ... }
]

Notes:

  • The first takeProfit is fixed to the provided tp; subsequent TPs are computed to meet each stage's profit target.
  • Per-level quantities follow the overall risk budget (riskPercent * accountSize) weighted by weights and distance to sl.

Precision and fees

  • qtyPrecision and pricePrecision control final rounding.
  • feePerUnit adds linear per-unit cost into the TP math.

Common errors

  • Missing valid levels: at least limit is required.
  • sl on the wrong side of the price (distance to SL must be > 0 for all levels).
  • accountSize and leverage must be > 0.
  • riskPercent must be in (0, 1).
  • weights and targetProfitPercents must match the active level count and contain values > 0.

TypeScript

Types are shipped under dist/types. Works out of the box.

License

MIT