It makes me a little sad because competition is always a good thing but when I compared Quantopian with Backtrader to decide which backtester to learn and use with Python, Backtrader won by far. Hopefully this will make the backtrader community grow....

Nicola Prada
@Nicola Prada
Senior Technical Analyst
Best posts made by Nicola Prada
-
RE: Quantopian drops live Brokerage support
-
RE: Tracking an Index with equal weight positions
Hi,
i do like this, as suggested by @backtrader: I preprocess all my securities data including as an extra column their membership status. I do this while creating the survivorship bias free dataset.
In 'next' I look for it as it is another indicator. If True I trigger a EW target buy order otherwise a 0% target sell order.
Latest posts made by Nicola Prada
-
RE: Tracking an Index with equal weight positions
Hi,
i do like this, as suggested by @backtrader: I preprocess all my securities data including as an extra column their membership status. I do this while creating the survivorship bias free dataset.
In 'next' I look for it as it is another indicator. If True I trigger a EW target buy order otherwise a 0% target sell order.
-
RE: Quantopian drops live Brokerage support
It makes me a little sad because competition is always a good thing but when I compared Quantopian with Backtrader to decide which backtester to learn and use with Python, Backtrader won by far. Hopefully this will make the backtrader community grow....
-
RE: TypeError: _get_credit_interest() takes 6 positional arguments but 7 were given??
Thanks, I hadn't noticed that in the examples in the documentation the subclassing is from CommissionInfo and not CommInfoBase and the signature of _get_credit_interest is different between the two methods!
-
TypeError: _get_credit_interest() takes 6 positional arguments but 7 were given??
Hello,
I am subclassing _get_credit_interest() as explained in the documentation with the goal to use it to take care of dividends but in runtime I get a TypeError:class CommInfo_Stocks(bt.CommInfoBase): params = ( ('percentage',0.019), ('min', None), ('max', None), ('stocklike', True), ('commtype', bt.CommInfoBase.COMM_PERC), ('interest_long', True) ) def _getcommission(self, size, price, pseudoexec): if self.params.percentage is None or self.params.percentage == 0: return 0 commission = abs(size) * price * self.params.percentage if self.params.min is not None and commission < self.params.min: commission = self.params.min if self.params.max is not None and commission > self.params.max: commission = self.params.max return commission def _get_credit_interest(self, size, price, days, dt0, dt1): print('Log Something') return 0
This is the log of the error, any hint to fix this?
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-45-57f80c4d0e3d> in <module> 59 60 # Run ---> 61 results = cerebro.run() 62 63 # Statistics ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\cerebro.py in run(self, **kwargs) 1125 # let's skip process "spawning" 1126 for iterstrat in iterstrats: -> 1127 runstrat = self.runstrategies(iterstrat) 1128 self.runstrats.append(runstrat) 1129 if self._dooptimize: ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\cerebro.py in runstrategies(self, iterstrat, predata) 1291 self._runonce_old(runstrats) 1292 else: -> 1293 self._runonce(runstrats) 1294 else: 1295 if self.p.oldsync: ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\cerebro.py in _runonce(self, runstrats) 1686 return 1687 -> 1688 self._brokernotify() 1689 if self._event_stop: # stop if requested 1690 return ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\cerebro.py in _brokernotify(self) 1358 notification to the strategy 1359 ''' -> 1360 self._broker.next() 1361 while True: 1362 order = self._broker.get_notification() ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\brokers\bbroker.py in next(self) 1194 comminfo = self.getcommissioninfo(data) 1195 dt0 = data.datetime.datetime() -> 1196 dcredit = comminfo.get_credit_interest(data, pos, dt0) 1197 self.d_credit[data] += dcredit 1198 credit += dcredit ~\AppData\Local\Continuum\anaconda3\lib\site-packages\backtrader\comminfo.py in get_credit_interest(self, data, pos, dt) 270 271 return self._get_credit_interest(data, size, price, --> 272 (dt0 - dt1).days, dt0, dt1) 273 274 def _get_credit_interest(self, data, size, price, days, dt0, dt1): TypeError: _get_credit_interest() takes 6 positional arguments but 7 were given
-
RE: Broker: how to force selling multiple securities to free up cash before buying in the same bar?
By "the data processing loop" do you mean the next method? The code above is inside next.
-
RE: Broker: how to force selling multiple securities to free up cash before buying in the same bar?
Right now I am keeping all the orders on hold until all the data are processed, then I send them to the broker like this:
self.long_signals = [] self.short_signals = [] # Logic for i, s in enumerate(self.datas): if not self.positions[s]: if s.membership[0]: self.long_signals.append(s) continue if self.positions[s]: if not s.membership[0]: self.short_signals.append(s) continue # Send orders to the broker. for s in self.short_signals: self.orders[s] = self.sell(s) for s in self.long_signals: self.orders[s] = self.buy(s)
-
RE: Broker: how to force selling multiple securities to free up cash before buying in the same bar?
What is the best pattern to do it? To hold all the signals in the strategy until all the data feeds are processed and then send all the sell order first?
Or we should subclass the brocker and modify its logic to process the sell orders first?
-
Broker: how to force selling multiple securities to free up cash before buying in the same bar?
Hi,
suppose there is no cash in the account but we have 10 long positions. The next open we have to sell 5 securities at market and to buy 5 securities at market with the money we should get from the selling.
Is it possible to force the broker to execute all the sell orders (market) before the buy orders to free up cash and avoid to end with 5 rejected orders because the account has not enough cash?
I wouldn't mind being unable to execute one of the buy orders because after all the selling there is not enough cash. In reality I will do all the selling before the buying so I can assume I will able to get a fill on the majority of buy orders. The selling and the buying must be in the same bar at open, I will increase the slippage because of the delay in filling the longs.
Thanks
-
When does the broker update the cash after a trade is closed?
I am working on a simple system that uses weekly data and must have a fixed size portfolio of securities: if it sells one security, I expect it to buy another one the next week.
To size the new position I use the available cash and I make an estimation using the last available close of the cash that will be available once the position I have to close is sold.
2012-02-03, I have a SELL signal.
2012-02-10 I start the week with 1074.87 in the account. During market hours, I expected the position to be closed and get new cash for 15410.02. According to the broker the position is sold for 15407.58. I expect to have 1074.87 + 15407.58 in my account.
Once the 2012-02-10 bar is fully CLOSED, I can compute new signals and, since I sold a security for 15407.58, I have to buy the new one using this amount + the cash I had in the account.
The size is computed using the last available close and a 5% buffer, like this: (cash * 0.95) / close[0]
After the signal, when the sizer computes the size, the cash in the broker is still 1074.87!
No sign of the 15407.58 I got from the security I sold and the new trade is sized using only 1074.87 * 95%!Here it is the full log:
2012-02-03 SELL: 63DU GY Equity
2012-02-03 SIZING 63DU GY Equity: 326 [CLOSE POSITION]2012-02-10 ORDER SUBMITTED: SELL 326 63DU GY Equity @ Market [last: 47.2700 expected: 15410.02]
2012-02-10 ORDER ACCEPTED: SELL 326 63DU GY Equity @ Market [last: 47.2700 expected: 15410.02]
2012-02-10 ORDER EXECUTED: SOLD 326 63DU GY Equity @ 47.2625 value: 15407.58 comm: 19.00
2012-02-10 TRADE CLOSED: 63DU GY Equity gross pnl: -2120.63 comm: 38.00 net pnl: -2158.632012-02-10 BUY: DB1 GY Equity
2012-02-10 SIZING DB1 GY Equity: 21 [last: 47.7270 cash: 1074.87 expected: 0.00 available: 1021.13]
2012-02-17 ORDER SUBMITTED: BUY 21 DB1 GY Equity @ Market [last: 47.7270 expected: 1002.27]
2012-02-17 ORDER ACCEPTED: BUY 21 DB1 GY Equity @ Market [last: 47.7270 expected: 1002.27]
2012-02-17 ORDER EXECUTED: BOUGHT 21 DB1 GY Equity @ 47.9264 value: 1006.46 comm: 19.00
2012-02-17 TRADE OPENED: 21 DB1 GY Equity @ 47.9264 - value: 1006.46 comm: 19.00Am I missing something?
-
RE: timeframe parameter for Analyzers and weekly data
You're right! I was adding all my weekly pandas data without specifying their timeframe because I wrongly assumed backtrader was able to identify it from the data:
for i, value in enumerate(data.items()): s, df = value if isinstance(df, DataFrame): cerebro.adddata(PandasData(dataname=df, name = df.name, plot=False)) # ......
As you said I missed this from the docs: timeframe (default: TimeFrame.Days)