areed1192 / td-ameritrade-api

The unofficial Python API client library for TD Ameritrade. This library allows for easy access of the Standard REST API and Streaming API.
MIT License
124 stars 37 forks source link

Unable to pass in Enum to get_market_hours in rest/market_hours.py #4

Closed monsieur40a closed 2 years ago

monsieur40a commented 3 years ago

This code outside the market_hours file works as expected, we get the enum replaced with its value.

class Markets(Enum):
    """Represents the different markets you can request
    hours for the `MarketHours` service.

    ### Usage
    ----
        >>> from td.enums import Markets
        >>> Markets.Bond.Value
    """

    Bond = 'BOND'
    Equity = 'EQUITY'
    Option = 'OPTION'
    Forex = 'FOREX'
    Futures = 'FUTURES'
def get_market_hours(market):
    from enum import Enum
    if isinstance(market, Enum):
        market = market.value
    content = f"marketdata/{market}/hours"
    return content
print(get_market_hours(Markets.Bond))

However, this code doesn't work:

    def get_market_hours(
        self,
        market: Union[str, Enum],
        date_time: Union[str, datetime] = None
    ) -> dict:
        """Returns the market hours for the specified market.

        ### Documentation
        ----
        https://developer.tdameritrade.com/market-hours/apis

        ### Parameters
        ----
        market: Union[str, Enum]
            A list of market IDs you want to return hours for.
            Possible values are: `EQUITY`, `OPTION`, `FUTURE`,
            `BOND`, or `FOREX`.

        date: Union[str, datetime, date]
            The date you wish to recieve market hours for. 
            Valid ISO-8601 formats are: yyyy-MM-dd and 
            yyyy-MM-dd'T'HH:mm:ssz

        ### Usage
        ----
            >>> from td.enums import Markets
            >>> market_hours_service = td_client.market_hours()
            >>> market_hours_service.get_market_hours(
                markets='EQUITY',
                date='2021-12-31'
            )
        """
        if isinstance(market, Enum):
            market = market.value

        # Grab the date_time.
        if isinstance(date_time, datetime):
            date_time = date_time.date().isoformat()
        elif isinstance(date_time, date):
            date_time = date_time.isoformat()

        params = {
            'date': date_time
        }

        content = self.session.make_request(
            method='get',
            endpoint=f'marketdata/{market}/hours',
            params=params
        )

        return content

called with

# Grab the hours for a specific market.
pprint(
    market_hours_service.get_market_hours(
        market=Markets.Bond,
        date_time=datetime.now()
    )
)

an http error is thrown. logging shows:

"response_body": {
    "timestamp": "2021-10-23",
    "status": 400,
    "error": "Bad Request",
    "message": "Market name not recognized",
    "path": "/markethours-v1/markets/Markets.Bond"
},

Not sure of the issue. Usage of isinstance to cast enum is similar to other code and to the other market_hours method, get_multiple_market_hours, which works fine.

monsieur40a commented 3 years ago

This appears to be an issue with utilizing Jupyter notebooks to test... same code executes fine without Jupyter.

Anyways, slowly working on reviewing the code, plan to do a PR for a few things or split off a new version. For instance, my version utilizes secret question and answer to skip any window opening whatsoever.

areed1192 commented 3 years ago

I just pushed the change to the repository, give it a try and let me know if it works.

monsieur40a commented 2 years ago

Whatever it was is fixed now. I copied the current use_market_hours.py that I just pushed into a jupyter notebook and it worked fine.