Skip to content
Open
Changes from all commits
Commits
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
11 changes: 10 additions & 1 deletion brokers/sinopac.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,16 @@ def _get_contract(self, stock_id: str) -> Any:
pass
stocks = getattr(getattr(self.api, "Contracts", None), "Stocks", None)
if stocks is not None:
return stocks[stock_id]
try:
contract = stocks[stock_id]
except KeyError:
contract = None
if contract is not None:
return contract
# Contracts are not populated yet. This happens on shioaji < 1.7 where
# login() used fetch_contract=False and no contracts were downloaded, so
# the lookup above misses even for actively traded stocks. Fetch on
# demand and retry instead of surfacing a bare "Contract not found".
self.api.fetch_contracts(contract_download=sj.constant.SecurityType.Stock)
return self.api.Contracts.Stocks[stock_id]

Expand Down