
Company News
Andrew Becherer Joins Socket as Chief Information Security Officer
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.
@tradecanvas/analytics
Advanced tools
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).
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.
npm install @tradecanvas/analytics @tradecanvas/commons
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)
| 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 |
FixedCommission(perTrade)PercentCommission(rate) — fraction of notional, e.g. 0.001 = 10 bpsPerShareCommission(perShare, minimum?)PercentSlippage(rate) — adverse fraction of priceRangeBasedSlippage(factor) — proportional to bar rangeTracks 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.
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
Reference strategies live under @tradecanvas/analytics — drop in, tune
parameters, run. All four implement the same StrategyFn shape and respect
the backtester's allowShort flag.
import {
Backtester,
smaCrossStrategy,
rsiReversionStrategy,
donchianBreakoutStrategy,
bollingerReversionStrategy,
} from '@tradecanvas/analytics';
const bt = new Backtester({ initialCash: 10_000 });
const result = bt.run(bars, smaCrossStrategy({
fastPeriod: 10,
slowPeriod: 30,
size: 1,
}));
| Strategy | Style | Tuning |
|---|---|---|
smaCrossStrategy | Trend-following | fastPeriod, slowPeriod, size |
rsiReversionStrategy | Mean-reversion (long-only) | period, oversold, overbought, size |
donchianBreakoutStrategy | Trend breakout (Turtle-style) | entryPeriod, exitPeriod, size |
bollingerReversionStrategy | Mean-reversion to SMA | period, stdDev, size |
Each is a one-line function call returning a StrategyFn — easy to wrap,
combine, or compare side-by-side in a backtest harness.
open and the limit price.open and the stop price.These choices are conservative. A future release will offer a configurable intra-bar fill model.
MIT
FAQs
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).
The npm package @tradecanvas/analytics receives a total of 43 weekly downloads. As such, @tradecanvas/analytics popularity was classified as not popular.
We found that @tradecanvas/analytics demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket’s first CISO brings deep experience securing high-growth SaaS companies as open source supply chain threats accelerate.

Company News
Replit is integrating Socket Firewall into its AI-powered development experience to help protect builders from malicious open source packages.

Security News
npm confirmed a tooling bug incorrectly marked several one-character packages as security holders and said it was working on a rollback.