Skip to content

Commit

Permalink
Eliminate market_closed named_object (using Exceptions this time)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgibson11 committed Jul 4, 2023
1 parent 3383935 commit 6b65f57
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
4 changes: 0 additions & 4 deletions sysbrokers/broker_futures_contract_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from sysobjects.production.trading_hours.trading_hours import listOfTradingHours
from syscore.exceptions import missingContract
from syscore.constants import market_closed
from sysdata.futures.contracts import futuresContractData
from sysdata.data_blob import dataBlob
from sysobjects.contract_dates_and_expiries import expiryDate
Expand Down Expand Up @@ -47,9 +46,6 @@ def less_than_N_hours_of_trading_left_for_contract(
trading_hours.less_than_N_hours_left(N_hours=N_hours)
)

if less_than_N_hours_of_trading_left_for_contract is market_closed:
return market_closed

return less_than_N_hours_of_trading_left_for_contract

def get_trading_hours_for_contract(
Expand Down
1 change: 0 additions & 1 deletion syscore/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def __repr__(self):
return self._name


market_closed = named_object("market closed")
fill_exceeds_trade = named_object("fill too big for trade")
arg_not_supplied = named_object("arg not supplied")
user_exit = named_object("exit")
Expand Down
4 changes: 4 additions & 0 deletions syscore/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class missingFile(Exception):
pass


class marketClosed(Exception):
pass


class existingData(Exception):
pass

Expand Down
14 changes: 7 additions & 7 deletions sysexecution/algos/algo_original_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"""
from typing import Union

from syscore.constants import market_closed
from syscore.exceptions import missingData
from syscore.exceptions import missingData, marketClosed
from sysexecution.orders.named_order_objects import missing_order

from sysdata.data_blob import dataBlob
Expand Down Expand Up @@ -331,12 +330,13 @@ def is_market_about_to_close(
log: pst_logger,
) -> bool:
data_broker = dataBroker(data)
short_of_time = data_broker.less_than_N_hours_of_trading_left_for_contract(
order.futures_contract,
N_hours=HOURS_BEFORE_MARKET_CLOSE_TO_SWITCH_TO_MARKET,
)

if short_of_time is market_closed:
try:
short_of_time = data_broker.less_than_N_hours_of_trading_left_for_contract(
order.futures_contract,
N_hours=HOURS_BEFORE_MARKET_CLOSE_TO_SWITCH_TO_MARKET,
)
except marketClosed:
log.warning("Market has closed for active limit order %s!" % str(order))
return True

Expand Down
8 changes: 3 additions & 5 deletions sysobjects/production/trading_hours/trading_hours.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
following_one_second_before_midnight_of_datetime,
preceeding_midnight_of_datetime,
)
from syscore.exceptions import marketClosed
from syscore.genutils import intersection_intervals
from syscore.constants import market_closed


@dataclass()
Expand All @@ -31,8 +31,6 @@ def okay_to_trade_now(self) -> bool:

def less_than_N_hours_left(self, N_hours: float = 1.0) -> bool:
hours_left = self.hours_left_before_market_close()
if hours_left is market_closed:
return market_closed

if hours_left < N_hours:
return True
Expand All @@ -42,7 +40,7 @@ def less_than_N_hours_left(self, N_hours: float = 1.0) -> bool:
def hours_left_before_market_close(self) -> float:
if not self.okay_to_trade_now():
# market closed
return market_closed
raise marketClosed

datetime_now = datetime.datetime.now()
time_left = self.closing_time - datetime_now
Expand Down Expand Up @@ -118,7 +116,7 @@ def less_than_N_hours_left(self, N_hours: float = 1.0):
# move on to next period
continue

return market_closed
raise marketClosed


def split_trading_hours_across_two_weekdays(
Expand Down

0 comments on commit 6b65f57

Please sign in to comment.