好吧,假设我有一张来自交易账户的简单表格,其中包含所有典型的交易信息:
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
我需要的是仅使用sql计算最大值。我可以很容易地在java上运行它(运行一个循环),但是这个东西应该是一个大查询的一部分,计算不同的acc参数。
所以让我解释一下这个公式:max drawdown是一个值,表示对于db中可用的所有acc记录,最大的平衡下降为负值。从上面看,第一次缩减是400美元(第一次负100美元),第二次是更大,1200美元(第二次)。如你所知,这些下降经常发生,所以acc历史应该有很多,而不仅仅是这里显示的2。通常它会在一个巨大的数据集上计算这种数据,从100,000条记录开始计算。
env是oracle 11gr2,只读访问权限。 任何聪明的想法都会非常感激!
好吧,看起来系统会覆盖编辑,因此我必须将其整合到一个整合的编辑中。 我试图添加一个artif列,其中地图负面交易为'Y'并试图找到一种方法如何在它之后对它们执行sum()。这里的问题是,任何单个记录都可以超过后续记录的任何总和()。答案 0 :(得分:1)
您想首先枚举绘图。我通过使用累积和的滞后函数来做到这一点。滞后通过查看先前的PL为正且当前为负,找到平局的开始。累积金额指定一个值。
其余的是确定这些时期。
select Account, MAX(DrawDownAmount)
from (select Account, DrawDownNum, SUM(-NetPl) as DrawDownAmount
from (select t.*,
sum(BeginDrawDown) over (partition by Account order by orderid) as DrawDownNum
from (select t.*,
(case when NetPL < 0 then 1 else 0 end) as isdrawdown,
(case when coalesce(lag(NetPl, 1) over (partition by Account order by orderid), 1) >= 0 and
NetPl < 0
then 1
else 0
end) as BeginDrawDown
from t
) t
) t
where isdrawdown = 1
group by Account, DrawDownNum
) t
group by Account