backtrader_plotting icon indicating copy to clipboard operation
backtrader_plotting copied to clipboard

Fixed running with multiple different strategies...

Open iSevenDays opened this issue 4 years ago • 3 comments

Fixed running with multiple different strategies using StFetcher (tested custom strategy and BuyAndHold backtrader test strategy); fixed plotting in Pycharm (iplot was set by default)

Example code with running multiple strategies: strategy_params is a dict of custom parameters:

            strategy_params = dict(fast=range(20, 30, 4),
                        slow=range(20, 30, 4),)
            class StFetcher(object):
                _STRATS = [] #list of dicts(key=strategy,value=kwargs)

                def __new__(cls, *args, **kwargs):
                    idx = kwargs.pop('idx')
                    (key, value), = cls._STRATS[idx].items()
                    obj = key(*args, **{**kwargs, **value})
                    return obj
            StFetcher._STRATS = [{SMACrossLongShort: strategy_params},
                                 {BuyAndHold_Buy: strategy_params}] #actually it should be an empty dict

            cerebro.optstrategy(StFetcher, idx=[0, 1])
            bo = Bokeh()
            browser = OptBrowser(bo, optimized_runs)
            browser.start(iplot=False)
SMAStrategy BuyAndHold

iSevenDays avatar Feb 08 '21 19:02 iSevenDays

The best-case scenario would be to create multiple tables for the number of strategy types we have, but it is better done than perfect.

I needed to compare my SMA cross strategy with BuyAndHold strategy and the pull request will allow others to do the same.

iSevenDays avatar Feb 08 '21 19:02 iSevenDays

Hi do you have any idea why when I use two shares to plot a realistic plot (like in Pairtrading algo) sell and buy arrows only show on one of the plots?

Thanks

behrad-ahmadpour avatar May 06 '21 21:05 behrad-ahmadpour

Sorry, I am quite busy with other projects at the moment. Tbh I don't understand what problem this PR fixes.

Also is it intended to comment out the source code printing from the meta tab?

Whats wrong with plotting in PyCharm? I used to use PyCharm myself and didn't notice that plotting didn't work.

verybadsoldier avatar Jul 19 '21 19:07 verybadsoldier