'''
pip install pandas,beautifulsoup4,finance-datareader,matplotlib -y
'''
import pandas as pd
import matplotlib.pyplot as plt
import FinanceDataReader as fdr
from datetime import datetime
from dateutil.relativedelta import relativedelta
def get_stock_code(name):
'''이름을 입력하면 코드를 리턴'''
df = fdr.StockListing('KRX')
stock_code = df[df['Name'] == name]['Code'].to_string(index = False)
return stock_code
if __name__ == '__main__':
before_standard = (datetime.now() - relativedelta(years = 2)).strftime('%Y-%m-%d')
df_exchange_rate = fdr.DataReader(symbol = 'USD/KRW', start = before_standard)[['Close']]
df_kospi = fdr.DataReader(symbol = 'KS11', start = before_standard)[['Close']]
df_lg_energy = fdr.DataReader(symbol = get_stock_code('LG에너지솔루션'), start = before_standard)[['Close']]
df_samsung_sdi = fdr.DataReader(symbol = get_stock_code('삼성SDI'), start = before_standard)[['Close']]
df_echoprobm = fdr.DataReader(symbol = get_stock_code('에코프로비엠'), start = before_standard)[['Close']]
df_lnf = fdr.DataReader(symbol = get_stock_code('엘앤에프'), start = before_standard)[['Close']]
df_ski_tech = fdr.DataReader(symbol = get_stock_code('SK아이이테크놀로지'), start = before_standard)[['Close']]
df = pd.concat([df_exchange_rate, df_kospi, df_lg_energy, df_samsung_sdi, df_echoprobm, df_lnf, df_ski_tech], axis = 1, join = 'inner')
df.columns = ['USD/KRW', 'KOSPI', 'LG_ENSOL', 'SS_SDI', 'ECHOPROBM', 'LNF', 'SKI_TECH']
df.reset_index(inplace = True)
df_copy = df.copy()
df_2_year = df_copy[df_copy['Date'] > str(datetime.now() - relativedelta(years = 2))]
df_1_year = df_copy[df_copy['Date'] > str(datetime.now() - relativedelta(years = 1))]
df_9_month = df_copy[df_copy['Date'] > str(datetime.now() - relativedelta(months = 9))]
df_6_month = df_copy[df_copy['Date'] > str(datetime.now() - relativedelta(months = 6))]
df_3_month = df_copy[df_copy['Date'] > str(datetime.now() - relativedelta(months = 3))]
df_1_month = df_copy[df_copy['Date'] > str(datetime.now() - relativedelta(months = 1))]
stock_list = ['USD/KRW', 'KOSPI', 'LG_ENSOL', 'SS_SDI', 'ECHOPROBM', 'LNF', 'SKI_TECH']
plt.figure(figsize=(20, 10))
plt.subplot(3,2,1)
for stock in stock_list:
plt.plot(df_2_year['Date'], df_2_year[stock].to_list(), label = stock, linewidth = '1')
plt.grid(True)
plt.legend(fontsize = 6)
plt.subplot(3,2,2)
for stock in stock_list:
plt.plot(df_1_year['Date'], df_1_year[stock].to_list(), label = stock, linewidth = '1')
plt.grid(True)
plt.legend(fontsize = 6)
plt.subplot(3,2,3)
for stock in stock_list:
plt.plot(df_9_month['Date'], df_9_month[stock].to_list(), label = stock, linewidth = '1')
plt.grid(True)
plt.legend(fontsize = 6)
plt.subplot(3,2,4)
for stock in stock_list:
plt.plot(df_6_month['Date'], df_6_month[stock].to_list(), label = stock, linewidth = '1')
plt.grid(True)
plt.legend(fontsize = 6)
plt.subplot(3,2,5)
for stock in stock_list:
plt.plot(df_3_month['Date'], df_3_month[stock].to_list(), label = stock, linewidth = '1')
plt.grid(True)
plt.legend(fontsize = 6)
plt.subplot(3,2,6)
for stock in stock_list:
plt.plot(df_1_month['Date'], df_1_month[stock].to_list(), label = stock, linewidth = '1')
plt.grid(True)
plt.legend(fontsize = 6)
plt.show()
'Programming Language > Python' 카테고리의 다른 글
[Conda] 윈도우에 64bit 아나콘다 설치하는 방법 (0) | 2023.02.08 |
---|---|
[Python] 은행관련 주식 그래프 그리는 방법 (0) | 2023.02.07 |
[Python] 주식을 종가 기준으로 그래프 그리는 방법 (0) | 2023.02.07 |
[Python] matplotlib 그래프 여러개 그리는 방법 (0) | 2023.02.07 |
[Python] ImportError: cannot import name 'MappingProxyType' from 'types' 에러해결 (0) | 2023.02.07 |