Add Generator as return type of open_trade_ledger

explicit_write_pps_on_exit
algorandpa 2023-01-27 18:39:00 -05:00 committed by jaredgoldman
parent a4bd51a01b
commit 9c28d7086e
2 changed files with 25 additions and 16 deletions

View File

@ -30,12 +30,10 @@ from typing import (
Callable, Callable,
) )
import uuid import uuid
import os.path
from bidict import bidict from bidict import bidict
import pendulum import pendulum
import trio import trio
import tractor import tractor
import logging
from .. import data from .. import data
from ..data._source import Symbol from ..data._source import Symbol
@ -43,7 +41,8 @@ from ..data.types import Struct
from ..pp import ( from ..pp import (
Position, Position,
Transaction, Transaction,
open_trade_ledger open_trade_ledger,
open_pps
) )
from ..data._normalize import iterticks from ..data._normalize import iterticks
from ..data._source import unpack_fqsn from ..data._source import unpack_fqsn
@ -82,6 +81,7 @@ class PaperBoi(Struct):
_reqids: bidict _reqids: bidict
_positions: dict[str, Position] _positions: dict[str, Position]
_trade_ledger: dict[str, Any] _trade_ledger: dict[str, Any]
_txn_dict: dict[str, Transaction] = {}
# init edge case L1 spread # init edge case L1 spread
last_ask: tuple[float, float] = (float('inf'), 0) # price, size last_ask: tuple[float, float] = (float('inf'), 0) # price, size
@ -273,13 +273,21 @@ class PaperBoi(Struct):
bsuid=key, bsuid=key,
) )
# Transacupdate ledger per trade # Update in memory ledger per trade
ledger_entry = {} ledger_entry = {}
ledger_entry[oid] = t.to_dict() 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)
# Store txn in state for PP update
self._txn_dict[oid] = t
self._trade_ledger.update(ledger_entry)
# Write to ledger toml
with open_trade_ledger(self.broker, 'paper') as ledger:
ledger.update(self._trade_ledger)
# Write to pps toml
with open_pps(self.broker, 'paper-id') as table:
table.update_from_trans(self._txn_dict)
pp.add_clear(t) pp.add_clear(t)

View File

@ -20,6 +20,7 @@ that doesn't try to cuk most humans who prefer to not lose their moneys..
(looking at you `ib` and dirt-bird friends) (looking at you `ib` and dirt-bird friends)
''' '''
from __future__ import annotations
from contextlib import contextmanager as cm from contextlib import contextmanager as cm
from pprint import pformat from pprint import pformat
import os import os
@ -34,11 +35,13 @@ from typing import (
Union, Union,
Generator Generator
) )
from typing import Generator
import pendulum import pendulum
from pendulum import datetime, now from pendulum import datetime, now
import tomli import tomli
import toml import toml
from . import config from . import config
from .brokers import get_brokermod from .brokers import get_brokermod
from .clearing._messages import BrokerdPosition, Status from .clearing._messages import BrokerdPosition, Status
@ -56,7 +59,7 @@ def open_trade_ledger(
trades: dict[str, Any] trades: dict[str, Any]
) -> Generator[dict, None, None]: ) -> Generator[dict, None, None]:
''' '''
Indempotently create and read in a trade log file from the Indempotently creat0616cbd1e and read in a trade log file from the
``<configuration_dir>/ledgers/`` directory. ``<configuration_dir>/ledgers/`` directory.
Files are named per broker account of the form Files are named per broker account of the form
@ -82,8 +85,6 @@ def open_trade_ledger(
ledger = tomli.load(cf) ledger = tomli.load(cf)
print(f'Ledger load took {time.time() - start}s') print(f'Ledger load took {time.time() - start}s')
cpy = ledger.copy() cpy = ledger.copy()
if trades:
cpy.update(trades)
try: try:
yield cpy yield cpy
finally: finally:
@ -93,8 +94,6 @@ def open_trade_ledger(
print(f'Updating ledger for {tradesfile}:\n') print(f'Updating ledger for {tradesfile}:\n')
ledger.update(cpy) ledger.update(cpy)
print("updated ledger")
print(ledger)
# we write on close the mutated ledger data # we write on close the mutated ledger data
with open(tradesfile, 'w') as cf: with open(tradesfile, 'w') as cf:
toml.dump(ledger, cf) toml.dump(ledger, cf)
@ -543,15 +542,16 @@ class PpTable(Struct):
self, self,
trans: dict[str, Transaction], trans: dict[str, Transaction],
cost_scalar: float = 2, cost_scalar: float = 2,
write_now: bool = False,
) -> dict[str, Position]: ) -> dict[str, Position]:
pps = self.pps pps = self.pps
updated: dict[str, Position] = {} updated: dict[str, Position] = {}
print('TRANSACTIONS')
print(trans.items)
# lifo update all pps from records # lifo update all pps from records
for tid, t in trans.items(): for tid, t in trans.items():
print(t)
pp = pps.setdefault( pp = pps.setdefault(
t.bsuid, t.bsuid,
@ -588,6 +588,7 @@ class PpTable(Struct):
pp.add_clear(t) pp.add_clear(t)
updated[t.bsuid] = pp updated[t.bsuid] = pp
# minimize clears tables and update sizing. # minimize clears tables and update sizing.
for bsuid, pp in updated.items(): for bsuid, pp in updated.items():
pp.ensure_state() pp.ensure_state()
@ -885,7 +886,7 @@ def open_pps(
acctid: str, acctid: str,
write_on_exit: bool = True, write_on_exit: bool = True,
) -> PpTable: ) -> Generator[PpTable, None, None]:
''' '''
Read out broker-specific position entries from Read out broker-specific position entries from
incremental update file: ``pps.toml``. incremental update file: ``pps.toml``.