from pathlib import Path

from A_Global import path_stock
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import numpy as np

'''画Introducion的关系图'''
if __name__ == '__main__':
    market      = 'NASDAQ'
    start_date  = '2018-01-02'
    end_date    = '2018-12-31'
    plt.figure(figsize=(8,4))

    plt.subplot(121)
    s1 = 'FB.O'
    s2 = 'GOOG.O'

    path_dir = path_stock / ('stocks_' + market);
    df1 = pd.read_csv(path_dir / s1, index_col = 0);
    df2 = pd.read_csv(path_dir / s2, index_col = 0);
    close1 = df1.loc[start_date: end_date, ['CLOSE']]
    close2 = df2.loc[start_date: end_date, ['CLOSE']]
    idx = np.arange(len(close1.index))

    close1 = MinMaxScaler().fit_transform(close1)
    close2 = MinMaxScaler().fit_transform(close2)
    close1 = close1.reshape(251)
    close2 = close2.reshape(251)

    line1, = plt.plot(idx, close1)
    line2, = plt.plot(idx, close2)
    lines = [line1, line2]

    plt.legend(handles=lines, labels=[s1, s2], loc='upper left')
    plt.xlabel('Trading Days')
    plt.ylabel('Price')
    plt.title('a')

    plt.subplot(122)
    s1 = 'NXPI.O'
    s2 = 'XEL.O'

    path_dir = path_stock / ('stocks_' + market);
    df1 = pd.read_csv(path_dir / s1, index_col=0);
    df2 = pd.read_csv(path_dir / s2, index_col=0);
    close1 = df1.loc[start_date: end_date, ['CLOSE']]
    close2 = df2.loc[start_date: end_date, ['CLOSE']]
    idx = np.arange(len(close1.index))

    close1 = MinMaxScaler().fit_transform(close1)
    close2 = MinMaxScaler().fit_transform(close2)
    close1 = close1.reshape(251)
    close2 = close2.reshape(251)

    line1, = plt.plot(idx, close1)
    line2, = plt.plot(idx, close2)
    lines = [line1, line2]

    plt.legend(handles=lines, labels=[s1, s2], loc=[0.5, 0.82])

    plt.xlabel('Trading Days') # , fontsize=10
    plt.ylabel('Price') # , fontsize=10
    plt.title('b')

    plt.savefig(Path('./') / 'SG-Relations-Intro', dpi=300, bbox_inches='tight')
    plt.show()
