def calc_ema(smoothed, new_data, N=22):
    """
    INPUT:
        smoothed (Series) - Last record's smoothed EMA value (a copy)
        new_data (Series) - New values, prepended with last value
        N (int) - Number of time periods for smoothing
    OUTPUT:
        Exponential Moving Average as a pandas Series
    """

    K = 2/(N + 1)    # coefficient for incoming datum
    J = 1 - K        # coefficient for fading data

  # if first smoothed, use last data value
    if not len(smoothed.index):
        smoothed[new_data.index.values[0]] = new_data[0]

    for n,index in enumerate(new_data[1:].index, start=1):
        smoothed[index] = K*new_data[n] + J*smoothed[n-1]

    return smoothed