Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added commodities methods and tests #71

Merged
merged 3 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alphavantage_api_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from alphavantage_api_client.client import AlphavantageClient
from alphavantage_api_client.models import GlobalQuote, Quote, AccountingReport, CompanyOverview, RealGDP, \
CsvNotSupported, TickerSearch, MarketStatus, MarketMovers, NewsAndSentiment, EarningsCalendar\
, EarningsCalendarItem,IpoCalendarItem, IpoCalendar, CurrencyQuote
, EarningsCalendarItem,IpoCalendarItem, IpoCalendar, CurrencyQuote, Commodity
from alphavantage_api_client.ticker import Ticker
256 changes: 251 additions & 5 deletions alphavantage_api_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from .response_validation_rules import ValidationRuleChecks
import json
from alphavantage_api_client.models import GlobalQuote, Quote, AccountingReport, CompanyOverview, RealGDP, \
CsvNotSupported, TickerSearch, MarketStatus, NewsAndSentiment, MarketMovers, EarningsCalendar\
, IpoCalendarItem, IpoCalendar, CurrencyQuote
CsvNotSupported, TickerSearch, MarketStatus, NewsAndSentiment, MarketMovers, EarningsCalendar \
, IpoCalendarItem, IpoCalendar, CurrencyQuote, Commodity
import copy
import logging
import hashlib
Expand Down Expand Up @@ -470,10 +470,9 @@ def get_crypto_intraday(self, event: dict) -> CurrencyQuote:
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)
#print(json.dumps(json_response))
# print(json.dumps(json_response))
return CurrencyQuote.parse_obj(json_response)


def get_crypto_daily(self, event: dict) -> CurrencyQuote:
"""
This API returns the daily historical time series for a digital currency (e.g., BTC)
Expand Down Expand Up @@ -930,4 +929,251 @@ def get_forex_monthly(self, event: dict) -> CurrencyQuote:
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return CurrencyQuote.parse_obj(json_response)
return CurrencyQuote.parse_obj(json_response)

def get_crude_oil_wti_prices(self, event) -> Commodity:
"""
This API returns the West Texas Intermediate (WTI) crude oil prices in daily, weekly, and monthly horizons.

Source: U.S. Energy Information Administration, Crude Oil Prices: West Texas Intermediate (WTI) - Cushing,
Oklahoma, retrieved from FRED, Federal Reserve Bank of St. Louis. This data feed uses the FRED® API but is not
endorsed or certified by the Federal Reserve Bank of St. Louis. By using this data feed, you agree to be
bound by the FRED® API Terms of Use.
Args:
event:

Returns:

"""
defaults = {
"function": "WTI",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_crude_oil_brent_prices(self, event: dict) -> Commodity:
"""
This API returns the Brent (Europe) crude oil prices in daily, weekly, and monthly horizons.
Source: U.S. Energy Information Administration, Crude Oil Prices: Brent - Europe, retrieved from FRED,
Federal Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or certified by the
Federal Reserve Bank of St. Louis. By using this data feed, you agree to be bound by the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "BRENT",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_natural_gas_prices(self, event: dict) -> Commodity:
"""
This API returns the Henry Hub natural gas spot prices in daily, weekly, and monthly horizons.
Source: U.S. Energy Information Administration, Henry Hub Natural Gas Spot Price, retrieved from FRED,
Federal Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or certified by the
Federal Reserve Bank of St. Louis. By using this data feed, you agree to be bound by the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "NATURAL_GAS",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_copper_prices(self, event: dict) -> Commodity:
"""
This API returns the global price of copper in monthly, quarterly, and annual horizons.
Source: International Monetary Fund (IMF Terms of Use), Global price of Copper, retrieved from FRED, Federal
Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or certified by the Federal
Reserve Bank of St. Louis. By using this data feed, you agree to be bound by the FRED® API Terms of Use.
Args:
event:

Returns:

"""
defaults = {
"function": "COPPER",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_aluminum_prices(self, event: dict) -> Commodity:
"""
This API returns the global price of aluminum in monthly, quarterly, and annual horizons.
Source: International Monetary Fund (IMF Terms of Use), Global price of Aluminum, retrieved from FRED, Federal
Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or certified by the Federal
Reserve Bank of St. Louis. By using this data feed, you agree to be bound by the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "ALUMINUM",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_wheat_prices(self, event: dict) -> Commodity:
"""
This API returns the global price of wheat in monthly, quarterly, and annual horizons.
Source: International Monetary Fund (IMF Terms of Use), Global price of Wheat, retrieved from FRED, Federal
Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or certified by the Federal
Reserve Bank of St. Louis. By using this data feed, you agree to be bound by the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "WHEAT",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_corn_prices(self, event: dict) -> Commodity:
"""
This API returns the global price of corn in monthly, quarterly, and annual horizons.
Source: International Monetary Fund (IMF Terms of Use), Global price of Corn, retrieved from FRED, Federal
Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or certified by the Federal
Reserve Bank of St. Louis. By using this data feed, you agree to be bound by the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "CORN",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_cotton_prices(self, event: dict) -> Commodity:
"""
This API returns the global price of cotton in monthly, quarterly, and annual horizons.
Source: International Monetary Fund (IMF Terms of Use), Global price of Cotton, retrieved from FRED, Federal
Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or certified by the Federal
Reserve Bank of St. Louis. By using this data feed, you agree to be bound by the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "COTTON",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_sugar_prices(self, event: dict) -> Commodity:
"""
This API returns the global price of sugar in monthly, quarterly, and annual horizons.
Source: International Monetary Fund (IMF Terms of Use), Global price of Sugar, No. 11, World, retrieved from
FRED, Federal Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or certified by
the Federal Reserve Bank of St. Louis. By using this data feed, you agree to be bound
by the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "SUGAR",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_coffee_prices(self, event: dict) -> Commodity:
"""
This API returns the global price of coffee in monthly, quarterly, and annual horizons.
Source: International Monetary Fund (IMF Terms of Use), Global price of Coffee, Other Mild Arabica,
retrieved from FRED, Federal Reserve Bank of St. Louis. This data feed uses the FRED® API but is not
endorsed or certified by the Federal Reserve Bank of St. Louis. By using this data feed, you agree to be
bound by the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "COFFEE",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)

def get_all_commodity_prices(self, event: dict) -> Commodity:
"""
This API returns the global price index of all commodities in monthly, quarterly, and annual temporal dimensions.
Source: International Monetary Fund (IMF Terms of Use), Global Price Index of All Commodities, retrieved
from FRED, Federal Reserve Bank of St. Louis. This data feed uses the FRED® API but is not endorsed or
certified by the Federal Reserve Bank of St. Louis. By using this data feed, you agree to be bound by
the FRED® API Terms of Use.
Args:
event: dict

Returns: Commodity

"""
defaults = {
"function": "ALL_COMMODITIES",
"interval": "monthly",
"datatype": "json"
}
json_request = self.__create_api_request_from__(defaults, event)
json_response = self.get_data_from_alpha_vantage(json_request, self.__retry__)

return Commodity.parse_obj(json_response)
6 changes: 6 additions & 0 deletions alphavantage_api_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def normalize_fields(cls, values):
or k.startswith("Realtime Currency Exchange Rate") else k: v for k, v in values.items()
}

class Commodity(BaseResponse):
name: str
interval: str
unit: str
data: list[dict]

class Quote(BaseQuote):
"""
data is this clients abstraction of the response from alpha vantage. Time Series, Technical Indicator
Expand Down
Loading