@run-out
Thanks,
getposition(data=None, broker=None) is helpfull.
Best posts made by MuSaCN
-
RE: I want to determine my position type in next (), buy or sell. What should I write?
Latest posts made by MuSaCN
-
RE: After optimization, how to obtain optimization results ?
thanks, it is useful to me.
-
After optimization, how to obtain optimization results ?
I usually use:
def stop(self): print("stop(): ", self.params.Para0, self.broker.getvalue(), self.broker.get_cash())
After optimization, It just print message. how to obtain opt results?
-
After backtest, how to get backtest's history trading particulars?
After backtest, how to get backtest's history trading particulars?
-
RE: I want to determine my position type in next (), buy or sell. What should I write?
@run-out
Thanks,
getposition(data=None, broker=None) is helpfull. -
I want to determine my position type in next (), buy or sell. What should I write?
I want to determine my position type in next (), buy or sell. What should I write?
-
how to set figsize when we use cerebro.plot()?
The original drawing is too small, I want to plot larger size. DPI parameter do not work well.
-
RE: How does the backtrader get the cash or value of each step?
Thank you for sharing your code.
Since the backtrader has a plot() function to draw the fund curve, it must store them internally. -
How does the backtrader get the cash or value of each step?
After the backtest, I want the value of funds for each step
results = cerebro.run()
After this code, what can I do to get it? I did not find a similar answer from the official document.
-
How to change the index of indicator to the opposite ?
if we use code:
self.sma = bt.indicators.MovingAverageSimple(data, period)
self.sma[0] --> current bar
self.sma[-1] --> last one barBut, I want it:
self.sma[0] --> current bar
self.sma[1] --> last one bar
Is there any other way besides the function ? -
optstrategy() get error, help me, thanks.
my code :
import backtrader as bt cerebro = bt.Cerebro() # ---read Data Path = "C:\\Users\\i2011\\OneDrive\\Book_Code&Data\\量化投资以python为工具\\数据及源代码\\033" CJSecurities = pd.read_csv(Path + '\\CJSecurities.csv', index_col=1, parse_dates=True) CJSecurities = CJSecurities.iloc[:, 1:] data0 = CJSecurities if "openinterest" not in data0.columns: data0['openinterest'] = 0 data = bt.feeds.PandasData(dataname=data0) # --- cerebro.adddata(data) cerebro.broker.setcash(10000.0) # --- class TestStrategy(bt.Strategy): params = ( ('maperiod', 15), ) def __init__(self): self.dataclose = self.datas[0].close self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.maperiod) self.bars_executed = 0 def next(self): if not self.position: if self.dataclose[0] > self.sma[0]: self.buy() else: if len(self) >= self.bars_executed + 5: self.sell() def notify_order(self, order): self.bars_executed = len(self) def stop(self): print(self.params.maperiod, self.broker.getvalue()) # ------ # cerebro.addstrategy(TestStrategy) --> addstrategy() can work with run(). cerebro.optstrategy(TestStrategy,maperiod=range(5, 10)) # --> This optstrategy() get error cerebro.run()
the error:
Process SpawnPoolWorker-4: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)> Process SpawnPoolWorker-2: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)> Process SpawnPoolWorker-5: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)> Process SpawnPoolWorker-3: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)> Process SpawnPoolWorker-1: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)>