JEX 全球领先的期权交易所
当我们分析比特币K线走势时,我们能够直接读取到的K线一般是1min,5min,15min,30min,1hour这种,如果我们需要其他周期的K线,那么需要我们自己进行转化,有时候即使交易所提供5min数据,我们也会直接通过1min等基础K线计算出5min的K线,这样可以减少对交易所服务器的请求次数,提高代码的效率,下面以1min转5min为例介绍一下,如何进行K线数据的转换:
timestamp | close | high | low | open | volume |
2017/11/1 0:00 | 6440.1 | 6458 | 6437.5 | 6457 | 1503847 |
2017/11/1 0:01 | 6425.4 | 6440.9 | 6420.1 | 6440.1 | 1755497 |
2017/11/1 0:02 | 6419.7 | 6425.3 | 6416.9 | 6425.4 | 2112786 |
2017/11/1 0:03 | 6401 | 6419.7 | 6392.5 | 6419.7 | 3059138 |
2017/11/1 0:04 | 6410 | 6412.1 | 6400 | 6401 | 1724514 |
2017/11/1 0:05 | 6425.9 | 6425.9 | 6404.3 | 6410 | 2084222 |
2017/11/1 0:06 | 6424.5 | 6430 | 6424.4 | 6425.9 | 589919 |
2017/11/1 0:07 | 6412 | 6424.4 | 6412 | 6424.5 | 897844 |
2017/11/1 0:08 | 6401 | 6415.1 | 6401 | 6412 | 575118 |
2017/11/1 0:09 | 6412 | 6412 | 6401 | 6401 | 1007341 |
2017/11/1 0:10 | 6412.9 | 6413 | 6405 | 6412 | 911109 |
2017/11/1 0:11 | 6416.5 | 6416.7 | 6412.9 | 6412.9 | 333443 |
2017/11/1 0:12 | 6410.4 | 6416.6 | 6410.3 | 6416.5 | 258638 |
2017/11/1 0:13 | 6416 | 6416 | 6410.3 | 6410.4 | 504926 |
对以上1min数据转换成3min的VBA代码如下
Sub min1to3min()
min1totalrows = Sheets("1min").Range("A1048576").End(xlUp).Row '1minK线总行数
k = 1
For i = 4 To min1totalrows
If Minute(Sheets("1min").Cells(i, 1)) / 3 = Int(Minute(Sheets("1min").Cells(i, 1)) / 3) Then
Sheets("3min").Cells(k, 1) = Format(Sheets("1min").Cells(i, 1), "yyyy/mm/dd hh:mm:ss") '计算时间''分钟是3的倍数
'计算收盘价
Sheets("3min").Cells(k, 2) = Sheets("1min").Cells(i, 2) '当前1min收盘价
'计算最高价
Sheets("3min").Cells(k, 3) = Application.Max(Sheets("1min").Range("c" & i & ":c" & i - 2)) '最近3跟1min最高价的最高价
'计算最低价
Sheets("3min").Cells(k, 4) = Application.Min(Sheets("1min").Range("d" & i & ":d" & i - 2)) '最近3跟1min最低的最低价
'计算开盘价
Sheets("3min").Cells(k, 5) = Sheets("1min").Cells(i - 2, 2) '往前两根K线的开盘价
'计算成交量
Sheets("3min").Cells(k, 6) = Application.Sum(Sheets("1min").Range("f" & i & ":f" & i - 2)) '最近3跟1min成交量总和
k = k + 1
End If
Next
End Sub