import pandas as pd
from stock.analysis.Common import Common
import plotly.graph_objs as go
from plotly import tools, subplots
import plotly.io as po

class BolingerBand:

    common = None

    def __init__(self):
        self.common = Common()
        return

    def apply(self, df, n=13, t=2):
        # 입력받은 값이 dataframe이라는 것을 정의해줌
        df = pd.DataFrame(df)

        max20 = df["close"].rolling(window=n).mean()
        stddev = df["close"].rolling(window=n).std()
        upper = max20 + (stddev * t) # 상단 볼리저 밴드
        lower = max20 - (stddev * t) # 하단 볼리저 밴드
        middle = (upper + lower ) / 2

        # dataframe에 컬럼 추가
        df = df.assign(upper=upper, middle=middle, lower=lower)

        return df

    def analyze(self, stock):
        df = pd.DataFrame()
        df = df.from_dict(stock['PRICE'])
        df = self.apply(df)

        for i in range(len(df.upper)):
            stock['PRICE'][i]['bolingerband_upper'] = df.upper.values[i]
            stock['PRICE'][i]['bolingerband_middle'] = df.middle.values[i]
            stock['PRICE'][i]['bolingerband_lower'] = df.lower.values[i]

        return