Untitled conversation
- User
[*****************100%*******************] 1 of 1 completed Traceback (most recent call last): File "c:\Users\Adrian\Desktop\Trader_1.py", line 181, in result = cerebro.run() ^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1217, in runstrategies strat = stratcls(*sargs, **skwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\metabase.py", line 88, in call _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\metabase.py", line 78, in doinit _obj.init(*args, **kwargs) File "c:\Users\Adrian\Desktop\Trader_1.py", line 115, in init (self.data0 / self.data1).rolling(window=self.params.lookback).mean() - 1.0 ^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\lineseries.py", line 461, in getattr return getattr(self.lines, name) ^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'data1'
import backtrader as bt import pandas as pd import yfinance as yf import requests
setup bot
token = ''
def send_message(text): chat_id = '' url = f"https://api.telegram.org/bot{token}/sendMessage" params = { "chat_id": chat_id, "text": text, } resp = requests.get(url, params=params) resp.raise_for_status()
class MovingAverageStrategy(bt.Strategy): params = (('fast_ma', 20), ('slow_ma', 50))
codedef __init__(self): self.fast_ma = bt.indicators.SMA(self.data, period=self.params.fast_ma) self.slow_ma = bt.indicators.SMA(self.data, period=self.params.slow_ma) self.crossover = bt.indicators.CrossOver(self.fast_ma, self.slow_ma) def next(self): if not self.position: if self.crossover > 0: self.buy() elif self.crossover < 0: self.sell()class MomentumStrategy(bt.Strategy): params = (('period', 14), ('mom', 10))
- Assistant
- User
[*****************100%] 1 of 1 completed [*****************100%] 1 of 1 completed Traceback (most recent call last): File "c:\Users\Adrian\Desktop\Trader_1.py", line 181, in result = cerebro.run() ^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1217, in runstrategies strat = stratcls(*sargs, **skwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\metabase.py", line 88, in call _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\metabase.py", line 78, in doinit _obj.init(*args, **kwargs) File "c:\Users\Adrian\Desktop\Trader_1.py", line 115, in (self.data0 / self.data1).rolling(window=self.params.lookback).mean() - 1.0 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'LinesOperation' object has no attribute 'rolling'
- Assistant
Apologies for the confusion earlier. Since
rollingis a native Pandas method, it doesn't work with backtrader'sLinesOperationobject. Instead, we can use backtrader's ownSimpleMovingAverageindicator to calculate the average of the ratio between the two data feeds.Here's the modified
__init__method forPairsTradingStrategy:pythonclass PairsTradingStrategy(bt.Strategy): params = ( ('lookback', 20), ('entry_threshold', 2.0), ('exit_threshold', 0.5), ) def __init__(self): ratio = self.data0 / self.data1 ratio_sma = bt.indicators.SimpleMovingAverage(ratio, period=self.params.lookback) ratio_stddev = bt.indicators.StandardDeviation(ratio, period=self.params.lookback) self.zscore = (ratio - ratio_sma) / ratio_stddev self.long_entry = self.params.entry_threshold self.long_exit = self.params.exit_threshold self.short_entry = -self.params.entry_threshold self.short_exit = -self.params.exit_threshold # ... - User
[*****************100%] 1 of 1 completed [*****************100%] 1 of 1 completed Traceback (most recent call last): File "c:\Users\Adrian\Desktop\Trader_1.py", line 183, in result = cerebro.run() ^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies self._runonce(runstrats) File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1695, in _runonce strat._oncepost(dt0) File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\strategy.py", line 311, in _oncepost self.nextstart() # only called for the 1st value ^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\lineiterator.py", line 347, in nextstart self.next() File "c:\Users\Adrian\Desktop\Trader_1.py", line 126, in next if self.zscore[-1] > self.long_entry: ^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\lineseries.py", line 461, in return getattr(self.lines, name) ^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'zscore'
- Assistant
My apologies for the oversight. It looks like we need to define the as a in the . Here's how you can do it:
- User
[*****************100%] 1 of 1 completed [*****************100%] 1 of 1 completed Traceback (most recent call last): File "c:\Users\Adrian\Desktop\Trader_1.py", line 184, in result = cerebro.run() ^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1293, in runstrategies self._runonce(runstrats) File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1695, in _runonce strat._oncepost(dt0) File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\strategy.py", line 311, in _oncepost self.nextstart() # only called for the 1st value ^^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\lineiterator.py", line 347, in nextstart self.next() File "c:\Users\Adrian\Desktop\Trader_1.py", line 127, in next if self.zscore[-1] > self.long_entry: ^^^^^^^^^^^^^^^ File "C:\Users\Adrian\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\lineseries.py", line 461, in return getattr(self.lines, name) ^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'long_entry' PS C:\Users\Adrian>
- Assistant
Apologies once again. There is another mistake in the code. You should replace all self.zscore, self.long_entry, self.long_exit, self.short_entry, and self.short_exit with their respective lines if defined.
Here's the updated next() method for the PairsTradingStrategy:
pythondef next(self): if self.position.size == 0: if self.lines.zscore[0] > self.long_entry: self.buy(data=self.data0) self.sell(data=self.data1) elif self.lines.zscore[0] < self.short_entry: self.sell(data=self.data0) self.buy(data=self.data1) elif self.position.size > 0: if self.lines.zscore[0] < self.long_exit: self.close() elif self.position.size < 0: if self.lines.zscore[0] > self.short_exit: self.close() - User
- Assistant
I apologize once again for the confusion. I mistakenly only replaced
self.zscorewithself.lines.zscoreand didn't replace other attributes with their respective lines. We also need to definelong_entry,long_exit,short_entry, andshort_exitas lines instead of using the attributes directly.Here's the updated
PairsTradingStrategyclass:python