Just finished implementing tqdm
for optimizations. The status bars are inline and stay at the top. This way I can view the ETAs and overview of the opt runs:
Best posts made by crazy25000
-
RE: progress bar / ETA during optimization
-
RE: INTRADAY STRATEGY
@ambrishooty recommend downloading a Python IDE like PyCharm. Has useful features like auto format and they have a free community edition that works well https://www.jetbrains.com/pycharm/download/#section=linux
Most importantly though, you need to learn Python and recommend stop using backtrader until then. You don't want to end up with something that likely won't be doing what you thought it was doing.
Here's a basic interactive site to practice https://www.learnpython.org/
Python wiki has more resources https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
-
RE: Flipping a position from long to short
@marketwizard said in Flipping a position from long to short:
However, when I analyze my trades, I see that the programm only flip the position once or twice at the beginning then it keeps going short.
Recommend initializing one crossover instead of 2. It doesn't answer the question, but makes calculation and strategy simpler.
self.crossover = bt.indicators.CrossOver(self.ema_fast, self.ema_slow, plot=False, subplot=False) ..... def next(self): if self.crossover > 0: #buy elif self.crossover < 0: #sell
When a long is taken, the size following short is much smaller than the long.
You're calculating the quantity for each trade so it's expected to vary.
-
RE: Creating a strategy based on Supports and Resistances and plotting said lines
@tirtharaj backtrader has at least 3 built in, here's an example https://backtrader.com/docu/indautoref/#pivotpoint
Plotting is also supported https://backtrader.com/blog/posts/2016-04-28-pivot-point-cross-plotting/pivotpoint-crossplotting/
The documentation and articles section are well detailed. Recommend reading them at least once to learn what's available.
-
RE: Getting different results from backtesters. Repo in description
@edlg took a quick look at your code for Pandas tester and noticed there's no lag. Could be causing incorrect results.
-
RE: Flipping a position from long to short
@marketwizard if you close an open position before issuing a new trade, it will work as expected. Here's an example of a crossover strategy that buys and sells on crossover direction https://github.com/mementum/backtrader/blob/master/samples/multitrades/multitrades.py#L75:L92
if self.signal > 0.0: # cross upwards if self.position: self.log('CLOSE SHORT , %.2f' % self.data.close[0]) self.close(tradeid=self.curtradeid) self.log('BUY CREATE , %.2f' % self.data.close[0]) self.curtradeid = next(self.tradeid) self.buy(size=self.p.stake, tradeid=self.curtradeid) elif self.signal < 0.0: if self.position: self.log('CLOSE LONG , %.2f' % self.data.close[0]) self.close(tradeid=self.curtradeid) if not self.p.onlylong: self.log('SELL CREATE , %.2f' % self.data.close[0]) self.curtradeid = next(self.tradeid) self.sell(size=self.p.stake, tradeid=self.curtradeid)
-
RE: Creating a strategy based on Supports and Resistances and plotting said lines
If the built in ones don't meet your needs, recommend reading the indicator development section. It's in the same area in the first link above.
-
RE: Multicore optimization not work for parallel multistrategy scenario
@yacc2000 I think you want the strategy selection/fetcher pattern. I've been using a similar/modified version of it for the same reason and working well.
Link to the updated pattern https://www.backtrader.com/blog/posts/2017-05-16-stsel-revisited/stsel-revisited/
The original post is good to read for details https://www.backtrader.com/blog/posts/2016-10-29-strategy-selection/strategy-selection/
-
RE: Flipping a position from long to short
@marketwizard backtrader has builtin sizers and one of them is AllInSizer https://www.backtrader.com/docu/sizers-reference/#allinsizer There are others like fixed and percent that you could swap out with the manual calculations.
How to add sizers in the docs https://www.backtrader.com/docu/sizers/sizers/#using-sizers
Have you tried using the builtin sizers instead of manually calculating it? Should make your position sizing strategy easier to implement.
Latest posts made by crazy25000
-
RE: Creating a strategy based on Supports and Resistances and plotting said lines
If the built in ones don't meet your needs, recommend reading the indicator development section. It's in the same area in the first link above.
-
RE: Creating a strategy based on Supports and Resistances and plotting said lines
@tirtharaj backtrader has at least 3 built in, here's an example https://backtrader.com/docu/indautoref/#pivotpoint
Plotting is also supported https://backtrader.com/blog/posts/2016-04-28-pivot-point-cross-plotting/pivotpoint-crossplotting/
The documentation and articles section are well detailed. Recommend reading them at least once to learn what's available.
-
RE: Multicore optimization not work for parallel multistrategy scenario
@vladisld thanks for catching that! Sounds like I wrongly assumed. I assumed he would pass the results of the optimizations to a central broker, another instance that decides what to do.
-
RE: progress bar / ETA during optimization
@fredyellow said in progress bar / ETA during optimization:
@crazy25000 said in progress bar / ETA during optimization:
Just finished implementing
tqdm
for optimizations. The status bars are inline and stay at the top. This way I can view the ETAs and overview of the opt runs:Excellent!
Do you have a minimal code example?
Here's a minimal code example in Jupyterlab Notebook:
from datetime import datetime import backtrader as bt from tqdm.auto import tqdm pbar = tqdm(desc='Opt runs', leave=True, position=1, unit='run', colour='violet') class OptimizeStrategy(bt.Strategy): params = ( ('p1', 9), ('p2', 21), ) def __init__(self): self.ema1 = bt.talib.EMA(self.data, timeperiod=self.p.p1, plotname='EMA1') self.ema2 = bt.talib.EMA(self.data, timeperiod=self.p.p2, plotname='EMA2') self.crossover = bt.indicators.CrossOver(self.ema1, self.ema2) def next(self): if self.crossover > 0: if self.position: self.close() self.buy() elif self.crossover < 0: if self.position: self.close() self.sell() def bt_opt_callback(cb): pbar.update() def runstrat(): cerebro = bt.Cerebro(maxcpus=1) cerebro.optstrategy(OptimizeStrategy, p1=range(5, 20, 5), p2=range(50, 200, 10)) data = bt.feeds.YahooFinanceData(dataname='AAPL', fromdate=datetime(2016, 1, 1), todate=datetime(2017, 1, 1)) cerebro.adddata(data) cerebro.optcallback(cb=bt_opt_callback) stratruns = cerebro.run() if __name__ == '__main__': runstrat() print("Done")
-
RE: Multicore optimization not work for parallel multistrategy scenario
@yacc2000 multiprocessing would speed up that pipeline as long as the strategy is complex enough and needs to do a lot of calculations. If it's a really simple strategy like the example you posted, multiprocessing would not speed it up since no need for more calculations.
Here's an example you can test and verify:
import os from datetime import datetime import backtrader as bt class OptimizeStrategy(bt.Strategy): params = ( ('p1', 15), ('p2', 12), ('instance', 0), ) def __init__(self): self.ema1 = bt.talib.EMA(self.data, timeperiod=self.p.p1, plotname='EMA1') self.ema2 = bt.talib.EMA(self.data, timeperiod=self.p.p2, plotname='EMA2') self.crossover = bt.indicators.CrossOver(self.ema1, self.ema2) def next(self): if self.crossover > 0: if self.position: self.close() self.buy(self.datas[self.p.instance]) elif self.crossover < 0: if self.position: self.close() self.sell(self.datas[self.p.instance]) def bt_opt_callback(cb): pbar.update() def runstrat(): cerebro = bt.Cerebro(maxcpus=15) for i in range(5): cerebro.optstrategy(OptimizeStrategy, instance=i, p1=range(9, 55, 10), p2=200) for i in range(5): data = bt.feeds.YahooFinanceData(dataname='AAPL', fromdate=datetime(2016, 1, 1), todate=datetime(2017, 1, 1)) cerebro.adddata(data) stratruns = cerebro.run() if __name__ == '__main__': runstrat()
I updated what you posted so that it can run calculations with indicators and buy, sell, close:
- Test 1 range=5, maxcpus=5: 90 seconds
- Test 2 range=5, maxcpus=15: 47 seconds
-
RE: Flipping a position from long to short
@marketwizard can you provide a minimal, reproducible example like @ab_trader mentioned? Difficult to troubleshoot why that is happening.
-
RE: progress bar / ETA during optimization
@fredyellow I can provide one later after work. Maybe during lunch if I have time.
-
RE: Multicore optimization not work for parallel multistrategy scenario
@yacc2000 can you provide a full minimal, reproducible example that I can run? It would be helpful to understand it better - I think I know what you're asking, but would be helpful to reproduce: multi-strategies + multiple assets and what you expect vs results.
-
RE: progress bar / ETA during optimization
Just finished implementing
tqdm
for optimizations. The status bars are inline and stay at the top. This way I can view the ETAs and overview of the opt runs: -
Are there plans to update Oanda API from v1 to v20?
Hey everyone,
Are there plans to update
backtrader
internal support for Oanda API from v1 to v20? I would like to help if so. I've been running/testingbtoandav20
on a cloud server, but it's unreliable: constantly disconnecting, timeouts, and missing trade signals as a result (it's the same server I run my other one and haven't had these issues).I wrote a custom wrapper around Oanda v20 REST API, but not all the endpoints, just the ones I used. So have some experience and I've read the entire documentation for
backtrader
and most of the articles. Have good overview of it, but haven't looked at the internals or reviewed the current Oanda implementation yet.