change open_trade_ledger typing to return a Generator type

paper_trade_improvements_rebase
algorandpa 2023-02-09 15:19:50 -05:00 committed by jaredgoldman
parent af4fb59fe8
commit 91492d7771
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
@ -247,8 +237,6 @@ 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:
@ -92,9 +93,12 @@ def open_trade_ledger(
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):