def write_bars_to_file(bars, filename, tz):
"""Creates CSV file from list of Bar instances"""
date_format_str = "%Y%m%d %H%M%S"
rows = [{'DateTime': bar.datetime.astimezone(tz).strftime(date_format_str),
'Open': bar.open,
'High': bar.high,
'Low': bar.low,
'Close': bar.close,
'Volume': bar.volume,
} for bar in bars]
if os.path.exists(filename):
raise Exception("File already exists!")
fd = os.popen("gzip > %s" % filename, 'w') if filename.endswith('.gz') else open(filename, 'w')
with fd:
csv_writer = csv.DictWriter(fd, ['DateTime', 'Open', 'High', 'Low', 'Close', 'Volume'])
csv_writer.writeheader()
csv_writer.writerows(rows)
评论列表
文章目录