feat: add ?years= param to underlying-only chart endpoint

Endpoint standalone /api/chart/underlying/{id} caricava un TOP 350
fisso; ora accetta ?years= (default 5, clamp 1-20) e la SP filtra per
data invece che per numero di righe.

Il prezzo usato resta Px_close/Px_closeadj in base ad
AdjustedPrices, senza fallback incrociato tra i due campi: quando
Px_closeadj non è backfillato prima di uno split (es. Apple, valorizzato
solo da inizio 2019), un fallback avrebbe mischiato dati adjusted/raw
creando un gradino artificiale nel grafico. La SP ora taglia lo storico
dove il campo scelto non è popolato.

Fix collaterale: skip righe con prezzo NULL prima del cast a decimal,
evitava un InvalidCastException con storici lunghi.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 17:54:25 +02:00
parent d59f5a0faa
commit e36ef3fe47
4 changed files with 35 additions and 25 deletions

View File

@@ -8,14 +8,20 @@ GO
-- Usata da UnderlyingChartDataService per il grafico standalone
-- sottostante.
--
-- Output (Px_date ASC, TOP 350 più recenti):
-- @Years: ampiezza storico in anni (default 5).
--
-- Output (Px_date ASC, ultimi @Years anni):
-- Nome VARCHAR -- "{Name} ({Ticker_bbg})", uguale su ogni riga
-- Px_date DATE
-- Px DECIMAL -- Px_close o Px_closeadj in base ad AdjustedPrices
-- Px DECIMAL -- Px_closeadj se AdjustedPrices=1, altrimenti
-- Px_close. Nessun fallback incrociato tra i due
-- campi: evita il gradino artificiale quando
-- Px_closeadj non è backfillato prima di uno split
-- ============================================================
CREATE OR ALTER PROCEDURE [dbo].[cedlab_Chart_UnderlyingOnly]
@IDUnderlyings INT
@IDUnderlyings INT,
@Years INT = 5
AS
BEGIN
SET NOCOUNT ON;
@@ -29,24 +35,17 @@ BEGIN
IF @AdjustedPrices IS NULL RETURN; -- non trovato / cancellato / sospeso
;WITH Ranked AS
(
SELECT
p.Px_date,
CASE WHEN @AdjustedPrices = 0 THEN p.Px_close ELSE p.Px_closeadj END AS Px,
ROW_NUMBER() OVER (ORDER BY p.Px_date DESC) AS rn
FROM dbo.Prices p
WHERE p.UnderlyingsID = @IDUnderlyings
AND (p.Px_low IS NOT NULL OR p.Px_high IS NOT NULL OR
p.Px_open IS NOT NULL OR p.Px_close IS NOT NULL)
)
DECLARE @FromDate DATE = DATEADD(YEAR, -@Years, CAST(GETDATE() AS DATE));
SELECT
CONCAT(u.Name, ' (', u.Ticker_bbg, ')') AS Nome,
r.Px_date,
r.Px
FROM Ranked r
p.Px_date,
CASE WHEN @AdjustedPrices = 0 THEN p.Px_close ELSE p.Px_closeadj END AS Px
FROM dbo.Prices p
CROSS JOIN (SELECT TOP 1 * FROM dbo.Underlyings WHERE IDUnderlyings = @IDUnderlyings) u
WHERE r.rn <= 350
ORDER BY r.Px_date ASC;
WHERE p.UnderlyingsID = @IDUnderlyings
AND p.Px_date >= @FromDate
AND (CASE WHEN @AdjustedPrices = 0 THEN p.Px_close ELSE p.Px_closeadj END) IS NOT NULL
ORDER BY p.Px_date ASC;
END
GO