-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_logger.py
More file actions
22 lines (20 loc) · 866 Bytes
/
Copy pathdata_logger.py
File metadata and controls
22 lines (20 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import csv
from datetime import datetime
import threading
"""This file is used for writing down the data and saving it to a csv file.
When restarting the program it wrties ower what has previously been in the file"""
class CSVLogger:
def __init__(self, symbol):
self.symbol = symbol
self.filename = f"data/{symbol}_prices.csv"
self.lock = threading.Lock()
# Write header once
with open(self.filename, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['timestamp', 'price', 'ma', 'rsi'])
def log_price(self, price):
timestamp = datetime.now().isoformat()
with self.lock:
with open(self.filename, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([timestamp, price])