ultra1971 / backtrader_ib_insync Goto Github PK
View Code? Open in Web Editor NEWAsync integration between backtrader and Interactive brokers.
License: GNU General Public License v3.0
Async integration between backtrader and Interactive brokers.
License: GNU General Public License v3.0
Dear,
I tried to run the example code ibdemo.py with latest IB TWS. It met with below error, anyone has any idea to solve it?
Regard
Aaron
backtrader_ib_insync-main\examples> py .\ibdemo.py
Using New IBstore
Data added to cerebro
Error 10314, reqId 4: End Date/Time: The date, time, or time-zone entered is invalid. The correct format is yyyymmdd hh:mm:ss xx/xxxx where yyyymmdd and xx/xxxx are optional. E.g.: 20031126 15:59:00 US/Eastern Note that there is a space between the date and time, and between the time and time-zone. If no date is specified, current date is assumed. If no time-zone is specified, local time-zone is assumed(deprecated). You can also provide yyyymmddd-hh:mm:ss time is in UTC. Note that there is a dash between the date and time in UTC notation., contract: Contract(secType='CASH', conId=12087792, symbol='EUR', exchange='IDEALPRO', currency='USD', localSymbol='EUR.USD', tradingClass='EUR.USD')
***** DATA NOTIF: DELAYED
***** DATA NOTIF: LIVE
Hi,
super useful project, thanks for publishing :)
I'm trying to run ibdemo.py live for several consecutive days. The job crashes when TWS auto-restarts. With _debug=True
I get:
2023-08-09 20:14:00,109 ib_insync.client ERROR Peer closed connection.
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 258, in __step
result = coro.throw(exc)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 654, in sleep
return await future
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/futures.py", line 284, in __await__
yield self # This tells Task to wait for completion.
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 328, in __wakeup
future.result()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/futures.py", line 196, in result
raise exc
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/admin/backtrader_ib_insync/venv/lib/python3.9/site-packages/ib_insync/util.py", line 341, in run
result = loop.run_until_complete(task)
File "/Users/admin/backtrader_ib_insync/venv/lib/python3.9/site-packages/nest_asyncio.py", line 99, in run_until_complete
return f.result()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/futures.py", line 196, in result
raise exc
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/admin/backtrader_ib_insync/examples/ibdemo_thomas.py", line 58, in <module>
run()
File "/Users/admin/backtrader_ib_insync/examples/ibdemo_thomas.py", line 54, in run
cerebro.run()
File "/Users/admin/backtrader/backtrader/cerebro.py", line 1132, in run
runstrat = self.runstrategies(iterstrat)
File "/Users/admin/backtrader/backtrader/cerebro.py", line 1303, in runstrategies
self._runnext(runstrats)
File "/Users/admin/backtrader/backtrader/cerebro.py", line 1547, in _runnext
drets.append(d.next(ticks=False))
File "/Users/admin/backtrader/backtrader/feed.py", line 407, in next
ret = self.load()
File "/Users/admin/backtrader/backtrader/feed.py", line 480, in load
_loadret = self._load()
File "/Users/admin/backtrader_ib_insync/backtrader_ib_insync/ibdata.py", line 448, in _load
self.q = self.ibstore.reqRealTimeBars(self.contract)
File "/Users/admin/backtrader_ib_insync/backtrader_ib_insync/ibstore.py", line 544, in reqRealTimeBars
self.ib.sleep(duration)
File "/Users/admin/backtrader_ib_insync/venv/lib/python3.9/site-packages/ib_insync/util.py", line 386, in sleep
run(asyncio.sleep(secs))
File "/Users/admin/backtrader_ib_insync/venv/lib/python3.9/site-packages/ib_insync/util.py", line 343, in run
raise globalErrorEvent.value() or e
ConnectionError: Socket disconnect
Any idea?
I am trying to integrate with this lib. (Thank you)
I am getting wrong value like below.
2022-02-02T19:41:54.263717,-1.000000,-1.000000,-1.000000,-1.000000,67418
2022-02-02T19:41:54.872746,0.000000,0.000000,0.000000,0.000000,0
datakwargs = dict(
timeframe=bt.TimeFrame.Seconds, compression=10,
fromdate=dt_now,
)
# data0 = ibdata(dataname='EUR.USD-CASH-IDEALPRO', **datakwargs)
data0 = ibdata(dataname='MNQ-FUT-GLOBEX-USD-202203-2', **datakwargs)
cerebro.adddata(data0)
is there anything need to set or fix?
@ultra1971 thanks for this!
Would you consider (down the track - once tested fully) working this into Backtrader and fully replacing IbPy2/IBStore?
I know there's still some work to do, but it's a much needed upgrade.
Happy to help test. I'll get it running in a better suited dev environment the weekend.
Greatly appreciate the efforts in building the ib_insync capability into backtrader. Have a small question on the following line of code under ibdata.py line 673:
AttributeError:`
self.lines.open[0] = rtbar.open_
I do not recall bar having the property .open_. Is there a reason why you have inserted this exception?
Hi @ultra1971 , I had been playing around with your codebase and had been impressed with your implementation with ib_insync so far.
However, when I observe the data receive from IB in backtrader. I saw there is a few lines of data comes in with 0.0 on OHLCV as attached in the post.
Is this a known issue that I should handle on the application end? Or there is a parameter that I had mislook in the documentation that will be able to solve the problem.
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.