For SQL Server and for one stock at a time, try this:
Create Procedure 'MDDCalc'(
@StartDate date,
@EndDate date,
@Stock int)
AS
DECLARE @MinVal Int
DECLARE @MaxVal Int
DECLARE @MaxDate date
SET @MaxVal = (
SELECT MAX(hi)
FROM Table
WHERE Stockid = @Stock
AND Day BETWEEN (@Startdate-1) AND (@EndDate+1))
SET @MaxDate=(
SELECT Min(Date)
FROM Table
WHERE Stockid = @Stock
AND hi = @MaxVal)
SET @MinVal = (
SELECT MIN(low)
FROM Table
WHERE Stockid = @Stock
AND Day BETWEEN (@MaxDate-1) AND (@EndDate+1))
SELECT (@MaxVal-@MinVal) AS 'MDD'