1 '''
2 Created on Feb 25, 2011
3
4 @note: This assumes that all the CSV files will have exactly 7 columns. It ignores the first row in the csv files because it it the heading.
5 All the data is converted from csv to pkl
6 @author: Shreyas Joshi
7 @contact: shreyasj@gatech.edu
8 @summary: This is used to convert CSV files from norgate to pkl files- which are used when the simulator runs.
9 '''
10
11 import numpy as np
12 import pickle as pkl
13 import qstkutil.utils as utils
14 import os
15 import dircache
16 import time
17 import sys
18
19
21
22 print "Starting..."+ str(time.strftime("%H:%M:%S"))
23
24 try:
25 rootdir = os.environ['QSDATA']
26 except KeyError:
27
28 print "Please be sure to set the value for QSDATA in config.sh or local.sh\n"
29
30 fileExtensionToRemove = ".csv"
31
32 listOfInputPaths= list()
33
34
35
36
37 listOfInputPaths.append (rootdir + "/Raw/Yahoo/US/AMEX/")
38 listOfInputPaths.append (rootdir + "/Raw/Yahoo/US/NASDAQ/")
39 listOfInputPaths.append (rootdir + "/Raw/Yahoo/US/NYSE/")
40
41
42
43 listOfOutputPaths= list()
44
45
46
47
48 listOfOutputPaths.append(rootdir + "/Processed/Yahoo/US/AMEX/")
49 listOfOutputPaths.append(rootdir + "/Processed/Yahoo/US/NASDAQ/")
50 listOfOutputPaths.append(rootdir + "/Processed/Yahoo/US/NYSE/")
51
52
53 for path in listOfOutputPaths:
54 if not (os.access(path, os.F_OK)):
55
56 os.makedirs(path)
57
58
59
60 utils.clean_paths (listOfOutputPaths)
61
62
63 if (len(listOfInputPaths)!= len(listOfOutputPaths)):
64 print "No. of input paths not equal to the number of output paths.. quitting"
65 sys.exit("FAILURE")
66
67
68 path_ctr = -1;
69 use_cols = range (1, 7 + 1)
70 for path in listOfInputPaths:
71 path_ctr = path_ctr + 1;
72 stocks_at_this_path = dircache.listdir(str(path))
73
74 filtered_names= filter (lambda x:(str(x).find(str(fileExtensionToRemove)) > -1), stocks_at_this_path)
75
76 filtered_names = map(lambda x:(x.partition(str(fileExtensionToRemove))[0]),filtered_names)
77 stock_ctr = -1
78 for stock in filtered_names:
79 stock_ctr = stock_ctr + 1
80 print "Reading file: " + str (path + stock)
81
82 stock_data= np.loadtxt (path + stock+".csv", np.float, None, ",", None, 1, use_cols)
83
84 stock_data_shape = stock_data.shape
85
86
87
88
89
90
91
92 f = open (listOfOutputPaths[path_ctr] + filtered_names[stock_ctr] + ".pkl", "wb" )
93 pkl.dump (stock_data, f, -1)
94 f.close()
95
96
97 print "Finished..."+ str(time.strftime("%H:%M:%S"))
98
99
100
101
102 if __name__ == '__main__':
103 main()
104