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 ( from ..pp import (
Position, Position,
Transaction, Transaction,
open_trade_ledger
) )
from ..data._normalize import iterticks from ..data._normalize import iterticks
from ..data._source import unpack_fqsn from ..data._source import unpack_fqsn
@ -86,17 +87,6 @@ class PaperBoi(Struct):
last_ask: tuple[float, float] = (float('inf'), 0) # price, size last_ask: tuple[float, float] = (float('inf'), 0) # price, size
last_bid: tuple[float, float] = (0, 0) 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( async def submit_limit(
self, self,
oid: str, # XXX: see return value oid: str, # XXX: see return value
@ -246,9 +236,7 @@ class PaperBoi(Struct):
) )
log.info(f'Fake filling order:\n{fill_msg}') log.info(f'Fake filling order:\n{fill_msg}')
await self.ems_trades_stream.send(fill_msg) await self.ems_trades_stream.send(fill_msg)
self._trade_ledger.update(fill_msg.to_dict())
if order_complete: if order_complete:
msg = BrokerdStatus( msg = BrokerdStatus(
reqid=reqid, reqid=reqid,
@ -284,7 +272,15 @@ class PaperBoi(Struct):
dt=pendulum.from_timestamp(fill_time_s), dt=pendulum.from_timestamp(fill_time_s),
bsuid=key, 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.add_clear(t)
pp_msg = BrokerdPosition( pp_msg = BrokerdPosition(

View File

@ -32,13 +32,13 @@ from typing import (
Iterator, Iterator,
Optional, Optional,
Union, Union,
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
@ -53,8 +53,8 @@ log = get_logger(__name__)
def open_trade_ledger( def open_trade_ledger(
broker: str, broker: str,
account: str, account: str,
trades: dict[str, Any]
) -> dict: ) -> Generator[dict, None, None]:
''' '''
Indempotently create and read in a trade log file from the Indempotently create and read in a trade log file from the
``<configuration_dir>/ledgers/`` directory. ``<configuration_dir>/ledgers/`` directory.
@ -82,7 +82,8 @@ 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:
@ -91,10 +92,13 @@ def open_trade_ledger(
# https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries # https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries
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)
return
class Transaction(Struct, frozen=True): class Transaction(Struct, frozen=True):