From 91492d7771aae3112af90b85390fd65798de728b Mon Sep 17 00:00:00 2001 From: algorandpa Date: Thu, 9 Feb 2023 15:19:50 -0500 Subject: [PATCH] change open_trade_ledger typing to return a Generator type --- piker/clearing/_paper_engine.py | 26 +++++++++++--------------- piker/pp.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/piker/clearing/_paper_engine.py b/piker/clearing/_paper_engine.py index 4f5cfee8..2e5c94c8 100644 --- a/piker/clearing/_paper_engine.py +++ b/piker/clearing/_paper_engine.py @@ -43,6 +43,7 @@ from ..data.types import Struct from ..pp import ( Position, Transaction, + open_trade_ledger ) from ..data._normalize import iterticks from ..data._source import unpack_fqsn @@ -86,17 +87,6 @@ class PaperBoi(Struct): last_ask: tuple[float, float] = (float('inf'), 0) # price, size last_bid: tuple[float, float] = (0, 0) - - def record_paper_trade(self): - try: - #create paper trades record - - print('RECORDING PAPER TRADE') - config, path = load('paper_trades') - except Exception as Arguement: - logging.exception('ERROR RECORDING PAPER TRADE') - pass - async def submit_limit( self, oid: str, # XXX: see return value @@ -246,9 +236,7 @@ class PaperBoi(Struct): ) log.info(f'Fake filling order:\n{fill_msg}') await self.ems_trades_stream.send(fill_msg) - - self._trade_ledger.update(fill_msg.to_dict()) - + if order_complete: msg = BrokerdStatus( reqid=reqid, @@ -284,7 +272,15 @@ class PaperBoi(Struct): dt=pendulum.from_timestamp(fill_time_s), bsuid=key, ) - self.record_paper_trade() + + # Transacupdate ledger per trade + ledger_entry = {} + ledger_entry[oid] = t.to_dict() + self._trade_ledger.update(ledger_entry) + with open_trade_ledger('paper', 'paper', self._trade_ledger) as ledger: + log.info(ledger) + + pp.add_clear(t) pp_msg = BrokerdPosition( diff --git a/piker/pp.py b/piker/pp.py index 6c5a60d8..92052a71 100644 --- a/piker/pp.py +++ b/piker/pp.py @@ -32,13 +32,13 @@ from typing import ( Iterator, Optional, Union, + Generator ) import pendulum from pendulum import datetime, now import tomli import toml - from . import config from .brokers import get_brokermod from .clearing._messages import BrokerdPosition, Status @@ -53,8 +53,8 @@ log = get_logger(__name__) def open_trade_ledger( broker: str, account: str, - -) -> dict: + trades: dict[str, Any] +) -> Generator[dict, None, None]: ''' Indempotently create and read in a trade log file from the ``/ledgers/`` directory. @@ -82,7 +82,8 @@ def open_trade_ledger( ledger = tomli.load(cf) print(f'Ledger load took {time.time() - start}s') cpy = ledger.copy() - + if trades: + cpy.update(trades) try: yield cpy finally: @@ -91,10 +92,13 @@ def open_trade_ledger( # https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries print(f'Updating ledger for {tradesfile}:\n') ledger.update(cpy) - + + print("updated ledger") + print(ledger) # we write on close the mutated ledger data with open(tradesfile, 'w') as cf: toml.dump(ledger, cf) + return class Transaction(Struct, frozen=True):