2024-07-22 13:48:55 +00:00
|
|
|
import csv
|
2024-07-22 22:15:01 +00:00
|
|
|
import dataclasses
|
2024-07-22 13:48:55 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import fin_depo
|
|
|
|
|
|
|
|
|
2024-07-22 22:15:01 +00:00
|
|
|
@dataclasses.dataclass(frozen=True)
|
2024-07-22 13:48:55 +00:00
|
|
|
class CsvFileLogger:
|
2024-07-22 22:15:01 +00:00
|
|
|
"""Outputs the given `TradeOrderDetails` to the CSV file at the specified
|
|
|
|
path.
|
|
|
|
"""
|
|
|
|
|
|
|
|
path: Path
|
2024-07-22 13:48:55 +00:00
|
|
|
|
|
|
|
def __call__(self, trade_order: fin_depo.data.TradeOrderDetails):
|
|
|
|
fieldnames: list[str] = [
|
2024-07-22 14:05:42 +00:00
|
|
|
'executed_time',
|
2024-07-22 13:48:55 +00:00
|
|
|
'input_asset',
|
|
|
|
'input_amount',
|
|
|
|
'output_asset',
|
|
|
|
'output_amount',
|
|
|
|
'fee_asset',
|
|
|
|
'fee_amount',
|
|
|
|
'order_id',
|
|
|
|
'raw_order_details',
|
|
|
|
]
|
|
|
|
|
2024-12-02 17:18:20 +00:00
|
|
|
# Select data to write
|
|
|
|
d = {
|
|
|
|
'executed_time': trade_order.executed_time,
|
|
|
|
'input_asset': trade_order.input.asset,
|
|
|
|
'input_amount': trade_order.input.amount,
|
|
|
|
'output_asset': trade_order.output.asset,
|
|
|
|
'output_amount': trade_order.output.amount,
|
|
|
|
'fee_asset': trade_order.fee.asset,
|
|
|
|
'fee_amount': trade_order.fee.amount,
|
|
|
|
'order_id': trade_order.order_id,
|
|
|
|
'raw_order_details': trade_order.raw_order_details,
|
|
|
|
}
|
|
|
|
|
2024-07-22 22:15:01 +00:00
|
|
|
# Ensure that directory exists
|
|
|
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
# Write row in CSV file
|
2024-07-22 13:48:55 +00:00
|
|
|
with open(self.path, 'a') as f:
|
|
|
|
writer = csv.DictWriter(f, fieldnames=fieldnames)
|
2024-11-28 20:56:36 +00:00
|
|
|
writer.writerow(d)
|
2024-12-02 17:18:20 +00:00
|
|
|
del d
|