Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stock Average Price Calculator

English | 中文

Still unsure what your average cost is after averaging down? Bought in multiple batches or traded in and out — and lost track of your blended price? Try this browser extension: enter each batch's quantity and price, and get your weighted average, total amount, and total shares instantly.

A lightweight browser extension for calculating the weighted average cost of stock positions across multiple purchases. Enter quantity and price for each batch to get real-time average cost, total amount, and total shares.

Chrome Extension License

Preview

Initial UI Results & History
Initial UI Results

The UI automatically switches between English and Chinese based on your browser language. Chinese screenshots: images/ScreenShot_ZH_01.png and images/ScreenShot_ZH_02.png.

Features

  • Real-time calculation — Average cost, total amount, and total quantity update as you type
  • Multi-batch input — Add multiple rows to simulate scaling in or averaging down
  • History — Automatically saves eligible calculations, up to 50 records
  • One-click restore — Click a history entry to refill the input fields
  • Clear & reset — Clear all inputs with one click
  • Multilingual — Built-in English and Simplified Chinese, follows browser locale
  • Privacy-friendly — Only uses the storage permission; all data stays local

Use Cases

  • Calculate blended cost after buying the same stock in multiple batches
  • Quickly estimate new average price after trading or adding to a position
  • Compare cost outcomes across different buying strategies

How It Works

The extension uses a weighted average formula:

Average Cost = Total Amount ÷ Total Quantity
Total Amount = Σ(Quantity × Price)
Total Quantity = Σ(Quantity)

Example: Buy 1,000 shares at 10, then 1,000 shares at 5

Metric Value
Total Amount 15,000.00
Total Quantity 2,000
Average Cost 7.50

Installation

Load from Source (Developer Mode)

  1. Clone this repository:

    git clone https://github.com/licc12/Stock-Price-Calculator.git
    cd Stock-Price-Calculator
  2. Open Chrome (or Edge, Brave, or other Chromium browsers) and go to the extensions page:

    • Chrome: chrome://extensions/
    • Edge: edge://extensions/
  3. Enable Developer mode in the top-right corner

  4. Click Load unpacked and select the project root directory

  5. Click the extension icon in the toolbar to start using it

Not yet published on the Chrome Web Store. Manual loading is required for now.

Usage

  1. Enter data — Fill in quantity and price for each batch (2 rows by default)
  2. Add rows — Click "+ Add Row" to record more batches
  3. View results — The top card shows average cost, total amount, and total quantity
  4. Save to history — On blur, eligible calculations are saved automatically when:
    • At least 2 valid rows exist (quantity > 0, price ≥ 0)
    • At least 2 different purchase prices are present
  5. Restore — Click a history entry to refill the inputs
  6. Delete — Click "Delete" on a history entry to remove it
  7. Clear — Click "Clear" to reset all input rows

Project Structure

Stock-Price-Calculator/
├── manifest.json          # Extension manifest (Manifest V3)
├── popup/
│   ├── popup.html         # Popup page
│   ├── popup.css          # Styles
│   └── main.js            # Entry point
├── src/
│   ├── calculator.js      # Core calculation logic
│   ├── constants.js       # Constants
│   ├── i18n.js            # Internationalization
│   ├── storage/
│   │   └── history.js     # History storage
│   ├── ui/
│   │   ├── inputs.js      # Input row management
│   │   ├── result.js      # Result display
│   │   └── history.js     # History list
│   └── utils/
│       └── format.js      # Number formatting
├── _locales/
│   ├── zh_CN/messages.json
│   └── en/messages.json
├── icons/                 # Extension icons
└── images/                # Screenshots

Tech Stack

  • Manifest V3 — Latest Chrome extension standard
  • Vanilla JavaScript (ES Modules) — No build step required
  • Chrome Storage API — Local history persistence
  • Chrome i18n — Native internationalization

Privacy

This extension:

  • Only requests the storage permission for local history
  • Does not collect or upload any user data
  • Does not access page content or inject scripts
  • Performs all calculations locally

License

This project is licensed under the MIT License.

Contributing

Issues and Pull Requests are welcome. Before submitting code:

  • Follow the existing code style and directory structure
  • Update both _locales/zh_CN and _locales/en when adding UI strings

If you find this project helpful, please give it a Star ⭐

About

Chrome extension to calculate weighted average stock cost after multiple purchases

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Contributors

Languages