modify state
This commit is contained in:
61
state.py
61
state.py
@@ -1,10 +1,8 @@
|
|||||||
from collections import deque
|
|
||||||
|
|
||||||
|
|
||||||
CONVERSION_FEE = 5
|
CONVERSION_FEE = 5
|
||||||
|
|
||||||
|
|
||||||
class StateManager:
|
class StateManager:
|
||||||
def __init__(self, tick_size=20):
|
def __init__(self):
|
||||||
symbols = ["BOND", "VALBZ", "VALE", "GS", "MS", "WFC", "XLF"]
|
symbols = ["BOND", "VALBZ", "VALE", "GS", "MS", "WFC", "XLF"]
|
||||||
self.positions = {s: 0 for s in symbols}
|
self.positions = {s: 0 for s in symbols}
|
||||||
self.orders = {s: {} 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.ask_prices = {s: None for s in symbols}
|
||||||
self.bid_depths = {s: 0 for s in symbols}
|
self.bid_depths = {s: 0 for s in symbols}
|
||||||
self.ask_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.last_prices = {s: None for s in symbols}
|
||||||
self.fair_values = {
|
self.fair_values = {
|
||||||
"BOND": 1000,
|
"BOND": 1000,
|
||||||
@@ -24,42 +20,21 @@ class StateManager:
|
|||||||
"WFC": None,
|
"WFC": None,
|
||||||
"XLF": None,
|
"XLF": None,
|
||||||
}
|
}
|
||||||
self.last_prices = {}
|
self.predicted_prices = {s: None for s in symbols}
|
||||||
self.etf_components = {
|
self.etf_components = {
|
||||||
"XLF": {"BOND": 3, "GS": 2, "MS": 3, "WFC": 2}
|
"XLF": {"BOND": 3, "GS": 2, "MS": 3, "WFC": 2}
|
||||||
}
|
}
|
||||||
self.etf_shares = {"XLF": 10}
|
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):
|
def get_last_price(self, symbol: str):
|
||||||
return self.last_prices.get(symbol)
|
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):
|
def update_position(self, symbol: str, quantity: int):
|
||||||
self.positions[symbol] = self.positions.get(symbol, 0) + quantity
|
self.positions[symbol] = self.positions.get(symbol, 0) + quantity
|
||||||
|
|
||||||
def update_bid_price(self, symbol: str, price: int):
|
def update_bid_ask_price(self, symbol: str, bid_price: int, ask_price: int):
|
||||||
self.bid_prices[symbol] = price
|
self.bid_prices[symbol] = bid_price
|
||||||
|
self.ask_prices[symbol] = ask_price
|
||||||
def update_ask_price(self, symbol: str, price: int):
|
|
||||||
self.ask_prices[symbol] = price
|
|
||||||
|
|
||||||
def get_position(self, symbol: str) -> int:
|
def get_position(self, symbol: str) -> int:
|
||||||
return self.positions.get(symbol, 0)
|
return self.positions.get(symbol, 0)
|
||||||
@@ -71,9 +46,6 @@ class StateManager:
|
|||||||
return ask - bid
|
return ask - bid
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def update_last_price(self, symbol: str, price: int):
|
|
||||||
self.last_prices[symbol] = price
|
|
||||||
|
|
||||||
def get_mid_price(self, symbol: str):
|
def get_mid_price(self, symbol: str):
|
||||||
bid = self.bid_prices[symbol]
|
bid = self.bid_prices[symbol]
|
||||||
ask = self.ask_prices[symbol]
|
ask = self.ask_prices[symbol]
|
||||||
@@ -91,22 +63,23 @@ class StateManager:
|
|||||||
return bid_depth - ask_depth
|
return bid_depth - ask_depth
|
||||||
|
|
||||||
def get_predicted_price(self, symbol: str) -> int:
|
def get_predicted_price(self, symbol: str) -> int:
|
||||||
mid_price = self.get_mid_price(symbol)
|
return self.predicted_prices.get(symbol)
|
||||||
imbalance = self.get_imbalance(symbol)
|
|
||||||
if mid_price is not None:
|
def update_predicted_price(self, symbol: str):
|
||||||
return self.get_last_price(symbol)
|
mid = self.get_mid_price(symbol)
|
||||||
|
if mid is not None:
|
||||||
|
self.predicted_prices[symbol] = mid
|
||||||
|
|
||||||
def update_fair_value(self):
|
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:
|
if valbz_price is not None:
|
||||||
self.fair_values["VALBZ"] = valbz_price
|
self.fair_values["VALBZ"] = valbz_price
|
||||||
self.fair_values["VALE"] = valbz_price - self.conversion_fee
|
self.fair_values["VALE"] = valbz_price - self.conversion_fee
|
||||||
|
|
||||||
bond_price = self.get_mid_price("BOND")
|
bond_price = self.get_predicted_price("BOND")
|
||||||
gs_price = self.get_mid_price("GS")
|
gs_price = self.get_predicted_price("GS")
|
||||||
ms_price = self.get_mid_price("MS")
|
ms_price = self.get_predicted_price("MS")
|
||||||
wfc_price = self.get_mid_price("WFC")
|
wfc_price = self.get_predicted_price("WFC")
|
||||||
if all(p is not None for p in [bond_price, gs_price, ms_price, wfc_price]):
|
if all(p is not None for p in [bond_price, gs_price, ms_price, wfc_price]):
|
||||||
component_value = (
|
component_value = (
|
||||||
bond_price * self.etf_components["XLF"]["BOND"]
|
bond_price * self.etf_components["XLF"]["BOND"]
|
||||||
|
|||||||
Reference in New Issue
Block a user