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

@tradecanvas/analytics

v0.8.2

Published

Backtesting, portfolio tracking, and risk analytics for TradeCanvas — bar-by-bar Backtester with virtual fills, commission/slippage models, and risk metrics (Sharpe, Sortino, Calmar, max drawdown).

Readme

@tradecanvas/analytics

Backtesting, portfolio tracking, and risk analytics for TradeCanvas.

Preview release. API is stable but the engine has only been validated against synthetic test fixtures. Treat results as indicative until you've cross-checked them against your own reference implementation.

Install

npm install @tradecanvas/analytics @tradecanvas/commons

Backtester

Bar-by-bar engine. Strategy fn runs at close of each bar; orders fill on the next bar (market → next-bar open, limit/stop → when the next bar trades through the trigger price).

import { Backtester, FixedCommission, PercentSlippage } from '@tradecanvas/analytics'

const bt = new Backtester({
  initialCash: 10_000,
  commission: new FixedCommission(2),
  slippage: new PercentSlippage(0.0005),
  allowShort: true,
})

const result = bt.run(historicalBars, (ctx) => {
  if (!ctx.position) {
    ctx.placeOrder({ side: 'long', type: 'market', quantity: 1 })
  } else if (ctx.bar.close > ctx.position.averagePrice * 1.02) {
    ctx.close()
  }
})

console.log(result.metrics.sharpe, result.metrics.maxDrawdownPct)

StrategyContext

Field / method | Description ---|--- bar | Current bar index | Index of bar in the input series history | Bars up to and including bar position | Current position or null cash | Available cash equity | Cash + mark-to-market position value placeOrder(order) | Queue order for next bar close(tag?) | Market-close current position cancel(orderId) | Cancel a pending order

Commission & slippage models

  • FixedCommission(perTrade)
  • PercentCommission(rate) — fraction of notional, e.g. 0.001 = 10 bps
  • PerShareCommission(perShare, minimum?)
  • PercentSlippage(rate) — adverse fraction of price
  • RangeBasedSlippage(factor) — proportional to bar range

Portfolio

Tracks cash, one net position, realized P&L, and the equity curve.

import { Portfolio } from '@tradecanvas/analytics'

const portfolio = new Portfolio({ initialCash: 10_000 })
portfolio.applyFill({ ... })
portfolio.mark(time, price)

portfolio.getPosition()        // → { side, quantity, averagePrice, ... } | null
portfolio.getTrades()          // → closed trades
portfolio.getEquityCurve()     // → equity points
portfolio.equity(price)        // mark-to-market

Currently single-position. Multi-symbol portfolios are on the roadmap.

Risk metrics

import { computeRiskMetrics } from '@tradecanvas/analytics'

const m = computeRiskMetrics(initialCash, equityCurve, trades, {
  periodsPerYear: 252,    // optional; auto-detected from timestamps
  riskFreeRate: 0.03,
})

m.totalReturnPct
m.cagr
m.sharpe
m.sortino
m.calmar
m.maxDrawdownPct
m.winRate
m.profitFactor
m.expectancy

Edge cases (current behavior)

  • Gaps past a limit price: if the bar opens already through the limit, the order fills at the better of open and the limit price.
  • Stop orders inside a gap: fill at the worse of open and the stop price.
  • Bar that touches both stop and limit on the same bar: order resolves to the more pessimistic price for the current side (no intra-bar tick simulation).

These choices are conservative. A future release will offer a configurable intra-bar fill model.

License

MIT