PublicFunctions/Get-FMPStockPeerComparison.ps1

function Get-FMPStockPeerComparison { 
 

    <#
        .SYNOPSIS
            Retrieves stock peer comparison data for a specified trading symbol using the Financial Modeling Prep API.

        .DESCRIPTION
            The Get-FMPStockPeerComparison function fetches data that compares the target company with its peers on the same exchange.
            The returned data includes key details such as the peer symbol, company name, price, and market capitalization.
            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 trading symbol of the target company (e.g., AAPL). This parameter is mandatory.

        .PARAMETER ApiKey
            Specifies your Financial Modeling Prep API key. If omitted, the function will attempt to retrieve it using Get-FMPCredential
            and prompt the user if necessary.

        .EXAMPLE
            Get-FMPStockPeerComparison -Symbol AAPL

            This example retrieves the stock peer comparison data for Apple Inc. using your Financial Modeling Prep API key.

        .NOTES
            This function utilizes the Financial Modeling Prep API's Stock Peer Comparison endpoint.
            For more information, visit: https://financialmodelingprep.com
    #>


    [CmdletBinding()]

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

        [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/stock-peers"
    }

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

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

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