Files
JSETC/state.py
2026-05-09 14:04:16 +09:00

95 lines
3.4 KiB
Python

CONVERSION_FEE = 5
class StateManager:
def __init__(self):
symbols = ["BOND", "VALBZ", "VALE", "GS", "MS", "WFC", "XLF"]
self.bid_prices = {s: None for s in symbols}
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.last_prices = {s: None for s in symbols}
self.fair_values = {
"BOND": 1000,
"VALBZ": None,
"VALE": None,
"GS": None,
"MS": None,
"WFC": None,
"XLF": None,
}
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 set_last_price(self, symbol: str, price: int):
self.last_prices[symbol] = price
def get_last_price(self, symbol: str):
return self.last_prices.get(symbol)
def update_position(self, symbol: str, quantity: int):
self.positions[symbol] = self.positions.get(symbol, 0) + quantity
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)
def get_spread(self, symbol: str) -> int:
bid = self.bid_prices.get(symbol)
ask = self.ask_prices.get(symbol)
if bid is not None and ask is not None:
return ask - bid
return None
def get_mid_price(self, symbol: str):
bid = self.bid_prices[symbol]
ask = self.ask_prices[symbol]
if bid is not None and ask is not None:
return (bid + ask) // 2
return self.last_prices.get(symbol)
def update_depth(self, symbol: str, bid_depth: int, ask_depth: int):
self.bid_depths[symbol] = bid_depth
self.ask_depths[symbol] = ask_depth
def get_imbalance(self, symbol: str) -> int:
bid_depth = self.bid_depths[symbol]
ask_depth = self.ask_depths[symbol]
return bid_depth - ask_depth
def get_predicted_price(self, symbol: str) -> int:
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.last_prices.get("VALBZ")
if valbz_price is not None:
self.fair_values["VALBZ"] = valbz_price
self.fair_values["VALE"] = valbz_price
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"]
+ gs_price * self.etf_components["XLF"]["GS"]
+ ms_price * self.etf_components["XLF"]["MS"]
+ wfc_price * self.etf_components["XLF"]["WFC"]
) / self.etf_shares["XLF"]
self.fair_values["XLF"] = component_value
def get_fair_value(self, symbol: str) -> int:
return self.fair_values.get(symbol)