"""
=========================================
CV6 AI Trading OS
VWAP Indicator
=========================================
"""

import pandas as pd


class VWAPIndicator:

    @staticmethod
    def calculate(data: pd.DataFrame):

        df = data.copy()

        typical_price = (
            df["High"] +
            df["Low"] +
            df["Close"]
        ) / 3

        cumulative_tp_volume = (
            typical_price * df["Volume"]
        ).cumsum()

        cumulative_volume = (
            df["Volume"]
        ).cumsum()

        df["VWAP"] = (
            cumulative_tp_volume /
            cumulative_volume
        )

        return df

    @staticmethod
    def latest(data: pd.DataFrame):

        result = VWAPIndicator.calculate(data)

        return round(
            result["VWAP"].iloc[-1],
            2
        )