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 6165999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:15:52+00:00 2026-05-23T22:15:52+00:00

I have a table of orders that looks like this: Ticket Open Time Close

  • 0

I have a table of orders that looks like this:

Ticket  Open Time               Close Time              Type    Size
253199  2010-09-09 20:40:00     2010-09-09 20:41:00     sell    0.1
255406  2010-09-13 19:30:00     2010-09-14 04:30:00     buy     0.1
258089  2010-09-15 07:00:00     2010-09-15 09:15:00     sell    0.1
258197  2010-09-15 09:15:00     2010-09-15 14:50:00     buy     0.1
258203  2010-09-15 09:20:00     2010-09-15 14:50:00     buy     0.1
259659  2010-09-16 04:35:00     2010-09-16 18:35:00     sell    0.1
261065  2010-09-17 07:05:00     2010-09-20 00:02:00     buy     0.1

For each order I would like to get a cumulative total of all orders (of the same Type) that are simultaneously opened. In other words, a sum of all Size of any order that opened before the given order and closed after the given order.

Here is my code:

DROP TABLE
IF EXISTS LongOrders;

CREATE TABLE LongOrders SELECT 
    B.Ticket AS Ticket,
    SUM(B.`Size`)AS `Cumulative Long`
FROM
    `orders`.eurusd_fx AS A
JOIN `orders`.eurusd_fx AS B ON(
    B.`Close Time` >= A.`Open Time`
    AND B.`Open Time` <= A.`Open Time`
    AND A.Type = B.Type
    AND A.Type = 'buy'
)
GROUP BY
    A.`Open Time`
ORDER BY
    A.`Open Time`;

DROP TABLE
IF EXISTS ShortOrders;

CREATE TABLE ShortOrders SELECT
    B.Ticket AS Ticket,
    SUM(B.`Size`)AS `Cumulative Short`
FROM
    `orders`.eurusd_fx AS A
JOIN `orders`.eurusd_fx AS B ON(
    B.`Close Time` >= A.`Open Time`
  AND B.`Open Time` <= A.`Open Time`
    AND A.Type = B.Type
    AND A.Type = 'sell'
)
GROUP BY
    A.`Open Time`
ORDER BY
    A.`Open Time`;

DROP TABLE
IF EXISTS CumLong;

CREATE TABLE CumLong SELECT
    A.*
FROM
    LongOrders AS A
JOIN `orders`.eurusd_fx AS B USING(Ticket);

DROP TABLE
IF EXISTS CumShort;

CREATE TABLE CumShort SELECT
    A.*
FROM
    ShortOrders AS A
JOIN `orders`.eurusd_fx AS B USING(Ticket);

SELECT DISTINCT
    A.*, CumLong.`Cumulative Long`,
    CumShort.`Cumulative Short`
FROM
    `orders`.eurusd_fx AS A
JOIN(CumShort, CumLong)ON(
    A.Ticket = CumLong.Ticket
    OR A.Ticket = CumShort.Ticket
) GROUP  BY A.`Open Time`;

And the output:

+--------+---------------------+------+------+--------+---------------------+------------+-----------------+------------------+
| Ticket | Open Time           | Type | Size | Item   | Close Time          | Commission | Cumulative Long | Cumulative Short |
+--------+---------------------+------+------+--------+---------------------+------------+-----------------+------------------+
| 253199 | 2010-09-09 20:40:00 | sell |  0.1 | eurusd | 2010-09-09 20:41:00 | -0.89      |         0.10000 |          0.10000 |
| 255406 | 2010-09-13 19:30:00 | buy  |  0.1 | eurusd | 2010-09-14 04:30:00 | -0.9       |         0.10000 |          0.10000 |
| 258089 | 2010-09-15 07:00:00 | sell |  0.1 | eurusd | 2010-09-15 09:15:00 | -0.91      |         0.10000 |          0.10000 |
| 258197 | 2010-09-15 09:15:00 | buy  |  0.1 | eurusd | 2010-09-15 14:50:00 | -0.91      |         0.10000 |          0.10000 |
| 259659 | 2010-09-16 04:35:00 | sell |  0.1 | eurusd | 2010-09-16 18:35:00 | -0.91      |         0.10000 |          0.10000 |
| 261065 | 2010-09-17 07:05:00 | buy  |  0.1 | eurusd | 2010-09-20 00:02:00 | -0.92      |         0.10000 |          0.10000 |
| 262121 | 2010-09-20 03:00:00 | sell |  0.1 | eurusd | 2010-09-20 05:55:00 | -0.91      |         0.10000 |          0.10000 |
| 262192 | 2010-09-20 05:50:00 | buy  |  0.1 | eurusd | 2010-09-20 09:50:00 | -0.92      |         0.10000 |          0.10000 |
| 262739 | 2010-09-20 16:55:00 | sell |  0.1 | eurusd | 2010-09-20 18:45:00 | -0.91      |         0.10000 |          0.90000 |
| 262822 | 2010-09-20 18:40:00 | buy  |  0.1 | eurusd | 2010-09-21 02:05:00 | -0.92      |         0.10000 |          0.10000 |
| 263801 | 2010-09-21 13:05:00 | buy  |  0.1 | eurusd | 2010-09-21 21:25:00 | -0.92      |         0.40000 |          0.10000 |

It does not seem to work properly. For instance the Cumulative Long for the first sell should be 0.

I’d really appreciate some help! Thanks.

Edit: 1 more question: How can I get the value for ABS(Cumulative Long-Cumulative Short)? I need the NET cumulative position size.

  • 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-05-23T22:15:53+00:00Added an answer on May 23, 2026 at 10:15 pm
    SELECT 
        A.*
      , SUM(CASE WHEN B.Type = 'buy'  THEN B.Size ELSE 0 END)
        AS `Cumulative Long`
      , SUM(CASE WHEN B.Type = 'sell' THEN B.Size ELSE 0 END) 
        AS `Cumulative Short`
    FROM
      orders.eurusd_fx AS A
        JOIN
      orders.eurusd_fx AS B
          ON  B.`Open Time` <= A.`Open Time` 
          AND A.`Open Time` <= B.`Close Time` 
    GROUP BY
        A.Ticket
    ORDER BY
        A.`Open Time`
      , A.Ticket ;
    

    or

    SELECT 
        A.*
      , ( SELECT COALESCE(SUM(B.Size),0)
          FROM orders.eurusd_fx AS B
          WHERE B.`Open Time` <= A.`Open Time` 
            AND A.`Open Time` <= B.`Close Time` 
            AND B.Type = 'buy' 
        ) AS `Cumulative Long`
      , ( SELECT COALESCE(SUM(C.Size),0)
          FROM orders.eurusd_fx AS C
          WHERE C.`Open Time` <= A.`Open Time` 
            AND A.`Open Time` <= C.`Close Time` 
            AND C.Type = 'sell' 
        ) AS `Cumulative Short`
    FROM
      orders.eurusd_fx AS A
    ORDER BY
        A.`Open Time`
      , A.Ticket ;
    

    To get that extra calculation, you either add another calulated column or add an extra layer (pushing the ORDER BY to that exterior layer):

    SELECT tmp.*
         , ABS(`Cumulative Long` - `Cumulative Short`) AS NetCumulativeSize 
    FROM
      ( SELECT 
            A.*
          , SUM(CASE WHEN B.Type = 'buy'  THEN B.Size ELSE 0 END)
            AS `Cumulative Long`
          , SUM(CASE WHEN B.Type = 'sell' THEN B.Size ELSE 0 END) 
            AS `Cumulative Short`
        FROM
          orders.eurusd_fx AS A
            JOIN
          orders.eurusd_fx AS B
              ON  B.`Open Time` <= A.`Open Time` 
              AND A.`Open Time` <= B.`Close Time` 
        GROUP BY
            A.Ticket
      ) AS tmp
    ORDER BY
        tmp.`Open Time`
      , tmp.Ticket ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this, Date Value 01/01/2010 03:59:00 324.44 01/02/2010
I have a table that looks like this: Customer ID, MDN, Plan, StartDate, EndDate,
I have a table that looks like this: id datatype name value 0 nvarchar(255)
I have a table that looks like this: (so you can picture it visually)
I have a table that looks like this: CREATE TABLE [dbo].[SomeTable]( [Guid] [uniqueidentifier] NOT
I have a schema that looks like this: Tables table_id seat_count Orders order_id table_id
I have a table that looks like this: CREATE TABLE [Test].[dbo].[MyTest] ( [Id] INT
I have a table with 8 million records that looks like this: CREATE TABLE
I have a table that looks like this: category category_id name category_seo_friendly_url left_id right_id
I have a table that looks like something like this: timestamp value person ===============================================

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.