calculate the exponential moving average ema on the series of stock prices wikipedia reference https en wikipedia orgwikiexponentialsmoothing https www investopedia comtermseema asptocwhatisanexponential movingaverageema exponential moving average is used in finance to analyze changes stock prices ema is used in conjunction with simple moving average sma ema reacts to the changes in the value quicker than sma which is one of the advantages of using ema yields exponential moving averages of the given stock prices tupleexponentialmovingaverageiter2 5 3 8 2 6 9 10 3 2 3 5 3 25 5 725 5 8625 7 43125 8 715625 param stockprices a stream of stock prices param windowsize the number of stock prices that will trigger a new calculation of the exponential average windowsize 0 return yields a sequence of exponential moving averages formula st alpha xt 1 alpha stprev where st exponential moving average at timestamp t xt stock price in from the stock prices at timestamp t stprev exponential moving average at timestamp t1 alpha 21 windowsize smoothing factor exponential moving average ema is a rule of thumb technique for smoothing time series data using an exponential window function calculating smoothing factor exponential average at timestamp t assigning simple moving average till the windowsize for the first time is reached calculating exponential moving average based on current timestamp data point and previous exponential average value yields exponential moving averages of the given stock prices tuple exponential_moving_average iter 2 5 3 8 2 6 9 10 3 2 3 5 3 25 5 725 5 8625 7 43125 8 715625 param stock_prices a stream of stock prices param window_size the number of stock prices that will trigger a new calculation of the exponential average window_size 0 return yields a sequence of exponential moving averages formula st alpha xt 1 alpha st_prev where st exponential moving average at timestamp t xt stock price in from the stock prices at timestamp t st_prev exponential moving average at timestamp t 1 alpha 2 1 window_size smoothing factor exponential moving average ema is a rule of thumb technique for smoothing time series data using an exponential window function calculating smoothing factor exponential average at timestamp t assigning simple moving average till the window_size for the first time is reached calculating exponential moving average based on current timestamp data point and previous exponential average value
from collections.abc import Iterator


def exponential_moving_average(
    stock_prices: Iterator[float], window_size: int
) -> Iterator[float]:
    

    if window_size <= 0:
        raise ValueError("window_size must be > 0")

    
    alpha = 2 / (1 + window_size)

    
    moving_average = 0.0

    for i, stock_price in enumerate(stock_prices):
        if i <= window_size:
            
            
            moving_average = (moving_average + stock_price) * 0.5 if i else stock_price
        else:
            
            
            moving_average = (alpha * stock_price) + ((1 - alpha) * moving_average)
        yield moving_average


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    stock_prices = [2.0, 5, 3, 8.2, 6, 9, 10]
    window_size = 3
    result = tuple(exponential_moving_average(iter(stock_prices), window_size))
    print(f"{stock_prices = }")
    print(f"{window_size = }")
    print(f"{result = }")