Skip to content

Commit ddfc202

Browse files
authored
Deephaven Python v2 (#39)
* Deephaven 2 type annotations. * Ported table writer. * Ported times. * Ported PythonFunction. * Ported PythonFunction. * Ported NULL_DOUBLE. * Ported PythonFunction. * Ported Table import. * Ported dtypes. * Ported dtypes. * Ported StringSet. * Ported DateTime. * Ported Table import. * Debugging * Porting move_columns_up * Porting natural_join * Porting rename_columns, drop_columns * Porting select_distinct * Porting last_by * Porting update * Porting column names * Adding time parsing debugging. * Fix date time parsing * Improved time parsing error messages. * Ported example scripts. * Make thread tracing more robust to threads disappearing. * Updated readme.
1 parent 172f2ec commit ddfc202

File tree

16 files changed

+536
-464
lines changed

16 files changed

+536
-464
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ See [Access your file system with Docker data volumes](https://deephaven.io/core
144144

145145
Follow these steps to run a [Deephaven](https://deephaven.io) plus [Interactive Brokers](https://interactivebrokers.com) system.
146146

147-
`<deephaven_version>` is the version of [Deephaven](https://deephaven.io) to run (e.g., `0.10.0`). A list of available versions
148-
can be found on the [Deephaven Releases GitHub page](https://github.com/deephaven/deephaven-core/releases).
147+
`<deephaven_version>` is the version of [Deephaven](https://deephaven.io) to run (e.g., `0.11.0`). A list of available versions
148+
can be found on the [Deephaven Releases GitHub page](https://github.com/deephaven/deephaven-core/releases).
149+
Version `0.11.0` or higher must be used.
149150

150151
**Windows users need to run the commands in WSL.**
151152

@@ -371,7 +372,7 @@ Market data can be requested from the client using:
371372
```python
372373
from ibapi.contract import Contract
373374
374-
from deephaven import DateTimeUtils as dtu
375+
from deephaven.time import to_datetime
375376
376377
contract = Contract()
377378
contract.symbol = "GOOG"
@@ -382,8 +383,8 @@ contract.exchange = "SMART"
382383
rc = client.get_registered_contract(contract)
383384
print(contract)
384385
385-
start = dtu.convertDateTime("2021-01-01T00:00:00 NY")
386-
end = dtu.convertDateTime("2021-01-10T00:00:00 NY")
386+
start = to_datetime("2021-01-01T00:00:00 NY")
387+
end = to_datetime("2021-01-10T00:00:00 NY")
387388
client.request_news_historical(rc, start=start, end=end)
388389
389390
client.request_news_article(provider_code="BRFUPDN", article_id="BRFUPDN$107d53ea")
@@ -485,8 +486,8 @@ bars_realtime = client.tables["bars_realtime"]
485486
486487
bars_dia = bars_realtime.where("Symbol=`DIA`")
487488
bars_spy = bars_realtime.where("Symbol=`SPY`")
488-
bars_joined = bars_dia.view("Timestamp", "TimestampEnd", "Dia=Close") \
489-
.naturalJoin(bars_spy, "TimestampEnd", "Spy=Close") \
489+
bars_joined = bars_dia.view(["Timestamp", "TimestampEnd", "Dia=Close"]) \
490+
.natural_join(bars_spy, on="TimestampEnd", joins="Spy=Close") \
490491
.update("Ratio = Dia/Spy")
491492
```
492493
@@ -518,9 +519,10 @@ client.request_tick_data_realtime(rc, dhib.TickDataType.BID_ASK)
518519
519520
ticks_bid_ask = client.tables["ticks_bid_ask"]
520521
521-
from deephaven import Plot
522-
plot_aapl = Plot.plot("Bid", ticks_bid_ask, "ReceiveTime", "BidPrice") \
523-
.plot("Ask", ticks_bid_ask, "ReceiveTime", "AskPrice") \
522+
from deephaven.plot import Figure
523+
524+
plot_aapl = Figure().plot_xy("Bid", t=ticks_bid_ask, x="ReceiveTime", y="BidPrice") \
525+
.plot_xy("Ask", t=ticks_bid_ask, x="ReceiveTime", y="AskPrice") \
524526
.show()
525527
```
526528
@@ -561,6 +563,7 @@ A discussion of available logging levels can be found in the [Python `logging` m
561563
562564
If you can not solve your problems through either the `errors` table or through logging, you can try:
563565
566+
* [deephaven-ib API Documentation](https://deephaven-examples.github.io/deephaven-ib/)
564567
* [Interactive Brokers Support](https://www.interactivebrokers.com/en/support/individuals.php)
565568
* [Gitter: A relaxed chat room about all things Deephaven](https://gitter.im/deephaven/deephaven)
566569
* [Deephaven Community Slack](https://http://deephavencommunity.slack.com/)

docs/assets/overview.png

144 KB
Loading

examples/example_all_functionality.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict
22

3-
from deephaven import DateTimeUtils as dtu
3+
from deephaven.time import to_datetime
44
from ibapi.contract import Contract
55
from ibapi.order import Order
66

@@ -12,6 +12,7 @@
1212

1313
print("==============================================================================================================")
1414
print("==== Create a client and connect.")
15+
print("==== ** Accept the connection in TWS **")
1516
print("==============================================================================================================")
1617

1718
client = dhib.IbSessionTws(host="host.docker.internal", port=7497, client_id=0, download_short_rates=False, read_only=False)
@@ -22,6 +23,7 @@
2223

2324
print("==============================================================================================================")
2425
print("==== Get registered contracts for all contract types.")
26+
print("==== See https://interactivebrokers.github.io/tws-api/basic_contracts.html for details on supported contract types.")
2527
print("==============================================================================================================")
2628

2729

@@ -88,22 +90,22 @@ def get_contracts() -> Dict[str, Contract]:
8890
contract.secType = "FUT"
8991
contract.exchange = "GLOBEX"
9092
contract.currency = "USD"
91-
contract.lastTradeDateOrContractMonth = "202203"
93+
contract.lastTradeDateOrContractMonth = "202206"
9294
rst["future_1"] = contract
9395

9496
contract = Contract()
9597
contract.secType = "FUT"
9698
contract.exchange = "GLOBEX"
9799
contract.currency = "USD"
98-
contract.localSymbol = "ESH2"
100+
contract.localSymbol = "MESZ2"
99101
rst["future_2"] = contract
100102

101103
contract = Contract()
102104
contract.symbol = "DAX"
103105
contract.secType = "FUT"
104106
contract.exchange = "DTB"
105107
contract.currency = "EUR"
106-
contract.lastTradeDateOrContractMonth = "202203"
108+
contract.lastTradeDateOrContractMonth = "202206"
107109
contract.multiplier = "5"
108110
rst["future_3"] = contract
109111

@@ -126,7 +128,7 @@ def get_contracts() -> Dict[str, Contract]:
126128
contract.secType = "OPT"
127129
contract.exchange = "BOX"
128130
contract.currency = "USD"
129-
contract.lastTradeDateOrContractMonth = "20220318"
131+
contract.lastTradeDateOrContractMonth = "20230120"
130132
contract.strike = 2800
131133
contract.right = "C"
132134
contract.multiplier = "100"
@@ -159,7 +161,7 @@ def get_contracts() -> Dict[str, Contract]:
159161
contract.secType = "FOP"
160162
contract.exchange = "GLOBEX"
161163
contract.currency = "USD"
162-
contract.lastTradeDateOrContractMonth = "202203"
164+
contract.lastTradeDateOrContractMonth = "202206"
163165
contract.strike = 4700
164166
contract.right = "C"
165167
contract.multiplier = "50"
@@ -213,12 +215,12 @@ def get_contracts() -> Dict[str, Contract]:
213215

214216
# Dutch warrants and structured products
215217

216-
contract = Contract()
217-
contract.localSymbol = "PJ07S"
218-
contract.secType = "IOPT"
219-
contract.exchange = "SBF"
220-
contract.currency = "EUR"
221-
rst["dutchwarrant_1"] = contract
218+
# contract = Contract()
219+
# contract.localSymbol = "B881G"
220+
# contract.secType = "IOPT"
221+
# contract.exchange = "SBF"
222+
# contract.currency = "EUR"
223+
# rst["dutchwarrant_1"] = contract
222224

223225
return rst
224226

@@ -257,8 +259,8 @@ def get_contracts() -> Dict[str, Contract]:
257259
rc = client.get_registered_contract(contract)
258260
print(contract)
259261

260-
start = dtu.convertDateTime("2021-01-01T00:00:00 NY")
261-
end = dtu.convertDateTime("2021-01-10T00:00:00 NY")
262+
start = to_datetime("2021-01-01T00:00:00 NY")
263+
end = to_datetime("2021-01-10T00:00:00 NY")
262264
client.request_news_historical(rc, start=start, end=end)
263265

264266
client.request_news_article(provider_code="BRFUPDN", article_id="BRFUPDN$107d53ea")
@@ -316,7 +318,7 @@ def get_contracts() -> Dict[str, Contract]:
316318
rc = client.get_registered_contract(contract)
317319
print(contract)
318320

319-
now = dtu.convertDateTime("2021-01-01T00:00:00 NY")
321+
now = to_datetime("2021-01-01T00:00:00 NY")
320322

321323
client.request_tick_data_historical(rc, dhib.TickDataType.MIDPOINT, 100, start=now)
322324
client.request_tick_data_historical(rc, dhib.TickDataType.MIDPOINT, 100, end=now)
@@ -390,7 +392,7 @@ def get_contracts() -> Dict[str, Contract]:
390392
contract.secType = "OPT"
391393
contract.exchange = "BOX"
392394
contract.currency = "USD"
393-
contract.lastTradeDateOrContractMonth = "20220318"
395+
contract.lastTradeDateOrContractMonth = "20230120"
394396
contract.strike = 2800
395397
contract.right = "C"
396398
contract.multiplier = "100"

examples/example_market_data.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import deephaven_ib as dhib
44

5+
print("==============================================================================================================")
6+
print("==== ** Accept the connection in TWS **")
7+
print("==============================================================================================================")
8+
59
client = dhib.IbSessionTws(host="host.docker.internal", port=7497, download_short_rates=False)
610
client.connect()
711

examples/example_overview_image.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import deephaven_ib as dhib
2+
3+
print("==============================================================================================================")
4+
print("==== ** Accept the connection in TWS **")
5+
print("==============================================================================================================")
6+
7+
client = dhib.IbSessionTws(host="host.docker.internal", port=7497)
8+
client.connect()
9+
10+
from ibapi.contract import Contract
11+
12+
c1 = Contract()
13+
c1.symbol = 'DIA'
14+
c1.secType = 'STK'
15+
c1.exchange = 'SMART'
16+
c1.currency = 'USD'
17+
18+
rc1 = client.get_registered_contract(c1)
19+
print(rc1)
20+
21+
c2 = Contract()
22+
c2.symbol = 'SPY'
23+
c2.secType = 'STK'
24+
c2.exchange = 'SMART'
25+
c2.currency = 'USD'
26+
27+
rc2 = client.get_registered_contract(c2)
28+
print(rc2)
29+
30+
client.set_market_data_type(dhib.MarketDataType.REAL_TIME)
31+
client.request_market_data(rc1)
32+
client.request_market_data(rc2)
33+
client.request_bars_realtime(rc1, bar_type=dhib.BarDataType.MIDPOINT)
34+
client.request_bars_realtime(rc2, bar_type=dhib.BarDataType.MIDPOINT)
35+
36+
bars_realtime = client.tables["bars_realtime"]
37+
38+
bars_dia = bars_realtime.where("Symbol=`DIA`")
39+
bars_spy = bars_realtime.where("Symbol=`SPY`")
40+
bars_joined = bars_dia.view(["Timestamp", "TimestampEnd", "Dia=Close"]) \
41+
.natural_join(bars_spy, on="TimestampEnd", joins="Spy=Close") \
42+
.update("Ratio = Dia/Spy")
43+
44+
from deephaven.plot import Figure
45+
46+
plot_prices = Figure().plot_xy("DIA", t=bars_dia, x="TimestampEnd", y="Close") \
47+
.x_twin() \
48+
.plot_xy("SPY", t=bars_dia, x="TimestampEnd", y="Close") \
49+
.show()
50+
51+
plot_ratio = Figure().plot_xy("Ratio", t=bars_joined, x="TimestampEnd", y="Ratio") \
52+
.show()
53+
54+

examples/example_query_and_plot.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import deephaven_ib as dhib
22

3+
print("==============================================================================================================")
4+
print("==== ** Accept the connection in TWS **")
5+
print("==============================================================================================================")
6+
37
client = dhib.IbSessionTws(host="host.docker.internal", port=7497)
48
client.connect()
59

@@ -33,18 +37,18 @@
3337

3438
bars_dia = bars_realtime.where("Symbol=`DIA`")
3539
bars_spy = bars_realtime.where("Symbol=`SPY`")
36-
bars_joined = bars_dia.view("Timestamp", "TimestampEnd", "Dia=Close") \
37-
.naturalJoin(bars_spy, "TimestampEnd", "Spy=Close") \
40+
bars_joined = bars_dia.view(["Timestamp", "TimestampEnd", "Dia=Close"]) \
41+
.natural_join(bars_spy, on="TimestampEnd", joins="Spy=Close") \
3842
.update("Ratio = Dia/Spy")
3943

40-
from deephaven import Plot
44+
from deephaven.plot import Figure
4145

42-
plot_prices = Plot.plot("DIA", bars_dia, "TimestampEnd", "Close") \
43-
.twinX() \
44-
.plot("SPY", bars_dia, "TimestampEnd", "Close") \
46+
plot_prices = Figure().plot_xy("DIA", t=bars_dia, x="TimestampEnd", y="Close") \
47+
.x_twin() \
48+
.plot_xy("SPY", t=bars_spy, x="TimestampEnd", y="Close") \
4549
.show()
4650

47-
plot_ratio = Plot.plot("Ratio", bars_joined, "TimestampEnd", "Ratio") \
51+
plot_ratio = Figure().plot_xy("Ratio", t=bars_joined, x="TimestampEnd", y="Ratio") \
4852
.show()
4953

5054

0 commit comments

Comments
 (0)