-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactor_cache.py
More file actions
22 lines (16 loc) · 940 Bytes
/
factor_cache.py
File metadata and controls
22 lines (16 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from functools import lru_cache
@lru_cache(maxsize=128)
def compute_moving_average(symbol: str, window: int, price_data: tuple) -> float:
"""Compute the moving average of the given price data over a specified window size.
Args:
symbol (str): The identifier for the financial instrument.
window (int): The number of periods over which to calculate the moving average.
price_data (tuple): A tuple of numeric price values, with the most recent data at the end.
Returns:
float: The computed moving average value.
Raises:
ValueError: If the length of price_data is less than the specified window size.
This function is typically used to obtain the latest moving average for a symbol to support caching or further analysis."""
if len(price_data) < window:
raise ValueError("Not enough data for the specified window size")
return sum(price_data[-window:]) / window