<s> import pandas as pd <EOL> import os <EOL> import time <EOL> from datetime import datetime <EOL> import re <EOL> from time import mktime <EOL> import matplotlib <EOL> import matplotlib . pyplot as plt <EOL> from matplotlib import style <EOL> style . use ( "<STR_LIT>" ) <EOL> path = "<STR_LIT>" <EOL> def Key_Stats ( gather = "<STR_LIT>" ) : <EOL> statspath = path + '<STR_LIT>' <EOL> stock_list = [ x [ <NUM_LIT:0> ] for x in os . walk ( statspath ) ] <EOL> df = pd . DataFrame ( <EOL> columns = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> ) <EOL> sp500_df = pd . DataFrame . from_csv ( "<STR_LIT>" ) <EOL> ticker_list = [ ] <EOL> for each_dir in stock_list [ <NUM_LIT:1> : <NUM_LIT> ] : <EOL> each_file = os . listdir ( each_dir ) <EOL> ticker = os . path . basename ( os . path . normpath ( each_dir ) ) <EOL> ticker_list . append ( ticker ) <EOL> starting_stock_value = False <EOL> starting_sp500_value = False <EOL> if len ( each_file ) > <NUM_LIT:0> : <EOL> for file in each_file : <EOL> date_stamp = datetime . strptime ( file , '<STR_LIT>' ) <EOL> unix_time = time . mktime ( date_stamp . timetuple ( ) ) <EOL> full_file_path = each_dir + '<STR_LIT:/>' + file <EOL> source = open ( full_file_path , '<STR_LIT:r>' ) . read ( ) <EOL> try : <EOL> try : <EOL> value = float ( source . split ( gather + '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ) <EOL> except Exception as e : <EOL> try : <EOL> value = float ( source . split ( gather + '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ) <EOL> except Exception as e : <EOL> pass <EOL> try : <EOL> sp500_date = datetime . fromtimestamp ( unix_time ) . strftime ( '<STR_LIT>' ) <EOL> row = sp500_df [ ( sp500_df . index == sp500_date ) ] <EOL> sp500_value = float ( row [ "<STR_LIT>" ] ) <EOL> except : <EOL> sp500_date = datetime . fromtimestamp ( unix_time - <NUM_LIT> ) . strftime ( '<STR_LIT>' ) <EOL> row = sp500_df [ ( sp500_df . index == sp500_date ) ] <EOL> sp500_value = float ( row [ "<STR_LIT>" ] ) <EOL> try : <EOL> stock_price = float ( source . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ) <EOL> except Exception as e : <EOL> try : <EOL> stock_price = ( source . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ) <EOL> stock_price = re . search ( r'<STR_LIT>' , stock_price ) <EOL> stock_price = float ( stock_price . group ( <NUM_LIT:1> ) ) <EOL> except Exception as e : <EOL> try : <EOL> stock_price = ( source . split ( '<STR_LIT>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ) <EOL> stock_price = re . search ( r'<STR_LIT>' , stock_price ) <EOL> stock_price = float ( stock_price . group ( <NUM_LIT:1> ) ) <EOL> except Exception as e : <EOL> print ( str ( e ) , '<STR_LIT>' , file , ticker ) <EOL> if not starting_stock_value : <EOL> starting_stock_value = stock_price <EOL> if not starting_sp500_value : <EOL> starting_sp500_value = sp500_value <EOL> stock_p_change = ( ( stock_price - starting_stock_value ) / starting_stock_value ) * <NUM_LIT:100> <EOL> sp500_p_change = ( ( sp500_value - starting_sp500_value ) / starting_sp500_value ) * <NUM_LIT:100> <EOL> df = df . append ( { '<STR_LIT>' : date_stamp , <EOL> '<STR_LIT>' : unix_time , <EOL> '<STR_LIT>' : ticker , <EOL> '<STR_LIT>' : value , <EOL> '<STR_LIT>' : stock_price , <EOL> '<STR_LIT>' : stock_p_change , <EOL> '<STR_LIT>' : sp500_value , <EOL> '<STR_LIT>' : sp500_p_change , <EOL> '<STR_LIT>' : stock_p_change - sp500_p_change } , <EOL> ignore_index = True <EOL> ) <EOL> except Exception as e : <EOL> pass <EOL> for each_ticker in ticker_list : <EOL> try : <EOL> plot_df = df [ ( df [ '<STR_LIT>' ] == each_ticker ) ] <EOL> plot_df = plot_df . set_index ( [ '<STR_LIT>' ] ) <EOL> plot_df [ '<STR_LIT>' ] . plot ( label = each_ticker ) <EOL> plt . legend ( ) <EOL> except : <EOL> pass <EOL> plt . show ( ) <EOL> save = gather . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . replace ( '<STR_LIT:)>' , '<STR_LIT>' ) . replace ( '<STR_LIT:(>' , '<STR_LIT>' ) . replace ( '<STR_LIT:/>' , '<STR_LIT>' ) + ( '<STR_LIT>' ) <EOL> print ( save ) <EOL> df . to_csv ( save ) <EOL> Key_Stats ( ) </s>
