{
  "schemaVersion": 1,
  "slug": "stock-average-calculator",
  "category": "finance",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "509b09fb8c47b62ea77d18dd1a43fbd2a96d4bd81ba9795a59f9686fbfeae8fb",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/stock-average-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "p_avg = (n₁p₁ + n₂p₂) ÷ (n₁ + n₂)",
    "vars": [
      [
        "n₁",
        "First lot shares"
      ],
      [
        "p₁",
        "First price"
      ],
      [
        "n₂",
        "Second lot shares"
      ],
      [
        "p₂",
        "Second price"
      ]
    ],
    "note": "Signed difference from the first purchase price = first price − weighted average price. A positive or negative result is not money saved and does not include fees or taxes."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "const totalShares=shares1+shares2; const totalCost=shares1*price1+shares2*price2; if(totalShares<=0) return { totalShares: null, totalCost: null, averagePrice: null, savingsVsFirst: null, __error: 'inputError' }; const avg=totalCost/totalShares; return {totalShares,totalCost,averagePrice:avg,savingsVsFirst:price1-avg};",
    "inputs": [
      {
        "name": "shares1",
        "type": "number",
        "default": 100,
        "min": 0,
        "max": 10000000,
        "step": 1
      },
      {
        "name": "price1",
        "type": "number",
        "default": 50,
        "min": 0,
        "max": 1000000,
        "step": 0.01
      },
      {
        "name": "shares2",
        "type": "number",
        "default": 50,
        "min": 0,
        "max": 10000000,
        "step": 1
      },
      {
        "name": "price2",
        "type": "number",
        "default": 40,
        "min": 0,
        "max": 1000000,
        "step": 0.01
      }
    ],
    "outputs": [
      {
        "name": "averagePrice",
        "precision": 4,
        "unit": "$"
      },
      {
        "name": "totalShares",
        "precision": 0,
        "unit": "shares"
      },
      {
        "name": "totalCost",
        "precision": 2,
        "unit": "$"
      },
      {
        "name": "savingsVsFirst",
        "precision": 4,
        "unit": "$"
      }
    ],
    "interpret": null
  }
}
