Median Price


Median Price is a simple indicator that calculates the midpoint between the High and Low prices for a given period. It is often used as a filter for trend analysis or as a component in more complex indicators like the Alligator.

MEDIAN_PRICE

=MEDIAN_PRICE(data)

Example Usage

=MEDIAN_PRICE(A2:E500)

Parameters

Parameter Type Description Status
data
Range
The input range of columns. Must include at least 5 columns (Date, Open, High, Low, Close) to ensure correct mapping.
Required

Return Value

A two-column array of dates and their corresponding Median Price values.

Median Price Formula Result in Google Sheets

Source Code

Copy the following code into your Apps Script editor (Extensions > Apps Script) to use the MEDIAN-PRICE function in your spreadsheet.

medianPrice.js
/**
 * Calculates the Median Price (High + Low) / 2.
 *
 * @param {array} data - The input range. Must include at least 5 columns (Date, Open, High, Low, Close) to ensure correct mapping of High/Low.
 * @returns {array} A two-column array with headers "Date" and "Median Price".
 * @customfunction
 */
function MEDIAN_PRICE(data) {
    checkPremium();

  // Argument validation
  if (arguments.length !== 1) {
    throw new Error(`Wrong number of arguments. Expected 1, but got ${arguments.length}.`);
  }

  const processedData = getData(data);

  // --- NEW: Function-level validation ---
  const columnCount = processedData[0].length;
  if (columnCount < 5) {
    throw new Error(`Invalid data structure for Median Price. Expected at least 5 columns (Date, Open, High, Low, Close), but got ${columnCount}.`);
  }
  // --- END of validation ---

  const dates = processedData.slice(1).map(row => row[0]);
  const highs = processedData.slice(1).map(row => row[2]);
  const lows = processedData.slice(1).map(row => row[3]);

  if (highs.length === 0) {
    throw new Error("Insufficient data for Median Price. No data points found after processing.");
  }

  const results = [["Date", "Median Price"]];

  for (let i = 0; i < highs.length; i++) {
    const medianPrice = (highs[i] + lows[i]) / 2;
    results.push([dates[i], medianPrice]);
  }

  return results;
}