@davidavr great
Best posts made by yacc2000
Latest posts made by yacc2000
-
About numpylines code
I'm trying to use numpylines branch, but got some exception. After inspect the code, I think the numpylines might not work properly. For example:
https://github.com/mementum/backtrader/blob/numpylines/backtrader/linebuffer.py
class LineBuffer(LineSingle): def forward(self, value=NAN, size=1): self.idx += size self.lencount += size if size > 1: if BCOLZLEVEL >= 1: # Use bcolz also for resized arrays self.array = bcolz.fill(size, dflt=NAN, expectedlen=size, dtype=np.float64) else: if not PANDIZE: self.array = array.array(str('d'), [NAN] * size) else: self.array = pd.Series([NAN] * size, dtype=np.float64) #Is this right? return if False and len(self) == 1: print(self._owner, 'current len:', len(self)) print('array is:', self.array) for i in range(size): self.array.append(value)
self.array = pd.Series([NAN] * size, dtype=np.float64) #Is this right?
I think the code will overwrite data in self.array instead of appending Nan
Am I right? -
Plot size miscalculated.
I have multiple data feed, but I want to plot each data feed in individual figure separately.
So base on the idea of this topic:
https://community.backtrader.com/topic/1041/plot-backtesting-results-in-multiple-graphsI did it in this way:
for i in range( len(cerebro.datas) ): # five data feed bt_data = cerebro.datas[i] for cb_data in cerebro.datas: cb_data.plotinfo.plot = False bt_data.plotinfo.plot = True #only plot one data per figure if i == 0: #index plot_range = None else: plot_range = stock_data[i]["plotrange"] fig2d = cerebro.plot()
But figure size is not correct:
We can see a big blank area under the plot.
The more date feed the bigger blank area:
How could I fix the plot size. Or could anybody please tell me which part of code should I inspect to correct the plot size?Thanks in advance.
-
RE: Multicore optimization not work for parallel multistrategy scenario
@vladisld Thank you. It's a great idea!
BTW, why doesn't numpylines merge back to master branch. Does numpylines helps to improve backtrader performance?
-
RE: Multicore optimization not work for parallel multistrategy scenario
@vladisld Thanks, perhaps I misunderstand the usage of optstragegy, maybe I should use addstrategy instead.
What I really want is that multi-strategy are working in parallel, trading on different asset, but using same account, with same broker, at same time. Maybe I should code in this way using addstrategy instead:
from __future__ import (absolute_import, division, print_function, unicode_literals) import argparse import datetime import time import os from backtrader.utils.py3 import range import backtrader as bt import backtrader.indicators as btind import backtrader.feeds as btfeeds class OptimizeStrategy(bt.Strategy): params = (('smaperiod', 15), ('macdperiod1', 12), ('macdperiod2', 26), ('macdperiod3', 9), ('instance', 0), ) def __init__(self): # Add indicators to add load print("instance:", self.p.instance, os.getppid()) btind.SMA(period=self.p.smaperiod) self.ind = btind.MACD(self.datas[self.p.instance], period_me1=self.p.macdperiod1, period_me2=self.p.macdperiod2, period_signal=self.p.macdperiod3) def next(self): if self.ind > 1: self.buy(self.datas[self.p.instance]) else: self.sell(self.datas[self.p.instance]) def runstrat(): # Create a cerebro entity cerebro = bt.Cerebro(maxcpus=10) #not args.no_optreturn) # Add a strategy for i in range(10): cerebro.addstrategy( OptimizeStrategy, instance=i ) ##!!!!!!!!!!!!!!!! # Create the 1st data for i in range(10): #could be different csv file per data data = btfeeds.BacktraderCSVData(dataname='../../datas/2006-day-001.txt') # Add the Data Feed to Cerebro cerebro.adddata(data) # clock the start of the process tstart = time.clock() # Run over everything stratruns = cerebro.run() # clock the end of the process tend = time.clock() print('==================================================') for stratrun in stratruns: print('**************************************************') for strat in stratrun: print('--------------------------------------------------') print(strat.p._getkwargs()) print('==================================================') # print out the result print('Time used:', str(tend - tstart)) if __name__ == '__main__': runstrat()
BTW, why doesn't numpylines merge back to master branch. Does numpylines helps to improve backtrader performance?
Thanks
-
RE: Multicore optimization not work for parallel multistrategy scenario
@crazy25000 Thank you very much.
Yes, you are right.
But if you set maxcpus=1, you will see the strategy is executed one by one, which mean each strategy are all independent, the output is like this:
in next: 0 24170 1996-05-30 23:59:59.999989 in next: 0 24170 1996-05-31 23:59:59.999989 in next: 0 24170 1996-06-03 23:59:59.999989 in next: 0 24170 1996-06-04 23:59:59.999989 in next: 0 24170 1996-06-05 23:59:59.999989 in next: 0 24170 1996-06-06 23:59:59.999989 in next: 1 24170 1996-05-30 23:59:59.999989 #strategy 1 is executed after strategy 0 finished
And in my case, all strategy are working in parallel, trading on different asset, but using same account, with same broker, at same time. the output is like this:
in next: 0 24119 1996-05-30 23:59:59.999989 in next: 1 24119 1996-05-30 23:59:59.999989 in next: 2 24119 1996-05-30 23:59:59.999989 in next: 3 24119 1996-05-30 23:59:59.999989 in next: 4 24119 1996-05-30 23:59:59.999989 in next: 5 24119 1996-05-30 23:59:59.999989 in next: 6 24119 1996-05-30 23:59:59.999989 in next: 7 24119 1996-05-30 23:59:59.999989 in next: 8 24119 1996-05-30 23:59:59.999989 in next: 9 24119 1996-05-30 23:59:59.999989 in next: 0 24119 1996-05-31 23:59:59.999989 in next: 1 24119 1996-05-31 23:59:59.999989 #all strategy finish on 1996-05-30, then carry on the next day
Could my use case be speed up by multicore?
Thanks
-
RE: Multicore optimization not work for parallel multistrategy scenario
@crazy25000
Here is the code:from __future__ import (absolute_import, division, print_function, unicode_literals) import argparse import datetime import time import os from backtrader.utils.py3 import range import backtrader as bt import backtrader.indicators as btind import backtrader.feeds as btfeeds class OptimizeStrategy(bt.Strategy): params = (('smaperiod', 15), ('macdperiod1', 12), ('macdperiod2', 26), ('macdperiod3', 9), ('instance', 0), ) def __init__(self): # Add indicators to add load print("instance:", self.p.instance, os.getppid()) btind.SMA(period=self.p.smaperiod) self.ind = btind.MACD(self.datas[self.p.instance], period_me1=self.p.macdperiod1, period_me2=self.p.macdperiod2, period_signal=self.p.macdperiod3) def next(self): if self.ind > 1: self.buy(self.datas[self.p.instance]) else: self.sell(self.datas[self.p.instance]) def runstrat(): # Create a cerebro entity cerebro = bt.Cerebro(maxcpus=10) #not args.no_optreturn) # Add a strategy for i in range(10): cerebro.optstrategy( OptimizeStrategy, instance=i ) # Create the 1st data for i in range(10): #could be different csv file per data data = btfeeds.BacktraderCSVData(dataname='../../datas/2006-day-001.txt') # Add the Data Feed to Cerebro cerebro.adddata(data) # clock the start of the process tstart = time.clock() # Run over everything stratruns = cerebro.run() # clock the end of the process tend = time.clock() print('==================================================') for stratrun in stratruns: print('**************************************************') for strat in stratrun: print('--------------------------------------------------') print(strat.p._getkwargs()) print('==================================================') # print out the result print('Time used:', str(tend - tstart)) if __name__ == '__main__': runstrat()
Thanks
-
RE: Multicore optimization not work for parallel multistrategy scenario
@crazy25000 Sorry, I might not clearly express what I mean.
My use case is like this one:
https://community.backtrader.com/topic/1337/running-multiple-strategies-combining-the-outputIn my use case, there are several strategy in one cerebro, each strategy trade one asset at same time.
Could this use case be executed by multiprocess to speed up?
-
Multicore optimization not work for parallel multistrategy scenario
After I study the /backtrader/samples/optimization/optimization.py, I realize that the strategys in the sample is all independent, and each strategy can be executed to the end of time series then execute another strategy. Here is the code example:
class UserStockStrategy(bt.Strategy): def next(self): print("in next:", self.p.instance, os.getpid(), date) cerebro = bt.Cerebro(maxcpus=1, runonce=not args.no_runonce, preload=True, optdatas=not args.no_optdatas, optreturn=not args.no_optreturn) cerebro.optstrategy( UserStockStrategy, instance=range(0, 10), #iterstrats ) ###########output################## in next: 0 24170 1996-05-30 23:59:59.999989 in next: 0 24170 1996-05-31 23:59:59.999989 in next: 0 24170 1996-06-03 23:59:59.999989 in next: 0 24170 1996-06-04 23:59:59.999989 in next: 0 24170 1996-06-05 23:59:59.999989 in next: 0 24170 1996-06-06 23:59:59.999989 in next: 1 24170 1996-05-30 23:59:59.999989 in next: 2 24170 1996-05-31 23:59:59.999989 in next: 3 24170 1996-06-03 23:59:59.999989 in next: 4 24170 1996-06-04 23:59:59.999989 in next: 5 24170 1996-06-05 23:59:59.999989 in next: 6 24170 1996-06-06 23:59:59.999989
If I code in multistratey way, all strategy execute in the same time, and all the strategy next function will be processed one by one in same process, so the multicore optimization doesn't work anymore. Here is the code example:
class UserStockStrategy(bt.Strategy): def next(self): print("in next:", self.p.instance, os.getpid(), date) cerebro = bt.Cerebro(maxcpus=10) for i in range(10): cerebro.optstrategy(UserStockStrategy, instance=i) ###########output################## in next: 0 24119 1996-05-30 23:59:59.999989 in next: 1 24119 1996-05-30 23:59:59.999989 in next: 2 24119 1996-05-30 23:59:59.999989 in next: 3 24119 1996-05-30 23:59:59.999989 in next: 4 24119 1996-05-30 23:59:59.999989 in next: 5 24119 1996-05-30 23:59:59.999989 in next: 6 24119 1996-05-30 23:59:59.999989 in next: 7 24119 1996-05-30 23:59:59.999989 in next: 8 24119 1996-05-30 23:59:59.999989 in next: 9 24119 1996-05-30 23:59:59.999989 in next: 0 24119 1996-05-31 23:59:59.999989 in next: 1 24119 1996-05-31 23:59:59.999989 in next: 2 24119 1996-05-31 23:59:59.999989 in next: 3 24119 1996-05-31 23:59:59.999989 in next: 4 24119 1996-05-31 23:59:59.999989 in next: 5 24119 1996-05-31 23:59:59.999989 in next: 6 24119 1996-05-31 23:59:59.999989 in next: 7 24119 1996-05-31 23:59:59.999989 in next: 8 24119 1996-05-31 23:59:59.999989 in next: 9 24119 1996-05-31 23:59:59.999989 in next: 0 24119 1996-06-03 23:59:59.999989 in next: 1 24119 1996-06-03 23:59:59.999989 in next: 2 24119 1996-06-03 23:59:59.999989
We can see all strategy share the same process.
My question is:- Is my unstanding about multicore optimization correct?
- Is there any way to run multistrategy in multiprocess? for example, each process only execute one strategy, but works in synchronous and parallel way.