def get_prices(request, date, full): timestamp = get_timestamp(date) old_timestamp = -2208988800 if not full: old_timestamp = get_timestamp(get_business_day(date, -10)) url = (f'{base_url}v8/finance/chart/{request.ticker}.SA?' f'period1={old_timestamp}&period2={timestamp}&interval=1d') try: key = f'{request.ticker}_price' response_cache = cache.get(key) if response_cache: return response_cache response = (http_caller.get(url=url, timeout=timeout, parse_json=True)['chart']['result'][0]) response_quotes = response['indicators']['quote'][0] quotes = dict() for index, value in enumerate(response['timestamp']): quote = QuoteDataclass.from_yahoo(request.ticker, response_quotes, index, value) if quote: quotes[quote.date] = quote cache[key] = quotes return quotes except ClientException as exc: logging.error('Erro ao realizar consulta') raise exc
def metrics(): try: stocks = http_caller.get(url=f'{base_url}get/ravensp_stocks', timeout=timeout, parse_json=True) setups = http_caller.get(url=f'{base_url}get/ravensp_setups', timeout=timeout, parse_json=True) errors = http_caller.get(url=f'{base_url}get/ravensp_errors', timeout=timeout, parse_json=True) return (f'stocks: {stocks}\n' f'setups: {setups}\n' f'errors: {errors}') except ClientException as exc: logging.exception(exc)
def send_message(html): url = f'{base_url}{token}/sendMessage' params = {'chat_id': '@ravenspalerts', 'parse_mode': 'html', 'text': html} try: return http_caller.get(url=url, timeout=timeout, params=params) except ClientException as exc: logging.exception(exc) raise exc
def get_prices(request, date, full): time_series = 'TIME_SERIES_DAILY' selector = 'Time Series (Daily)' if request.time_frame == 'weekly': time_series = 'TIME_SERIES_WEEKLY' selector = 'Weekly Time Series' url = ( base_url + f'?function={time_series}&symbol={request.ticker}.SAO&apikey={token}') if full: url += '&outputsize=full' try: key = f'{request.ticker}_price' response_cache = cache.get(key) if response_cache: return response_cache response = (http_caller.get(url=url, timeout=timeout, parse_json=True)[selector]) quotes = dict() for date, stock in response.items(): quote = QuoteDataclass.from_alphavantage(request.ticker, stock, date) if quote: quotes[quote.date] = quote cache[key] = quotes return quotes except ClientException as exc: logging.error('Erro ao realizar consulta') raise exc
def health(): try: return http_caller.get(url=f'{base_url}health', timeout=timeout) except ClientException as exc: logging.exception(exc) raise exc
def count_errors(): try: return http_caller.get(url=f'{base_url}hit/ravensp_errors', timeout=timeout) except ClientException as exc: logging.exception(exc)