Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Sep 2 13:26:48 2016
  4.  
  5. @author: Brian Christopher, CFA [Blackarbs LLC]
  6. """
  7.  
  8. import time
  9. import pandas as pd
  10. import numpy as np
  11. from copy import copy
  12. from tqdm import tqdm
  13. import logging
  14.  
  15. from _blk_barchart_options_scraper_ import barchart_options
  16. p = print
  17. # ------------------------------------------ \\\\
  18. ETFS = {
  19. 'Large Cap' :['SPY','IVV','VOO','IWB'],
  20. 'Mid Cap' :['MDY','IJH','VO','IWR'],
  21. 'Small Cap' :['IWM','IJR','VB'],
  22. 'Global Equity' :['VEU','ACWI','VXUS','DGT'],
  23. 'AsiaPac Equity' :['EWT','EWY','EWA','EWS','AAXJ','FXI','EWH','EWM','EPI','INDA','RSX'],
  24. 'Europe Equity' :['FEZ','EZU','VGK','HEDJ','EWU','EWI','EWP','EWQ','EWL','EWD'],
  25. 'Emerging | Frontier' :['EWZ','EWW','ECH','GAF','FM','EEM','VWO'],
  26. 'Real Estate' :['RWO','RWX','RWR','IYR','VNQ'],
  27. 'Consumer Discretionary':['XLY','XRT','FXD','VCR','RTH','IYC'],
  28. 'Consumer Staples' :['XLP','FXG','VDC','ECON','IYK'],
  29. 'Energy' :['XLE','IPW','XOP','VDE','IYE','IXC','OIH'],
  30. 'Financials' :['XLF','KBE','KIE','IYG','KRE'],
  31. 'Healthcare' :['XLV','XBI','IBB'],
  32. 'Industrial' :['XLI','IYT','VIS','IYJ'],
  33. 'Materials' :['XLB','XHB','XME','IGE','MOO','LIT','GUNR'],
  34. 'Technology' :['XLK','SMH','HACK','FDN'],
  35. 'Telecom' :['IYZ','IXP','VOX'],
  36. 'Utilities' :['IDU','XLU','VPU'],
  37. 'Oil | Gas' :['UNG','BNO','OIL'],
  38. 'Precious Metals' :['GLD','SLV','IAU'],
  39. 'Bonds' :['BND','AGG','JNK','LQD'],
  40. 'T-Bond' :['TLT','IEF','IEI','SHY','BIL','MINT'],
  41. 'Precious Metals Miners':['SIL','GDX','GDXJ','PLTM']
  42. }
  43. # ------------------------------------------ \\\\
  44. today = pd.datetime.today().date().strftime('%m-%d-%y')
  45.  
  46. datapath = r"..\YOUR_PROJECT_DIR\Barchart_Options_Data\\"
  47. logpath = datapath + r'Barchart_OptionsData_LogFiles\\'
  48.  
  49. logging.basicConfig(filename=logpath + 'BRC_Options_Log_{}.log'.format(today),
  50. format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',
  51. level=logging.INFO)
  52. # ------------------------------------------ \\\\
  53. etfstore = pd.HDFStore(datapath + 'ETF_options_data_{}.h5'.format(today))
  54. etf_missing_symbols = []
  55.  
  56. etf_val_count = 0
  57. for i in ETFS.values(): etf_val_count += len(i)
  58. etf_sym_count = etf_val_count
  59. N = copy(etf_sym_count)
  60. # ------------------------------------------ \\\\
  61. for category, symbols in tqdm(ETFS.items()):
  62. logging.info("---------- {} ---------".format(category))
  63. # \\\
  64. for i, symbol in tqdm(enumerate(symbols, start=1)):
  65. N -= 1
  66. # \\\
  67. if not pd.isnull(symbol):
  68. try:
  69. brc = barchart_options(symbol)
  70. expirys = brc._extract_expiry_dates()
  71. appended_data = []
  72. # \\\
  73. for expiry in tqdm(expirys):
  74. grk = brc._get_greeks_src(symbol, expiry)
  75. calls = brc._create_calls_df(grk, expiry)
  76. puts = brc._create_puts_df(grk, expiry)
  77. mrg = pd.concat([calls, puts])
  78. appended_data.append(mrg)
  79. # \\\
  80. data = pd.concat(appended_data)
  81. logging.info(data.describe())
  82. etfstore.put(symbol, data, format='table')
  83. except Exception as e:
  84. logging.error("ERROR: {}".format(e), exc_info=True)
  85. etf_missing_symbols.append(symbol)
  86. continue
  87. pct_total_left = (N / etf_sym_count)
  88. logging.info('{}..[done] | {} of {} ETF symbols collected | {:>.2%}'.
  89. format(symbol, i, len(symbols), pct_total_left))
  90. p('{}..[done] | {} of {} ETF symbols collected | {:>.2%}'.
  91. format(symbol, i, len(symbols), pct_total_left))
  92. time.sleep(np.random.choice(np.random.uniform(0.5,1.5, [3]), p=[.7, .2, .1]))
  93. # ------------------------------------------ \\\\
  94. etfstore.close()
  95. N_etf_errors = len(etf_missing_symbols)
  96. etf_error_rate = N_etf_errors / etf_sym_count
  97. logging.info('etf missing symbols:\n{}'.format(etf_missing_symbols))
  98. logging.info('etf error rate: {} errors / {} symbols = {:3.2%}'
  99. .format(N_etf_errors, etf_sym_count, etf_error_rate))
  100. # \\\\ ___________________________________________________________________ \\\\
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement