Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

michalhnat/backtesting.py-metalabel

Open more actions menu
 
 

Repository files navigation

Backtesting.py - metalabeling

This fork intruduces simple implemention of meta-labeling module build on top of Backtesting.py library.

The module integrates with Backtesting.py, allowing users to easily add meta-labeling to their existing strategies. It supports various machine learning models and provides utilities for feature engineering and signal filtering.

Key features:

  • Easy integration with Backtesting.py strategies

  • Support for multiple classifier models

  • Ensemble model builder

Usage

# clone this repo

cd backtesting.py-metalabel
uv sync

uv run python showcase.py
#ASSET = GOOG

class SmaCross(Strategy):
    def init(self):
        price = self.data.Close
        self.ma1 = self.I(SMA, price, 5)
        self.ma2 = self.I(SMA, price, 9)

    def next(self):
        if crossover(self.ma1, self.ma2):
            self.buy()
        elif crossover(self.ma2, self.ma1):
            self.sell()


split_idx = int(len(ASSET) * 0.8)
train_data = ASSET.iloc[:split_idx].copy()
test_data = ASSET.iloc[split_idx:].copy()

rf_parms = {
    'n_estimators': 10000,
    'max_depth': 15,
    'random_state': 223145
}

xgb_params = {
    "n_estimators": 700,
    "learning_rate": 0.05,
}

ensemble = (
    EnsembleBuilder()
    .add_model("rf", RandomForrestModel(**rf_parms), weight=0.7)
    .add_model("xg", XGBClassifier(**xgb_params), weight=0.3)
    .soft_voting()
    .build()
)

model = ensemble 


curr_strat = SmaCross

ml = MetaLabeler(strategy=curr_strat, model=model, window_size=100,
                 data=train_data, commission=.002, exclusive_orders=True,
                 finalize_trades=True, cash=1_000_000)

gate = ml.create_filter(curr_strat, window_size=100, threshold=0.7, runtime_data=test_data)

enhanced_strategy = make_enhanced_strategy(curr_strat, gate)

bt = Backtest(test_data, enhanced_strategy, cash=1_000_000,
               commission=.002, exclusive_orders=True, finalize_trades=True)

Full script can be found in showcase.py.

Results

Here is a markdown table comparing the results of the Base Strategy and Enhanced Strategy:

Metric Base Strategy Enhanced Strategy
Start 2011-06-15 00:00:00 2011-06-15 00:00:00
End 2013-03-01 00:00:00 2013-03-01 00:00:00
Duration 625 days 00:00:00 625 days 00:00:00
Exposure Time [%] 97.2093 97.2093
Equity Final [$] 1,167,385.86 1,631,710.29
Equity Peak [$] 1,380,412.05 1,653,523.12
Commissions [$] 217,971.24 141,266.02
Return [%] 16.74 63.17
Buy & Hold Return [%] 66.98 66.98
Return (Ann.) [%] 9.49 33.24
Volatility (Ann.) [%] 28.80 35.60
CAGR [%] 6.44 21.83
Sharpe Ratio 0.33 0.93
Sortino Ratio 0.59 1.96
Calmar Ratio 0.34 1.81
Alpha [%] 15.82 21.85
Beta 0.01 0.62
Max. Drawdown [%] -27.54 -18.41
Avg. Drawdown [%] -8.51 -4.84
Max. Drawdown Duration 560 days 00:00:00 246 days 00:00:00
Avg. Drawdown Duration 119 days 00:00:00 41 days 00:00:00
# Trades 46 27
Win Rate [%] 34.78 55.56
Best Trade [%] 23.23 17.47
Worst Trade [%] -6.81 -6.35
Avg. Trade [%] 0.34 1.83
Max. Trade Duration 83 days 00:00:00 108 days 00:00:00
Avg. Trade Duration 14 days 00:00:00 23 days 00:00:00
Profit Factor 1.32 3.16
Expectancy [%] 0.48 1.97
SQN 0.40 1.90
Kelly Criterion 0.06 0.37
_strategy SmaCross Enhanced_SmaCross

About

Fork introducing meta-labeling module

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 99.5%
  • JavaScript 0.5%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.