from datetime import datetime
import csv
import pandas
import os
import sys


os.chdir(sys.argv[1])
ticker_f = open(sys.argv[2], "rb")
ticker_reader = csv.reader(ticker_f)
tickers = [r[0] for r in ticker_reader][1:]
ticker_f.close()
tln = len(tickers)


t_1 = datetime.now()
# build full data frame
res = None
for i, t in enumerate(tickers):
    


res = res.dropna(axis=0, int(sys.argv[3])) # drop many missing obs
res = res.dropna(axis=1, int(sys.argv[4])) # drop many missing vars
res = res.dropna()
res.to_csv(sys.argv[5])

t_n = t.split("/")[1]
    df = pandas.io.parsers.read_csv("%s.csv" % t_n)
    df[t_n] = (df["Close"].shift(1) - df["Close"]) / df["Close"]
    df = df[["Date", t_n]]
    df.set_index("Date")
    if res is None:
        res = df
    else:
        res = res.merge(df, on="Date", how="outer")
    print i, i * 100. / tln, datetime.now() - t_1