From d9dca41f18fc5a2ff48696fee7680996df4a707c Mon Sep 17 00:00:00 2001 From: yenru0 Date: Sat, 9 May 2026 13:02:28 +0900 Subject: [PATCH] modify state --- state.py | 61 ++++++++++++++++---------------------------------------- 1 file changed, 17 insertions(+), 44 deletions(-) diff --git a/state.py b/state.py index 9b41d1d..54832d7 100644 --- a/state.py +++ b/state.py @@ -1,10 +1,8 @@ -from collections import deque - - CONVERSION_FEE = 5 + class StateManager: - def __init__(self, tick_size=20): + def __init__(self): symbols = ["BOND", "VALBZ", "VALE", "GS", "MS", "WFC", "XLF"] self.positions = {s: 0 for s in symbols} self.orders = {s: {} for s in symbols} @@ -12,8 +10,6 @@ class StateManager: self.ask_prices = {s: None for s in symbols} self.bid_depths = {s: 0 for s in symbols} self.ask_depths = {s: 0 for s in symbols} - self.tick_size = tick_size - self.price_ticks = {s: deque(maxlen=tick_size) for s in symbols} self.last_prices = {s: None for s in symbols} self.fair_values = { "BOND": 1000, @@ -24,42 +20,21 @@ class StateManager: "WFC": None, "XLF": None, } - self.last_prices = {} + self.predicted_prices = {s: None for s in symbols} self.etf_components = { "XLF": {"BOND": 3, "GS": 2, "MS": 3, "WFC": 2} } self.etf_shares = {"XLF": 10} - def add_price_tick(self, symbol: str, price: int): - self.price_ticks[symbol].append(price) - self.last_prices[symbol] = price - - def get_price_ticks(self, symbol: str): - return list(self.price_ticks.get(symbol, [])) - def get_last_price(self, symbol: str): return self.last_prices.get(symbol) - def get_avg_price(self, symbol: str): - ticks = self.get_price_ticks(symbol) - if ticks: - return sum(ticks) / len(ticks) - return None - - def get_ma(self, symbol: str, period: int): - ticks = self.get_price_ticks(symbol) - if len(ticks) >= period: - return sum(list(ticks)[-period:]) / period - return None - def update_position(self, symbol: str, quantity: int): self.positions[symbol] = self.positions.get(symbol, 0) + quantity - def update_bid_price(self, symbol: str, price: int): - self.bid_prices[symbol] = price - - def update_ask_price(self, symbol: str, price: int): - self.ask_prices[symbol] = price + def update_bid_ask_price(self, symbol: str, bid_price: int, ask_price: int): + self.bid_prices[symbol] = bid_price + self.ask_prices[symbol] = ask_price def get_position(self, symbol: str) -> int: return self.positions.get(symbol, 0) @@ -71,9 +46,6 @@ class StateManager: return ask - bid return None - def update_last_price(self, symbol: str, price: int): - self.last_prices[symbol] = price - def get_mid_price(self, symbol: str): bid = self.bid_prices[symbol] ask = self.ask_prices[symbol] @@ -91,22 +63,23 @@ class StateManager: return bid_depth - ask_depth def get_predicted_price(self, symbol: str) -> int: - mid_price = self.get_mid_price(symbol) - imbalance = self.get_imbalance(symbol) - if mid_price is not None: - return self.get_last_price(symbol) - + return self.predicted_prices.get(symbol) + + def update_predicted_price(self, symbol: str): + mid = self.get_mid_price(symbol) + if mid is not None: + self.predicted_prices[symbol] = mid def update_fair_value(self): - valbz_price = self.get_mid_price("VALBZ") + valbz_price = self.get_predicted_price("VALBZ") if valbz_price is not None: self.fair_values["VALBZ"] = valbz_price self.fair_values["VALE"] = valbz_price - self.conversion_fee - bond_price = self.get_mid_price("BOND") - gs_price = self.get_mid_price("GS") - ms_price = self.get_mid_price("MS") - wfc_price = self.get_mid_price("WFC") + bond_price = self.get_predicted_price("BOND") + gs_price = self.get_predicted_price("GS") + ms_price = self.get_predicted_price("MS") + wfc_price = self.get_predicted_price("WFC") if all(p is not None for p in [bond_price, gs_price, ms_price, wfc_price]): component_value = ( bond_price * self.etf_components["XLF"]["BOND"]