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

Refactor FinancialDatasetAPI integration and add configuration classes for API setup. #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anhquan075
Copy link

@anhquan075 anhquan075 commented Jan 1, 2025

This pull request focuses on refactoring the codebase to use a new FinancialDatasetAPI class for accessing financial data, replacing individual API functions. The changes involve using this new class, removing the old API functions, and implementing a base API client class for better structure and error handling.

Refactoring to use FinancialDatasetAPI:

  • src/agents/market_data.py: Replaced individual API function calls with methods from the new FinancialDatasetAPI class. Adjusted the import statements accordingly.
  • src/agents/risk_manager.py, src/agents/technicals.py, src/backtester.py: Updated to use FinancialDatasetAPI.prices_to_df for converting price data to a DataFrame.

Removal of old API functions:

  • src/tools/api.py: Removed all old API functions such as get_financial_metrics, search_line_items, get_insider_trades, get_market_cap, get_prices, and prices_to_df.

Implementation of base API client:

  • src/tools/api/base.py: Made an abstract base class BaseAPIClient to handle common API client functionality such as making requests and handling responses.
  • src/tools/api/config.py: Added configuration classes BaseAPIConfig and FinancialDatasetAPIConfig for managing API configuration.
  • src/tools/api/financial_dataset.py: Created the FinancialDatasetAPI class extending BaseAPIClient, implementing methods for fetching financial metrics, insider trades, market cap, and price data.

@anhquan075 anhquan075 changed the title Refactor API integration to use FinancialDatasetAPI and add configuration classes for API setup. Refactor FinancialDatasetAPI integration and add configuration classes for API setup. Jan 1, 2025
@anhquan075 anhquan075 marked this pull request as ready for review January 1, 2025 03:46
@hoainam-nguyen
Copy link

Awesome

Copy link

@marc-at-brightnight marc-at-brightnight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 👏🏼 Would probably just recommend some typing improvements

**data,
"prices": prices,
"start_date": start_date,
**data,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.9+ syntax is preferred, i.e. data | { ... }, rather than { **data, ... }

from .config import FinancialDatasetAPIConfig


class Period(str, Enum):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think you need str

def _get_headers(self) -> Dict[str, str]:
return {"X-API-KEY": self.config.api_key}

def _handle_response(self, response: requests.Response) -> Dict[str, Any]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use dict instead of Dict. I would add a type alias for dict[str, Any] in an appropriate file, since it's used so often (arguably too often)


return df.sort_index()

def get_financial_metrics(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public methods usually come before private methods

return response.json()

@staticmethod
def prices_to_df(prices: List[Dict[str, Any]]) -> pd.DataFrame:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use list instead of List

)

def get_insider_trades(
self, ticker: str, end_date: Union[str, datetime], limit: int = 5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of Union, use str | datetime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants