calling d[:history] in a for loop #40
-
Appreciate any time you can give to what's probably a simple issue.
This gives
The REPL is kept open however, and if I type Benefited a lot from this answer of yours: This library is pretty awesome, and I'm really appreciating how thorough everything is and how you have responded to new users. Thanks a lot. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The main issue is that IB API is designed around an asynchronous communication model: i.e. conceptually, sending and receiving data are handled by separate processes which are not in sync. In particular, functions that send requests return immediately, without waiting for responses to arrive. Some general references on the topic from Julia docs and the IB API docs. In your example, the attempt to access A quick and dirty, certainly frowned upon and ultimately wrong fix is to insert a In general, the coordination between processes requires more planning and a different mindset compared to the usual deterministic sequential execution and involves the use of tools like An quick alternative to your example (which still involves a using DataFrames, Jib
symbols = ["EUR", "CHF", "NZD"]
history = Dict{String,DataFrame}()
wrap = Jib.Wrapper(
# Save history
historicalData= function(reqId, bar)
println("history $reqId")
history[symbols[reqId]] = bar
end,
# Boilerplate methods
error= (id, errorCode, errorString, advancedOrderRejectJson) ->
println("Error: $(something(id, "NA")) $errorCode $errorString $advancedOrderRejectJson"),
nextValidId= (orderId) -> println("Next OrderId: $orderId"),
managedAccounts= (accountsList) -> println("Managed Accounts: $accountsList")
)
ib = Jib.connect(4002,22)
Jib.start_reader(ib, wrap)
for (idx, s) in enumerate(symbols)
contract = Jib.Contract(symbol=s, secType="CASH", exchange="IDEALPRO", currency="USD")
Jib.reqHistoricalData(ib, idx, contract, "", "1 D", "10 mins", "MIDPOINT", true, 1, false)
end
# Wait up to 10s
res = timedwait(() -> length(history) == length(symbols), 10)
if res == :ok
# All data has arrived
history["EUR"]
history["CHF"]
# ...
else
# Data has not arrived, maybe needs more time...
# Do something else
end |
Beta Was this translation helpful? Give feedback.
The main issue is that IB API is designed around an asynchronous communication model: i.e. conceptually, sending and receiving data are handled by separate processes which are not in sync.
In particular, functions that send requests return immediately, without waiting for responses to arrive.
Responses, if any, arrive at unpredictable times and are handled in a separate line of execution ("reader process").
Some general references on the topic from Julia docs and the IB API docs.
In your example, the attempt to access
d[history]
happens right after sending the request, when the requested data has not arrived yet (in fact the request itself may not even have left your computer!).A quick a…