Skip to content

Commit

Permalink
fix datetime bug (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Graeme22 authored Dec 9, 2024
1 parent 2382352 commit a11df92
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
18 changes: 10 additions & 8 deletions tastytrade/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,32 +1002,34 @@ def get_transaction(self, session: Session, id: int) -> Transaction:
return Transaction(**data)

async def a_get_total_fees(
self, session: Session, date: date = today_in_new_york()
self, session: Session, day: Optional[date] = None
) -> FeesInfo:
"""
Get the total fees for a given date.
:param session: the session to use for the request.
:param date: the date to get fees for.
:param day: the date to get fees for.
"""
if not day:
day = today_in_new_york()
data = await session._a_get(
f"/accounts/{self.account_number}/transactions/total-fees",
params={"date": date},
params={"date": day},
)
return FeesInfo(**data)

def get_total_fees(
self, session: Session, date: date = today_in_new_york()
) -> FeesInfo:
def get_total_fees(self, session: Session, day: Optional[date] = None) -> FeesInfo:
"""
Get the total fees for a given date.
:param session: the session to use for the request.
:param date: the date to get fees for.
:param day: the date to get fees for.
"""
if not day:
day = today_in_new_york()
data = session._get(
f"/accounts/{self.account_number}/transactions/total-fees",
params={"date": date},
params={"date": day},
)
return FeesInfo(**data)

Expand Down
48 changes: 32 additions & 16 deletions tastytrade/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,32 @@ def today_in_new_york() -> date:
return now_in_new_york().date()


def is_market_open_on(day: date = today_in_new_york()) -> bool:
def is_market_open_on(day: Optional[date] = None) -> bool:
"""
Returns whether the market was/is/will be open at ANY point
during the given day.
:param day: date to check
:param day: date to check. If not provided defaults to current NY date.
:return: whether the market opens on given day
"""
if not day:
day = today_in_new_york()
date_range = NYSE.valid_days(day, day)
return not date_range.empty


def get_third_friday(day: date = today_in_new_york()) -> date:
def get_third_friday(day: Optional[date] = None) -> date:
"""
Gets the monthly expiration associated with the month of the given date,
or the monthly expiration associated with today's month.
:param day: the date to check, defaults to today
:param day: date to check. If not provided defaults to current NY date.
:return: the associated monthly
"""
if not day:
day = today_in_new_york()
day = day.replace(day=1)
day += timedelta(weeks=2)
while day.weekday() != 4: # Friday
Expand Down Expand Up @@ -91,16 +95,18 @@ def _get_last_day_of_month(day: date) -> date:
return last - timedelta(days=1)


def get_future_fx_monthly(day: date = today_in_new_york()) -> date:
def get_future_fx_monthly(day: Optional[date] = None) -> date:
"""
Gets the monthly expiration associated with the FX futures: /6E, /6A, etc.
As far as I can tell, these expire on the first Friday prior to the second
Wednesday.
:param day: the date to check, defaults to today
:param day: date to check. If not provided defaults to current NY date.
:return: the associated monthly
"""
if not day:
day = today_in_new_york()
day = day.replace(day=1)
day += timedelta(weeks=1)
while day.weekday() != 2: # Wednesday
Expand All @@ -110,17 +116,19 @@ def get_future_fx_monthly(day: date = today_in_new_york()) -> date:
return day


def get_future_treasury_monthly(day: date = today_in_new_york()) -> date:
def get_future_treasury_monthly(day: Optional[date] = None) -> date:
"""
Gets the monthly expiration associated with the treasury futures: /ZN,
/ZB, etc. According to CME, these expire the Friday before the 2nd last
business day of the month. If this is not a business day, they expire 1
business day prior.
:param day: the date to check, defaults to today
:param day: date to check. If not provided defaults to current NY date.
:return: the associated monthly
"""
if not day:
day = today_in_new_york()
last_day = _get_last_day_of_month(day)
first_day = last_day.replace(day=1)
valid_range = [d.date() for d in NYSE.valid_days(first_day, last_day)]
Expand All @@ -132,17 +140,19 @@ def get_future_treasury_monthly(day: date = today_in_new_york()) -> date:
return itr - timedelta(days=1)


def get_future_metal_monthly(day: date = today_in_new_york()) -> date:
def get_future_metal_monthly(day: Optional[date] = None) -> date:
"""
Gets the monthly expiration associated with the metals futures: /GC, /SI,
etc. According to CME, these expire on the 4th last business day of the
month, unless that day occurs on a Friday or the day before a holiday, in
which case they expire on the prior business day.
:param day: the date to check, defaults to today
:param day: date to check. If not provided defaults to current NY date.
:return: the associated monthly
"""
if not day:
day = today_in_new_york()
last_day = _get_last_day_of_month(day)
first_day = last_day.replace(day=1)
valid_range = [d.date() for d in NYSE.valid_days(first_day, last_day)]
Expand All @@ -153,16 +163,18 @@ def get_future_metal_monthly(day: date = today_in_new_york()) -> date:
return itr


def get_future_grain_monthly(day: date = today_in_new_york()) -> date:
def get_future_grain_monthly(day: Optional[date] = None) -> date:
"""
Gets the monthly expiration associated with the grain futures: /ZC, /ZW,
etc. According to CME, these expire on the Friday which precedes, by at
least 2 business days, the last business day of the month.
:param day: the date to check, defaults to today
:param day: date to check. If not provided defaults to current NY date.
:return: the associated monthly
"""
if not day:
day = today_in_new_york()
last_day = _get_last_day_of_month(day)
first_day = last_day.replace(day=1)
valid_range = [d.date() for d in NYSE.valid_days(first_day, last_day)]
Expand All @@ -172,33 +184,37 @@ def get_future_grain_monthly(day: date = today_in_new_york()) -> date:
return itr


def get_future_oil_monthly(day: date = today_in_new_york()) -> date:
def get_future_oil_monthly(day: Optional[date] = None) -> date:
"""
Gets the monthly expiration associated with the WTI oil futures: /CL and
/MCL. According to CME, these expire 6 business days before the 25th day
of the month, unless the 25th day is not a business day, in which case
they expire 7 business days prior to the 25th day of the month.
:param day: the date to check, defaults to today
:param day: date to check. If not provided defaults to current NY date.
:return: the associated monthly
"""
if not day:
day = today_in_new_york()
last_day = day.replace(day=25)
first_day = last_day.replace(day=1)
valid_range = [d.date() for d in NYSE.valid_days(first_day, last_day)]
return valid_range[-7]


def get_future_index_monthly(day: date = today_in_new_york()) -> date:
def get_future_index_monthly(day: Optional[date] = None) -> date:
"""
Gets the monthly expiration associated with the index futures: /ES, /RTY,
/NQ, etc. According to CME, these expire on the last business day of the
month.
:param day: the date to check, defaults to today
:param day: date to check. If not provided defaults to current NY date.
:return: the associated monthly
"""
if not day:
day = today_in_new_york()
last_day = _get_last_day_of_month(day)
first_day = last_day.replace(day=1)
valid_range = [d.date() for d in NYSE.valid_days(first_day, last_day)]
Expand Down

0 comments on commit a11df92

Please sign in to comment.