如何将FOREX历史数据从M1转换为M2,M5和M15时间帧?

时间:2017-06-07 18:50:27

标签: sql excel data-conversion forex

我有几年的外汇历史数据,但它是按分钟列出的。我想知道如何将其转换为M2,M5和M15。我尝试了很多东西和方法,但都没有用。

欢迎使用Excel函数或SQL脚本。

下面列出了一些数据格式样本。

Date        Time    Open    High    Low     Close
2016.01.03  17:00   1.08701 1.08713 1.08701 1.08713
2016.01.03  17:01   1.08712 1.08712 1.08712 1.08712
2016.01.03  17:02   1.08708 1.08722 1.08708 1.08722
2016.01.03  17:03   1.08717 1.08723 1.08717 1.08723
2016.01.03  17:04   1.08718 1.08718 1.08711 1.08711
2016.01.03  17:05   1.08703 1.08716 1.08701 1.08712
2016.01.03  17:06   1.08721 1.08721 1.0871  1.0871
2016.01.03  17:07   1.08712 1.08715 1.08712 1.08712
2016.01.03  17:08   1.08711 1.0872  1.08711 1.08713
2016.01.03  17:09   1.08716 1.08723 1.08708 1.08708

2 个答案:

答案 0 :(得分:1)

以下是M2的示例。应该很容易看到如何改变M5和M15

declare @forexDate table
(
recDate datetime,
openVal float,
highVal float,
lowVal float,
closeVal float
)
INSERT INTO @forexDate VALUES('2016.01.03 17:00', 1.08701, 1.08713, 1.08701, 1.08713)
INSERT INTO @forexDate VALUES('2016.01.03 17:01', 1.08712, 1.08712, 1.08712, 1.08712)
INSERT INTO @forexDate VALUES('2016.01.03 17:02', 1.08708, 1.08722, 1.08708, 1.08722)
INSERT INTO @forexDate VALUES('2016.01.03 17:03', 1.08717, 1.08723, 1.08717, 1.08723)
INSERT INTO @forexDate VALUES('2016.01.03 17:04', 1.08718, 1.08718, 1.08711, 1.08711)
INSERT INTO @forexDate VALUES('2016.01.03 17:05', 1.08703, 1.08716, 1.08701, 1.08712)
INSERT INTO @forexDate VALUES('2016.01.03 17:06', 1.08721, 1.08721, 1.0871,  1.0871)
INSERT INTO @forexDate VALUES('2016.01.03 17:07', 1.08712, 1.08715, 1.08712, 1.08712)
INSERT INTO @forexDate VALUES('2016.01.03 17:08', 1.08711, 1.0872, 1.08711, 1.08713)
INSERT INTO @forexDate VALUES('2016.01.03 17:09', 1.08716, 1.08723, 1.08708, 1.08708)

SELECT o.recDate, o.openVal, c.closeVal, mx.highVal, mn.lowVal FROM 
(SELECT recDate, openVal, highVal, lowVal, closeVal, 
CAST((RowNum + 1) / 2 as int) AS Batch, (RowNum + 1) % 2 AS BatchPos FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY recDate) AS RowNum FROM @forexDate) x) o
inner join
(SELECT recDate, openVal, highVal, lowVal, closeVal, 
CAST((RowNum + 1) / 2 as int) AS Batch, (RowNum + 1) % 2 AS BatchPos FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY recDate) AS RowNum FROM @forexDate) x) c
on o.Batch = c.Batch
inner join
(SELECT Batch, MAX(highVal) as highVal FROM
(SELECT recDate, openVal, highVal, lowVal, closeVal, 
CAST((RowNum + 1) / 2 as int) AS Batch, (RowNum + 1) % 2 AS BatchPos FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY recDate) AS RowNum FROM @forexDate) x) mxpre GROUP BY Batch) mx
on mx.Batch = o.Batch
inner join
(SELECT Batch, MIN(lowVal) as lowVal FROM
(SELECT recDate, openVal, highVal, lowVal, closeVal, 
CAST((RowNum + 1) / 2 as int) AS Batch, (RowNum + 1) % 2 AS BatchPos FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY recDate) AS RowNum FROM @forexDate) x) mnpre GROUP BY Batch) mn
on mn.Batch = o.Batch
WHERE o.BatchPos = 0 AND c.BatchPos = 1

结果应如下所示:

recDate openVal closeVal    highVal lowVal
2016-01-03 17:00:00.000 1.08701 1.08712 1.08713 1.08701
2016-01-03 17:02:00.000 1.08708 1.08723 1.08723 1.08708
2016-01-03 17:04:00.000 1.08718 1.08712 1.08718 1.08701
2016-01-03 17:06:00.000 1.08721 1.08712 1.08721 1.0871
2016-01-03 17:08:00.000 1.08711 1.08708 1.08723 1.08708

我希望这就是你所追求的!

实际上,您希望将Batch和BatchPos预结果插入到临时表中,然后执行一系列自连接,而不是像上面那样动态实现所有内容!

答案 1 :(得分:1)

我认为数据透视表会为您完成这项工作,但由于“自动”选择组大小仅限于几天(我不知道为什么),所以需要一些黑客提供一些需要仔细解释的结果,尽管要改变分组的大小很简单:

SO44420634 example

ColumnA在B2中已经复制并适合:

=INT(B2)-42372+MOD(B2,1)*60*24

可以使用VLOOKUP将所选单元格解释回间隔的开头,如突出显示的那样,但对于没有现有数据点的任何时间范围,它都不会那么简单。

这取每个间隔的所有可用读数的平均值,而不是第一个。