Exemplo n.º 1
0
 def test_companyDF(self):
     from pyEX import companyDF
     with patch('requests.get') as mock:
         mock.return_value = MagicMock()
         mock.return_value.status_code = 200
         mock.return_value.json = MagicMock(return_value={'test': [4], 'symbol': ['test']})
         companyDF('test')
Exemplo n.º 2
0
    def test_companyDF(self):
        from pyEX import companyDF

        with patch("requests.get") as mock, patch("pickle.dump"):
            mock.return_value = MagicMock()
            mock.return_value.status_code = 200
            mock.return_value.json = MagicMock(
                return_value={"test": [4], "symbol": ["test"]}
            )
            companyDF("test")
Exemplo n.º 3
0
    def get_co_sector_industry_tags(self, row):

        ticker = row.symbol
        self.logger.info(
            "IEXTradingApi.get_co_sector_industry_tags(): getting sector, industry, tags for ticker %s",
            ticker)
        df = pyex.companyDF(symbol=ticker)
        co_sector_industry_tags = (
            df.loc[ticker,
                   ['sector', 'industry', 'tags', 'companyName', 'website']])
        co_sector_industry_tags['symbol'] = ticker
        # co_sector_industry_tags is a pandas Series
        # access the symbol (ticker) by using ".name" of pd.Series object co_sector_industry_tags
        alt_industries = co_sector_industry_tags['tags']
        rows_to_insert = []
        for alt_industry in alt_industries:
            if alt_industry != co_sector_industry_tags['sector'] and \
                    alt_industry != co_sector_industry_tags['industry']:
                rows_to_insert.append(
                    [co_sector_industry_tags['sector'], alt_industry])
        rows_to_insert.append([
            co_sector_industry_tags['sector'],
            co_sector_industry_tags['industry']
        ])
        for sector_industry in rows_to_insert:
            if self.master_sector_indusry_df.index.size == 0:
                next_index = 0
            else:
                next_index = self.master_sector_indusry_df.index[-1] + 1
            if sector_industry[0] != '' and sector_industry[1] != '':
                self.master_sector_indusry_df.loc[next_index] = sector_industry
        print(self.master_sector_indusry_df)
Exemplo n.º 4
0
def refetch(field, symbol):
    if field == 'TICK':
        return p.chartDF(symbol, '1d')
    if field == 'FINANCIALS':
        return p.financialsDF(symbol)
    elif field == 'DAILY':
        return p.chartDF(symbol, '5y')
    elif field == 'COMPANY':
        return p.companyDF(symbol)
    elif field == 'EARNINGS':
        return p.earningsDF(symbol)
    elif field == 'DIVIDENDS':
        return p.dividendsDF(symbol)
    elif field == 'NEWS':
        return p.newsDF(symbol)
    elif field == 'STATS':
        return p.stockStatsDF(symbol)
    elif field == 'COMPOSITION':
        return _fetchComposition(symbol)
    elif field == 'PEERS':
        return p.peersDF(symbol)
    raise NotImplementedError('%s - %s' % (field, symbol))
Exemplo n.º 5
0
 def test_companyDF(self):
     from pyEX import companyDF
     companyDF(C)
Exemplo n.º 6
0
companies = {company: {} for company in companies}

for company in topCompanies:
    companies[company['name']]['symbol'] = company['symbol']
    companies[company['name']]['industry_id'] = company['industry_id']

arbSymbol = 'AAPL'

# arbitrarily create df object
# get stock history for last month
arbChart = p.chartDF(arbSymbol, timeframe='1m')
# add stock symbol to df to act as a foreign key for company table
arbChart['symbol'] = "Arbitrary"
arbChart['industry_id'] = 0
# get company info
arbCompany = p.companyDF(arbSymbol)
# join the 2 tables and add to rest of stocks df
stocks_df = pd.merge(arbChart, arbCompany, how='left', on=['symbol'])

for company in companies.values():
    symbol = company['symbol']
    industry_id = company['industry_id']
    # get stock history for last 1 month
    chart_df = p.chartDF(symbol, timeframe='1y')
    # add stock symbol to df to act as a foreign key for company table
    chart_df['symbol'] = symbol
    # add our unique identifier for industry to the table
    chart_df['industry_id'] = industry_id
    # get company info
    company_df = p.companyDF(symbol)
    # join the 2 tables and add to rest of stocks df
Exemplo n.º 7
0
 def test_companyDF(self):
     from pyEX import companyDF
     with patch('requests.get') as mock:
         mock.return_value = MagicMock()
         mock.return_value.status_code = 200
         companyDF('test')
Exemplo n.º 8
0
    def fetchDF(self, key, field, _ret=True):
        # tickers always caps
        key = key.upper()

        # fields always lower
        field = field.lower()

        if not (self._tickers['symbol'] == key).any():
            # FIXME
            return pd.DataFrame()

        if key not in self._cache:
            # initialize cache
            self._cache[key] = {}
            self._cache[key]['timestamp'] = {}

        if field in ('financials', 'all'):
            if 'financials' not in self._cache[key] or self._check_timestamp(
                    key, 'financials'):
                try:
                    self._cache[key]['financials'] = p.financialsDF(key)
                except KeyError:
                    self._cache[key]['financials'] = pd.DataFrame()
                self._cache[key]['timestamp']['financials'] = datetime.now()

        if field in ('chart', 'all'):
            if 'chart' not in self._cache[key] or self._check_timestamp(
                    key, 'chart'):
                try:
                    self._cache[key]['chart'] = p.chartDF(key, '1y')
                except KeyError:
                    self._cache[key]['chart'] = pd.DataFrame()

                self._cache[key]['timestamp']['chart'] = datetime.now()

        if field in ('company', 'all'):
            if 'company' not in self._cache[key] or self._check_timestamp(
                    key, 'company'):
                self._cache[key]['company'] = p.companyDF(key)
                self._cache[key]['timestamp']['company'] = datetime.now()

        if field in ('quote', 'all'):
            # always update
            self._cache[key]['quote'] = p.quoteDF(key)

        if field in ('dividends', 'all'):
            if 'dividends' not in self._cache[key] or self._check_timestamp(
                    key, 'dividends'):
                try:
                    self._cache[key]['dividends'] = p.dividendsDF(key)
                except KeyError:
                    self._cache[key]['dividends'] = pd.DataFrame()
                self._cache[key]['timestamp']['dividends'] = datetime.now()

        if field in ('earnings', 'all'):
            if 'earnings' not in self._cache[key] or self._check_timestamp(
                    key, 'earnings'):
                try:
                    self._cache[key]['earnings'] = p.earningsDF(key)
                except KeyError:
                    self._cache[key]['earnings'] = pd.DataFrame()
                self._cache[key]['timestamp']['earnings'] = datetime.now()

        if field in ('news', 'all'):
            if 'news' not in self._cache[key] or self._check_timestamp(
                    key, 'news'):
                try:
                    self._cache[key]['news'] = p.newsDF(key)
                except KeyError:
                    self._cache[key]['news'] = pd.DataFrame()
                self._cache[key]['timestamp']['news'] = datetime.now()

        if field in ('peers', 'all'):
            if 'peers' not in self._cache[key] or self._check_timestamp(
                    key, 'peers'):
                try:
                    peers = p.peersDF(key)
                except KeyError:
                    peers = pd.DataFrame()

                if peers is not None and not peers.empty:
                    peers = peers.replace({np.nan: None})
                    infos = pd.concat(
                        [p.companyDF(item) for item in peers['symbol'].values])
                    self._cache[key]['peers'] = infos
                else:
                    self._cache[key]['peers'] = pd.DataFrame()
                self._cache[key]['timestamp']['peers'] = datetime.now()

        if field in ('stats', 'all'):
            if 'stats' not in self._cache[key] or self._check_timestamp(
                    key, 'stats'):
                try:
                    self._cache[key]['stats'] = p.stockStatsDF(key)
                except KeyError:
                    self._cache[key]['stats'] = pd.DataFrame()
                self._cache[key]['timestamp']['stats'] = datetime.now()

        if field in ('composition', 'all'):
            if 'company' not in self._cache[key]:
                self.fetchDF(key, 'company', _ret=False)

            try:
                self._cache[key]['composition'] = pd.read_html(
                    ETF_URL % key, attrs={'id': 'etfs-that-own'})[0]
                self._cache[key]['composition']['% of Total'] = self._cache[
                    key]['composition']['% of Total'].str.rstrip('%').astype(
                        float) / 100.0
                self._cache[key]['composition'].columns = [
                    'Symbol', 'Name', 'Percent'
                ]
                self._cache[key]['composition'] = self._cache[key][
                    'composition'][['Symbol', 'Percent', 'Name']]

            except (IndexError, requests.HTTPError, ValueError, HTTPError):
                self._cache[key]['composition'] = pd.DataFrame()

            self._cache[key]['timestamp']['composition'] = datetime.now()

        if _ret:
            # pull data
            if field == 'all':
                ret = copy.deepcopy(self._cache[key])
                del ret['timestamp']
                ret = pd.concat(ret)

            elif field in self._cache[key]:
                # or i have that field
                ret = pd.concat({field: self._cache[key][field]})
            else:
                raise Exception('No ticker provided!')

            return ret