From af297824a6124c1ec62f0912db3b4c48cd197e2d Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Wed, 2 Jun 2021 22:49:54 +0200 Subject: [PATCH] alphavantage: request compact data if full data isn't needed. --- src/pricehist/sources/alphavantage.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/pricehist/sources/alphavantage.py b/src/pricehist/sources/alphavantage.py index b468527..082bc27 100644 --- a/src/pricehist/sources/alphavantage.py +++ b/src/pricehist/sources/alphavantage.py @@ -1,5 +1,6 @@ import csv import dataclasses +from datetime import datetime, timedelta import json import logging import os @@ -137,7 +138,7 @@ class AlphaVantage(BaseSource): params = { "function": "TIME_SERIES_DAILY_ADJUSTED", "symbol": series.base, - "outputsize": "full", + "outputsize": self._outputsize(series.start), "apikey": self._apikey(), } response = self.log_curl(requests.get(self.QUERY_URL, params=params)) @@ -159,7 +160,7 @@ class AlphaVantage(BaseSource): "function": "FX_DAILY", "from_symbol": series.base, "to_symbol": series.quote, - "outputsize": "full", + "outputsize": self._outputsize(series.start), "apikey": self._apikey(), } response = self.log_curl(requests.get(self.QUERY_URL, params=params)) @@ -170,6 +171,13 @@ class AlphaVantage(BaseSource): } return normalized_data + def _outputsize(self, start): + almost_100_days_ago = (datetime.now().date() - timedelta(days=95)).isoformat() + if start < almost_100_days_ago: + return "full" + else: + return "compact" + def _digital_data(self, series): params = { "function": "DIGITAL_CURRENCY_DAILY",