Problems to solve
-data ranges
-multiple stocks
-align dates
-proper date order
Building a dataframe
'''Build a dataframe in pandas''' import pandas as pd def test_run(): start_date='2010-01-22' end_date='2010-01-26' dates=pd.date_range(start_date,end_date) print dates if __name__ == "__main__": test_run()
'''Build a dataframe in pandas''' import pandas as pd def test_run(): start_date='2010-01-22' end_date='2010-01-26' dates=pd.date_range(start_date,end_date) #Create an empty dataframe df1=pd.DataFrame(index=dates) #Read SPY data into temporary dataframe dfSPY = pd.read_csv("data/SPY.csv",index_col="Date",parse_dates=True) print dfSPY #Join the two dataframes using DataFrame.join() #df1=df1.join(dfSPY) #print df1 if __name__ == "__main__": test_run()
"""Utility functions""" import os import pandas as pd def symbol_to_path(symbol, base_dir="data"): """Return CSV file path given ticker symbol.""" return os.path.join(base_dir, "{}.csv".format(str(symbol))) def get_data(symbols, dates): """Read stock data (adjusted close) for given symbols from csv files""" df = pd.DataFrame(index=dates) if 'SPY' not in symbols: symbols.insert(0, 'SPY') for symbol in symbols: return df def test_run(): # Define a data range dates = pd.date_range('2010-01-22','2010-01-26') # Choose stock symbols to read symbols = ['GOOG', 'IBM', 'GLD'] # Get stock data df = get_data(symbols, dates) print df if __name__ == "__main__": test_run()