def get_ema(self, prices: int, parameter: str, shift: int = 0, sma_prices: int = 5, round_value: bool = True, update: bool = True) -> float: """ Returns the exponential moving average with data provided. :param update: Boolean for whether function should call API and get latest data or not. :param shift: Prices shifted from current period. :param round_value: Boolean that specifies whether return value should be rounded :param int sma_prices: SMA prices to get first EMA over :param int prices: Days to iterate EMA over (or the period) :param str parameter: Parameter to get the average of (e.g. open, close, high, or low values) :return: EMA """ if not self.is_valid_average_input(shift, prices, sma_prices): raise ValueError('Invalid average input specified.') elif sma_prices <= 0: raise ValueError("Initial amount of SMA values for initial EMA must be greater than 0.") if not self.data_is_updated(): # Check if data is valid. If not, memoized data will be corrupted. self.ema_dict = {} self.update_data() data = [self.get_current_data()] + self.data if update else self.get_total_non_updated_data() data = data[shift:] ema, self.ema_dict = get_ema(data, prices, parameter, sma_prices, self.ema_dict) if round_value: return round(ema, self.precision) return ema
def get_ema(self, data: list, prices: int, parameter: str, sma_prices: int = 5, round_value: bool = True) -> float: if sma_prices <= 0: raise ValueError("Initial amount of SMA values for initial EMA must be greater than 0.") elif sma_prices > len(data): sma_prices = len(data) - 1 ema, self.ema_dict = get_ema(data, prices, parameter, sma_prices, self.ema_dict, desc=False) return round(ema, self.precision) if round_value else ema
def test_ema(dummy_data: List[Dict[str, float]], prices: int, parameter: str, sma_prices: int, expected: float): ema, _ = get_ema(data=dummy_data, prices=prices, parameter=parameter, sma_prices=sma_prices, desc=False) assert ema == expected
def test_ema(dummy_data: DATA_HINT, prices: int, parameter: str, sma_prices: int, expected: float): """ Test EMA. """ ema, _ = get_ema(data=dummy_data, prices=prices, parameter=parameter, sma_prices=sma_prices, desc=False) assert ema == expected