"""
=========================================
CV6 AI Trading OS
Bollinger Bands Indicator
=========================================
"""

import pandas as pd


class BollingerBandsIndicator:

    @staticmethod
    def calculate(
        prices,
        period: int = 20,
        std_dev: int = 2
    ):

        prices = pd.Series(prices)

        sma = prices.rolling(period).mean()
        std = prices.rolling(period).std()

        upper = sma + (std * std_dev)
        lower = sma - (std * std_dev)

        return {
            "middle": sma,
            "upper": upper,
            "lower": lower
        }

    @staticmethod
    def latest(
        prices,
        period: int = 20,
        std_dev: int = 2
    ):

        result = BollingerBandsIndicator.calculate(
            prices,
            period,
            std_dev
        )

        return {
            "upper": round(result["upper"].iloc[-1], 2),
            "middle": round(result["middle"].iloc[-1], 2),
            "lower": round(result["lower"].iloc[-1], 2)
        }