Spaces:
Runtime error
Runtime error
import pandas as pd | |
from datetime import datetime | |
from datetime import timedelta | |
import yfinance as yf | |
from dataset_creation import nasdaq100_crawling | |
# Execute "nasdaq100_crawling" Module | |
nasdaq_dic = pd.DataFrame(nasdaq100_crawling.get_nasdaq100()) | |
# Get Nasdaq 100 List | |
nasdaq100_symbols = list(nasdaq_dic.ticker) | |
# ๋ฐ๋ชจ์์ ๋ฉ๋ด์์ ํ์ํ ์ ๋ณด๋ค | |
# Ticker, Name, Diff, Open, Close, Sector, Industry, Date | |
def get_list(tickers=nasdaq100_symbols): | |
demo_dic = pd.DataFrame(nasdaq100_crawling.get_nasdaq100()) | |
for i in range(len(demo_dic.index)): | |
ticker = demo_dic.loc[i, 'ticker'] | |
# ticker์ ์ฃผ์ ์ ๋ณด ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค. | |
data = get_data(tickers=[ticker], numOfDay=2)[0] | |
try: | |
yesterday = data.iloc[-2, 3] | |
today = data.iloc[-1, 3] | |
except IndexError: | |
print("Oops! That was no valid number. Try again...") | |
from IPython import embed; embed() | |
demo_dic.loc[i, 'diff'] = round(((today-yesterday)/today) * 100.0, 2) | |
demo_dic.loc[i, 'open'] = round(data.iloc[-1, 0], 2) # Open | |
demo_dic.loc[i, 'close'] = round(data.iloc[-1, 3], 2) # Close | |
date = str(data.index[-1].year) + "-" + str(data.index[-1].month) + "-" + str(data.index[-1].day) | |
demo_dic.loc[i, 'date'] = date # Date | |
return demo_dic | |
# ์ฃผ์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ | |
def get_data(tickers=nasdaq100_symbols, numOfDay=2):#numOfDay: ๋ ์ง ๊ฐ๊ฒฉ | |
output = [] | |
delta = (numOfDay / 7) * 2 | |
# ์์ ๋ ์ง ~ ์ต๊ทผ๊น์ง ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ | |
now = datetime.now() # ์ค๋ ๋ ์ง | |
date = now.weekday() # ์์ผ ํ์ธ | |
if date == 5: # Saturday | |
start_date = datetime(now.year, now.month, now.day, 0, 0) - timedelta(days=numOfDay+4+delta) | |
end_date = datetime(now.year, now.month, now.day, 0, 0) | |
elif date == 6 or date == 0 or date == 1: # Sunday or Monday or Tuesday | |
start_date = datetime(now.year, now.month, now.day, 0, 0) - timedelta(days=numOfDay+5+delta) | |
end_date = datetime(now.year, now.month, now.day, 0, 0) | |
else: # Others | |
start_date = datetime(now.year, now.month, now.day, 0, 0) - timedelta(days=numOfDay+3+delta) | |
end_date = datetime(now.year, now.month, now.day, 0, 0) | |
# Check if it is included in the Nasdaq_100 | |
for ticker in tickers: | |
ticker = ticker.upper() | |
if ticker not in nasdaq100_symbols: | |
print("Nasdaq 100 ์์ ํฌํจ๋์ง ์์ต๋๋ค ") | |
else: | |
print("[ {} Finance Data ]".format(ticker)) | |
ticker_yf = yf.Tickers(ticker) | |
abc = ticker_yf.tickers[ticker].history(start=start_date, end=end_date, period='max') | |
output.append(abc) | |
# print("Output : ", output) | |
# from IPython import embed; embed() | |
if numOfDay != 60 and len(list(output[0].index.values)) < numOfDay: | |
print(numOfDay+numOfDay-len(list(output[0].index.values))) | |
output = get_data(tickers, numOfDay+numOfDay-len(list(output[0].index.values))) | |
return output | |
if __name__ == '__main__': | |
print(get_list()) | |
start_date = datetime(2021,1,1) | |
end_date = datetime(2023,2,3) | |
get_data(['meta'], numOfDay=2) | |
get_data(tickers=nasdaq100_symbols, numOfDay=2) |