Skip to content

Commit

Permalink
Dh 0 16 release fixes (#94)
Browse files Browse the repository at this point in the history
* Fix dev docker build.

* Add timezones to IB formatted times.

* Fix missing imports and Optional annotations.
Remove deprecated fields from logs.
Fix broken examples.

* Fix broken example.
  • Loading branch information
chipkent authored Sep 9, 2022
1 parent 379dedb commit cc77c4d
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 39 deletions.
1 change: 1 addition & 0 deletions docker/dev/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cd ${__dir}
rm -rf build
mkdir build
rsync -av ../.. build --exclude docker
rm -rf build/dist

docker build --build-arg IB_VERSION=1016.01 -t deephaven-examples/deephaven-ib:dev -f Dockerfile .

Expand Down
12 changes: 3 additions & 9 deletions examples/example_all_functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,13 @@ def get_contracts() -> Dict[str, Contract]:
contract.exchange = "PAXOS"
contract.currency = "USD"

rc = client.get_registered_contract(c2)
client.request_bars_historical(rc2, duration=dhib.Duration.days(253),
rc = client.get_registered_contract(contract)
client.request_bars_historical(rc, duration=dhib.Duration.days(253),
bar_size=dhib.BarSize.DAY_1,
bar_type=dhib.BarDataType.AGGTRADES,
keep_up_to_date = False
)
client.request_bars_realtime(rc2, bar_type=dhib.BarDataType.TRADES)
client.request_bars_realtime(rc, bar_type=dhib.BarDataType.TRADES)

client.request_bars_historical(rc, duration=dhib.Duration.days(10), bar_size=dhib.BarSize.MIN_5,
bar_type=dhib.BarDataType.MIDPOINT)
Expand Down Expand Up @@ -483,8 +483,6 @@ def get_contracts() -> Dict[str, Contract]:
order.orderType = "LIMIT"
order.totalQuantity = 1
order.lmtPrice = 3000
order.eTradeOnly = False
order.firmQuoteOnly = False

print("Placing order: START")
client.order_place(rc, order)
Expand All @@ -496,8 +494,6 @@ def get_contracts() -> Dict[str, Contract]:
order.orderType = "LIMIT"
order.totalQuantity = 1
order.lmtPrice = 2600
order.eTradeOnly = False
order.firmQuoteOnly = False

print("Placing order: START")
client.order_place(rc, order)
Expand All @@ -509,8 +505,6 @@ def get_contracts() -> Dict[str, Contract]:
order.orderType = "LIMIT"
order.totalQuantity = 1
order.lmtPrice = 2700
order.eTradeOnly = False
order.firmQuoteOnly = False

print("Placing order: START")
req = client.order_place(rc, order)
Expand Down
2 changes: 0 additions & 2 deletions examples/example_beta_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ def check_table_size(dh_table, table_name, expected_size=1):
order.orderType = "LIMIT"
order.totalQuantity = hedge_qty
order.lmtPrice = hedge_limit_px
order.eTradeOnly = False
order.firmQuoteOnly = False

print('Order: ' + str(order))

Expand Down
12 changes: 3 additions & 9 deletions examples/example_read_only_functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,13 @@ def get_contracts() -> Dict[str, Contract]:
contract.exchange = "PAXOS"
contract.currency = "USD"

rc = client.get_registered_contract(c2)
client.request_bars_historical(rc2, duration=dhib.Duration.days(253),
rc = client.get_registered_contract(contract)
client.request_bars_historical(rc, duration=dhib.Duration.days(253),
bar_size=dhib.BarSize.DAY_1,
bar_type=dhib.BarDataType.AGGTRADES,
keep_up_to_date = False
)
client.request_bars_realtime(rc2, bar_type=dhib.BarDataType.TRADES)
client.request_bars_realtime(rc, bar_type=dhib.BarDataType.TRADES)

client.request_bars_historical(rc, duration=dhib.Duration.days(10), bar_size=dhib.BarSize.MIN_5,
bar_type=dhib.BarDataType.MIDPOINT)
Expand Down Expand Up @@ -479,8 +479,6 @@ def get_contracts() -> Dict[str, Contract]:
order.orderType = "LIMIT"
order.totalQuantity = 1
order.lmtPrice = 3000
order.eTradeOnly = False
order.firmQuoteOnly = False

print("Placing order -- confirm fail: START")
try:
Expand All @@ -498,8 +496,6 @@ def get_contracts() -> Dict[str, Contract]:
order.orderType = "LIMIT"
order.totalQuantity = 1
order.lmtPrice = 2600
order.eTradeOnly = False
order.firmQuoteOnly = False

print("Placing order -- confirm fail: START")
try:
Expand All @@ -517,8 +513,6 @@ def get_contracts() -> Dict[str, Contract]:
order.orderType = "LIMIT"
order.totalQuantity = 1
order.lmtPrice = 2700
order.eTradeOnly = False
order.firmQuoteOnly = False

print("Placing order -- confirm fail: START")
try:
Expand Down
7 changes: 4 additions & 3 deletions src/deephaven_ib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum
from typing import Dict, List, Callable
from typing import Dict, List, Callable, Optional
import json

from deephaven.table import Table
from deephaven.dtypes import DateTime
Expand Down Expand Up @@ -546,7 +547,7 @@ def annotate_ticks(t):

return rst

def deephaven_ib_float_value(s: str) -> Union[float, None]:
def deephaven_ib_float_value(s: str) -> Optional[float]:
if not s:
return NULL_DOUBLE

Expand All @@ -555,7 +556,7 @@ def deephaven_ib_float_value(s: str) -> Union[float, None]:
except ValueError:
return NULL_DOUBLE

def deephaven_ib_parse_note(note:str, key:str) -> Union[str,None]:
def deephaven_ib_parse_note(note:str, key:str) -> Optional[str]:
dict = json.loads(note)

if key in dict:
Expand Down
6 changes: 3 additions & 3 deletions src/deephaven_ib/_internal/tablewriter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Functionality for creating Deephaven tables."""

import logging
from typing import List, Any, Sequence, Union, Set
from typing import List, Any, Sequence, Optional, Set
import collections

from deephaven.time import now
Expand Down Expand Up @@ -114,7 +114,7 @@ def map_values(value, map, default=lambda v: f"UNKNOWN({v})") -> Any:
return default(value)


def to_string_val(value) -> Union[str, None]:
def to_string_val(value) -> Optional[str]:
""" Converts a value to a string. """

if value is None:
Expand All @@ -123,7 +123,7 @@ def to_string_val(value) -> Union[str, None]:
return str(value)


def to_string_set(value: Sequence) -> Union[ArrayStringSet, None]:
def to_string_set(value: Sequence) -> Optional[ArrayStringSet]:
""" Converts an iterable to a string set. """

if value is None:
Expand Down
13 changes: 5 additions & 8 deletions src/deephaven_ib/_tws/ib_type_logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Functionality for logging IB types to Deephaven tables."""

import sys
from typing import Any, List, Tuple, Dict, Callable, Union
from typing import Any, List, Tuple, Dict, Callable, Optional

from deephaven import dtypes

Expand Down Expand Up @@ -73,7 +73,7 @@ def _details_family_code() -> List[Tuple]:
def _details_contract() -> List[Tuple]:
""" Details for logging Contract. """

def map_right(right: str) -> Union[str, None]:
def map_right(right: str) -> Optional[str]:
if right == "?":
return None

Expand Down Expand Up @@ -110,7 +110,7 @@ def map_right(right: str) -> Union[str, None]:
def _details_contract_details() -> List[Tuple]:
"""Details for logging ContractDetails."""

def map_null_int(value: int) -> Union[int, None]:
def map_null_int(value: int) -> Optional[int]:

if value == 2147483647:
return None
Expand Down Expand Up @@ -445,9 +445,6 @@ def _details_order() -> List[Tuple]:

# SMART routing only
("DiscretionaryAmt", dtypes.float64, lambda o: o.discretionaryAmt),
("ETradeOnly", dtypes.bool_, lambda o: o.eTradeOnly),
("FirmQuoteOnly", dtypes.bool_, lambda o: o.firmQuoteOnly),
("NbboPriceCap", dtypes.float64, lambda o: o.nbboPriceCap),
("OptOutSmarRouting", dtypes.bool_, lambda o: o.optOutSmartRouting),

# BOX exchange orders only
Expand Down Expand Up @@ -650,7 +647,7 @@ def _details_execution() -> List[Tuple]:
def _details_commission_report() -> List[Tuple]:
""" Details for logging CommissionReport. """

def format_yield_redemption_date(date: int) -> Union[str, None]:
def format_yield_redemption_date(date: int) -> Optional[str]:
if date == 0:
return None

Expand All @@ -660,7 +657,7 @@ def format_yield_redemption_date(date: int) -> Union[str, None]:
y = int(date / 10000)
return f"{y:04}-{m:02}-{d:02}"

def map_null_value(value: float) -> Union[float, None]:
def map_null_value(value: float) -> Optional[float]:

if value == sys.float_info.max:
return None
Expand Down
6 changes: 3 additions & 3 deletions src/deephaven_ib/_tws/tws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import xml.etree.ElementTree as ET
from functools import wraps
from threading import Thread
from typing import Set
from typing import Set, Optional

import decimal
from decimal import Decimal
Expand Down Expand Up @@ -392,8 +392,8 @@ def _subscribe(self) -> None:
####################################################################################################################
####################################################################################################################

def log_request(self, req_id: int, request_type: str, contract: Union[Contract, None],
notes: Union[Dict[str, Any], None]):
def log_request(self, req_id: int, request_type: str, contract: Optional[Contract],
notes: Optional[Dict[str, Any]]):
"""Log a data request."""

if notes is None:
Expand Down
9 changes: 7 additions & 2 deletions src/deephaven_ib/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from deephaven.dtypes import DateTime

_SimpleDateFormat = jpy.get_type("java.text.SimpleDateFormat")
_TimeZone = jpy.get_type("java.util.TimeZone")

_ib_date_time_pattern_sec = "yyyyMMdd HH:mm:ss"
_ib_date_time_pattern_subsec = "yyyyMMdd HH:mm:ss.S"
Expand All @@ -15,9 +16,13 @@
"yyyy-MM-dd HH:mm:ss",
]
_ib_date_time_formatter_sec = _SimpleDateFormat(_ib_date_time_pattern_sec)
_ib_date_time_formatter_sec.setTimeZone(_TimeZone.getTimeZone("US/Eastern"))
_ib_date_time_formatter_subsec = _SimpleDateFormat(_ib_date_time_pattern_subsec)
_ib_date_time_formatter_subsec.setTimeZone(_TimeZone.getTimeZone("US/Eastern"))
_ib_date_time_formatters = [_SimpleDateFormat(pattern) for pattern in _ib_date_time_patterns]

for _f in _ib_date_time_formatters:
_f.setTimeZone(_TimeZone.getTimeZone("US/Eastern"))

def dh_to_ib_datetime(time: DateTime, sub_sec: bool = True) -> str:
"""Convert a DH DateTime to an IB timestamp string.
Expand All @@ -31,9 +36,9 @@ def dh_to_ib_datetime(time: DateTime, sub_sec: bool = True) -> str:
return ""

if sub_sec:
return _ib_date_time_formatter_subsec.format(time.getDate())
return _ib_date_time_formatter_subsec.format(time.getDate()) + " US/Eastern"
else:
return _ib_date_time_formatter_sec.format(time.getDate())
return _ib_date_time_formatter_sec.format(time.getDate()) + " US/Eastern"


def ib_to_dh_datetime(time: str) -> DateTime:
Expand Down

0 comments on commit cc77c4d

Please sign in to comment.