Private/Domain/MarketObservation.ps1

function New-MarketObservation {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$InstrumentId,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$SourceId,

        [Parameter(Mandatory)]
        [DateTimeOffset]$ObservedAtUtc,

        [Parameter(Mandatory)]
        [DateTimeOffset]$RetrievedAtUtc,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Interval,

        [Parameter()]
        [Nullable[decimal]]$Open,

        [Parameter()]
        [Nullable[decimal]]$High,

        [Parameter()]
        [Nullable[decimal]]$Low,

        [Parameter()]
        [Nullable[decimal]]$Close,

        [Parameter()]
        [Nullable[decimal]]$Value,

        [Parameter()]
        [Nullable[decimal]]$Volume,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$VolumeUnit,

        [Parameter()]
        [ValidatePattern('^[A-Za-z]{3}$')]
        [string]$Currency,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$Unit,

        [Parameter()]
        [switch]$IsAdjusted,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$AdjustmentBasis,

        [Parameter(Mandatory)]
        [ValidateSet('live', 'delayed', 'cached', 'partial', 'unavailable')]
        [string]$Freshness,

        [Parameter()]
        [switch]$IsProxy,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$ProxyFor,

        [Parameter()]
        [ValidateRange(0, [int]::MaxValue)]
        [int]$ValueRevision = 0
    )

    $ohlcValues = @($Open, $High, $Low, $Close)
    $ohlcCount = @($ohlcValues | Where-Object { $null -ne $_ }).Count
    if ($ohlcCount -notin @(0, 4)) {
        throw 'OHLC observations must provide all four values.'
    }

    if ($ohlcCount -eq 4) {
        if ($High -lt $Low -or $Open -lt $Low -or $Open -gt $High -or $Close -lt $Low -or $Close -gt $High) {
            throw 'OHLC values must satisfy high >= low and encompass open and close.'
        }
    }

    if ($null -eq $Value -and $ohlcCount -eq 0) {
        throw 'A market observation must contain at least one value.'
    }

    if ($null -ne $Volume -and [string]::IsNullOrWhiteSpace($VolumeUnit)) {
        throw 'Volume observations require an explicit volume unit.'
    }

    if ($IsAdjusted.IsPresent -and [string]::IsNullOrWhiteSpace($AdjustmentBasis)) {
        throw 'Adjusted price observations require an adjustment basis.'
    }

    if ($IsProxy.IsPresent -and [string]::IsNullOrWhiteSpace($ProxyFor)) {
        throw 'A proxy observation must identify its underlying instrument.'
    }

    if (-not $IsProxy.IsPresent -and -not [string]::IsNullOrWhiteSpace($ProxyFor)) {
        throw 'ProxyFor may only be set for a proxy observation.'
    }

    $observedUtc = $ObservedAtUtc.ToUniversalTime()
    $retrievedUtc = $RetrievedAtUtc.ToUniversalTime()
    $identity = '{0}|{1}|{2}|{3}|{4}' -f $SourceId, $InstrumentId, $observedUtc.Ticks, $Interval, $ValueRevision
    $hasher = [System.Security.Cryptography.SHA256]::Create()
    try {
        $identityBytes = [System.Text.Encoding]::UTF8.GetBytes($identity)
        $digest = $hasher.ComputeHash($identityBytes)
    }
    finally {
        $hasher.Dispose()
    }

    $observationId = [System.BitConverter]::ToString($digest).Replace('-', '').ToLowerInvariant()

    [pscustomobject][ordered]@{
        observationId    = $observationId
        instrumentId     = $InstrumentId
        sourceId         = $SourceId
        observedAtUtc    = $observedUtc
        retrievedAtUtc   = $retrievedUtc
        interval         = $Interval
        open             = $Open
        high             = $High
        low              = $Low
        close            = $Close
        value            = $Value
        volume           = $Volume
        volumeUnit       = $VolumeUnit
        currency         = if ([string]::IsNullOrWhiteSpace($Currency)) { $null } else { $Currency.ToUpperInvariant() }
        unit             = $Unit
        adjustmentBasis  = $AdjustmentBasis
        freshness        = $Freshness
        isProxy          = $IsProxy.IsPresent
        proxyFor         = $ProxyFor
        valueRevision    = $ValueRevision
    }
}