
WITH (TABLOCKX) - IMPORTANT: lock the table to avoid data changes in between

Running_Sum = - store the value in the requested format
RETURN ON MAXIMUM DRAWDOWN UPDATE
UPDATE = CASE WHEN Sequence = + 1 THEN Sequence ELSE 1/0 END, - double check to be still in = - calculate the new value , Row_Order AS Sequence - define the sequence for data processing define some variable to help with the quirky (38, 6), - to hold the intermediate INT - verify that the Sequence of processing is the same as the reqired Sequence not sure about required level of precision , tradedatetime, sum(pctaccountchange) as net_pctChangeĬreate clustered index ix_row_order on dbo.grouped_Performance(Row_Order) Select row_number() over (order by tradedatetime) as Row_Order not sure if this is required but seems logical I'll keep playing around to see if I can find something better. That said, the final query to return the result looks quite inefficient (triangular join?) - there's probably a smarter way. (38, 6) NOT NULL,ĬONSTRAINT PRIMARY KEY NONCLUSTEREDĬREATE CLUSTERED INDEX ON. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'.') AND type in (N'U')) You can ignore the market as that's not relevant, really. Here's the sample data and table definition. You could think of it as the largest percentage decrease in the account equity from a peak to a trough. I need a query that returns the largest drawdown in terms of percentage of the account.

So, if we start with $1,000 we could see the final balance being $1,607.16. In the sample data below, you could seed the account with any number and at the end get a final account balance. The data I have is tradeId, market, tradeDateTime, pctAccountChange. From this the largest drawdown in percentage terms should be able to be calculated. I do not have the account balances, but I do have the percent change on the account per trade.
RETURN ON MAXIMUM DRAWDOWN SERIES
Drawdown is defined as the largest percent loss in equity that has happened over a series of trades. I'm looking for a query on some financial data to show me the largest drawdown that has occurred on an account.
