any update on when amend would be available in backtrader.
Posts made by skytrading54
order price modification support ?
Looking for a use case to be able to modify low / high side of a bracket order once the main order is filled, brokers like IB support modification of existing order price etc.
Any plans to support order modification on backtrader - ideally price modification on unfilled limit/stop-limit orders.
While for a single order it would be easier to cancel and replace it with new one, how do we achieve it for bracket orders low and high side once main order is filled, ability to modify the original order seems to be the solution, any thoughts in current backtrader framework.
RE: Issue with Stop-Loss on Multiple Stock Datas
@cwse to answer your question -- Will OCO be supported by interactive brokers in the near future if i go down this avenue?
There is something better available in IB called bracket order which combines OCO , Stop Loss Order and Take Profit Order together with main order.
It works like this.
Issue 3 orders in one go,
- The main order (Limit)
- Take Profit (Limit)
- Stop Loss (this could be actual stop loss or trail or trail limit order)
All of these are in one group (grouped by parent orders id) and when we create the orders one by one; the transmit flag (of IBOrder) is false for first 2 and should be true on last one. So all of them gets into IB system at once based on last orders transmit=true when you submit them one by one.
From IB side, Only first main order is sent/live at the exchange (rest 2 are not active), Only when first one is filled next 2 are propagated to exchange. Since these 2 (take profit & stop loss) are OCO; whichever gets filled at exchange first, IB cancels the other one.
- Main Order - Buy @ Limit price 100 - transmit=false
- Take Profit - Sell @ Limit price 110 - transmit=false, m_parentId= main order id.
- Stop Loss - Sell @ Stop Loss price 98 - transmit=true, m_parentId= main order id.
submit all 3, they are entered in IB system, however IB will send only parent order to exchange first. Only when that is executed it will send next 2 as OCO. When one of this is filled (resulting in profit or loss) other is cancelled automatically.
exception in ibbroker.py
I am using following methods to set commission in both cases getting following exception, any pointers what's going wrong...
Product is emini future.
cerebro.broker.setcommission(commission=1.25, margin=2000.0, mult=50.0)
comminfo = CommInfo_FIXED(commission=1.25,stocklike=False) # 1$
where CommInfo_FIXED is following class.
params = (('stocklike', False), ('commtype', bt.CommInfoBase.COMM_FIXED),)
def _getcommission(self, size, price, pseudoexec):
both varieties gives following error when trade is executed.
ERROR Exception in message dispatch. Handler 'commissionReport' for 'commissionReport'
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\ib\opt\dispatcher.py", line 44, in call
File "build\bdist.win32\egg\backtrader\stores\ibstore.py", line 1377, in commissionReport
File "build\bdist.win32\egg\backtrader\brokers\ibbroker.py", line 525, in push_commissionreport
closedvalue = comminfo.getoperationcost(closed, pprice_orig)
AttributeError: 'NoneType' object has no attribute 'getoperationcost'
indicator using a value from the data other than close
I believe most of the built in indicators are using close value from the data for computation, for example SMA indicator is using close values of all the bars to derive sma... (is this correct)
I have following 2 scenarios to build indicators
- if I would like to force this to use say high values of the bar to compute the average, what would be the change.
- if I would like to use volume from the bar to compute volume average over a period, what would be the change.
appreciate any example if available.
RE: Suggestion: caching
my 2 cents..
following approach might work using existing multiprocessing architecture.
- since optimization iterations are mainly based on parameters which are passed on as ranges... we might simply consider to compute first, all the indicators which make use of those range based parameters and save all of those to the file, even this could be done by having multiple processes each dedicated to that specific indicator.
- Once this computation is done, then actual strategy processing should begin using various combinations of those indicators as applicable. It would only need to read the cached data from file in read only mode for that specific indicator, so no worry about locking, this would as well be multiprocessing as is used today, just that indicators have been computed earlier separately.
RE: Memory error in optimization run.
well, my data size is not big... its 1 min ohlc bar for 1 year and resampled to 5 mins compression.
When I ran it with about 50 iteration it works fine. A higher number of iterations (150++) gives this error, which means memory is increasing per iteration.
I see any point of time 10 process spawned by backtrader for this optimization run, is there a way it can release the memory from some of the previous runs as it progresses into next iterations....
Memory error in optimization run.
Hi, I am getting memory error while running optimization and the run does not continue after that.
-E--Strategy::init--loop params-- xception in thread Thread-3: Traceback (most recent call last): File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner self.run() File "C:\Python27\lib\threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) File "C:\Python27\lib\multiprocessing\pool.py", line 389, in _handle_results task = get() MemoryError
I am using 4 parameters variations..
cerebro.optstrategy(TestStrategy,smaperiod_fast=xrange(40,150,15), smaperiod_slow=range(150,300,15), p1=range(25,45,8), p2=range(50,300,15))
However the max memory consumed is 1.7 GB, with 32 bit it should go upto 4GB without any issue. Attached is snapshot of the all the processes spawned for optimization run.
Any idea what could be wrong here... I am using backtrader Release 184.108.40.206
RE: Anyone using Backtrader for live trading
I am facing a delay issue in processing updates for IB when using multiple products having different market timings (meaning for some of them market is open and for some its not.)
eg: IBAU200, USO, IBUS100, Emini future.
while running this during US market hours while for 3 of them has market open but not Australia, the historical data is downloaded first 3 very quickly for 4th one is stuck somewhere and so the next in strategy is happening very slowly it takes up to 10 minutes to have next completed for historical data 1 minute compression/timeframe. Same is applicable if I run this Australia timing as US is not open by then.
If I remove IBAU200 (and add many other US products) it works fine (all next calls are done for historical data within few seconds). Wander what could be the issue.
I tried passing session start / session end time while creating each of the data feed, its still the same issue.
It seems something to with backtrader being waiting for response for first real time update of IBAU200 so that it can subscribe for historical data for the same which does not happen and so there is some sort of looping.
Any idea how this could be fixed ... if resorted this will help run one program across multiple market...
RE: Problem updating to Version 1.9.24 from 1.8.10, Win 10 Anaconda
A question on similar lines..
I am relatively new to python...
Appreciate that Backtrader is quite well maintained with follows regular releases with fixes/new features.
I am using windows 10 & python 2.7 - pip install or using setup.py in backtrader to install for manual install, in both ways it overrides the existing installation. At any point of time I wish to maintain 2 versions in same environment.
- Keep existing stuff running with the version I had it tested with.
- use newer version for subsequent testing and once stable update it..
did some google, but prefer if we have some inbuild setup in backtrade to be able to have multiple versions installed at the same time and we can choose to point to specific versions.
RE: Anyone using Backtrader for live trading
The issues in processing the data received for CFD's.
Basically the error is its processing the market data returned for CFD's.
CFD's we request only BID and IB provides it in callback tickPrice()
The current implementation of tickprice checks if its CASH then only it process, ignore rest.
tickerId = msg.tickerId
I have made the to include if its CFD's as well.
main change is
tickerId = msg.tickerId
if self.iscash[tickerId] or self.iscfd[tickerId]:
if msg.field == 1: # Bid Price
rest of the changes are to create/push/pop self.iscfd dictionary.
All the changes are in ibstore.py
I had forked a copy from dev branch and my changes are checked it in the GitHub
somehow couldn't create a pull request under
Please take a look to merge in the next cycle.
It now works as expected ....
python ibtest_12Jan17.py --port 8099 --clientId 1 --data0 IBHK50-CFD-SMART --timeframe Minutes --compression 1
Data0, 0215, 736342.252778, 2017-01-13T06:04:00.000000, 22953.87, 22954.87, 22952.86, 22953.87, -1.0, 0, 22950.47
Data0, 0216, 736342.253472, 2017-01-13T06:05:00.000000, 22953.87, 22957.87, 22952.86, 22957.87, -1.0, 0, 22952.47
Data0, 0217, 736342.254167, 2017-01-13T06:06:00.000000, 22957.87, 22964.87, 22955.86, 22963.87, -1.0, 0, 22955.67
Data0, 0218, 736342.254861, 2017-01-13T06:07:00.000000, 22963.87, 22963.87, 22960.86, 22960.87, -1.0, 0, 22958.07
***** DATA NOTIF: LIVE
Data0, 0219, 736342.255556, 2017-01-13T06:08:00.000000, 22960.87, 22961.87, 22959.87, 22960.87, 0.0, 0, 22959.47
Getting Bid/Ask along with ohlc in next()
As per the backtrader framework out strategies order would be triggered from strategies next call which gets called as my be set up based on timeframe/compression and resampling/replaying.
While this is okay for backtesting for real life orders I am trying to get top bid and ask price as well into the next(), however it would not be a line. These would be two standalone float keeping the latest values.
This should happen irrespective of --rtbar enabled or not which means real time data should always be subscribed and it will keep on updating bid and ask as received and whenever next is called we need to be able to reference these variables in next.
uses: to be used to decide the limit prices for orders. Also would be would be used to see if market is trading in extreme conditions (when the spread of bid ask increases drastically compared).
I am looking to implement this from IB perspective.
Could you please help what would be optimal way to do this with pointers as to which class should own these variables and how to make it available in next.
Thanks in advance.