我有一个看起来像这样的订单表:
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
对于每个订单,我希望获得同时打开的所有订单(同一Type
)的累计总数。换句话说,在给定订单之前打开并在给定订单之后关闭的任何订单的所有Size
的总和。
这是我的代码:
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`;
输出:
+--------+---------------------+------+------+--------+---------------------+------------+-----------------+------------------+
| 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 |
它似乎无法正常工作。例如,第一个sell
的累积长度应为0。
我真的很感激一些帮助!感谢。
编辑:还有一个问题:如何获得ABS(
累积期长-
累积空)
的值?我需要NET累积位置大小。
答案 0 :(得分:1)
这就是我理解你的要求的方式。如果有错,请澄清。
SELECT
A.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.`Ticket`
ORDER BY
A.`Ticket`;
答案 1 :(得分:1)
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 ;
或
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 ;
要获得额外的计算,您可以添加另一个计算列或添加一个额外的图层(将ORDER BY
推送到该外部图层):
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 ;
答案 2 :(得分:0)
我插入了不同票号的测试数据,不会改变程序。
SELECT DISTINCT A.*, CumLong.`Cumulative Long`, CumShort.`Cumulative Short`
FROM `orders`.eurusd_fx AS A
LEFT JOIN Cumlong ON A.Ticket = CumLong.Ticket
LEFT JOIN cumshort ON A.Ticket = Cumshort.Ticket
GROUP BY A.`Open Time`
结果:
ticket open time close time type size Cumulative Long Cumulative Short
1 2010-09-09 20:40:00 2010-09-09 20:41:00 sell 0.1 NULL 0.100000001490116
2 2010-09-13 19:30:00 2010-09-14 04:30:00 buy 0.1 0.100000001490116 NULL
3 2010-09-15 07:00:00 2010-09-15 09:15:00 sell 0.1 NULL 0.100000001490116
4 2010-09-15 09:15:00 2010-09-15 14:50:00 buy 0.1 0.100000001490116 NULL
5 2010-09-15 09:20:00 2010-09-15 14:50:00 buy 0.1 NULL NULL
6 2010-09-16 04:35:00 2010-09-16 18:35:00 sell 0.1 NULL 0.100000001490116
7 2010-09-17 07:05:00 2010-09-20 00:02:00 buy 0.1 0.100000001490116 NULL
这就是你想要的?我不这么认为,因为唯一的订单打开而另一个打开也是258203.Nr 258197与258203同时关闭但是之前打开(我明白如果时间重叠你想要一个thar更早开始)
所以我猜结果应该是一行,订单258203,短期为0,.2?