Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:19:35+00:00 2026-06-07T21:19:35+00:00

ok lets say i have a simple table from trading account with all the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:19:36+00:00Added an answer on June 7, 2026 at 9:19 pm

    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.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have two tables, one for transactions, and another table who's primary
Lets say I have a simple many-to-many table between tables table1 and table2 that
Lets say I have a simple table that only contains two columns: MailingListUser -
I have a simple table in Sybase, let's say it looks as follows: CREATE
Lets say I have a simple ecommerce site that sells 100 different t-shirt designs.
Lets say I have a simple 1D array with 10-20 entries. Some will be
To keep things simple, lets say I have a Node class, each node has
Lets say i have a simple array of x rows and y columns with
For examples sake, lets say I have a table containing these columns ID (primary
Let's say I have a simple table of: id,name,salary The query I have is:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.