def __init__(self): self.base_currency = "USDT" self.coins_filename = "spot_tickers" self.coins: Set[str] = set(read_from_json(self.coins_filename)) self.binance: Client = Binance() self.spot_order_history: List[Dict[str, Any]] = [] self.spot_trades: Dict[str, List[Dict[str, Any]]] = defaultdict(list) self.spot_balance: Dict[str, Dict[str, Any]] = [] self.ticker_prices: Dict[str, Dict[str, str]] = defaultdict(dict)
def write_portfolio_stats(trade_history_filename:str) -> None: ''' write portfolio statistics to a json file ''' trade_history_dict: Dict[str, List[Dict[str, Any]]] = defaultdict(list) for spot_trade in read_from_json(trade_history_filename): if spot_trade["status"] == "FILLED": trade_history_dict[spot_trade["symbol"]].append(resolve_spot_trade(spot_trade)) write_to_json(trade_history_dict, "portfolio_stats")
def write_portfolio_summary(portfolio_stats_filename: str) -> None: ''' write portfolio summary to a json file and an excel file ''' portfolio_summary = [] portfolio_stats = read_from_json(portfolio_stats_filename) for ticker, trades in portfolio_stats.items(): ticker_summary = create_ticker_summary(ticker, trades) portfolio_summary.append(ticker_summary) portfolio_summary = resolve_portfolio_summary_old(portfolio_summary) write_to_json(portfolio_summary, "portfolio_summary") write_to_excel(portfolio_summary, "portfolio_summary")
def write_trade_history(symbols: List[str]) -> None: ''' write the trade history of symbol pairs to a json file and excel file ''' trade_history: List[Dict[str, Any]] = [] for symbol in symbols: symbol_order_history = binance.get_all_orders(symbol) trade_history.extend(symbol_order_history) format_trade_history(trade_history) filename = 'spot_order_history' full_trade_history: List[Dict[str, Any]] = read_from_json(filename) reduce_trade_history(full_trade_history, trade_history) write_to_json(full_trade_history, filename) write_to_excel(full_trade_history, filename)
def _write_spot_order_history(self) -> None: ''' write the spot trade history of symbol pairs to a json file and excel file ''' log(LogLevel.INFO, "Starting spot trade order history update.") for symbol in self.coins: log(LogLevel.INFO, "Fetching spot order history for symbol: ", symbol) symbol_order_history = self.binance.get_all_orders(symbol + self.base_currency) self.spot_order_history.extend(symbol_order_history) format_trade_history(self.spot_order_history) filename = 'spot_order_history' log(LogLevel.INFO, "Fetching old trade order history.") full_trade_history: List[Dict[str, Any]] = read_from_json(filename) reduce_trade_history(full_trade_history, self.spot_order_history) self.spot_order_history = full_trade_history write_to_json(full_trade_history, filename) write_to_excel(full_trade_history, filename) log(LogLevel.INFO, "Success updating spot trade order history.")