使用 Python 将 Tick 数据转换为 OHLC(无外部库)

Convert Tick data to OHLC with Python (No External Libraries)

假设我有这样的数据:

[
 {'time': 1626459705; 'price': 278.989978}, 
 {'time': 1626459695; 'price': 279.437975}
]

注意 : 这只是我自己创建的示例数据。实际上,每分钟可能有任意数量的交易。因此,数据每分钟都会有所不同。

如何使用 Python 将其转换为 1 或 3 或 5 分钟 的 OHLC 烛台数据,而不使用任何外部库,例如Pandas?有没有可能用简单的方法来做?

提前致谢

这是生成随机数据并创建 OHLC table 的代码。

import random
import pprint

# Generate random walk data..

base = 1626459705
price = 278.989978
data = []
for i in range(600):
    data.append( {'time':base+10*i, 'price':price} )
    price += random.random() * 3 - 1.5
print(data)

# Produce 3 minute intervals.

ohlc = []
interval = 180

base = 0
# start time, open, high, low, close
rec = [ 0, 0, 0, 99999, 0 ]
ohlc = []
for row in data:
    rec[2] = max(rec[2],row['price'])
    rec[3] = min(rec[3],row['price'])
    if row['time'] >= base+interval:
        if rec[0]:
            rec[4] = row['price']
            ohlc.append( dict(zip(('time','open','high','low','close'),rec)) )
        base = rec[0] = row['time']
        rec[1] = rec[2] = rec[3] = row['price']

pprint.pprint(ohlc)

跟进

好的,这是一个可以处理您的数据的工具。我刚刚将该文件复制到“mydata.json”(并删除了第一个“data =”)。请注意,这会以实际的 3 分钟间隔打印输出,而不是基于输入的每一行。

import pprint
import json
import time

# Produce 3 minute intervals.

data = json.load(open('mydata.json'))
data.reverse()

interval = 180
base = data[0]['time'] // interval * interval

# start time, open, high, low, close
rec = [ base, data[0]['price'], data[0]['price'], data[0]['price'], 0 ]

ohlc = []

i = 0
while i < len(data):
    row = data[i]

    # If this sample is beyond the 3 minutes:
    if row['time'] > rec[0]+interval:
        ohlc.append( dict(zip(('time','open','high','low','close'),rec)) )
        rec[0] += interval
        rec[1] = rec[2] = rec[3] = rec[4]
    else:
        rec[2] = max(rec[2],row['price'])
        rec[3] = min(rec[3],row['price'])
        rec[4] = row['price']
        i += 1

for row in ohlc:
    row['ctime'] = time.ctime(row['time'])
    print( "%(ctime)s: %(open)12f %(high)12f %(low)12f %(close)12f" % row )

示例输出:

Wed Dec 22 22:27:00 2021:   454.427421   454.427421   454.427421   454.427421
Wed Dec 22 22:30:00 2021:   454.427421   454.427421   454.427421   454.427421
Wed Dec 22 22:33:00 2021:   454.427421   454.427421   454.427421   454.427421
Wed Dec 22 22:36:00 2021:   454.427421   457.058452   453.411757   453.411757
Wed Dec 22 22:39:00 2021:   453.411757   455.199204   452.589304   455.199204
Wed Dec 22 22:42:00 2021:   455.199204   455.199204   455.199204   455.199204
Wed Dec 22 22:45:00 2021:   455.199204   455.199204   455.199204   455.199204
Wed Dec 22 22:48:00 2021:   455.199204   455.768577   455.199204   455.768577
Wed Dec 22 22:51:00 2021:   455.768577   455.768577   455.768577   455.768577
Wed Dec 22 22:54:00 2021:   455.768577   455.768577   452.348469   454.374116