strategy

Q18 Machine Learning on a Rolling Basis

This example shows how to make a submission to the stock contest using machine learning and retraining.

You can clone and edit this example there (tab Examples).


In this example we predict whether the price will rise or fall by using supervised learning (Bayesian Ridge Regression). This template represents a starting point for developing a system which can take part to the Q18 NASDAQ-100 Stock Long-Short contest.

It consists of two parts.

  • In the first part we just perform a global training of the time series using all time series data. We disregard the sequential aspect of the data and use also future data to train past data.

  • In the second part we use the built-in backtester and perform training and prediction on a rolling basis in order to avoid forward looking. Please note that we are using a specialized version of the Quantiacs backtester which dramatically speeds up the the backtesting process by retraining your model on a regular basis.

Features for learning: we will use several technical indicators trying to capture different features. You can have a look at Technical Indicators.

Please note that:

  • Your trading algorithm can open short and long positions.

  • At each point in time your algorithm can trade all or a subset of the stocks which at that point of time are or were part of the NASDAQ-100 stock index. Note that the composition of this set changes in time, and Quantiacs provides you with an appropriate filter function for selecting them.

  • The Sharpe ratio of your system since January 1st, 2006, has to be larger than 1.

  • Your system cannot be a copy of the current examples. We run a correlation filter on the submissions and detect duplicates.

  • For simplicity we will use a single asset. It pays off to use more assets, ideally uncorrelated, and diversify your positions for a more solid Sharpe ratio.

More details on the rules can be found here.

Need help? Check the Documentation and find solutions/report problems in the Forum section.

More help with Jupyter? Check the official Jupyter page.

Once you are done, click on Submit to the contest and take part to our competitions.

API reference:

  • data: check how to work with data;

  • backtesting: read how to run the simulation and check the results.

Need to use the optimizer function to automate tedious tasks?

  • optimization: read more on our article.
In [1]:
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) { return false; }
// disable widget scrolling
In [2]:
import logging

import xarray as xr  # xarray for data manipulation

import qnt.data as qndata     # functions for loading data
import qnt.backtester as qnbt # built-in backtester
import qnt.ta as qnta         # technical analysis library
import qnt.stats as qnstats   # statistical functions

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt

np.seterr(divide = "ignore")

from qnt.ta.macd import macd
from qnt.ta.rsi  import rsi
from qnt.ta.stochastic import stochastic_k, stochastic, slow_stochastic

from sklearn import linear_model
from sklearn.metrics import r2_score
from sklearn.metrics import explained_variance_score
from sklearn.metrics import mean_absolute_error
In [3]:
# loading nasdaq-100 stock data

stock_data = qndata.stocks.load_ndx_data(tail = 365 * 5, assets = ["NAS:AAPL", "NAS:AMZN"])
100% (367973 of 367973) |################| Elapsed Time: 0:00:00 Time:  0:00:00
100% (39443 of 39443) |##################| Elapsed Time: 0:00:00 Time:  0:00:00
100% (186804 of 186804) |################| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 1/1 0s
Data loaded 0s
In [4]:
def get_features(data):
    """Builds the features used for learning:
       * a trend indicator;
       * the moving average convergence divergence;
       * a volatility measure; 
       * the stochastic oscillator;
       * the relative strength index;
       * the logarithm of the closing price.
       These features can be modified and new ones can be added easily.
    """
   
    # trend:
    trend = qnta.roc(qnta.lwma(data.sel(field="close"), 60), 1)
     
    # moving average convergence  divergence (MACD):
    macd = qnta.macd(data.sel(field="close"))
    macd2_line, macd2_signal, macd2_hist = qnta.macd(data, 12, 26, 9)

    # volatility:
    volatility = qnta.tr(data.sel(field="high"), data.sel(field="low"), data.sel(field="close"))
    volatility = volatility / data.sel(field="close")
    volatility = qnta.lwma(volatility, 14)

    # the stochastic oscillator:
    k, d = qnta.stochastic(data.sel(field="high"), data.sel(field="low"), data.sel(field="close"), 14)
    
    # the relative strength index: 
    rsi = qnta.rsi(data.sel(field="close"))
    
    # the logarithm of the closing price:
    price = data.sel(field="close").ffill("time").bfill("time").fillna(0) # fill NaN
    price = np.log(price)
    
    # combine the six features:
    result = xr.concat(
        [trend, macd2_signal.sel(field="close"), volatility,  d, rsi, price],
        pd.Index(
            ["trend",  "macd", "volatility", "stochastic_d", "rsi", "price"],
            name = "field"
        )
    )

    return result.transpose("time", "field", "asset")
In [5]:
# displaying the features:
my_features = get_features(stock_data)
display(my_features.sel(field="trend").to_pandas())
asset NAS:AAPL NAS:AMZN
time
2019-05-02 NaN NaN
2019-05-03 NaN NaN
2019-05-06 NaN NaN
2019-05-07 NaN NaN
2019-05-08 NaN NaN
... ... ...
2024-04-24 -0.142910 0.019401
2024-04-25 -0.119379 -0.038844
2024-04-26 -0.124994 0.065824
2024-04-29 -0.040311 0.082888
2024-04-30 -0.096520 -0.032991

1258 rows × 2 columns

In [6]:
def get_target_classes(data):
    """ Target classes for predicting if price goes up or down."""
    
    price_current = data.sel(field="close")
    price_future  = qnta.shift(price_current, -1)

    class_positive = 1 # prices goes up
    class_negative = 0 # price goes down

    target_price_up = xr.where(price_future > price_current, class_positive, class_negative)

    return target_price_up
In [7]:
# displaying the target classes:
my_targetclass = get_target_classes(stock_data)
display(my_targetclass.to_pandas())
asset NAS:AAPL NAS:AMZN
time
2019-05-02 1 1
2019-05-03 0 0
2019-05-06 0 0
2019-05-07 1 0
2019-05-08 0 0
... ... ...
2024-04-24 1 0
2024-04-25 0 1
2024-04-26 1 1
2024-04-29 0 0
2024-04-30 0 0

1258 rows × 2 columns

In [8]:
def get_model():
    """This is a constructor for the ML model (Bayesian Ridge) which can be easily 
       modified for using different models.
    """
    
    model = linear_model.BayesianRidge()
    return model
In [9]:
# Create and train the models working on an asset-by-asset basis.

asset_name_all = stock_data.coords["asset"].values

models = dict()

for asset_name in asset_name_all:

        # drop missing values:
        target_cur   = my_targetclass.sel(asset=asset_name).dropna("time", "any")
        features_cur = my_features.sel(asset=asset_name).dropna("time", "any")
        
        # align features and targets:
        target_for_learn_df, feature_for_learn_df = xr.align(target_cur, features_cur, join="inner")

        if len(features_cur.time) < 10:
            # not enough points for training
                continue

        model = get_model()

        try:
            model.fit(feature_for_learn_df.values, target_for_learn_df)
            models[asset_name] = model
                
        except:
            logging.exception("model training failed")
            
print(models)
{'NAS:AAPL': BayesianRidge(), 'NAS:AMZN': BayesianRidge()}
In [10]:
# Showing which features are more important in predicting:

importance = models["NAS:AAPL"].coef_
importance

for i,v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i,v))
    
plt.bar([x for x in range(len(importance))], importance)
plt.show()
Feature: 0, Score: -0.00001
Feature: 1, Score: -0.00028
Feature: 2, Score: -0.00000
Feature: 3, Score: 0.00007
Feature: 4, Score: -0.00032
Feature: 5, Score: -0.00011
In [11]:
# Performs prediction and generates output weights:

asset_name_all = stock_data.coords["asset"].values
weights = xr.zeros_like(stock_data.sel(field="close"))
    
for asset_name in asset_name_all:
    if asset_name in models:
        model = models[asset_name]
        features_all = my_features
        features_cur = features_all.sel(asset=asset_name).dropna("time", "any")
        if len(features_cur.time) < 1:
            continue
        try:
            weights.loc[dict(asset=asset_name, time=features_cur.time.values)] = model.predict(features_cur.values)
        except KeyboardInterrupt as e:
            raise e
        except:
            logging.exception("model prediction failed")
            
print(weights)
<xarray.DataArray 'stocks_nasdaq100' (time: 1258, asset: 2)>
array([[0.        , 0.        ],
       [0.        , 0.        ],
       [0.        , 0.        ],
       ...,
       [0.52402839, 0.52780879],
       [0.5221206 , 0.51916534],
       [0.52417105, 0.51980274]])
Coordinates:
  * asset    (asset) <U8 'NAS:AAPL' 'NAS:AMZN'
  * time     (time) datetime64[ns] 2019-05-02 2019-05-03 ... 2024-04-30
    field    <U5 'close'
In [12]:
def get_sharpe(stock_data, weights):
    """Calculates the Sharpe ratio"""
    rr = qnstats.calc_relative_return(stock_data, weights)
    sharpe = qnstats.calc_sharpe_ratio_annualized(rr).values[-1]
    return sharpe

sharpe = get_sharpe(stock_data, weights)
sharpe
Out[12]:
0.7015068745711643

The sharpe ratio using the method above follows from forward looking. Predictions for (let us say) 2017 know about the relation between features and targets in 2020. Let us visualize the results:

In [13]:
import qnt.graph as qngraph

statistics = qnstats.calc_stat(stock_data, weights)

display(statistics.to_pandas().tail())

performance = statistics.to_pandas()["equity"]
qngraph.make_plot_filled(performance.index, performance, name="PnL (Equity)", type="log")

display(statistics[-1:].sel(field = ["sharpe_ratio"]).transpose().to_pandas())

# check for correlations with existing strategies:
qnstats.print_correlation(weights,stock_data)
field equity relative_return volatility underwater max_drawdown sharpe_ratio mean_return bias instruments avg_turnover avg_holding_time
time
2024-04-24 2.556911 -0.002014 0.294764 -0.050221 -0.414604 0.704389 0.207628 1.0 2.0 0.018619 298.129234
2024-04-25 2.542200 -0.005753 0.294661 -0.055685 -0.414604 0.699272 0.206049 1.0 2.0 0.018634 298.142054
2024-04-26 2.580891 0.015220 0.294614 -0.041313 -0.414604 0.711198 0.209529 1.0 2.0 0.018638 298.162730
2024-04-29 2.622164 0.015992 0.294574 -0.025982 -0.414604 0.723752 0.213198 1.0 2.0 0.018631 298.335365
2024-04-30 2.555081 -0.025583 0.294696 -0.050901 -0.414604 0.701507 0.206731 1.0 2.0 0.018633 339.920926
time 2024-04-30
field
sharpe_ratio 0.701507
WARNING! Can't calculate correlation.
ERROR:root:Correlation check failed.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/qnt/stats.py", line 823, in check_correlation
    cr_list = calc_correlation(rr, False)
  File "/usr/local/lib/python3.7/site-packages/qnt/stats.py", line 923, in calc_correlation
    raise e
  File "/usr/local/lib/python3.7/site-packages/qnt/stats.py", line 886, in calc_correlation
    with request.urlopen(ENGINE_CORRELATION_URL + "?participantId=" + PARTICIPANT_ID) as response:
  File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/usr/local/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/lib/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: 
In [14]:
"""R2 (coefficient of determination) regression score function."""
r2_score(my_targetclass, weights, multioutput="variance_weighted")
Out[14]:
-0.048440636067483535
In [15]:
"""The explained variance score explains the dispersion of errors of a given dataset"""
explained_variance_score(my_targetclass, weights, multioutput="uniform_average")
Out[15]:
-0.0460692578173747
In [16]:
"""The explained variance score explains the dispersion of errors of a given dataset"""
mean_absolute_error(my_targetclass, weights)
Out[16]:
0.49926590489368355

Let us now use the Quantiacs backtester for avoiding forward looking.

The backtester performs some transformations: it trains the model on one slice of data (using only data from the past) and predicts the weights for the following slice on a rolling basis:

In [17]:
def train_model(data):
    """Create and train the model working on an asset-by-asset basis."""
    
    asset_name_all = data.coords["asset"].values
    features_all   = get_features(data)
    target_all     = get_target_classes(data)

    models = dict()

    for asset_name in asset_name_all:

        # drop missing values:
        target_cur   = target_all.sel(asset=asset_name).dropna("time", "any")
        features_cur = features_all.sel(asset=asset_name).dropna("time", "any")
        
        target_for_learn_df, feature_for_learn_df = xr.align(target_cur, features_cur, join="inner")
        
        if len(features_cur.time) < 10:
                continue
                
        model = get_model()
        
        try:
            model.fit(feature_for_learn_df.values, target_for_learn_df)
            models[asset_name] = model
                
        except:
            logging.exception("model training failed")

    return models
In [18]:
def predict_weights(models, data):
    """The model predicts if the price is going up or down.
       The prediction is performed for several days in order to speed up the evaluation."""
    
    asset_name_all = data.coords["asset"].values
    weights = xr.zeros_like(data.sel(field="close"))
    
    for asset_name in asset_name_all:
        if asset_name in models:
            model = models[asset_name]
            features_all = get_features(data)
            features_cur = features_all.sel(asset=asset_name).dropna("time", "any")

            if len(features_cur.time) < 1:
                continue

            try:
                weights.loc[dict(asset=asset_name, time=features_cur.time.values)] = model.predict(features_cur.values)

            except KeyboardInterrupt as e:
                raise e
            
            except:
                logging.exception("model prediction failed")                

    return weights
In [19]:
# Calculate weights using the backtester:
weights = qnbt.backtest_ml(
    train                         = train_model,
    predict                       = predict_weights,
    train_period                  =  2 *365,  # the data length for training in calendar days
    retrain_interval              = 10 *365,  # how often we have to retrain models (calendar days)
    retrain_interval_after_submit = 1,        # how often retrain models after submission during evaluation (calendar days)
    predict_each_day              = False,    # Is it necessary to call prediction for every day during backtesting?
                                              # Set it to True if you suspect that get_features is looking forward.
    competition_type              = "stocks_nasdaq100",  # competition type
    lookback_period               = 365,                 # how many calendar days are needed by the predict function to generate the output
    start_date                    = "2005-01-01",        # backtest start date
    analyze                       = True,
    build_plots                   = True  # do you need the chart?
)
Run the last iteration...
100% (39443 of 39443) |##################| Elapsed Time: 0:00:00 Time:  0:00:00
100% (9005692 of 9005692) |##############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 1/1 3s
Data loaded 3s
100% (756972 of 756972) |################| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 1/1 3s
Data loaded 3s
Output cleaning...
fix uniq
ffill if the current price is None...
Check liquidity...
WARNING! Strategy trades non-liquid assets.
Fix liquidity...
Ok.
Check missed dates...
Ok.
Normalization...
Output cleaning is complete.
Write output: /root/fractions.nc.gz
State saved.
---
Run First Iteration...
100% (39443 of 39443) |##################| Elapsed Time: 0:00:00 Time:  0:00:00
100% (9041556 of 9041556) |##############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 1/1 3s
Data loaded 3s
---
Run all iterations...
Load data...
100% (39443 of 39443) |##################| Elapsed Time: 0:00:00 Time:  0:00:00
100% (14595768 of 14595768) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 1/7 1s
100% (14598364 of 14598364) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 2/7 2s
100% (14598364 of 14598364) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 3/7 3s
100% (14595744 of 14595744) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 4/7 4s
100% (14595672 of 14595672) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 5/7 5s
100% (14595672 of 14595672) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 6/7 6s
100% (13381356 of 13381356) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 7/7 7s
Data loaded 7s
100% (14729328 of 14729328) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 1/6 1s
100% (14732356 of 14732356) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 2/6 2s
100% (14729296 of 14729296) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 3/6 3s
100% (14729212 of 14729212) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 4/6 4s
100% (14729212 of 14729212) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 5/6 5s
100% (13678636 of 13678636) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 6/6 6s
Data loaded 6s
Backtest...
100% (39443 of 39443) |##################| Elapsed Time: 0:00:00 Time:  0:00:00
100% (14850448 of 14850448) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 1/6 1s
100% (14853476 of 14853476) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 2/6 2s
100% (14850416 of 14850416) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 3/6 3s
100% (14850332 of 14850332) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 4/6 4s
100% (14850332 of 14850332) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 5/6 5s
100% (13791116 of 13791116) |############| Elapsed Time: 0:00:00 Time:  0:00:00
fetched chunk 6/6 6s
Data loaded 6s
Output cleaning...
fix uniq
ffill if the current price is None...
Check liquidity...
WARNING! Strategy trades non-liquid assets.
Fix liquidity...
Ok.
Check missed dates...
Ok.
Normalization...
Output cleaning is complete.
Write output: /root/fractions.nc.gz
State saved.
---
Analyze results...
Check...
Check liquidity...
Ok.
Check missed dates...
Ok.
Check the sharpe ratio...
Period: 2006-01-01 - 2024-04-30
Sharpe Ratio = 0.4895180986587352
ERROR! The Sharpe Ratio is too low. 0.4895180986587352 < 1
Improve the strategy and make sure that the in-sample Sharpe Ratio more than 1.
Check correlation.
WARNING! Can't calculate correlation.
Correlation check failed.
---
Align...
Calc global stats...
---
Calc stats per asset...
Build plots...
---
Output:
asset NAS:AAL NAS:AAPL NAS:ABNB NAS:ADBE NAS:ADI NAS:ADP NAS:ADSK NAS:AEP NAS:AKAM NAS:ALGN
time
2024-04-17 0.0 0.006730 0.0 0.007773 0.006742 0.007155 0.006613 0.006655 0.0 0.0
2024-04-18 0.0 0.006769 0.0 0.007889 0.006767 0.007131 0.006605 0.006621 0.0 0.0
2024-04-19 0.0 0.006781 0.0 0.008078 0.006837 0.006879 0.006550 0.006589 0.0 0.0
2024-04-22 0.0 0.006807 0.0 0.007920 0.006793 0.006688 0.006577 0.006614 0.0 0.0
2024-04-23 0.0 0.006816 0.0 0.007564 0.006735 0.006941 0.006595 0.006636 0.0 0.0
2024-04-24 0.0 0.006803 0.0 0.007654 0.006631 0.007186 0.006632 0.006655 0.0 0.0
2024-04-25 0.0 0.006773 0.0 0.008077 0.006619 0.007215 0.006626 0.006654 0.0 0.0
2024-04-26 0.0 0.006784 0.0 0.007984 0.006581 0.007421 0.006640 0.006708 0.0 0.0
2024-04-29 0.0 0.006742 0.0 0.008133 0.006564 0.007047 0.006653 0.006703 0.0 0.0
2024-04-30 0.0 0.006715 0.0 0.008266 0.006619 0.006937 0.006631 0.006676 0.0 0.0
Stats:
field equity relative_return volatility underwater max_drawdown sharpe_ratio mean_return bias instruments avg_turnover avg_holding_time
time
2024-04-17 5.319675 -0.005325 0.182364 -0.032314 -0.560319 0.496862 0.090610 1.0 214.0 0.021799 134.477549
2024-04-18 5.300878 -0.003533 0.182348 -0.035733 -0.560319 0.495702 0.090390 1.0 214.0 0.021798 134.492917
2024-04-19 5.281341 -0.003686 0.182332 -0.039287 -0.560319 0.494495 0.090162 1.0 214.0 0.021796 134.500569
2024-04-22 5.304151 0.004319 0.182315 -0.035138 -0.560319 0.495770 0.090386 1.0 214.0 0.021794 134.507261
2024-04-23 5.336400 0.006080 0.182301 -0.029272 -0.560319 0.497583 0.090710 1.0 214.0 0.021791 134.503439
2024-04-24 5.352938 0.003099 0.182283 -0.026263 -0.560319 0.498484 0.090865 1.0 214.0 0.021791 134.497209
2024-04-25 5.348230 -0.000879 0.182265 -0.027120 -0.560319 0.498155 0.090796 1.0 214.0 0.021790 134.488724
2024-04-26 5.364201 0.002986 0.182247 -0.024214 -0.560319 0.499021 0.090945 1.0 214.0 0.021789 134.488160
2024-04-29 5.378484 0.002663 0.182229 -0.021616 -0.560319 0.499788 0.091076 1.0 214.0 0.021788 134.485584
2024-04-30 5.334834 -0.008116 0.182220 -0.029557 -0.560319 0.497177 0.090596 1.0 214.0 0.021786 139.240679