|
| 1 | +import pandas as pd |
| 2 | +import os |
| 3 | +import time |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +# path = "X:/Backups/intraQuarter" # for Windows with X files :) |
| 7 | +# if git clone'ed then use relative path, |
| 8 | +# assuming you extracted the downloaded zip into this project's folder: |
| 9 | +path = "intraQuarter" |
| 10 | + |
| 11 | +def Key_Stats(gather="Total Debt/Equity (mrq)"): |
| 12 | + statspath = path+'/_KeyStats' |
| 13 | + stock_list = [x[0] for x in os.walk(statspath)] |
| 14 | + df = pd.DataFrame( |
| 15 | + columns = [ |
| 16 | + 'Date', |
| 17 | + 'Unix', |
| 18 | + 'Ticker', |
| 19 | + 'DE Ratio', |
| 20 | + 'Price', |
| 21 | + 'stock_p_change', |
| 22 | + 'SP500', |
| 23 | + 'sp500_p_change' |
| 24 | + ] |
| 25 | + ) |
| 26 | + sp500_df = pd.DataFrame.from_csv("YAHOO-INDEX_GSPC.csv") |
| 27 | + |
| 28 | + ticker_list = [] |
| 29 | + |
| 30 | + for each_dir in stock_list[1:25]: |
| 31 | + each_file = os.listdir(each_dir) |
| 32 | + # ticker = each_dir.split("\\")[1] # Windows only |
| 33 | + # ticker = each_dir.split("/")[1] # this didn't work so do this: |
| 34 | + ticker = os.path.basename(os.path.normpath(each_dir)) |
| 35 | + # print(ticker) # uncomment to verify |
| 36 | + ticker_list.append(ticker) |
| 37 | + |
| 38 | + starting_stock_value = False |
| 39 | + starting_sp500_value = False |
| 40 | + if len(each_file) > 0: |
| 41 | + for file in each_file: |
| 42 | + date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html') |
| 43 | + unix_time = time.mktime(date_stamp.timetuple()) |
| 44 | + full_file_path = each_dir+'/'+file |
| 45 | + source = open(full_file_path,'r').read() |
| 46 | + try: |
| 47 | + value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0]) |
| 48 | + |
| 49 | + try: |
| 50 | + sp500_date = datetime.fromtimestamp(unix_time).strftime('%Y-%m-%d') |
| 51 | + row = sp500_df[(sp500_df.index == sp500_date)] |
| 52 | + sp500_value = float(row["Adjusted Close"]) |
| 53 | + except: |
| 54 | + sp500_date = datetime.fromtimestamp(unix_time-259200).strftime('%Y-%m-%d') |
| 55 | + row = sp500_df[(sp500_df.index == sp500_date)] |
| 56 | + sp500_value = float(row["Adjusted Close"]) |
| 57 | + |
| 58 | + stock_price = float(source.split('</small><big><b>')[1].split('</b></big>')[0]) |
| 59 | + #print("stock_price:",stock_price,"ticker:", ticker) |
| 60 | + |
| 61 | + if not starting_stock_value: |
| 62 | + starting_stock_value = stock_price |
| 63 | + if not starting_sp500_value: |
| 64 | + starting_sp500_value = sp500_value |
| 65 | + stock_p_change = ((stock_price - starting_stock_value) / starting_stock_value) * 100 |
| 66 | + sp500_p_change = ((sp500_value - starting_sp500_value) / starting_sp500_value) * 100 |
| 67 | + df = df.append({'Date':date_stamp, |
| 68 | + 'Unix':unix_time, |
| 69 | + 'Ticker':ticker, |
| 70 | + 'DE Ratio':value, |
| 71 | + 'Price':stock_price, |
| 72 | + 'stock_p_change':stock_p_change, |
| 73 | + 'SP500':sp500_value, |
| 74 | + 'sp500_p_change':sp500_p_change}, |
| 75 | + ignore_index = True |
| 76 | + ) |
| 77 | + except Exception as e: |
| 78 | + pass |
| 79 | + #print(str(e)) |
| 80 | + |
| 81 | + save = gather.replace(' ','').replace(')','').replace('(','').replace('/','')+('.csv') |
| 82 | + print(save) |
| 83 | + df.to_csv(save) |
| 84 | + |
| 85 | +Key_Stats() |
0 commit comments