edtechre / pybroker

Algorithmic Trading in Python with Machine Learning

Home Page:https://www.pybroker.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can't backtest with fresh installation

frankygtd opened this issue · comments

when I run backtesting example in doc, I got this issue.
Then I change window_dates.sort() to window_dates.argsort().
There is not one order in the result of backtesting.


AttributeError Traceback (most recent call last)
Cell In[1], line 18
15 strategy.add_execution(
16 exec_fn, ['AAPL', 'MSFT'], indicators=highest('high_10d', 'close', period=10))
17 # Run the backtest after 20 days have passed.
---> 18 result = strategy.backtest(warmup=20)

File ~\pybroker\src\pybroker\strategy.py:1067, in Strategy.backtest(self, start_date, end_date, timeframe, between_time, days, lookahead, train_size, shuffle, calc_bootstrap, disable_parallel, warmup)
1000 def backtest(
1001 self,
1002 start_date: Optional[Union[str, datetime]] = None,
(...)
1012 warmup: Optional[int] = None,
1013 ) -> Optional[TestResult]:
1014 """Backtests the trading strategy by running executions that were added
1015 with :meth:.add_execution.
1016
(...)
1065 history, and evaluation metrics.
1066 """
-> 1067 return self.walkforward(
1068 windows=1,
1069 lookahead=lookahead,
1070 start_date=start_date,
1071 end_date=end_date,
1072 timeframe=timeframe,
1073 between_time=between_time,
1074 days=days,
1075 train_size=train_size,
1076 shuffle=shuffle,
1077 calc_bootstrap=calc_bootstrap,
1078 disable_parallel=disable_parallel,
1079 warmup=warmup,
1080 )

File ~\pybroker\src\pybroker\strategy.py:1218, in Strategy.walkforward(self, windows, lookahead, start_date, end_date, timeframe, between_time, days, train_size, shuffle, calc_bootstrap, disable_parallel, warmup)
1205 train_only = (
1206 self._before_exec_fn is None
1207 and self._after_exec_fn is None
1208 and all(map(lambda e: e.fn is None, self._executions))
1209 )
1210 portfolio = Portfolio(
1211 self._config.initial_cash,
1212 self._config.fee_mode,
(...)
1216 self._config.max_short_positions,
1217 )
-> 1218 self._run_walkforward(
1219 portfolio=portfolio,
1220 df=df,
1221 indicator_data=indicator_data,
1222 tf_seconds=tf_seconds,
1223 between_time=between_time,
1224 days=day_ids,
1225 windows=windows,
1226 lookahead=lookahead,
1227 train_size=train_size,
1228 shuffle=shuffle,
1229 train_only=train_only,
1230 warmup=warmup,
1231 )
1232 if train_only:
1233 self._logger.walkforward_completed()

File ~\pybroker\src\pybroker\strategy.py:1289, in Strategy._run_walkforward(self, portfolio, df, indicator_data, tf_seconds, between_time, days, windows, lookahead, train_size, shuffle, train_only, warmup)
1287 sym_dates.sort()
1288 exit_dates[sym] = sym_dates[-1]
-> 1289 for train_idx, test_idx in self.walkforward_split(
1290 df=df,
1291 windows=windows,
1292 lookahead=lookahead,
1293 train_size=train_size,
1294 shuffle=shuffle,
1295 ):
1296 models: dict[ModelSymbol, TrainedModel] = {}
1297 train_data = df.loc[train_idx]

File ~\pybroker\src\pybroker\strategy.py:653, in WalkforwardMixin.walkforward_split(self, df, windows, lookahead, train_size, shuffle)
651 print(window_dates)
652 #window_dates=list(window_dates)
--> 653 window_dates.sort()
654 #window_dates.argsort()
655 #window_dates=sorted(window_dates)
656 error_msg = f"""
657 Invalid params for {len(window_dates)} dates:
658 windows: {windows}
659 lookahead: {lookahead}
660 train_size: {train_size}
661 """

AttributeError: 'DatetimeArray' object has no attribute 'sort'

lower pandas edition to solve it.

Hi @frankygtd,

Thank you for closing this out. Can you share which version of Pandas was causing this problem and which version fixed it?

Thank you!