Skip to content

Commit

Permalink
Add trade days remaining in month/year to toolbar
Browse files Browse the repository at this point in the history
Helps for estimation if targeting gains per day for future goals.
  • Loading branch information
mattsta committed Jul 16, 2024
1 parent e85ce60 commit 50bebee
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion icli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import pandas as pd
import pendulum

from pandas.tseries.offsets import MonthEnd, YearEnd
from prompt_toolkit import Application, print_formatted_text, PromptSession
from prompt_toolkit.application import get_app
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
Expand Down Expand Up @@ -200,6 +201,29 @@ def fetchDateTimeOfEndOfMarketDay():
return [(soonestStart, soonestEnd), (nextStart, nextEnd)]


@cached(cache=TTLCache(maxsize=128, ttl=60 * 60 * 90))
def tradingDaysRemainingInMonth():
"""Return how many trading days until the month ends..."""
found = mcal.getMarketCalendar(
"NASDAQ", start=pd.Timestamp("now"), stop=pd.Timestamp("now") + MonthEnd(0)
)

# just length because the 'found' calendar has one row for each market day in the result set...
distance = len(found)
return distance


@cached(cache=TTLCache(maxsize=128, ttl=60 * 60 * 90))
def tradingDaysRemainingInYear():
"""Return how many trading days until the year ends..."""
found = mcal.getMarketCalendar(
"NASDAQ", start=pd.Timestamp("now"), stop=pd.Timestamp("now") + YearEnd(0)
)

distance = len(found)
return distance


# expire this cache once every 15 minutes so we only have up to 15 minutes of wrong dates after EOD
@cached(cache=TTLCache(maxsize=128, ttl=60 * 15))
def fetchEndOfMarketDay():
Expand Down Expand Up @@ -2655,10 +2679,12 @@ def undX(spxd, spxIn):
# on if we are out of market hours or not, but we aren't bothering with the extra logic for now.
untilClose = fetchEndOfMarketDay() - self.now
todayclose = f"mktclose: {untilClose.in_words()}"
daysInMonth = f"dim: {tradingDaysRemainingInMonth()}"
daysInYear = f"diy: {tradingDaysRemainingInYear()}"

return HTML(
# all these spaces look weird, but they (kinda) match the underlying column-based formatting offsets
f"""[{ICLI_CLIENT_ID}] {self.now}{onc} [{self.updates:,}] {spxbreakers} {openorders} {openpositions} {todayexecutions} {todayclose}\n"""
f"""[{ICLI_CLIENT_ID}] {self.now}{onc} [{self.updates:,}] {spxbreakers} {openorders} {openpositions} {todayexecutions} {todayclose} ({daysInMonth} :: {daysInYear})\n"""
+ "\n".join(
[
f"{qp:>2}) " + formatTicker(quote)
Expand Down

0 comments on commit 50bebee

Please sign in to comment.