tastyware / tastytrade

An unofficial Python SDK for Tastytrade!
MIT License
98 stars 33 forks source link

Account.get_margin_requirements not working with options on futures #144

Closed Quenos closed 1 month ago

Quenos commented 1 month ago

Describe the bug Calling Account.get_margin_requirements(session) will throw Pydantic exceptions for missing and extra fields. For options on Equity it seems to work just fine.

How to reproduce With options on futures in your portfolio call Account.get_margin_requirements(session)

Working on a solution but running in some strange issues. Pydantic is e.g. complaining about the fact that underlying-symbol is missing

groups.0.MarginReportEntry.underlying-symbol
  Field required [type=missing, input_value={'description': '/ZB', 'c...turn-percent': '-0.615'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.6/v/missing

But changing the underlying_symbol field in Account.MarginReport to Optional[str] does not solve the issue.

What am I missing here?

EDIT: It helps if you also add '= None' to the Optional field :)

Graeme22 commented 1 month ago

Looks like it's a MarginReportEntry field not MarginReport. Maybe that's the issue? Thanks for opening this! Could you submit a PR to both the advanced-streamer and master branches?

Quenos commented 1 month ago

Looks like it's a MarginReportEntry field not MarginReport. Maybe that's the issue?

Yes, that's a typo in the original post. I've changed the following (not the complete class yet.

class MarginReportEntry(TastytradeJsonDataclass):
    """
    Dataclass containing an individual entry (relating to a specific position)
    as part of the overall margin report.
    """
    description: str
    code: str
    underlying_symbol: Optional[str]
    underlying_type: Optional[str]
    margin_calculation_type: str
    margin_requirement: Decimal
    margin_requirement_effect: PriceEffect
    maintenance_requirement: Decimal
    maintenance_requirement_effect: PriceEffect
    buying_power: Decimal
    buying_power_effect: PriceEffect
    groups: List[Dict[str, Any]]
    price_increase_percent: Decimal
    price_decrease_percent: Decimal
    expected_price_range_up_percent: Optional[Decimal] = None
    expected_price_range_down_percent: Optional[Decimal] = None
    point_of_no_return_percent: Optional[Decimal] = None
    initial_requirement: Optional[Decimal] = None
    initial_requirement_effect: Optional[PriceEffect] = None

Thanks for opening this! Could you submit a PR to both the advanced-streamer and master branches?

Yes I will.