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

crypto-portfolio-risk-analyzer

v1.0.3

Published

A JavaScript library for analyzing and assessing the risk of cryptocurrency portfolios, including calculations for volatility, Sharpe ratio, VaR, CVaR, max drawdown, and Sortino ratio.

Readme

Crypto Portfolio Risk Analyzer

A simple JavaScript library to analyze and assess the risk of cryptocurrency portfolios. It provides important risk metrics including:

  • Volatility
  • Sharpe Ratio
  • Value at Risk (VaR)
  • Conditional Value at Risk (CVaR)
  • Max Drawdown
  • Sortino Ratio

Features

  • Risk Analysis: Quickly analyze the risk metrics of your crypto portfolio.
  • Easy Integration: Install with npm or yarn and use it directly in your JavaScript projects.
  • Modular Design: Functions are neatly organized into different modules for better maintainability and extensibility.

Installation

You can install the crypto-portfolio-risk-analyzer library via npm or yarn:

Using npm:

npm install crypto-portfolio-risk-analyzer

Using yarn:

yarn add crypto-portfolio-risk-analyzer

Usage

After installation, you can use the library to analyze your crypto portfolio by passing a list of assets, including the coin ID and other parameters.

Example Code:

const { analyzePortfolio } = require('crypto-portfolio-risk-analyzer');

// Define your portfolio (coinId, allocation, etc.)
const portfolio = [
  { coinId: 'bitcoin', allocation: 50, days: 10, varConfidence: 0.99 },
  { coinId: 'ethereum', allocation: 30, days: 10, cvarConfidence: 0.80 },
  { coinId: 'litecoin', allocation: 20, days: 10 }
];

// Analyze the portfolio
analyzePortfolio(portfolio)
  .then(result => {
    if (result.success) {
      console.log('Portfolio Analysis:', result.data);
    } else {
      console.log('Failed to analyze portfolio');
    }
  })
  .catch(error => {
    console.error('Error:', error);
  });

Expected output:

{
  "success": true,
  "data": [
    {
      "asset": "bitcoin",
      "volatility": "2.35%",
      "sharpeRatio": "1.32",
      "allocation": "50%",
      "var95": "3.12%",
      "cvar95": "4.25%",
      "maxDrawdown": "12.54%",
      "sortinoRatio": "1.67",
      "riskSummary": "Risk Analysis for BITCOIN....."
    },
  ]
}

Functions

analyzePortfolio(portfolio)

This function performs the risk analysis for each asset in your portfolio.

Parameters:

  • portfolio (Array): An array of portfolio objects, where each object contains:
    • coinId (String): The coin’s ID (e.g., 'bitcoin', 'ethereum').
    • allocation (Number): The percentage of the total portfolio allocated to this coin.
    • days (Number, optional): The number of days to fetch historical data for risk calculations (default is 30).
    • varConfidence (Number, optional): The confidence level for Value at Risk (default is 0.95).
    • cvarConfidence (Number, optional): The confidence level for Conditional VaR (default is 0.95).

Returns:

  • A Promise that resolves to an object with a success field and data field containing the risk analysis results.

Explain Risk Metrics

  • Volatility measures how much an asset's price fluctuates over a given period. It is calculated as the standard deviation of daily returns.
  • Sharpe Ratio measures the risk-adjusted return of an asset. It is calculated as the ratio of the asset's excess return over the risk-free rate to its volatility.
  • Value at Risk (VaR) estimates the maximum potential loss of an asset at a given confidence level over a specified time horizon.
  • Conditional Value at Risk (CVaR) estimates the expected loss of an asset beyond the VaR at a given confidence level.
  • Max Drawdown measures the maximum loss from a peak to a trough of an asset's value.
  • Sortino Ratio measures the risk-adjusted return of an asset using downside risk (volatility of negative returns) instead of total volatility.

Contributing

If you’d like to contribute to this project, feel free to fork the repository, create a feature branch, and submit a pull request with your changes.

License

This project is licensed under the MIT License.