PageRenderTime 86ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/python/algo_local/readAllSymbols.py

https://bitbucket.org/vroomzel/semester2
Python | 65 lines | 49 code | 8 blank | 8 comment | 2 complexity | eb249e75da28889922e6e9be9e248d50 MD5 | raw file
  1. import os
  2. import sys
  3. import datetime as dt
  4. import pandas
  5. import numpy as np
  6. def load_data_frame_from_hdf5(fullname):
  7. store = pandas.HDFStore(fullname, mode='r')
  8. output = store["data_frame"]
  9. store.close()
  10. return output
  11. os.putenv("TZ","America/New_York")
  12. #just one column of symbols
  13. symbol_list_file = sys.argv[1]
  14. start=dt.datetime(2011,1,2,9,30)
  15. end=dt.datetime(2012,4,13,15,59)
  16. symbol_list = open(symbol_list_file, 'rb')
  17. #create a list of tickers (we'll need them anyway later)
  18. tickers=[]
  19. for symbol in symbol_list:
  20. tickers.append(symbol.rstrip('\n'))
  21. symbol_list.close()
  22. #read the first symbol and create starting data frames from it
  23. hdf5FileName = "/backtest_data/daily-1min/tbFrame1min/" + tickers[0] + ".tb.frame.1min.hdf5"
  24. print hdf5FileName
  25. data=load_data_frame_from_hdf5(hdf5FileName)
  26. cp=pandas.DataFrame(data['cp'].ix[start:end])
  27. op=pandas.DataFrame(data['op'].ix[start:end])
  28. vol=cp=pandas.DataFrame(data['vol'].ix[start:end])
  29. #load all other tickers
  30. count=1
  31. for symbol in tickers[1:]:
  32. hdf5FileName = "/backtest_data/daily-1min/tbFrame1min/" + symbol + ".tb.frame.1min.hdf5"
  33. print hdf5FileName
  34. data=load_data_frame_from_hdf5(hdf5FileName)
  35. cp=pandas.merge(cp, pandas.DataFrame(data['cp'].ix[start:end]), left_index=True, right_index=True, how='outer')
  36. op=pandas.merge(op, pandas.DataFrame(data['op'].ix[start:end]), left_index=True, right_index=True, how='outer')
  37. vol=pandas.merge(vol, pandas.DataFrame(data['vol'].ix[start:end]), left_index=True, right_index=True, how='outer')
  38. col_ind=np.arange(0,count+1)
  39. cp.columns=col_ind
  40. op.columns=col_ind
  41. vol.columns=col_ind
  42. count=count+1
  43. #rename the columns by tickers
  44. cp.columns=tickers
  45. op.columns=tickers
  46. vol.columns=tickers
  47. path="/backtest_data/1second/teams_data/team3/data/"
  48. cp.save(path+"close_px_all.bin")
  49. op.save(path +"open_px_all.bin")
  50. vol.save(path+"volume_all.bin")
  51. '''
  52. #for some reason the first symbol closing prices get screwed up
  53. #so reload AAPL
  54. hdf5FileName = "/backtest_data/daily-1min/tbFrame1min/AAPL.tb.frame.1min.hdf5"
  55. data=load_data_frame_from_hdf5(hdf5FileName)
  56. cp['AAPL']=data['cp']
  57. '''