Add Generator as return type of open_trade_ledger
							parent
							
								
									a4bd51a01b
								
							
						
					
					
						commit
						9c28d7086e
					
				|  | @ -30,12 +30,10 @@ from typing import ( | |||
|     Callable, | ||||
| ) | ||||
| import uuid | ||||
| import os.path | ||||
| from bidict import bidict | ||||
| import pendulum | ||||
| import trio | ||||
| import tractor | ||||
| import logging | ||||
| 
 | ||||
| from .. import data | ||||
| from ..data._source import Symbol | ||||
|  | @ -43,7 +41,8 @@ from ..data.types import Struct | |||
| from ..pp import ( | ||||
|     Position, | ||||
|     Transaction, | ||||
|     open_trade_ledger | ||||
|     open_trade_ledger, | ||||
|     open_pps | ||||
| ) | ||||
| from ..data._normalize import iterticks | ||||
| from ..data._source import unpack_fqsn | ||||
|  | @ -82,11 +81,12 @@ class PaperBoi(Struct): | |||
|     _reqids: bidict | ||||
|     _positions: dict[str, Position] | ||||
|     _trade_ledger: dict[str, Any] | ||||
|     _txn_dict: dict[str, Transaction] = {} | ||||
| 
 | ||||
|     # init edge case L1 spread | ||||
|     last_ask: tuple[float, float] = (float('inf'), 0)  # price, size | ||||
|     last_bid: tuple[float, float] = (0, 0) | ||||
| 
 | ||||
|          | ||||
|     async def submit_limit( | ||||
|         self, | ||||
|         oid: str,  # XXX: see return value | ||||
|  | @ -273,13 +273,21 @@ class PaperBoi(Struct): | |||
|             bsuid=key, | ||||
|         ) | ||||
| 
 | ||||
|         # Transacupdate ledger per trade | ||||
|         # Update in memory ledger per trade | ||||
|         ledger_entry = {} | ||||
|         ledger_entry[oid] = t.to_dict()  | ||||
|          | ||||
|         # Store txn in state for PP update | ||||
|         self._txn_dict[oid] = t | ||||
|         self._trade_ledger.update(ledger_entry) | ||||
|         with open_trade_ledger('paper', 'paper', self._trade_ledger) as ledger: | ||||
|             log.info(ledger) | ||||
|          | ||||
|         # 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) | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										19
									
								
								piker/pp.py
								
								
								
								
							
							
						
						
									
										19
									
								
								piker/pp.py
								
								
								
								
							|  | @ -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) | ||||
| 
 | ||||
| ''' | ||||
| from __future__ import annotations | ||||
| from contextlib import contextmanager as cm | ||||
| from pprint import pformat | ||||
| import os | ||||
|  | @ -34,11 +35,13 @@ from typing import ( | |||
|     Union, | ||||
|     Generator | ||||
| ) | ||||
| from typing import 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 | ||||
|  | @ -56,7 +59,7 @@ def open_trade_ledger( | |||
|     trades: dict[str, Any]  | ||||
| ) -> 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. | ||||
| 
 | ||||
|     Files are named per broker account of the form | ||||
|  | @ -82,8 +85,6 @@ 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: | ||||
|  | @ -93,8 +94,6 @@ 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) | ||||
|  | @ -543,15 +542,16 @@ class PpTable(Struct): | |||
|         self, | ||||
|         trans: dict[str, Transaction], | ||||
|         cost_scalar: float = 2, | ||||
| 
 | ||||
|         write_now: bool = False, | ||||
|     ) -> dict[str, Position]: | ||||
| 
 | ||||
|         pps = self.pps | ||||
|         updated: dict[str, Position] = {} | ||||
| 
 | ||||
|         print('TRANSACTIONS') | ||||
|         print(trans.items) | ||||
|         # lifo update all pps from records | ||||
|         for tid, t in trans.items(): | ||||
| 
 | ||||
|             print(t) | ||||
|             pp = pps.setdefault( | ||||
|                 t.bsuid, | ||||
| 
 | ||||
|  | @ -587,6 +587,7 @@ class PpTable(Struct): | |||
|             # update clearing table | ||||
|             pp.add_clear(t) | ||||
|             updated[t.bsuid] = pp | ||||
|              | ||||
| 
 | ||||
|         # minimize clears tables and update sizing. | ||||
|         for bsuid, pp in updated.items(): | ||||
|  | @ -885,7 +886,7 @@ def open_pps( | |||
|     acctid: str, | ||||
|     write_on_exit: bool = True, | ||||
| 
 | ||||
| ) -> PpTable: | ||||
| ) -> Generator[PpTable, None, None]: | ||||
|     ''' | ||||
|     Read out broker-specific position entries from | ||||
|     incremental update file: ``pps.toml``. | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue