Coverage for source/indicators/donchain_channels_indicator.py: 100%
10 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-30 15:13 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-30 15:13 +0000
1# indicators/donchain_channels_indicator.py
3from .indicator_base import *
5class DonchainChannelsIndicatorHandler(IndicatorHandlerBase):
6 """
7 Implements donchain channels indicator. It indicates the highest and the
8 lowest price for the certain period, creating so called channel. It also
9 calculates average price as the average of the lowest and the highest price.
10 """
12 def __init__(self, window_size: int = 20) -> None:
13 """
14 Class constructor.
16 Parameters:
17 window_size (int): Length of window that indicator should be applied over.
18 """
20 self.window_size = window_size
22 def calculate(self, data: pd.DataFrame) -> pd.DataFrame:
23 """
24 Calculates donchain channels indicator values for given data.
26 Parameters:
27 data (pd.DataFrame): Data frame with input data.
29 Returns:
30 (pd.DataFrame): Output data with calculated donchain channels values.
31 """
33 donchian_df = pd.DataFrame(index = data.index)
34 donchian_df['upper_channel'] = data['high'].rolling(window = self.window_size, min_periods = 1).max()
35 donchian_df['lower_channel'] = data['low'].rolling(window = self.window_size, min_periods = 1).min()
36 donchian_df['middle_channel'] = (donchian_df['upper_channel'] + donchian_df['lower_channel']) / 2
38 return donchian_df