-
Hi all That said I am having real difficulty with the callback functionality. I understand the asych concept and the posting of a series of requests to IB, it's the RESPONSE mechanism I am having trouble with. I just don't get it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
It's useful to remember that in an asynchronous setting, a program is not executed sequentially, i.e. in a linear fashion, but functions invocation is driven by external events, in this case response arrivals. In the case of An issue that arises in this context is how to pass data around different parts of a program, since it's not possible to use the usual function arguments nor their return values. Different solutions are available, like:
Here's an example using closures that stores an up-to-date snapshot of ticker prices in As the response handling is done in a separate using Jib
symbols = ["AAPL", "TSLA", "AMD", "INTC"]
# Data structure shared among the main program and the callbacks
mdata = Dict(tkr => Dict{String,Any}() for tkr in symbols)
wrap = Jib.Wrapper(
# These events are always sent
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"),
# Market Data specific events
tickPrice= function(tickerId, field, price, size, attrib)
mdata[symbols[tickerId]][field] = price
end,
tickSize= function(tickerId, field, size)
mdata[symbols[tickerId]][field] = size
end,
tickGeneric= function(tickerId, tickType, value)
mdata[symbols[tickerId]][tickType] = value
end,
tickString= function(tickerId, tickType, value)
mdata[symbols[tickerId]][tickType] = value
end,
marketDataType= function(reqId, marketDataType)
# Do nothing
end,
tickReqParams= function(tickerId, minTick, bboExchange, snapshotPermissions)
# Do nothing
end
)
ib = Jib.connect(4002, 1)
Jib.start_reader(ib, wrap)
for (idx, s) in enumerate(symbols)
contract = Jib.Contract(symbol=s, secType="STK", exchange="SMART", currency="USD")
Jib.reqMktData(ib, idx, contract, "", false)
end
# From this point on, mdata is kept up to date under the hood by the reader task
# It can be accessed and passed around
mdata
# or just AAPL bid
mdata["AAPL"]["BID"] |
Beta Was this translation helpful? Give feedback.
-
A question about approach. Why Dict and not dataframe? this has REALLY helped me a lot with my julia adventure. Thanks for doing such a great job. |
Beta Was this translation helpful? Give feedback.
It's useful to remember that in an asynchronous setting, a program is not executed sequentially, i.e. in a linear fashion, but functions invocation is driven by external events, in this case response arrivals.
In the case of
reqMktData()
, whatever needs to be done to handle the market data goes into the body of the functionstickPrice()
,tickSize()
and the like, of which the user has total control.An issue that arises in this context is how to pass data around different parts of a program, since it's no 8000 t possible to use the usual function arguments nor their return values. Different solutions are available, like: