change open_trade_ledger typing to return a Generator type

explicit_write_pps_on_exit
algorandpa 2023-02-09 15:19:50 -05:00 committed by jaredgoldman
parent b67d020e23
commit a4bd51a01b
2 changed files with 20 additions and 20 deletions

View File

@ -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(

View File

@ -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
``<configuration_dir>/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):