File size: 3,316 Bytes
c109682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1654fa
55c7203
 
 
 
 
 
 
c109682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2d2569
c109682
 
 
 
 
 
a2d2569
c109682
 
a2d2569
c109682
 
a2d2569
c109682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2d2569
 
 
 
c109682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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)