ok lets say i have a simple table from trading account with all the typical transactions info:
Account ID Type OrderID Points NetPL Balance
13543564678 16 BUY 389745683 4.55 100.00 1,000,000.00
13543564678 16 BUY 389745684 4.55 100.00 1,000,100.00
13543564678 16 BUY 389745685 4.55 100.00 1,000,200.00
13543564678 16 SELL 389745686 4.55 100.00 1,000,300.00
13543564678 16 BUY 389745687 4.55 100.00 1,000,400.00
13543564678 16 SELL 389745688 4.55 100.00 1,000,500.00
13543564678 16 SELL 389745689 4.55 100.00 1,000,600.00
13543564678 16 SELL 389745690 4.55 -100.00 1,000,700.00
13543564678 16 SELL 389745691 4.55 -100.00 1,000,600.00
13543564678 16 SELL 389745692 4.55 -100.00 1,000,500.00
13543564678 16 SELL 389745693 4.55 -100.00 1,000,400.00
13543564678 16 SELL 389745694 4.55 100.00 1,000,300.00
13543564678 16 SELL 389745695 4.55 100.00 1,000,400.00
13543564678 16 BUY 389745696 4.55 100.00 1,000,500.00
13543564678 16 BUY 389745697 4.55 100.00 1,000,600.00
13543564678 16 BUY 389745698 4.55 100.00 1,000,700.00
13543564678 16 BUY 389745699 4.55 100.00 1,000,800.00
13543564678 16 BUY 389745700 4.55 100.00 1,000,900.00
13543564678 16 BUY 389745701 4.55 100.00 1,001,000.00
13543564678 16 BUY 389745702 4.55 100.00 1,001,100.00
13543564678 16 BUY 389745703 4.55 100.00 1,001,200.00
13543564678 16 BUY 389745704 4.55 -100.00 1,001,300.00
13543564678 16 BUY 389745705 4.55 -100.00 1,001,200.00
13543564678 16 BUY 389745706 4.55 -100.00 1,001,100.00
13543564678 21 BUY 389745707 4.55 -100.00 1,001,000.00
13543564678 21 SELL 389745708 4.55 -100.00 1,000,900.00
13543564678 21 SELL 389745709 4.55 -100.00 1,000,800.00
13543564678 21 SELL 389745710 4.55 -100.00 1,000,700.00
13543564678 21 BUY 389745711 4.55 -100.00 1,000,600.00
13543564678 21 SELL 389745712 4.55 -100.00 1,000,500.00
13543564678 21 BUY 389745713 4.55 -100.00 1,000,400.00
13543564678 21 SELL 389745714 4.55 -100.00 1,000,300.00
13543564678 21 SELL 389745715 4.55 100.00 1,000,200.00
13543564678 21 BUY 389745716 4.55 100.00 1,000,300.00
what i need here is to calculate a max drawdown using sql ONLY. I can easily do it on java (run a loop), but this thing shouold be a part of a big query, calculating different acc parameters.
so let me explain the formula : max drawdown is a value showing biggest balance drop toward negative, for all the acc records available in the db. looking into ex above, first drawdown is $400 (first negative $100 section), second one is bigger, its $1200 (second one). As you understand, these drawdowns happend regularly so acc history should have it a lot, not just 2 as shown here. usually it it calculates this kind of data on a huge dataset, starting from 100,000 records and up.
env is oracle 11gr2, read access only.
any smart ideas would be very appreciated!
ok, looks like system overwrites edits so i have to put it into 1 consolidated one.
I tried to add an artif column where map negative trades as ‘Y’ and tried to find a way how to do sum() on them after it. the problem here is that any single record can be more then any sum() of consequential records.
You want to first enumerate the draw downs. I do this by using the lag function with a cumulative sum. The lag finds the start of a draw down, by looking at the previous PL being positive and the current negative. The cumulative sum assigns a value.
The rest follows from identifying these periods.