﻿CREATE PROCEDURE [dbo].[BO_GetKnoksLossCount]
    @UserId INT = NULL
AS
	
	SELECT 1 as Id, COUNT(1) as [Count]  FROM Signals s
	JOIN UserKnoks uk ON uk.KnokId = s.KnokId AND uk.UserId = ISNULL(@UserId, uk.UserId)
	JOIN _KnokStatuses ks ON ks.KnokStatusId = s.KnokStatusId
	WHERE s.KnokStatusId = 5 AND s.StopLossTouched = 0 AND s.ExitPriceTouched = 0 AND ks.IsClosed = 0 -- ExitPriceMissed
	
	UNION ALL
	
	SELECT 2 as Id, COUNT(1) as [Count]  FROM Signals s
	JOIN UserKnoks uk ON uk.KnokId = s.KnokId AND uk.UserId = ISNULL(@UserId, uk.UserId)
	WHERE KnokStatusId = 5 AND StopLossTouched = 1 -- -- Stop loss reached
	
	UNION ALL
	
	SELECT 3 as Id, COUNT(1) as [Count]  FROM Signals s
	JOIN UserKnoks uk ON uk.KnokId = s.KnokId AND uk.UserId = ISNULL(@UserId, uk.UserId)
	JOIN _KnokStatuses ks ON ks.KnokStatusId = s.KnokStatusId
	WHERE s.KnokStatusId = 5 AND ks.IsClosed = 1 --ClosedByKnokser
	
	RETURN 0 
GO