Volume-Weighted Average Price (VWAP) is the preeminent benchmark used by algorithmic execution desks at Tower Research and HRT to assess execution quality and minimize market impact.
Given a continuous chronological stream of trades, each characterized by a tuple , design an online data structure MovingVWAP that maintains the rolling VWAP over a trailing time window of seconds:
where . If the total traded volume in the window is zero, return .
Your Task
Implement the MovingVWAP class:
__init__(window_seconds: float): Initializes the object with the time window parameter .add_trade(timestamp: float, price: float, volume: int) -> None: Appends a new executed trade. Timestamps are guaranteed to be non-decreasing.get_vwap(current_timestamp: float) -> float: Evicts trades strictly older than and returns the active VWAP rounded to 4 decimal places.Constraints
Examples
vwap = MovingVWAP(window_seconds=10.0)
vwap.add_trade(1.0, 100.0, 10)
vwap.add_trade(5.0, 102.0, 20)
# At t=6.0: (100*10 + 102*20) / (10 + 20) = 3040 / 30 = 101.3333
vwap.get_vwap(6.0) # returns 101.3333
vwap.add_trade(12.0, 105.0, 30)
# At t=12.0: window is [2.0, 12.0]. Trade at t=1.0 is evicted!
# Active trades: t=5.0 (102*20) and t=12.0 (105*30). Sum PV = 2040 + 3150 = 5190, Sum V = 50. VWAP = 103.8
vwap.get_vwap(12.0) # returns 103.8