하지만 팬더없이 :
#!/usr/bin/env python
import datetime
from decimal import Decimal
class Tick(object):
pass
ticks = []
with open('data.csv') as f:
ticksTemp = []
lines = [x.strip('\n') for x in f.readlines()]
for line in lines:
columns = [x.strip() for x in line.split(',')]
if len(columns) != 3:
continue;
timeStr = columns[0] + '/' + columns[1]
time = datetime.datetime.strptime(timeStr, "%m/%d/%y/%H:%M:%S")
price = columns[2]
tick = Tick()
tick.time = time
tick.price = Decimal(price)
ticksTemp.append(tick)
ticks = sorted(ticksTemp, key = lambda x: x.time, reverse=False)
lines = []
first = ticks[0]
last = ticks[-1]
time = first.time
o,h,l,c = first.price, first.price, first.price, first.price
def appendLine():
lines.append(time.strftime('%Y-%m-%d')+','+str(o)+ ','+str(h)+','+str(l)+','+str(c))
for tick in ticks:
if(tick.time.year != time.year or tick.time.day != time.day):
appendLine()
time = tick.time
o = tick.price
c = tick.price
if tick.price > h:
h = tick.price
if tick.price < l:
l = tick.price
if last != first:
appendLine()
with open('ohlc.csv', 'w') as f:
f.write('\n'.join(lines))
data.csv :
01/18/14, 04:09:28, 55.0
01/18/14, 02:18:31, 55.4
01/17/14, 10:42:34, 55.3
01/17/14, 03:18:07, 55.2
ohlc.csv :
2014-01-17,55.2,55.3,55.2,55.3
2014-01-18,55.4,55.4,55.0,55.0
그건 고전입니다. 날짜별로 그룹화하고 각 OHLC 열에 해당 기능을 적용하십시오. 의사 코드에서 : data.groupby ("Date") .L에 대해 H 또는 .apply (min)에 적용 (최대) – tschm