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.
or
To get that extra calculation, you either add another calulated column or add an extra layer (pushing the
ORDER BYto that exterior layer):