|
2 | 2 | from decimal import Decimal |
3 | 3 | from typing import Any, Dict, List, Literal, Optional, Union |
4 | 4 |
|
| 5 | +import httpx |
5 | 6 | from pydantic import BaseModel, model_validator |
6 | 7 | from typing_extensions import Self |
7 | 8 |
|
| 9 | +from tastytrade import VAST_URL |
8 | 10 | from tastytrade.order import ( |
9 | 11 | InstrumentType, |
10 | 12 | NewComplexOrder, |
11 | 13 | NewOrder, |
12 | 14 | OrderAction, |
| 15 | + OrderChain, |
13 | 16 | OrderStatus, |
14 | 17 | PlacedComplexOrder, |
15 | 18 | PlacedComplexOrderResponse, |
|
26 | 29 | validate_response, |
27 | 30 | ) |
28 | 31 |
|
| 32 | +TT_DATE_FMT = "%Y-%m-%dT%H:%M:%SZ" |
| 33 | + |
29 | 34 |
|
30 | 35 | class EmptyDict(BaseModel): |
31 | 36 | class Config: |
@@ -1049,7 +1054,7 @@ async def a_get_net_liquidating_value_history( |
1049 | 1054 | params = {} |
1050 | 1055 | if start_time: |
1051 | 1056 | # format to Tastytrade DateTime format |
1052 | | - params = {"start-time": start_time.strftime("%Y-%m-%dT%H:%M:%SZ")} |
| 1057 | + params = {"start-time": start_time.strftime(TT_DATE_FMT)} |
1053 | 1058 | elif not time_back: |
1054 | 1059 | msg = "Either time_back or start_time must be specified." |
1055 | 1060 | raise TastytradeError(msg) |
@@ -1083,7 +1088,7 @@ def get_net_liquidating_value_history( |
1083 | 1088 | params = {} |
1084 | 1089 | if start_time: |
1085 | 1090 | # format to Tastytrade DateTime format |
1086 | | - params = {"start-time": start_time.strftime("%Y-%m-%dT%H:%M:%SZ")} |
| 1091 | + params = {"start-time": start_time.strftime(TT_DATE_FMT)} |
1087 | 1092 | elif not time_back: |
1088 | 1093 | msg = "Either time_back or start_time must be specified." |
1089 | 1094 | raise TastytradeError(msg) |
@@ -1641,3 +1646,80 @@ def replace_order( |
1641 | 1646 | ), |
1642 | 1647 | ) |
1643 | 1648 | return PlacedOrder(**data) |
| 1649 | + |
| 1650 | + async def a_get_order_chains( |
| 1651 | + self, |
| 1652 | + session: Session, |
| 1653 | + symbol: str, |
| 1654 | + start_time: datetime, |
| 1655 | + end_time: datetime, |
| 1656 | + ) -> List[OrderChain]: |
| 1657 | + """ |
| 1658 | + Get a list of order chains (open + rolls + close) for given symbol |
| 1659 | + over the given time frame, with total P/L, commissions, etc. |
| 1660 | +
|
| 1661 | + :param session: the session to use for the request. |
| 1662 | + :param symbol: the underlying symbol for the chains. |
| 1663 | + :param start_time: the beginning time of the query. |
| 1664 | + :param end_time: the ending time of the query. |
| 1665 | + """ |
| 1666 | + params = { |
| 1667 | + "account-numbers[]": self.account_number, |
| 1668 | + "underlying-symbols[]": symbol, |
| 1669 | + "start-at": start_time.strftime(TT_DATE_FMT), |
| 1670 | + "end-at": end_time.strftime(TT_DATE_FMT), |
| 1671 | + "defer-open-winner-loser-filtering-to-frontend": False, |
| 1672 | + "per-page": 250, |
| 1673 | + } |
| 1674 | + headers = { |
| 1675 | + "Authorization": session.session_token, |
| 1676 | + "Accept": "application/json", |
| 1677 | + "Content-Type": "application/json", |
| 1678 | + } |
| 1679 | + async with httpx.AsyncClient() as client: |
| 1680 | + response = await client.get( |
| 1681 | + f"{VAST_URL}/order-chains", |
| 1682 | + headers=headers, |
| 1683 | + params=params, |
| 1684 | + ) |
| 1685 | + validate_response(response) |
| 1686 | + chains = response.json()["data"]["items"] |
| 1687 | + return [OrderChain(**i) for i in chains] |
| 1688 | + |
| 1689 | + def get_order_chains( |
| 1690 | + self, |
| 1691 | + session: Session, |
| 1692 | + symbol: str, |
| 1693 | + start_time: datetime, |
| 1694 | + end_time: datetime, |
| 1695 | + ) -> List[OrderChain]: |
| 1696 | + """ |
| 1697 | + Get a list of order chains (open + rolls + close) for given symbol |
| 1698 | + over the given time frame, with total P/L, commissions, etc. |
| 1699 | +
|
| 1700 | + :param session: the session to use for the request. |
| 1701 | + :param symbol: the underlying symbol for the chains. |
| 1702 | + :param start_time: the beginning time of the query. |
| 1703 | + :param end_time: the ending time of the query. |
| 1704 | + """ |
| 1705 | + params = { |
| 1706 | + "account-numbers[]": self.account_number, |
| 1707 | + "underlying-symbols[]": symbol, |
| 1708 | + "start-at": start_time.strftime(TT_DATE_FMT), |
| 1709 | + "end-at": end_time.strftime(TT_DATE_FMT), |
| 1710 | + "defer-open-winner-loser-filtering-to-frontend": False, |
| 1711 | + "per-page": 250, |
| 1712 | + } |
| 1713 | + headers = { |
| 1714 | + "Authorization": session.session_token, |
| 1715 | + "Accept": "application/json", |
| 1716 | + "Content-Type": "application/json", |
| 1717 | + } |
| 1718 | + response = httpx.get( |
| 1719 | + f"{VAST_URL}/order-chains", |
| 1720 | + headers=headers, |
| 1721 | + params=params, |
| 1722 | + ) |
| 1723 | + validate_response(response) |
| 1724 | + chains = response.json()["data"]["items"] |
| 1725 | + return [OrderChain(**i) for i in chains] |
0 commit comments