MCPcopy Index your code
hub / github.com/kernc/backtesting.py / run

Method run

backtesting/backtesting.py:1256–1369  ·  view source on GitHub ↗

Run the backtest. Returns `pd.Series` with results and statistics. Keyword arguments are interpreted as strategy parameters. >>> Backtest(GOOG, SmaCross).run() Start 2004-08-19 00:00:00 End 2013-03-01 00

(self, **kwargs)

Source from the content-addressed store, hash-verified

1254 self._finalize_trades = bool(finalize_trades)
1255
1256 def run(self, **kwargs) -> pd.Series:
1257 """
1258 Run the backtest. Returns `pd.Series` with results and statistics.
1259
1260 Keyword arguments are interpreted as strategy parameters.
1261
1262 >>> Backtest(GOOG, SmaCross).run()
1263 Start 2004-08-19 00:00:00
1264 End 2013-03-01 00:00:00
1265 Duration 3116 days 00:00:00
1266 Exposure Time [%] 96.74115
1267 Equity Final [$] 51422.99
1268 Equity Peak [$] 75787.44
1269 Return [%] 414.2299
1270 Buy & Hold Return [%] 703.45824
1271 Return (Ann.) [%] 21.18026
1272 Volatility (Ann.) [%] 36.49391
1273 CAGR [%] 14.15984
1274 Sharpe Ratio 0.58038
1275 Sortino Ratio 1.08479
1276 Calmar Ratio 0.44144
1277 Alpha [%] 394.37391
1278 Beta 0.03803
1279 Max. Drawdown [%] -47.98013
1280 Avg. Drawdown [%] -5.92585
1281 Max. Drawdown Duration 584 days 00:00:00
1282 Avg. Drawdown Duration 41 days 00:00:00
1283 # Trades 66
1284 Win Rate [%] 46.9697
1285 Best Trade [%] 53.59595
1286 Worst Trade [%] -18.39887
1287 Avg. Trade [%] 2.53172
1288 Max. Trade Duration 183 days 00:00:00
1289 Avg. Trade Duration 46 days 00:00:00
1290 Profit Factor 2.16795
1291 Expectancy [%] 3.27481
1292 SQN 1.07662
1293 Kelly Criterion 0.15187
1294 _strategy SmaCross
1295 _equity_curve Eq...
1296 _trades Size EntryB...
1297 dtype: object
1298
1299 .. warning::
1300 You may obtain different results for different strategy parameters.
1301 E.g. if you use 50- and 200-bar SMA, the trading simulation will
1302 begin on bar 201. The actual length of delay is equal to the lookback
1303 period of the `Strategy.I` indicator which lags the most.
1304 Obviously, this can affect results.
1305 """
1306 data = _Data(self._data.copy(deep=False))
1307 broker: _Broker = self._broker(data=data)
1308 strategy: Strategy = self._strategy(broker, data, kwargs)
1309
1310 strategy.init()
1311 data._update() # Strategy.init might have changed/added to data.df
1312
1313 # Indicators used in Strategy.next()

Callers 15

_optimize_gridMethod · 0.95
memoized_runMethod · 0.95
_optimize_samboMethod · 0.95
test_runMethod · 0.95
test_run_speedMethod · 0.95
test_assertionsMethod · 0.95
test_broker_paramsMethod · 0.95
test_strategy_strMethod · 0.95
test_file_sizeMethod · 0.95
test_paramsMethod · 0.95

Calls 11

_DataClass · 0.85
_strategy_indicatorsFunction · 0.85
_indicator_warmup_nbarsFunction · 0.85
_tqdmFunction · 0.85
try_Function · 0.85
_updateMethod · 0.80
_set_lengthMethod · 0.80
compute_statsFunction · 0.70
initMethod · 0.45
nextMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected