Skip to content

Commit f587e94

Browse files
committed
Reformat for style
`ruff format` via ruff 0.11.13
1 parent 0a6c078 commit f587e94

File tree

12 files changed

+154
-114
lines changed

12 files changed

+154
-114
lines changed

ib_async/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,10 @@ def placeOrder(self, orderId, contract, order):
482482
# ONLY volatility orders can have 'volatility' set when sending API data.
483483
order.volatility = None
484484

485+
# The IBKR API protocol is just a series of in-order arguments denoted by position.
486+
# The upstream API parses all fields based on the first value (the message type).
485487
fields = [
486-
3,
488+
3, # PLACE_ORDER message type
487489
orderId,
488490
contract,
489491
contract.secIdType,
@@ -558,9 +560,9 @@ def placeOrder(self, orderId, contract, order):
558560
order.allOrNone,
559561
order.minQty,
560562
order.percentOffset,
561-
order.eTradeOnly,
562-
order.firmQuoteOnly,
563-
order.nbboPriceCap,
563+
order.eTradeOnly, # always False
564+
order.firmQuoteOnly, # always False
565+
order.nbboPriceCap, # always UNSET
564566
order.auctionStrategy,
565567
order.startingPrice,
566568
order.stockRefPrice,

ib_async/ib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ def cancelMktData(self, contract: Contract) -> bool:
14421442
self.client.cancelMktData(reqId)
14431443
return True
14441444

1445-
self._logger.error("cancelMktData: " f"No reqId found for contract {contract}")
1445+
self._logger.error(f"cancelMktData: No reqId found for contract {contract}")
14461446

14471447
return False
14481448

@@ -2160,7 +2160,7 @@ async def qualifyContractsAsync(
21602160
possibles = [details.contract for details in detailsList]
21612161

21622162
self._logger.warning(
2163-
f"Ambiguous contract: {contract}, " f"possibles are {possibles}"
2163+
f"Ambiguous contract: {contract}, possibles are {possibles}"
21642164
)
21652165

21662166
if returnAll:

ib_async/order.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ def total(self) -> float:
296296
WorkingStates: ClassVar[frozenset[str]] = frozenset(
297297
[
298298
"Submitted",
299+
# ValidationError can happen on submit or modify.
300+
# If ValidationError happens on submit, the states go PreSubmitted -> ValidationError -> Submitted (if it can be ignored automatically), so order is still live.
301+
# If ValidationError happens on modify, the update is just ValidationError with no new Submitted, so the previous order state remains active.
299302
"ValidationError",
300303
"ApiUpdate",
301304
]

ib_async/wrapper.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def orderStatus(
700700
trade.cancelledEvent.emit(trade)
701701
else:
702702
self._logger.error(
703-
"orderStatus: No order found for " "orderId %s and clientId %s",
703+
"orderStatus: No order found for orderId %s and clientId %s",
704704
orderId,
705705
clientId,
706706
)
@@ -944,9 +944,9 @@ def priceSizeTick(self, reqId: int, tickType: int, price: float, size: float):
944944
# self._logger.error(f"[{tickType=}] SET {ticker.prevLast=} = {ticker.last=}; {ticker.prevLastSize=} = {ticker.lastSize=}")
945945
# self._logger.error(f"[{tickType=}] updating last price size: {price=} {size=} :: AFTER {ticker=}")
946946
else:
947-
assert (
948-
tickType in PRICE_TICK_MAP
949-
), f"Received tick {tickType=} {price=} but we don't have an attribute mapping for it? Triggered from {ticker.contract=}"
947+
assert tickType in PRICE_TICK_MAP, (
948+
f"Received tick {tickType=} {price=} but we don't have an attribute mapping for it? Triggered from {ticker.contract=}"
949+
)
950950

951951
setattr(ticker, PRICE_TICK_MAP[tickType], price)
952952

@@ -1001,9 +1001,9 @@ def tickSize(self, reqId: int, tickType: int, size: float):
10011001
ticker.prevLastSize = ticker.lastSize
10021002
ticker.lastSize = size
10031003
else:
1004-
assert (
1005-
tickType in SIZE_TICK_MAP
1006-
), f"Received tick {tickType=} {size=} but we don't have an attribute mapping for it? Triggered from {ticker.contract=}"
1004+
assert tickType in SIZE_TICK_MAP, (
1005+
f"Received tick {tickType=} {size=} but we don't have an attribute mapping for it? Triggered from {ticker.contract=}"
1006+
)
10071007

10081008
setattr(ticker, SIZE_TICK_MAP[tickType], size)
10091009

@@ -1182,14 +1182,13 @@ def tickString(self, reqId: int, tickType: int, value: str):
11821182
)
11831183
else:
11841184
self._logger.error(
1185-
f"tickString with tickType {tickType}: "
1186-
f"unhandled value: {value!r}"
1185+
f"tickString with tickType {tickType}: unhandled value: {value!r}"
11871186
)
11881187

11891188
self.pendingTickers.add(ticker)
11901189
except ValueError:
11911190
self._logger.error(
1192-
f"tickString with tickType {tickType}: " f"malformed value: {value!r}"
1191+
f"tickString with tickType {tickType}: malformed value: {value!r}"
11931192
)
11941193

11951194
def tickGeneric(self, reqId: int, tickType: int, value: float):
@@ -1206,9 +1205,9 @@ def tickGeneric(self, reqId: int, tickType: int, value: float):
12061205
)
12071206
return
12081207

1209-
assert (
1210-
tickType in GENERIC_TICK_MAP
1211-
), f"Received tick {tickType=} {value=} but we don't have an attribute mapping for it? Triggered from {ticker.contract=}"
1208+
assert tickType in GENERIC_TICK_MAP, (
1209+
f"Received tick {tickType=} {value=} but we don't have an attribute mapping for it? Triggered from {ticker.contract=}"
1210+
)
12121211

12131212
setattr(ticker, GENERIC_TICK_MAP[tickType], value)
12141213

@@ -1336,9 +1335,9 @@ def tickOptionComputation(
13361335
# reply from reqMktData
13371336
# https://interactivebrokers.github.io/tws-api/tick_types.html
13381337

1339-
assert (
1340-
tickType in GREEKS_TICK_MAP
1341-
), f"Received tick {tickType=} {tickAttrib=} but we don't have an attribute mapping for it? Triggered from {ticker.contract=}"
1338+
assert tickType in GREEKS_TICK_MAP, (
1339+
f"Received tick {tickType=} {tickAttrib=} but we don't have an attribute mapping for it? Triggered from {ticker.contract=}"
1340+
)
13421341

13431342
setattr(ticker, GREEKS_TICK_MAP[tickType], comp)
13441343
self.pendingTickers.add(ticker)
@@ -1542,7 +1541,7 @@ def error(
15421541
# invalid price for a new order must cancel it
15431542
isWarning = False
15441543

1545-
msg = f'{"Warning" if isWarning else "Error"} {errorCode}, reqId {reqId}: {errorString}'
1544+
msg = f"{'Warning' if isWarning else 'Error'} {errorCode}, reqId {reqId}: {errorString}"
15461545

15471546
contract = self._reqId2Contract.get(reqId)
15481547
if contract:

notebooks/bar_data.ipynb

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
],
2626
"source": [
2727
"from ib_async import *\n",
28+
"\n",
2829
"util.startLoop()\n",
2930
"\n",
3031
"ib = IB()\n",
31-
"ib.connect('127.0.0.1', 7497, clientId=14)"
32+
"ib.connect(\"127.0.0.1\", 7497, clientId=14)"
3233
]
3334
},
3435
{
@@ -62,9 +63,9 @@
6263
}
6364
],
6465
"source": [
65-
"contract = Stock('TSLA', 'SMART', 'USD')\n",
66+
"contract = Stock(\"TSLA\", \"SMART\", \"USD\")\n",
6667
"\n",
67-
"ib.reqHeadTimeStamp(contract, whatToShow='TRADES', useRTH=True)"
68+
"ib.reqHeadTimeStamp(contract, whatToShow=\"TRADES\", useRTH=True)"
6869
]
6970
},
7071
{
@@ -81,13 +82,14 @@
8182
"outputs": [],
8283
"source": [
8384
"bars = ib.reqHistoricalData(\n",
84-
" contract,\n",
85-
" endDateTime='',\n",
86-
" durationStr='60 D',\n",
87-
" barSizeSetting='1 hour',\n",
88-
" whatToShow='TRADES',\n",
89-
" useRTH=True,\n",
90-
" formatDate=1)"
85+
" contract,\n",
86+
" endDateTime=\"\",\n",
87+
" durationStr=\"60 D\",\n",
88+
" barSizeSetting=\"1 hour\",\n",
89+
" whatToShow=\"TRADES\",\n",
90+
" useRTH=True,\n",
91+
" formatDate=1,\n",
92+
")"
9193
]
9294
},
9395
{
@@ -392,7 +394,7 @@
392394
}
393395
],
394396
"source": [
395-
"df.plot(y='close');"
397+
"df.plot(y=\"close\");"
396398
]
397399
},
398400
{
@@ -443,17 +445,18 @@
443445
"metadata": {},
444446
"outputs": [],
445447
"source": [
446-
"contract = Forex('EURUSD')\n",
448+
"contract = Forex(\"EURUSD\")\n",
447449
"\n",
448450
"bars = ib.reqHistoricalData(\n",
449-
" contract,\n",
450-
" endDateTime='',\n",
451-
" durationStr='900 S',\n",
452-
" barSizeSetting='10 secs',\n",
453-
" whatToShow='MIDPOINT',\n",
454-
" useRTH=True,\n",
455-
" formatDate=1,\n",
456-
" keepUpToDate=True)"
451+
" contract,\n",
452+
" endDateTime=\"\",\n",
453+
" durationStr=\"900 S\",\n",
454+
" barSizeSetting=\"10 secs\",\n",
455+
" whatToShow=\"MIDPOINT\",\n",
456+
" useRTH=True,\n",
457+
" formatDate=1,\n",
458+
" keepUpToDate=True,\n",
459+
")"
457460
]
458461
},
459462
{
@@ -497,12 +500,14 @@
497500
"from IPython.display import display, clear_output\n",
498501
"import matplotlib.pyplot as plt\n",
499502
"\n",
503+
"\n",
500504
"def onBarUpdate(bars, hasNewBar):\n",
501505
" plt.close()\n",
502506
" plot = util.barplot(bars)\n",
503507
" clear_output(wait=True)\n",
504508
" display(plot)\n",
505509
"\n",
510+
"\n",
506511
"bars.updateEvent += onBarUpdate\n",
507512
"\n",
508513
"ib.sleep(10)\n",
@@ -544,7 +549,7 @@
544549
"metadata": {},
545550
"outputs": [],
546551
"source": [
547-
"bars = ib.reqRealTimeBars(contract, 5, 'MIDPOINT', False)\n",
552+
"bars = ib.reqRealTimeBars(contract, 5, \"MIDPOINT\", False)\n",
548553
"bars.updateEvent += onBarUpdate"
549554
]
550555
},

notebooks/basics.ipynb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
],
2525
"source": [
2626
"import ib_async\n",
27+
"\n",
2728
"print(ib_async.__all__)"
2829
]
2930
},
@@ -43,6 +44,7 @@
4344
"outputs": [],
4445
"source": [
4546
"from ib_async import *\n",
47+
"\n",
4648
"util.startLoop()"
4749
]
4850
},
@@ -81,7 +83,7 @@
8183
],
8284
"source": [
8385
"ib = IB()\n",
84-
"ib.connect('127.0.0.1', 7497, clientId=10)"
86+
"ib.connect(\"127.0.0.1\", 7497, clientId=10)"
8587
]
8688
},
8789
{
@@ -137,7 +139,11 @@
137139
}
138140
],
139141
"source": [
140-
"[v for v in ib.accountValues() if v.tag == 'NetLiquidationByCurrency' and v.currency == 'BASE']"
142+
"[\n",
143+
" v\n",
144+
" for v in ib.accountValues()\n",
145+
" if v.tag == \"NetLiquidationByCurrency\" and v.currency == \"BASE\"\n",
146+
"]"
141147
]
142148
},
143149
{
@@ -169,13 +175,13 @@
169175
"outputs": [],
170176
"source": [
171177
"Contract(conId=270639)\n",
172-
"Stock('AMD', 'SMART', 'USD')\n",
173-
"Stock('INTC', 'SMART', 'USD', primaryExchange='NASDAQ')\n",
174-
"Forex('EURUSD')\n",
175-
"CFD('IBUS30')\n",
176-
"Future('ES', '20180921', 'GLOBEX')\n",
177-
"Option('SPY', '20170721', 240, 'C', 'SMART')\n",
178-
"Bond(secIdType='ISIN', secId='US03076KAA60');"
178+
"Stock(\"AMD\", \"SMART\", \"USD\")\n",
179+
"Stock(\"INTC\", \"SMART\", \"USD\", primaryExchange=\"NASDAQ\")\n",
180+
"Forex(\"EURUSD\")\n",
181+
"CFD(\"IBUS30\")\n",
182+
"Future(\"ES\", \"20180921\", \"GLOBEX\")\n",
183+
"Option(\"SPY\", \"20170721\", 240, \"C\", \"SMART\")\n",
184+
"Bond(secIdType=\"ISIN\", secId=\"US03076KAA60\");"
179185
]
180186
},
181187
{
@@ -204,7 +210,7 @@
204210
}
205211
],
206212
"source": [
207-
"contract = Stock('TSLA', 'SMART', 'USD')\n",
213+
"contract = Stock(\"TSLA\", \"SMART\", \"USD\")\n",
208214
"ib.reqContractDetails(contract)"
209215
]
210216
},
@@ -285,6 +291,7 @@
285291
"outputs": [],
286292
"source": [
287293
"import logging\n",
294+
"\n",
288295
"util.logToConsole(logging.DEBUG)"
289296
]
290297
},

notebooks/contract_details.ipynb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
],
2727
"source": [
2828
"from ib_async import *\n",
29+
"\n",
2930
"util.startLoop()\n",
3031
"\n",
3132
"import logging\n",
3233
"# util.logToConsole(logging.DEBUG)\n",
3334
"\n",
3435
"ib = IB()\n",
35-
"ib.connect('127.0.0.1', 7497, clientId=11)"
36+
"ib.connect(\"127.0.0.1\", 7497, clientId=11)"
3637
]
3738
},
3839
{
@@ -60,7 +61,7 @@
6061
}
6162
],
6263
"source": [
63-
"amd = Stock('AMD')\n",
64+
"amd = Stock(\"AMD\")\n",
6465
"\n",
6566
"cds = ib.reqContractDetails(amd)\n",
6667
"\n",
@@ -1090,7 +1091,7 @@
10901091
"metadata": {},
10911092
"outputs": [],
10921093
"source": [
1093-
"amd = Stock('AMD', 'SMART', 'USD')\n",
1094+
"amd = Stock(\"AMD\", \"SMART\", \"USD\")\n",
10941095
"\n",
10951096
"assert len(ib.reqContractDetails(amd)) == 1"
10961097
]
@@ -1108,7 +1109,7 @@
11081109
"metadata": {},
11091110
"outputs": [],
11101111
"source": [
1111-
"intc = Stock('INTC', 'SMART', 'USD')\n",
1112+
"intc = Stock(\"INTC\", \"SMART\", \"USD\")\n",
11121113
"\n",
11131114
"assert len(ib.reqContractDetails(intc)) == 1"
11141115
]
@@ -1134,7 +1135,7 @@
11341135
}
11351136
],
11361137
"source": [
1137-
"xxx = Stock('XXX', 'SMART', 'USD')\n",
1138+
"xxx = Stock(\"XXX\", \"SMART\", \"USD\")\n",
11381139
"\n",
11391140
"assert len(ib.reqContractDetails(xxx)) == 0"
11401141
]
@@ -1152,7 +1153,7 @@
11521153
"metadata": {},
11531154
"outputs": [],
11541155
"source": [
1155-
"eurusd = Forex('EURUSD')\n",
1156+
"eurusd = Forex(\"EURUSD\")\n",
11561157
"\n",
11571158
"assert len(ib.reqContractDetails(eurusd)) == 1"
11581159
]
@@ -1300,7 +1301,7 @@
13001301
}
13011302
],
13021303
"source": [
1303-
"matches = ib.reqMatchingSymbols('intc')\n",
1304+
"matches = ib.reqMatchingSymbols(\"intc\")\n",
13041305
"matchContracts = [m.contract for m in matches]\n",
13051306
"\n",
13061307
"matches"

0 commit comments

Comments
 (0)