PublicFunctions/Get-FMPUnadjustedStockPrice.ps1

function Get-FMPUnadjustedStockPrice { 
 

    <#
        .SYNOPSIS
            Retrieves unadjusted end-of-day stock price and volume data for a specified stock symbol using the Financial Modeling Prep API.

        .DESCRIPTION
            The Get-FMPUnadjustedStockPrice function fetches stock price and trading volume data without adjustments for stock splits.
            It provides key data points such as the unadjusted open, high, low, close prices (prefixed with "adj" to indicate they are unadjusted),
            and trading volume. Users can filter results by a date range using the "FromDate" and "ToDate" parameters.
            If no API key is provided, the function attempts to retrieve it using the Get-FMPCredential function and will prompt the user if necessary.

        .PARAMETER Symbol
            Specifies the stock symbol for which the unadjusted price data is to be retrieved (e.g., AAPL). This parameter is mandatory.

        .PARAMETER FromDate
            (Optional) Specifies the start date (inclusive) for retrieving unadjusted stock data. The date must be a [datetime] object and is formatted as "yyyy-MM-dd".

        .PARAMETER ToDate
            (Optional) Specifies the end date (inclusive) for retrieving unadjusted stock data. The date must be a [datetime] object and is formatted as "yyyy-MM-dd".

        .PARAMETER ApiKey
            Specifies your Financial Modeling Prep API key. If omitted, the function attempts to retrieve it using the Get-FMPCredential function.

        .EXAMPLE
            Get-FMPUnadjustedStockPrice -Symbol AAPL -FromDate (Get-Date "2025-01-08") -ToDate (Get-Date "2025-04-08")

            This example retrieves unadjusted end-of-day stock price and volume data for Apple Inc. from January 8, 2025 to April 8, 2025.

        .NOTES
            This function utilizes the Financial Modeling Prep API's Non-Split-Adjusted Historical Price endpoint.
            For more information, visit: https://financialmodelingprep.com
    #>


    [CmdletBinding()]

    Param (
        [Parameter(Mandatory = $true)]
        [string] $Symbol,

        [Parameter(Mandatory = $false)]
        [datetime] $FromDate,

        [Parameter(Mandatory = $false)]
        [datetime] $ToDate,

        [Parameter(Mandatory = $false)]
        [string] $ApiKey = (Get-FMPCredential)
    )

    Begin {
        if (-not $ApiKey) {
            $ApiKey = Read-Host "Please enter your Financial Modeling Prep API key"
        }
        $baseUrl = "https://financialmodelingprep.com/stable/historical-price-eod/non-split-adjusted"
    }

    Process {
        $url = "{0}?symbol={1}&apikey={2}" -f $baseUrl, $Symbol, $ApiKey

        if ($FromDate) {
            $url += "&from=" + $FromDate.ToString("yyyy-MM-dd")
        }
        if ($ToDate) {
            $url += "&to=" + $ToDate.ToString("yyyy-MM-dd")
        }

        $headers = @{
            "Upgrade-Insecure-Requests" = "1"
        }

        try {
            $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers -ErrorAction Stop
            return $response
        }
        catch {
            throw "Error retrieving unadjusted stock price data: $_"
        }
    }
 
 };