Skip to content

Instantly share code, notes, and snippets.

@yoonbae81
Created December 2, 2021 12:05
Show Gist options
  • Save yoonbae81/93250831679d307c255bf9920a417f45 to your computer and use it in GitHub Desktop.
Save yoonbae81/93250831679d307c255bf9920a417f45 to your computer and use it in GitHub Desktop.
Convert timestamp to hhmm format
#!/usr/bin/env python3
import argparse
from collections import namedtuple, defaultdict
from collections.abc import Generator
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo # requires `pip install tzdata` on Windows
FIELDS = ['TICKER', 'PRICE', 'VOL', 'TIMESTAMP']
Record = namedtuple('Record', ['ticker', 'price', 'volume', 'datetime'])
def read(file: Path) -> Generator[str]:
with file.open() as f:
while line := f.readline():
yield line
def parse(line: str) -> Record:
values = line.strip().split('\t')
result = dict(zip(FIELDS, values))
dt = datetime.utcfromtimestamp(int(result['TIMESTAMP']))
result['DATETIME'] = dt.isoformat()
return Record(
result['TICKER'],
result['PRICE'],
result['VOL'],
result['DATETIME']
)
def convert(source, output):
print(f'Looking for txt files in {source}')
for f in source.glob('**/*.txt'):
print(f'Loading {f}')
with Path(output / f.name).open('w', encoding='utf-8') as o:
print(f'Writing {o}')
count = 0
for line in read(f):
r = parse(line)
# print(f'{r.ticker}\t{r.price}\t{r.volume}\t{r.datetime[11:16]}')
o.write(f'{r.ticker}\t{r.price}\t{r.volume}\t{r.datetime[11:16]}\n')
count += 1
print(f'Found {count} record')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source', default=Path.cwd() / 'source')
parser.add_argument('-o', '--output', default=Path.cwd() / 'output')
args = parser.parse_args()
convert(args.source, args.output)
""" Sample Data
<TICKER>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT>
TQQQ.US,5,20211012,153000,126.17,126.62,124.75,126.35,1251346,0
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment