Guest User

Untitled

a guest
May 27th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. import pandas as pd
  2. import pandas_datareader.data as web
  3. from datetime import datetime, timedelta, date
  4. from pandas_datareader._utils import RemoteDataError
  5.  
  6. #============================================================
  7. '''
  8. Get list of tickers (stock symbols) from companylist.csv
  9. '''
  10. def getTickerList(ticker_file):
  11. return pd.read_csv(ticker_file)['Symbol']
  12.  
  13.  
  14. '''
  15. Download daily stock prices from Quandl or Morningstar
  16. '''
  17. def downloadSingleStock(ticker,database,filepath):
  18. start = datetime(2000,1,1)
  19. end = date.today()
  20. prices = web.DataReader(ticker, database, start, end)
  21. out_filename = filepath + ticker + '.csv'
  22. prices.to_csv(out_filename)
  23.  
  24. '''
  25. Download daily stock prices fro multiple stocks
  26. '''
  27. def downloadMultipleStocks(ticker_list,database,filepath):
  28. for ticker in ticker_list:
  29. try:
  30. downloadSingleStock(ticker,database,filepath)
  31. except RemoteDataError:
  32. pass
  33.  
  34. '''
  35. Collect stock symbols in specific dirs
  36. '''
  37. def collectStockName(filepath):
  38. stock_list=[]
  39. for root, dirs, files in os.walk(filepath):
  40. if files:
  41. for f in files:
  42. if 'csv' in f:
  43. stock_list.append(f.split('.csv')[0])
  44. return stock_list
  45.  
  46.  
  47. #==================================================================
  48.  
  49. '''
  50. Please change filepaths here.
  51. '''
  52.  
  53. ticker_file = 'D:/companylist.csv'
  54. database = 'quandl'
  55. filepath = 'D:/data_quandl/'
  56.  
  57. database2 = 'morningstar'
  58. filepath2 = 'D:/data_morningstar'
  59.  
  60. #====================================================================
  61.  
  62. '''
  63. Download stock prices data from Quandl
  64. '''
  65.  
  66. ticker_list = getTickerList(ticker_file)
  67. downloadMultipleStocks(ticker_list,database,filepath)
  68.  
  69. '''
  70. Get stock names inaccessible to Quandl
  71. '''
  72.  
  73. download_quandl = collectStockName(filepath)
  74. unsucess_quandl = [ticker for ticker in ticker_list and not in download_quandl]
  75.  
  76.  
  77.  
  78. #====================================================================
  79.  
  80. '''
  81. Use Morningstar database to download prices data of stocks not accessible to Quandl
  82. '''
  83. downloadMultipleStocks(unsucess_quandl,database2,filepath2)
  84.  
  85. '''
  86. Get stock names inaccessible to Morningstar
  87. '''
  88.  
  89. download_morningstar = collectStockName(filepath2)
  90. unsucess_morningstar = [ticker for ticker in unsucess_quandl and not in download_morningstar]
Add Comment
Please, Sign In to add comment