Skip to main content
2 de 2
etiquetas editadas
Alfabravo
  • 9.9k
  • 5
  • 24
  • 32

¿Cómo reducir la velocidad de las consultas de la API de una libreria?

Estoy tratando de acceder a una biblioteca de precios de acciones con una máquina virtual de Google, sin embargo tengo la impresión de que esta es demasiado poderosa. Cuando hago mi solicitud, la envía:

Traceback (most recent call last):
  File "/home/mikempc3/.local/lib/python3.5/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/mikempc3/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 846, in urlopen
    **response_kw
  File "/home/mikempc3/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 846, in urlopen
    **response_kw
  File "/home/mikempc3/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 846, in urlopen
    **response_kw
  File "/home/mikempc3/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 846, in urlopen
    **response_kw
  File "/home/mikempc3/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 846, in urlopen
    **response_kw
  File "/home/mikempc3/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 819, in urlopen
    retries = retries.increment(method, url, response=response, _pool=self)
  File "/home/mikempc3/.local/lib/python3.5/site-packages/urllib3/util/retry.py", line 436, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='query2.finance.yahoo.com', port=443): Max retries excee
ded with url: /v8/finance/chart/APC?interval=1d&events=div%2Csplit&period2=1596585600&corsDomain=finance.yahoo.com&
region=US&lang=en-US&formatted=false&period1=1451865600 (Caused by ResponseError('too many 404 error responses',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "minimum_variance_portfolio_maker.py", line 74, in <module>
    main()  
  File "minimum_variance_portfolio_maker.py", line 58, in main
    df = price_maker(tickers)
  File "minimum_variance_portfolio_maker.py", line 12, in price_maker
    df = ticker.history(period='max', interval='1d', start='2016-01-04', end='2020-08-05')
  File "/home/mikempc3/.local/lib/python3.5/site-packages/yahooquery/ticker.py", line 955, in history
    data = self._get_data('chart', params)
  File "/home/mikempc3/.local/lib/python3.5/site-packages/yahooquery/base.py", line 378, in _get_data
    urls = self._construct_urls(config, params)
  File "/home/mikempc3/.local/lib/python3.5/site-packages/yahooquery/base.py", line 421, in _construct_urls
    for symbol in self._symbols]
  File "/home/mikempc3/.local/lib/python3.5/site-packages/yahooquery/base.py", line 421, in <listcomp>
    for symbol in self._symbols]
  File "/home/mikempc3/.local/lib/python3.5/site-packages/requests/sessions.py", line 543, in get
    return self.request('GET', url, **kwargs)
  File "/home/mikempc3/.local/lib/python3.5/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/mikempc3/.local/lib/python3.5/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/home/mikempc3/.local/lib/python3.5/site-packages/requests/adapters.py", line 507, in send
    raise RetryError(e, request=request)
requests.exceptions.RetryError: HTTPSConnectionPool(host='query2.finance.yahoo.com', port=443): Max retries exceede
d with url: /v8/finance/chart/APC?interval=1d&events=div%2Csplit&period2=1596585600&corsDomain=finance.yahoo.com&re
gion=US&lang=en-US&formatted=false&period1=1451865600 (Caused by ResponseError('too many 404 error responses',))
mikempc3@instance-1:~$ 

Aqui esta el mio codigo:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from yahooquery import Ticker
#import scipy.optimize as scoplt.style.use('fivethirtyeight')
np.random.seed(777)
def price_maker(tickers):
    df_concated = pd.DataFrame()
    for ticker_name in tickers:
        ticker = Ticker(ticker_name)
        df = ticker.history(period='max', interval='1d', start='2016-01-04', end='2020-08-05')
        try:
            df_concated = pd.concat([df_concated, df['close'].rename(str(ticker_name))], axis=1)
        except KeyError:
            print("ticker_name: ", ticker_name)
    return df_concated

def main():
    df_red = pd.read_csv('C:/Users/antoi/Documents/Programming/portfolio-advisor/dashboard/data/tickers_september_2017_red.csv')
    tickers = df_red['Ticker'].values
    df = price_maker(tickers)

if __name__ == '__main__':
    main()  

Sin embargo, funciona bien cuando soy de aquí. ¿Qué opinas? Supongo que es porque mi máquina virtual es demasiado rápida. ¿Cómo puedo reducir su velocidad de consulta si ese es el caso?