64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate ESC/POS ticker tape output from trade CSV data.
|
|
|
|
Reads a CSV of trades (timestamp, symbol, lots, price) and generates
|
|
ESC/POS commands with 90° rotated text to emulate an old stock ticker tape.
|
|
|
|
When the receipt paper is turned sideways:
|
|
- Symbol appears at the top of the tape
|
|
- Price (and lot count) appears at the bottom
|
|
"""
|
|
|
|
import csv
|
|
import sys
|
|
|
|
from escpos.printer import Dummy
|
|
|
|
# TM-T88V: 42 columns at Font A on 80mm paper
|
|
LINE_WIDTH = 42
|
|
|
|
|
|
def format_trade(symbol, lots, price):
|
|
"""Format a trade line for the ticker tape.
|
|
|
|
Symbol is placed at the start of the line, price info at the end.
|
|
With 90° rotation, start-of-line = top of tape, end-of-line = bottom.
|
|
"""
|
|
if lots > 1:
|
|
price_str = f"{lots}s{price:.2f}"
|
|
else:
|
|
price_str = f"{price:.2f}"
|
|
|
|
padding = LINE_WIDTH - len(symbol) - len(price_str)
|
|
if padding < 1:
|
|
padding = 1
|
|
return symbol + " " * padding + price_str
|
|
|
|
|
|
def main():
|
|
csv_file = sys.argv[1] if len(sys.argv) > 1 else "trades_sample_sorted.csv"
|
|
output_file = sys.argv[2] if len(sys.argv) > 2 else "ticker_tape.bin"
|
|
|
|
p = Dummy(profile="TM-T88V")
|
|
p.hw("INIT")
|
|
# ESC V 1: 90° clockwise character rotation (not exposed by python-escpos)
|
|
p._raw(b"\x1b\x56\x01")
|
|
|
|
with open(csv_file) as f:
|
|
reader = csv.reader(f)
|
|
for row in reader:
|
|
_timestamp, symbol, lots_str, price_str = row
|
|
lots = int(lots_str)
|
|
price = float(price_str)
|
|
line = format_trade(symbol, lots, price)
|
|
p.text(line + "\n")
|
|
|
|
p.cut()
|
|
|
|
with open(output_file, "wb") as f:
|
|
f.write(p.output)
|
|
print(f"Wrote {len(p.output)} bytes to {output_file}", file=sys.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|