Ejemplo n.º 1
0
 def test_dont_overwrite_data(self):
     df = EURUSD.copy()
     bt = Backtest(df, SmaCross)
     bt.run()
     bt.optimize(fast=4, slow=[6, 8])
     bt.plot(plot_drawdown=True, open_browser=False)
     self.assertTrue(df.equals(EURUSD))
Ejemplo n.º 2
0
    def test_trading_with_Machine_Learning(self):
        test_funcs = BacktestingTutorialWithML

        data = EURUSD.copy()

        close = data.Close.values
        sma10 = SMA(data.Close, 10)
        sma20 = SMA(data.Close, 20)
        sma50 = SMA(data.Close, 50)
        sma100 = SMA(data.Close, 100)
        upper, lower = test_funcs.BBANDS(data, 20, 2)

        # Design matrix / independent features:

        # Price-derived features
        data['X_SMA10'] = (close - sma10) / close
        data['X_SMA20'] = (close - sma20) / close
        data['X_SMA50'] = (close - sma50) / close
        data['X_SMA100'] = (close - sma100) / close

        data['X_DELTA_SMA10'] = (sma10 - sma20) / close
        data['X_DELTA_SMA20'] = (sma20 - sma50) / close
        data['X_DELTA_SMA50'] = (sma50 - sma100) / close

        # Indicator features
        data['X_MOM'] = data.Close.pct_change(periods=2)
        data['X_BB_upper'] = (upper - close) / close
        data['X_BB_lower'] = (lower - close) / close
        data['X_BB_width'] = (upper - lower) / close
        data['X_Sentiment'] = ~data.index.to_series().between(
            '2017-09-27', '2017-12-14')

        # Some datetime features for good measure
        data['X_day'] = data.index.dayofweek
        data['X_hour'] = data.index.hour

        data = data.dropna().astype(float)

        X, y = test_funcs.get_clean_Xy(data)
        X_train, X_test, y_train, y_test = train_test_split(X,
                                                            y,
                                                            test_size=.5,
                                                            random_state=0)

        clf = KNeighborsClassifier(
            7)  # Model the output based on 7 "nearest" examples
        clf.fit(X_train, y_train)

        y_pred = clf.predict(X_test)

        _ = pd.DataFrame({
            'y_true': y_test,
            'y_pred': y_pred
        }).plot(figsize=(15, 2), alpha=.7)
        print('Classification accuracy: ', np.mean(y_test == y_pred))

        bt = Backtest(data, MLTrainOnceStrategy, commission=.0002, margin=.05)
        bt.run()
Ejemplo n.º 3
0
    def test_nowrite_df(self):
        # Test we don't write into passed data df by default.
        # Important for copy-on-write in Backtest.optimize()
        df = EURUSD.astype(float)
        values = df.values.ctypes.data
        assert values == df.values.ctypes.data

        class S(SmaCross):
            def init(self):
                super().init()
                assert values == self.data.df.values.ctypes.data

        bt = Backtest(df, S)
        _ = bt.run()
        assert values == bt._data.values.ctypes.data
# ---

# # Trading with Machine Learning Models
#
# This tutorial will show how to train and backtest a
# [machine learning](https://en.wikipedia.org/wiki/Machine_learning)
# price forecast model with _backtesting.py_ framework. It is assumed you're already familiar with
# [basic framework usage](https://kernc.github.io/backtesting.py/doc/examples/Quick Start User Guide.html)
# and machine learning in general.
#
# For this tutorial, we'll use almost a year's worth sample of hourly EUR/USD forex data:

# +
from backtesting.test import EURUSD, SMA

data = EURUSD.copy()
data

# -

# In
# [supervised machine learning](https://en.wikipedia.org/wiki/Supervised_learning),
# we try to learn a function that maps input feature vectors (independent variables) into known output values (dependent variable):
#
# $$ f\colon X \to \mathbf{y} $$
#
# That way, provided our model function is sufficient, we can predict future output values from the newly acquired input feature vectors to some degree of certainty.
# In our example, we'll try to map several price-derived features and common technical indicators to the price point two days in the future.
# We construct [model design matrix](https://en.wikipedia.org/wiki/Design_matrix) $X$ below: