Coverage for source/indicators/ema_indicator_handler.py: 55%

11 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-07-30 20:59 +0000

1# indicators/ema_indicator_handler.py 

2 

3# global imports 

4import pandas as pd 

5 

6# local imports 

7from source.indicators import IndicatorHandlerBase 

8 

9class ExponentialMovingAverageIndicatorHandler(IndicatorHandlerBase): 

10 """ 

11 Implements Exponential Moving Average (EMA) indicator. It gives more weight to recent prices 

12 and reacts more quickly to price changes than a simple moving average. 

13 """ 

14 

15 def __init__(self, window_size: int = 20) -> None: 

16 """ 

17 Class constructor. 

18 

19 Parameters: 

20 window_size (int): Length of window that indicator should be applied over. 

21 """ 

22 

23 self.__window_size = window_size 

24 

25 def calculate(self, data: pd.DataFrame) -> pd.DataFrame: 

26 """ 

27 Calculates Exponential Moving Average (EMA) indicator values for given data. 

28 

29 Parameters: 

30 data (pd.DataFrame): Data frame with input data. 

31 

32 Returns: 

33 (pd.DataFrame): Output data with calculated EMA values. 

34 """ 

35 

36 ema_data_df = pd.DataFrame(index = data.index) 

37 ema_data_df['ema'] = data['close'].ewm(span = self.__window_size, adjust = False).mean() 

38 

39 return ema_data_df 

40 

41 def can_be_normalized(self) -> bool: 

42 """ 

43 Checks if the indicator can be normalized. 

44 

45 Returns: 

46 (bool): True if the indicator can be normalized, False otherwise. 

47 """ 

48 

49 return True