lbilli / jib.jl Goto Github PK
View Code? Open in Web Editor NEWA Julia implementation of Interactive Brokers API
License: MIT License
A Julia implementation of Interactive Brokers API
License: MIT License
Line 32 in ecce955
My machine (Windows 10) responds with "Central Standard Time" here.
Remove the functionality to infer the time zone that TWS is using, which is set at login time.
Let then the user be responsible of keeping track of it.
IB is also moving away from implicit time zones and this feature's cons might outweigh the pros.
I get this error from IB server after calling Jib.Requests.reqRealTimeBars(ib, 2, Jib.Contract(;conId=544894145756509053,exchange="SMART") , 5, "TRADES", true)
Error: 2 320 Erreur de lecture de la demande.Message id 2. Unable to parse data. java.lang.NumberFormatException: For input string: "544894145756509053"
Versions :
IB gateway :
Build 981.2r, Feb 10, 2021 10:36:16 AM
Java Version: 1.8.0_152, OS: Linux (amd64, 5.4.101-1-MANJARO)
Jib
[f310f2d2] Jib v0.11.1 https://github.com/lbilli/Jib.jl#master
I'm having some trouble connecting to the API.
julia> ib = Jib.connect("172.20.176.1", 7496, 1)
ERROR: EOFError: read end of file
Stacktrace:
[1] (::Base.var"#wait_locked#715")(s::Sockets.TCPSocket, buf::IOBuffer, nb::Int64)
@ Base ./stream.jl:947
[2] unsafe_read(s::Sockets.TCPSocket, p::Ptr{UInt8}, nb::UInt64)
@ Base ./stream.jl:955
[3] unsafe_read
@ ./io.jl:761 [inlined]
[4] unsafe_read(s::Sockets.TCPSocket, p::Base.RefValue{UInt32}, n::Int64)
@ Base ./io.jl:760
[5] read!
@ ./io.jl:762 [inlined]
[6] read
@ ./io.jl:766 [inlined]
[7] read_one
@ ~/.julia/packages/Jib/51eDB/src/client.jl:34 [inlined]
[8] read_msg(socket::Sockets.TCPSocket)
@ Jib.Reader ~/.julia/packages/Jib/51eDB/src/reader.jl:11
[9] connect(host::String, port::Int64, clientId::Int64, connectOptions::String, optionalCapabilities::String)
@ Jib ~/.julia/packages/Jib/51eDB/src/Jib.jl:54
[10] connect(host::String, port::Int64, clientId::Int64)
@ Jib ~/.julia/packages/Jib/51eDB/src/Jib.jl:42
[11] top-level scope
@ REPL[3]:1
Apparently on Windows TCPSocket
status stays StatusOpen
regardless whether there's data available to read.
Compare to linux where it changes to StatusPaused
when data is available.
Therefore at the moment check_all()
blocks on Windows and hence it's rather useless.
What is the supported Julia version that is tested for the package?
I'm using Julia Version 1.4.2 and notices several issues, such as:
*) check_all function is blocking execution (I suppose not working properly?). It looks that the connection status is always open (=3) and the connection has 0 bytes available, even though there are messages that can be processed separately with check_msg function.
*) Base.show function goes in the loop when presenting data, the information from the type is repeatedly displayed after any other command is executed. Very annoying.
Regarding possible enhancements:
would be good if another time zone can be added.
elseif tz == "EET"
tz"Europe/Riga"
Would be helpful to add another attribute for Contract definition - "primaryExchange"
Would be great to have a function that returns the relevant response for the specific response
New to Julia, I'm using julia version 1.8.5 on macOS 13.2.1 and IB Gateway 10.21 on a paper account:
`julia> using Jib
julia> ib = Jib.connect(4002, 2)
ERROR: EOFError: read end of file
Stacktrace:
[1] (::Base.var"#wait_locked#680")(s::Sockets.TCPSocket, buf::IOBuffer, nb::Int64)
@ Base ./stream.jl:945
[2] unsafe_read(s::Sockets.TCPSocket, p::Ptr{UInt8}, nb::UInt64)
@ Base ./stream.jl:953
[3] unsafe_read
@ ./io.jl:759 [inlined]
[4] unsafe_read(s::Sockets.TCPSocket, p::Base.RefValue{UInt32}, n::Int64)
@ Base ./io.jl:758
[5] read!
@ ./io.jl:760 [inlined]
[6] read
@ ./io.jl:764 [inlined]
[7] read_one
@ ~/.julia/packages/Jib/pHjze/src/client.jl:34 [inlined]
[8] read_msg(socket::Sockets.TCPSocket)
@ Jib.Reader ~/.julia/packages/Jib/pHjze/src/reader.jl:11
[9] connect(host::Sockets.IPv6, port::Int64, clientId::Int64, connectOptions::String, optionalCapabilities::String)
@ Jib ~/.julia/packages/Jib/pHjze/src/Jib.jl:54
[10] connect(port::Int64, clientId::Int64, connectOptions::String, optionalCapabilities::String)
@ Jib ~/.julia/packages/Jib/pHjze/src/Jib.jl:74
[11] connect(port::Int64, clientId::Int64)
@ Jib ~/.julia/packages/Jib/pHjze/src/Jib.jl:72
[12] top-level scope
@ REPL[3]:1
`
Can connect to same environment with a python client.
Any hints?
Hi all
using ZMQ ( if relevant) code has been working excellently ( all hail ibilli ) but I am getting this message this am. I suck at error handling so anyone got any clues as to the cause? Thanks
ERROR: ┌ Warning: connection terminated
└ @ Jib.Reader ~/.julia/packages/Jib/12qUA/src/reader.jl:70
LoadError: [ Info: reader exiting
IOError: stream is closed or unusable
Stacktrace:
[1] check_open
@ ./stream.jl:388 [inlined]
[2] uv_write_async(s::Sockets.TCPSocket, p::Ptr{UInt8}, n::UInt64)
@ Base ./stream.jl:1069
[3] uv_write(s::Sockets.TCPSocket, p::Ptr{UInt8}, n::UInt64)
@ Base ./stream.jl:1032
[4] unsafe_write(s::Sockets.TCPSocket, p::Ptr{UInt8}, n::UInt64)
@ Base ./stream.jl:1115
[5] unsafe_write
@ ./io.jl:683 [inlined]
[6] write
@ ./io.jl:706 [inlined]
[7] write_one(socket::Sockets.TCPSocket, buf::IOBuffer)
@ Jib.Client ~/.julia/packages/Jib/12qUA/src/client.jl:28
[8] sendmsg
@ ~/.julia/packages/Jib/12qUA/src/requests.jl:27 [inlined]
[9] reqMktData(ib::Jib.Connection, tickerId::Int64, contract::Jib.Contract, genericTicks::String, snapshot::Bool, regulatorySnaphsot::Bool, mktDataOptions::NamedTuple{(), Tuple{}})
@ Jib.Requests ~/.julia/packages/Jib/12qUA/src/requests.jl:70
[10] reqMktData
@ ~/.julia/packages/Jib/12qUA/src/requests.jl:47 [inlined]
[11] top-level scope
@ ~/tontine_2022/2022_live/9_6_22_LIVE_STK_IV_VOL_LAST.jl:171
in expression starting at /home/dave/tontine_2022/2022_live/9_6_22_LIVE_STK_IV_VOL_LAST.jl:160
here's the relevant "code" no giggling :-)
Jib.start_reader(ib, wrap)
context = Context()
stk_socket = Socket(context, PUSH)
ZMQ.connect(stk_socket, "tcp://localhost:5555")
for (idx, s) in enumerate(eachrow(symbol_list))
contract = Jib.Contract(symbol=s.Sym, secType="STK" , exchange=s.exchange , currency="USD")
Jib.reqMktData(ib, idx, contract,"106", false) # https://interactivebrokers.github.io/tws-api/tick_types.html
end
Hello,
When I try to run my_wrap() below with the execDetails function syntax from your source code I get an error message from Julia:
UndefVarError: Execution not defined
All other functions in the Jib.Wrapper( ) are fine.
Is there a way around this?
Thank you in advance for the help.
Regards,
function my_wrap( )
...
wrap = Jib.Wrapper(
... some functions (ok)
execDetails = function(reqId::Int, contract::Contract, execution::Execution)
d[:ex_con] = contract
d[:execution] = execution
println("ExecDetails: $reqId")
end,
... more functions (ok)
end # wrap
...
end # my_wrap()
What would be an example use of this API ? Thank you.
Hi, first of all, thank you for this package! 🙇♂️
I'm trying to make my software more robust in its usage of Jib.jl around Gateway disconnections.
Currently, when IB Gateway (auto) restarts, my Julia client prints:
Warning: connection terminated
Info: reader exiting
I was wondering if / how the client could get notified in a way that allows it to take action in response to a disconnection from the Gateway?
For instance, when the Gateway (auto) restarts, it disconnects, and I would like to get notified of the disconnection (not just by a message print, but by a callback or maybe a variable I can repeatedly check) and take action.
The action I would take could be for example: trying connecting again repeatedly until Gateway is finished restarting and the connection succeeds, after which I would request data streams again (e.g. with reqMktData
).
I went through the list of wrapper callbacks (in wrapper.jl
) and did not see any that corresponds to what I need. However, there is this commented-out connectionClosed
that sounded promising. Maybe it is planned for the future?
In the meantime, do you see any easy workaround?
Maybe it's even something that looks easy to contribute, if that's the case, let me know and I can try to make a PR with your guidance :-)
Versions tested:
Hi there
thanks SO MUCH for doing this, you've saved me a LOT of time. I'm coming to this from a python perspective and just learning julia. In python I get my IB data using a great framework called https://ib-insync.readthedocs.io/api.html and I use the asynch approach (see below for example) I don't really see how to achieve this using your framework and julia.
I can connect, run your examples in the REPL so I can see how to get contract information into a structure. So I know my system is set up to get information from IB but that's for a single data request, I need to stream data for a trading session.
It's just the asynch event loop I am having difficulty with.
how do I post reqMktdata requests as below
and
how do I consume the results?
I can code, in julia, how to set up the symbols to be requested and how to structure the contract parameters. Everything but the most important part, how to process the loop.
Can you give me some pointers please?
`
import asyncio
import ib_insync as ibi
class App:
async def run(self):
self.ib = ibi.IB()
with await self.ib.connectAsync():
contracts = [
ibi.Stock(symbol, 'SMART', 'USD')
for symbol in ['AAPL', 'TSLA', 'AMD', 'INTC']]
for contract in contracts:
self.ib.reqMktData(contract)
async for tickers in self.ib.pendingTickersEvent:
for ticker in tickers:
print(ticker)
def stop(self):
self.ib.disconnect()
app = App()
try:
asyncio.run(app.run())
except (KeyboardInterrupt, SystemExit):
app.stop()
`
Just started happening since 12/16/22 did something change in Jib.jl or IB on or near 12/15/22 which is the last day the code ran.
┌ Warning: connection terminated
└ @ Jib.Reader ~/.julia/packages/Jib/JOmEj/src/reader.jl:70
ERROR: [ Info: reader exiting
LoadError: IOError: write: broken pipe (EPIPE)
Stacktrace:
[1] uv_write(s::Sockets.TCPSocket, p::Ptr{UInt8}, n::UInt64)
@ Base ./stream.jl:1064
[2] unsafe_write(s::Sockets.TCPSocket, p::Ptr{UInt8}, n::UInt64)
@ Base ./stream.jl:1118
[3] unsafe_write
@ ./io.jl:683 [inlined]
[4] write
@ ./io.jl:706 [inlined]
[5] write_one(socket::Sockets.TCPSocket, buf::IOBuffer)
@ Jib.Client ~/.julia/packages/Jib/JOmEj/src/client.jl:28
[6] sendmsg
@ ~/.julia/packages/Jib/JOmEj/src/requests.jl:27 [inlined]
[7] reqMktData(ib::Jib.Connection, tickerId::Int64, contract::Jib.Contract, genericTicks::String, snapshot::Bool, regulatorySnaphsot::Bool, mktDataOptions::NamedTuple{(), Tuple{}})
@ Jib.Requests ~/.julia/packages/Jib/JOmEj/src/requests.jl:70
[8] reqMktData
@ ~/.julia/packages/Jib/JOmEj/src/requests.jl:47 [inlined]
[9] top-level scope
@ ~/tontine_2022/2022_live/9_6_22_LIVE_STK_IV_VOL_LAST.jl:171
in expression starting at /home/dave/tontine_2022/2022_live/9_6_22_LIVE_STK_IV_VOL_LAST.jl:160
running latest Jib.jl updated pkg and julia latest version.
@v1.8) pkg> up
Updating registry at `~/.julia/registries/General.toml`
Updating git-repo `https://github.com/lbilli/Jib.jl`
No Changes to `~/.julia/environments/v1.8/Project.toml`
No Changes to `~/.julia/environments/v1.8/Manifest.toml`
(@v1.8) pkg> up
Updating registry at `~/.julia/registries/General.toml`
Updating git-repo `https://github.com/lbilli/Jib.jl`
No Changes to `~/.julia/environments/v1.8/Project.toml`
No Changes to `~/.julia/environments/v1.8/Manifest.toml`
(@v1.8) pkg> st
Status `~/.julia/environments/v1.8/Project.toml`
[c52e3926] Atom v0.12.38
[336ed68f] CSV v0.10.8
[1b08a953] Dash v1.1.2
⌃ [a93c6f00] DataFrames v1.3.6
[1313f7d8] DataFramesMeta v0.12.0
[31a5f54b] Debugger v0.7.6
⌅ [c43c736e] Genie v4.18.1
⌅ [cd3eb016] HTTP v0.9.17
[f310f2d2] Jib v0.19.3 `https://github.com/lbilli/Jib.jl#master`
[e5e0dc1b] Juno v0.8.4
[f0f68f2c] PlotlyJS v0.18.10
⌃ [c3e4b0f8] Pluto v0.19.9
⌅ [4acbeb90] Stipple v0.24.5
⌃ [ec984513] StipplePlotly v0.12.4
⌃ [a3c5d34a] StippleUI v0.19.4
[9e3dc215] TimeSeries v0.23.1
[c2297ded] ZMQ v1.2.2
[ade2ca70] Dates
[56ddb016] Logging
[10745b16] Statistics
Info Packages marked with ⌃ and ⌅ have new versions available, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use `status --outdated`
(@v1.8) pkg>
dave@deepthought:~$ juliaup update
dave@deepthought:~$ juliaup status
Default Channel Version Update
-------------------------------------------------
* release 1.8.3+0.x64.linux.gnu
dave@deepthought:~$ juliaup update
dave@deepthought:~$ julia
this is the call that breaks
for (idx, s) in enumerate(eachrow(symbol_list))
contract = Jib.Contract(symbol=s.Sym, secType="STK" , exchange=s.exchange , currency="USD")
Jib.reqMktData(ib, idx, contract,"106", false) # https://interactivebrokers.github.io/tws-api/tick_types.html
end
BUT this one works properly. They both share the same ZMQ configuration code and worked fine up until 12/16/22.
for (idx, s) in enumerate(eachrow(symbol_list))
sleep(5)
contract = Jib.Contract(symbol=s.Sym, secType="STK" , exchange=s.exchange , currency="USD")
try
Jib.reqHistoricalData(ib, idx ,contract,"","1 Y","1 day","TRADES",true,1,false )
catch e
println("something went wrong with : " , s.Sym)
stk_send_msg = "STK" * "~" * s.Sy * "~" * "CLOSE" * "~" * string( 999 ) #show there was an error
ZMQ.send( stk_socket, stk_send_msg )
continue
end
end
As of API v163, official IB implementations (except C++, see InteractiveBrokers/tws-api#1061) started reporting market data sizes and volumes as decimals instead of integers.
Here standard Float64
are used instead, which limits the precision to ~16 decimal digits.
The following is Apple Inc description from Interactive Brokers Trader Workstation TWS
Underlying: AAPL
Security Type: STK
Currency: USD
Exchange: SMART
Primary Exchange: NASDAQ
Symbol: AAPL
Sector: Technology
Industry: Computers
Category: Computers
Issuer Country: US
Stock type: COMMON_
This is the jib contract I am trying to create
JC = Jib.Contract(symbol = "AAPL", secType = "STK", currency = "USD", exchange = "SMART", primaryExchange="NASDAQ");
I get the following error
got unsupported keyword argument "primaryExchange"
But when I check the documentation (?Jib.Contract) it says the "primaryExchange" is a valid keyword.
Could you please emphasize how to state the primary exchange?
Thank you!
Hi there
remember I am a noob so please bear with me.I am trying not to bother you.
I added a 10 sec sleep and made the method override to see how it works but I got an error I don't know how to diagnose. I am running all of this in the repl and text editor.
ALSO I feel bad about raising these things as issues, do you have a julia discourse thread please?
using Jib
symbols = ["AAPL", "TSLA", "AMD", "INTC"]
ib = Jib.connect("192.168.1.219",1234, 21)
d, wrap = Jib.simple_wrap();
wrap.tickPrice = (tickerId, field, price, size, attrib) -> println("tickPrice2: $(symbols[tickerId]) $field $price $size $attrib")
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
sleep(10)
for idx in 1:length(symbols)
Jib.cancelMktData(ib, idx)
end
Jib.disconnect(ib)
and got this error
Info: connected
│ V = "165"
└ T = "20220207 13:44:45 Central Standard Time"
error: -1 2104 Market data farm connection is OK:uscrypto
error: -1 2104 Market data farm connection is OK:usfuture.nj
error: -1 2104 Market data farm connection is OK:usfarm.nj
error: -1 2104 Market data farm connection is OK:usfuture
error: -1 2104 Market data farm connection is OK:cashfarm
error: -1 2104 Market data farm connection is OK:usopt
error: -1 2104 Market data farm connection is OK:usfarm
error: -1 2106 HMDS data farm connection is OK:ushmds.nj
error: -1 2106 HMDS data farm connection is OK:ushmds
error: -1 2158 Sec-def data farm connection is OK:secdefil
marketDataType: 1 REALTIME
tickReqParams: 1 0.01 9c0001 3
tickString: 1 LAST_TIMESTAMP 1644263088
tickString: 1 LAST_EXCH Q
tickPrice2: AAPL LAST 172.81 1.0 (canAutoExecute = false, pastLimit = false, preOpen = false)
tickSize: 1 LAST_SIZE 1.0
tickSize: 1 VOLUME 519927.0
tickPrice2: AAPL HIGH 173.95 0.0 (canAutoExecute = false, pastLimit = false, preOpen = false)
tickPrice2: AAPL LOW 171.53 0.0 (canAutoExecute = false, pastLimit = false, preOpen = false)
tickPrice2: AAPL CLOSE 172.39 0.0 (canAutoExecute = false, pastLimit = false, preOpen = false)
tickPrice2: AAPL OPEN 172.76 0.0 (canAutoExecute = false, pastLimit = false, preOpen = false)
tickPrice2: AAPL BID 172.8 23.0 (canAutoExecute = true, pastLimit = false, preOpen = false)
tickPrice2: AAPL ASK 172.81 33.0 (canAutoExecute = true, pastLimit = false, preOpen = false)
tickSize: 1 BID_SIZE 23.0
tickSize: 1 ASK_SIZE 33.0
tickString: 1 BID_EXCH KPQXZU
tickString: 1 ASK_EXCH KPQZNU
marketDataType: 2 REALTIME
tickReqParams: 2 0.01 9c0001 3
┌ Error: exception thrown
│ e = TypeError: in typeassert, expected Float64, got a value of type Nothing
└ @ Jib.Reader ~/.julia/packages/Jib/SeCxV/src/reader.jl:74
[ Info: reader exiting
Hi - is it possible to have an example of how would I get past and future stock options prices ? Thank you.
hi,
this looks like an excellent package, thanks for putting this together. for those of us who are new to Julia, from python, could you please illustrate an example of how to integrate this with Rocket.jl?
thanks
Limit support to API version > v140
or thereabout.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.