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



col_dict = {
    'r1d': 'CLOSE',
    'r5d': 'avg_5',
    'r20d': 'avg_20',
    'r60d': 'avg_60'
}
plt.rcParams['axes.facecolor']='f4b183'
def pic_close_2stocks(market, col, s1, s2, title, start_date, end_date, i):
    plt.figure(figsize=(4, 3))
    ax = plt.axes()
    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, [col]]
    close2 = df2.loc[start_date: end_date, [col]]
    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(close1, color='red')
    line2, = plt.plot(close2, color='black')
    lines = [line1, line2]

    ax.axes.xaxis.set_visible(False)
    ax.axes.yaxis.set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)

    # plt.xlabel('Trade Days', fontsize=10)
    # plt.ylabel('Price', fontsize=10)
    # plt.title(title)
    plt.savefig(Path('./') / (s1[:-2] + '-' + s2[:-2] + '-' + title), dpi=300, bbox_inches='tight')
    plt.show()


'''
画实验一最后的不同关系股票股价变化图

'''
if __name__ == '__main__':
    stock_pair_arr = [

        ('r20d', ('NXPI.O', 'XEL.O'),''),
        ('r1d', ('NXPI.O', 'ADI.O'), '')
    ]


    for i in range(len(stock_pair_arr)):
        e = stock_pair_arr[i]
        pic_close_2stocks('NASDAQ', col_dict[e[0]], e[1][0], e[1][1], e[2], '2018-01-02', '2018-12-31', i)
    # plt.savefig(Path('./') / 'SG-Relations-3', dpi=300, bbox_inches='tight')
    # plt.show()
