Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python logging switch #1279

Merged
merged 24 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b54fd3c
refactor some simple log.setup() usages - pass temporary log attribut…
bug-or-feature Oct 10, 2023
2e5b6ce
removing usages of log.setup() which have no effect
bug-or-feature Oct 10, 2023
7708841
removing unnecessary use of log.setup(), logger is set up in __init__()
bug-or-feature Oct 10, 2023
f851a7a
removing unnecessary use of log.setup(), system_init() adds 'stage' a…
bug-or-feature Oct 10, 2023
17d0d53
removing unnecessary use of log.setup(), system_init() adds 'stage' a…
bug-or-feature Oct 10, 2023
2ca048e
refactoring away log.setup() in dataBlob
bug-or-feature Oct 13, 2023
7e5e566
refactoring away contract.specific_log(), which uses log.setup()
bug-or-feature Oct 13, 2023
35add55
updating logging TODOs
bug-or-feature Oct 13, 2023
bfc5114
safe logger setup
bug-or-feature Oct 17, 2023
0f37379
fix logger attributes
bug-or-feature Oct 17, 2023
cf146e3
remove pst_logger type hints
bug-or-feature Oct 17, 2023
99ed02a
better logging for submit_order()
bug-or-feature Oct 17, 2023
24a9c0f
add missing dict expanders **
bug-or-feature Oct 18, 2023
de693e5
fix temp fx log attributes
bug-or-feature Oct 18, 2023
04027f0
adding more tests for temp method
bug-or-feature Oct 18, 2023
346bdd5
expanding log attributes
bug-or-feature Oct 18, 2023
737643d
fixing duplicated log attribute
bug-or-feature Oct 18, 2023
ec1c616
refactoring away contract.log(), which uses log.setup()
bug-or-feature Oct 20, 2023
cffecf4
log twice twice
bug-or-feature Oct 24, 2023
e63e585
Merge branch 'develop' into python_logging_switch
bug-or-feature Oct 24, 2023
d0a1e9c
Merge branch 'develop' into python_logging_switch
bug-or-feature Nov 10, 2023
6d9b076
Merge branch 'develop' into python_logging_switch
bug-or-feature Nov 14, 2023
e6b1f0b
Merge branch 'develop' into python_logging_switch
bug-or-feature Nov 28, 2023
0b4ed2e
Merge branch 'develop' into python_logging_switch
bug-or-feature Dec 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ Here's a quick whistle-stop tour of dataBlob's other features:


- you can create it with a starting class list by passing the `parameter class_list=...`
- it includes a `log` attribute that is passed to create data storage instances (you can override this by passing in a pst_logger via the `log=` parameter when dataBlob is created), the log will have top level type attribute as defined by the log_name parameter
- it includes a `log` attribute that is passed to create data storage instances (you can override this by passing in a logger via the `log=` parameter when dataBlob is created), the log will have top level type attribute as defined by the log_name parameter
- when required it creates a `mongoDb` instance that is passed to create data storage instances (you can override this by passing in a `mongoDb` instance via the `mongo_db=` parameter when dataBlob is created)
- when required it creates a `connectionIB` instance that is passed to create data storage instances (you can override this by passing in a connection instance via the `ib_conn=` parameter when dataBlob is created)
- The parameter `csv_data_paths` will allow you to use different .csv data paths, not the defaults. The dict should have the keys of the class names, and values will be the paths to use.
Expand Down
4 changes: 1 addition & 3 deletions sysbrokers/IB/client/ib_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ class ibClient(object):

"""

def __init__(
self, ibconnection: connectionIB, log: pst_logger = get_logger("ibClient")
):
def __init__(self, ibconnection: connectionIB, log=get_logger("ibClient")):
# means our first call won't be throttled for pacing
self.last_historic_price_calltime = (
datetime.datetime.now()
Expand Down
76 changes: 43 additions & 33 deletions sysbrokers/IB/client/ib_contracts_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ def broker_get_futures_contract_list(
) -> list:
## Returns list of contract date strings YYYYMMDD

specific_log = self.log.setup(
instrument_code=futures_instrument_with_ib_data.instrument_code
)

ibcontract_pattern = ib_futures_instrument(futures_instrument_with_ib_data)
contract_list = self.ib_get_contract_chain(
ibcontract_pattern, allow_expired=allow_expired
Expand All @@ -72,9 +68,11 @@ def broker_get_single_contract_expiry_date(
:param futures_contract_with_ib_data: contract where instrument has ib metadata
:return: YYYYMMDD str
"""
specific_log = futures_contract_with_ib_data.specific_log(self.log)
log_attrs = {**futures_contract_with_ib_data.log_attributes(), "method": "temp"}
if futures_contract_with_ib_data.is_spread_contract():
specific_log.warning("Can only find expiry for single leg contract!")
self.log.warning(
"Can only find expiry for single leg contract!", **log_attrs
)
raise missingContract

try:
Expand All @@ -84,7 +82,7 @@ def broker_get_single_contract_expiry_date(
always_return_single_leg=True,
)
except missingContract:
specific_log.warning("Contract is missing can't get expiry")
self.log.warning("Contract is missing can't get expiry", **log_attrs)
raise missingContract

expiry_date = ibcontract.lastTradeDateOrContractMonth
Expand All @@ -103,16 +101,16 @@ def ib_get_trading_hours(
def _ib_get_uncached_trading_hours(
self, contract_object_with_ib_data: futuresContract
) -> listOfTradingHours:
specific_log = contract_object_with_ib_data.specific_log(self.log)

try:
trading_hours_from_ib = self.ib_get_trading_hours_from_IB(
contract_object_with_ib_data
)
except Exception as e:
specific_log.warning(
self.log.warning(
"%s when getting trading hours from %s!"
% (str(e), str(contract_object_with_ib_data))
% (str(e), str(contract_object_with_ib_data)),
**contract_object_with_ib_data.log_attributes(),
method="temp",
)
raise missingData

Expand All @@ -136,8 +134,6 @@ def _ib_get_uncached_trading_hours(
def ib_get_trading_hours_from_IB(
self, contract_object_with_ib_data: futuresContract
) -> listOfTradingHours:
specific_log = contract_object_with_ib_data.specific_log(self.log)

try:
ib_contract_details = self.ib_get_contract_details(
contract_object_with_ib_data
Expand All @@ -146,9 +142,11 @@ def ib_get_trading_hours_from_IB(
ib_contract_details
)
except Exception as e:
specific_log.warning(
self.log.warning(
"%s when getting trading hours from %s!"
% (str(e), str(contract_object_with_ib_data))
% (str(e), str(contract_object_with_ib_data)),
**contract_object_with_ib_data.log_attributes(),
method="temp",
)
raise missingData

Expand Down Expand Up @@ -212,15 +210,16 @@ def ib_get_saved_weekly_trading_hours_custom_for_contract(
def ib_get_saved_weekly_trading_hours_for_timezone_of_contract(
self, contract_object_with_ib_data: futuresContract
) -> weekdayDictOfListOfTradingHoursAnyDay:
specific_log = contract_object_with_ib_data.log(self.log)
log_attrs = {**contract_object_with_ib_data.log_attributes(), "method": "temp"}

try:
time_zone_id = self.ib_get_timezoneid(contract_object_with_ib_data)
except missingData:
# problem getting timezoneid
specific_log.warning(
self.log.warning(
"No time zone ID, can't get trading hours for timezone for %s"
% str(contract_object_with_ib_data)
% str(contract_object_with_ib_data),
**log_attrs,
)
raise missingData

Expand All @@ -233,22 +232,23 @@ def ib_get_saved_weekly_trading_hours_for_timezone_of_contract(
"Check ib_config_trading_hours in sysbrokers/IB or private directory, hours for timezone %s not found!"
% time_zone_id
)
specific_log.log.critical(error_msg)
self.log.critical(error_msg, **log_attrs)
raise missingData

return weekly_hours_for_timezone

def ib_get_timezoneid(self, contract_object_with_ib_data: futuresContract) -> str:
specific_log = contract_object_with_ib_data.specific_log(self.log)
try:
ib_contract_details = self.ib_get_contract_details(
contract_object_with_ib_data
)
time_zone_id = ib_contract_details.timeZoneId
except Exception as e:
specific_log.warning(
self.log.warning(
"%s when getting time zone from %s!"
% (str(e), str(contract_object_with_ib_data))
% (str(e), str(contract_object_with_ib_data)),
**contract_object_with_ib_data.log_attributes(),
method="temp",
)
raise missingData

Expand All @@ -271,23 +271,27 @@ def _get_all_saved_weekly_trading_hours_from_file(self):
def ib_get_min_tick_size(
self, contract_object_with_ib_data: futuresContract
) -> float:
specific_log = contract_object_with_ib_data.specific_log(self.log)
log_attrs = {**contract_object_with_ib_data.log_attributes(), "method": "temp"}
try:
ib_contract = self.ib_futures_contract(
contract_object_with_ib_data, always_return_single_leg=True
)
except missingContract:
specific_log.warning("Can't get tick size as contract missing")
self.log.warning(
"Can't get tick size as contract missing",
**log_attrs,
)
raise

ib_contract_details = self.ib.reqContractDetails(ib_contract)[0]

try:
min_tick = ib_contract_details.minTick
except Exception as e:
specific_log.warning(
self.log.warning(
"%s when getting min tick size from %s!"
% (str(e), str(ib_contract_details))
% (str(e), str(ib_contract_details)),
**log_attrs,
)
raise missingContract

Expand All @@ -296,36 +300,42 @@ def ib_get_min_tick_size(
def ib_get_price_magnifier(
self, contract_object_with_ib_data: futuresContract
) -> float:
specific_log = contract_object_with_ib_data.specific_log(self.log)
log_attrs = {**contract_object_with_ib_data.log_attributes(), "method": "temp"}
try:
ib_contract = self.ib_futures_contract(
contract_object_with_ib_data, always_return_single_leg=True
)
except missingContract:
specific_log.warning("Can't get price magnifier as contract missing")
self.log.warning(
"Can't get price magnifier as contract missing", **log_attrs
)
raise

ib_contract_details = self.ib.reqContractDetails(ib_contract)[0]

try:
price_magnifier = ib_contract_details.priceMagnifier
except Exception as e:
specific_log.warning(
self.log.warning(
"%s when getting price magnifier from %s!"
% (str(e), str(ib_contract_details))
% (str(e), str(ib_contract_details)),
**log_attrs,
)
raise missingContract

return price_magnifier

def ib_get_contract_details(self, contract_object_with_ib_data: futuresContract):
specific_log = contract_object_with_ib_data.specific_log(self.log)
try:
ib_contract = self.ib_futures_contract(
contract_object_with_ib_data, always_return_single_leg=True
)
except missingContract:
specific_log.warning("Can't get trading hours as contract is missing")
self.log.warning(
"Can't get trading hours as contract is missing",
**contract_object_with_ib_data.log_attributes(),
method="temp",
)
raise

# returns a list but should only have one element
Expand Down Expand Up @@ -526,7 +536,7 @@ def _get_vanilla_ib_futures_contract(

return resolved_contract

def ib_resolve_unique_contract(self, ibcontract_pattern, log: pst_logger = None):
def ib_resolve_unique_contract(self, ibcontract_pattern, log=None):
"""
Returns the 'resolved' IB contract based on a pattern. We expect a unique contract.

Expand Down
1 change: 1 addition & 0 deletions sysbrokers/IB/client/ib_fx_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def broker_get_daily_fx_data(
"""

ccy_code = ccy1 + ccy2
# TODO log.setup
log = self.log.setup(currency_code=ccy_code)

try:
Expand Down
44 changes: 19 additions & 25 deletions sysbrokers/IB/client/ib_price_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def broker_get_historical_futures_data_for_contract(
:param freq: str; one of D, H, 5M, M, 10S, S
:return: futuresContractPriceData
"""

# TODO specific_log
specific_log = contract_object_with_ib_broker_config.specific_log(self.log)

try:
Expand Down Expand Up @@ -91,17 +91,17 @@ def get_ib_ticker_object(
contract_object_with_ib_data: futuresContract,
trade_list_for_multiple_legs: tradeQuantity = None,
) -> "ib.ticker":
specific_log = contract_object_with_ib_data.specific_log(self.log)

try:
ibcontract = self.ib_futures_contract(
contract_object_with_ib_data,
trade_list_for_multiple_legs=trade_list_for_multiple_legs,
)
except missingContract:
specific_log.warning(
self.log.warning(
"Can't find matching IB contract for %s"
% str(contract_object_with_ib_data)
% str(contract_object_with_ib_data),
**contract_object_with_ib_data.log_attributes(),
method="temp",
)
raise

Expand All @@ -121,17 +121,17 @@ def cancel_market_data_for_contract_and_trade_qty(
contract_object_with_ib_data: futuresContract,
trade_list_for_multiple_legs: tradeQuantity = None,
):
specific_log = contract_object_with_ib_data.specific_log(self.log)

try:
ibcontract = self.ib_futures_contract(
contract_object_with_ib_data,
trade_list_for_multiple_legs=trade_list_for_multiple_legs,
)
except missingContract:
specific_log.warning(
self.log.warning(
"Can't find matching IB contract for %s"
% str(contract_object_with_ib_data)
% str(contract_object_with_ib_data),
**contract_object_with_ib_data.log_attributes(),
method="temp",
)
raise

Expand All @@ -148,21 +148,19 @@ def _ib_get_recent_bid_ask_tick_data_using_reqHistoricalTicks(
:param contract_object_with_ib_data:
:return:
"""
specific_log = self.log.setup(
instrument_code=contract_object_with_ib_data.instrument_code,
contract_date=contract_object_with_ib_data.date_str,
)
log_attrs = {**contract_object_with_ib_data.log_attributes(), "method": "temp"}
if contract_object_with_ib_data.is_spread_contract():
error_msg = "Can't get historical data for combo"
specific_log.critical(error_msg)
self.log.critical(error_msg, **log_attrs)
raise Exception(error_msg)

try:
ibcontract = self.ib_futures_contract(contract_object_with_ib_data)
except missingContract:
specific_log.warning(
self.log.warning(
"Can't find matching IB contract for %s"
% str(contract_object_with_ib_data)
% str(contract_object_with_ib_data),
**log_attrs,
)
raise

Expand All @@ -175,10 +173,10 @@ def _ib_get_recent_bid_ask_tick_data_using_reqHistoricalTicks(

return tick_data

def _get_generic_data_for_contract(
def _get_generic_data_for_contract( # TODO passed logger instance
self,
ibcontract: ibContract,
log: pst_logger = None,
log=None,
bar_freq: Frequency = DAILY_PRICE_FREQ,
whatToShow: str = "TRADES",
) -> pd.DataFrame:
Expand Down Expand Up @@ -214,9 +212,7 @@ def _get_generic_data_for_contract(

return price_data_as_df

def _raw_ib_data_to_df(
self, price_data_raw: pd.DataFrame, log: pst_logger
) -> pd.DataFrame:
def _raw_ib_data_to_df(self, price_data_raw: pd.DataFrame, log) -> pd.DataFrame:
if price_data_raw is None:
log.warning("No price data from IB")
raise missingData
Expand Down Expand Up @@ -269,7 +265,7 @@ def _ib_get_historical_data_of_duration_and_barSize(
durationStr: str = "1 Y",
barSizeSetting: str = "1 day",
whatToShow="TRADES",
log: pst_logger = None,
log=None,
) -> pd.DataFrame:
"""
Returns historical prices for a contract, up to today
Expand Down Expand Up @@ -341,9 +337,7 @@ def _get_barsize_and_duration_from_frequency(bar_freq: Frequency) -> (str, str):
return ib_barsize, ib_duration


def _avoid_pacing_violation(
last_call_datetime: datetime.datetime, log: pst_logger = get_logger("")
):
def _avoid_pacing_violation(last_call_datetime: datetime.datetime, log=get_logger("")):
printed_warning_already = False
while _pause_for_pacing(last_call_datetime):
if not printed_warning_already:
Expand Down
Loading