How Machine Learning Can Be Used to Find Trading Signals: A Practical Guide



How Machine Learning Can Be Used to Find Trading Signals: A Practical Guide

A technical introduction to feature engineering, time-series validation, XGBoost, SHAP, and risk-aware machine learning research.

Authored by Thanuja Jeewanthi

Computer Engineering graduate with an interest in machine learning, software systems, and quantitative modeling.

Important: This article is for educational and technical research purposes. It explains how machine learning can be applied to financial datasets and does not provide personalized investment, financial, or trading advice. Machine learning models can produce misleading results because of data leakage, overfitting, changing market conditions, transaction costs, and other limitations. Historical or simulated results should not be treated as evidence of future returns.

Machine learning has become a popular research tool for studying financial markets. Instead of relying on a single indicator, a model can combine information from several variables—such as recent returns, volatility, trading volume, momentum, and other market features—to look for relationships that may be difficult to capture with a simple rule.

That does not mean machine learning can reliably predict what the market will do next. Financial data is noisy, market relationships can change, and a model that looks impressive on historical data can perform very differently when exposed to new observations.

The more useful way to think about machine learning in quantitative research is as an experimental framework. The goal is to construct features carefully, test hypotheses without leaking future information, measure performance on genuinely unseen data, and investigate whether a model’s apparent signal survives reasonable changes to the research setup.


1. What a Machine Learning Trading Signal Actually Means

A trading signal is simply a rule, score, or model output that provides information a researcher may use when evaluating a potential trade. A signal might estimate the probability that an asset’s return will be positive over a particular future period, classify market conditions, or rank several assets according to a chosen characteristic.

Machine learning changes how these signals are constructed. Rather than manually deciding that one indicator should trigger a trade, a supervised learning model can be trained on historical examples containing both features and an outcome of interest.

A Simple Signal Research Pipeline

A practical research workflow can be organized into four broad stages:

  • Feature construction:
    Transform raw market observations into variables that can be tested by the model.
  • Target definition:
    Clearly define what the model is attempting to predict and the time horizon involved.
  • Time-aware validation:
    Evaluate the model using historical-to-future splits rather than randomly mixing observations.
  • Interpretation and robustness checks:
    Investigate which variables influence predictions and whether the apparent relationship remains stable across different periods.


2. Feature Engineering for Financial Time Series

The quality of the input data often matters more than the complexity of the model. Financial prices are typically non-stationary, meaning that their statistical properties can change over time. Feeding raw price levels directly into a model may therefore create relationships that are difficult to interpret or that do not remain stable.

Researchers commonly experiment with returns, rolling volatility, momentum measures, volume-related variables, moving-average relationships, and other transformations. The appropriate feature set depends heavily on the market, timeframe, prediction target, and research question.

One more advanced technique is fractional differentiation. Instead of taking a full first difference, fractional differentiation applies a fractional order $d$ and can sometimes retain more information from the original series while reducing certain forms of non-stationarity.

$$
(1-B)^d =
\sum_{k=0}^{\infty}
(-1)^k
\binom{d}{k}
B^k
$$

Here, $B$ is the backshift operator and $d$ is the fractional differentiation order. In practice, the value of $d$ is a research parameter rather than a universal setting.

Importantly, fractional differentiation is not something every financial machine learning project needs. A simpler return transformation may be sufficient for one dataset, while another project may benefit from investigating alternative transformations. Statistical tests can provide useful evidence, but they do not prove that a feature will remain predictive in the future.


3. Avoiding Look-Ahead Bias With Time-Aware Validation

One of the most important issues in financial machine learning is look-ahead bias. It occurs when information that would not have been available at the time of a historical prediction accidentally enters the training process.

For example, suppose a model uses today’s information to predict the return over the following five trading days. If the dataset is randomly shuffled before cross-validation, observations from that future period may end up influencing the training process. The resulting score can look much better than what would have been possible in a real historical decision process.

For ordinary time-ordered datasets, scikit-learn’s TimeSeriesSplit provides a useful starting point because training observations occur earlier than the corresponding test observations. It also supports a gap parameter that can leave observations between the training and test sets. :contentReference[oaicite:3]{index=3}

Time-Series Validation Checklist

  • Keep observations in chronological order.
  • Make sure every feature uses only information available at prediction time.
  • Fit preprocessing steps such as scaling or feature selection using training data only.
  • Consider a gap between training and validation periods when the prediction horizon creates overlapping observations.
  • Keep a final untouched test period for the last evaluation.

More specialized research designs may require purging or embargoing observations, particularly when labels overlap or multiple assets share information. Those methods should be implemented deliberately rather than simply calling an ordinary time-series splitter “purged cross-validation.”


4. Using XGBoost and SHAP to Explore Nonlinear Relationships

Tree-based ensemble models are useful candidates for financial research because they can capture nonlinear relationships and interactions between variables. XGBoost, for example, provides a gradient-boosted tree implementation with a scikit-learn-compatible classifier interface. :contentReference[oaicite:4]{index=4}

A model might discover that the relationship between momentum and future returns changes depending on volatility, volume, or another market condition. Whether such a relationship is genuine is a separate question. A flexible model can find patterns in historical noise just as easily as it can find useful structure.

This is where model interpretation can help. SHAP provides feature-attribution methods that can be used to examine how individual variables contribute to model predictions. SHAP’s API includes tools such as TreeExplainer for tree-based models. :contentReference[oaicite:5]{index=5}

Rather than automatically deleting every feature with a small SHAP value, it is better to examine feature importance across multiple validation periods and compare the results with simpler benchmark models. A feature that appears important in one short period but disappears in other periods deserves additional scrutiny.

$$
\phi_i(x) =
\sum_{S \subseteq F \setminus \{i\}}
\frac{|S|!(|F|-|S|-1)!}{|F|!}
\left[
f_x(S \cup \{i\}) – f_x(S)
\right]
$$

In simplified terms, a SHAP value describes a feature’s contribution to a particular model prediction relative to a baseline.


5. A Practical Python Research Workflow

The following example demonstrates a basic time-aware classification workflow using XGBoost and scikit-learn. It is intentionally presented as a research example rather than a ready-to-use trading system.

One important distinction is that TimeSeriesSplit is not the same thing as a specialized purged cross-validation implementation. The example below therefore uses time-ordered validation and leaves more advanced leakage controls as a separate research consideration.

import numpy as np
import pandas as pd
import xgboost as xgb

from sklearn.model_selection import TimeSeriesSplit
from sklearn.metrics import accuracy_score, precision_score


class TradingSignalResearch:

    def __init__(self, X: pd.DataFrame, y: pd.Series):

        self.X = X
        self.y = y

        self.model = xgb.XGBClassifier(
            n_estimators=100,
            max_depth=3,
            learning_rate=0.05,
            subsample=0.8,
            colsample_bytree=0.8,
            random_state=42,
            eval_metric="logloss"
        )

    def evaluate(self, n_splits=5, gap=0):

        splitter = TimeSeriesSplit(
            n_splits=n_splits,
            gap=gap
        )

        results = []

        for fold, (train_idx, test_idx) in enumerate(
            splitter.split(self.X)
        ):

            X_train = self.X.iloc[train_idx]
            X_test = self.X.iloc[test_idx]

            y_train = self.y.iloc[train_idx]
            y_test = self.y.iloc[test_idx]

            self.model.fit(X_train, y_train)

            predictions = self.model.predict(X_test)

            accuracy = accuracy_score(
                y_test,
                predictions
            )

            precision = precision_score(
                y_test,
                predictions,
                zero_division=0
            )

            results.append({
                "fold": fold + 1,
                "accuracy": accuracy,
                "precision": precision
            })

        return pd.DataFrame(results)


# Example:
#
# X should contain features constructed
# using information available at prediction time.
#
# y should represent the predefined target.
#
# researcher = TradingSignalResearch(X, y)
# results = researcher.evaluate(
#     n_splits=5,
#     gap=5
# )
#
# print(results)

Even this apparently simple workflow requires care. If features are normalized, selected, or engineered using statistics calculated across the entire dataset before the split, information can still leak from the future into the training process. A robust pipeline should therefore perform those operations inside each training fold whenever appropriate.


Sage & Budget Financial Tool

Compare the Cost of Renting vs. Buying

Machine learning research is only one part of financial decision-making. For everyday money decisions, a clear comparison of costs can be just as useful. Try our free rent vs. buy calculator to explore different assumptions for your situation.


6. Why Backtests Can Be Misleading

A high backtest score can be exciting, but it is not the same as discovering a reliable trading strategy. Financial datasets contain many opportunities to find accidental relationships, especially when researchers test a large number of features, models, time periods, and parameter combinations.

Several problems deserve attention before interpreting a result as meaningful:

  • Look-ahead bias:
    Future information enters the features, labels, or preprocessing pipeline.
  • Overfitting:
    The model learns patterns specific to the historical sample rather than relationships likely to persist.
  • Multiple testing:
    Testing enough hypotheses can produce apparently impressive results by chance.
  • Transaction costs:
    A theoretical signal may disappear after commissions, spreads, market impact, and other trading costs.
  • Regime changes:
    Relationships that existed during one market environment may weaken or disappear under different conditions.
  • Implementation constraints:
    A model that works on end-of-day data may not behave the same way when actual execution timing and liquidity are considered.

For these reasons, a useful research process should include simple baselines, multiple historical periods, an untouched test set, sensitivity analysis, and realistic assumptions about costs and execution. A model that remains reasonably consistent after these checks is more interesting than one that produces a spectacular result under a single set of assumptions.


7. Frequently Asked Questions

Can machine learning accurately predict stock prices?

Machine learning can identify statistical relationships in historical financial data, but that does not mean it can reliably predict future prices. Markets are influenced by many changing factors, and apparent historical patterns may not persist.

Is XGBoost useful for financial machine learning?

XGBoost can be useful for experimentation with structured financial datasets because boosted decision trees can model nonlinear relationships and interactions. Its usefulness depends on the quality of the features, target definition, validation methodology, and research design.

What is SHAP used for?

SHAP provides feature-attribution methods that help researchers examine how individual features contribute to model predictions. It can make a complex model easier to investigate, but SHAP results should be interpreted alongside out-of-sample performance and other robustness checks.

Why is time-series cross-validation important?

Financial observations have a natural time order. A validation method that randomly mixes past and future observations can produce an unrealistic estimate of performance. Time-aware methods such as scikit-learn’s TimeSeriesSplit help preserve chronological ordering during evaluation. :contentReference[oaicite:6]{index=6}

Should every trading model use fractional differentiation?

No. Fractional differentiation is one possible feature transformation. Whether it is useful depends on the dataset and research objective. Simpler transformations, such as returns or changes in volatility, may be more appropriate for some projects.

The Bigger Picture

The most valuable part of machine learning in quantitative finance is not finding a model that appears to predict the market perfectly. It is building a research process that makes it difficult to fool yourself. Careful feature construction, chronological validation, transparent model interpretation, realistic cost assumptions, and independent testing can help separate an interesting historical pattern from a result that deserves further investigation.

Financial & Technical Disclaimer:
This article is provided for educational and informational purposes only. It does not constitute investment, financial, tax, legal, or trading advice. The examples and code are simplified for research and learning and should not be treated as a recommendation to buy, sell, or trade any security. Historical backtests and simulated results can differ substantially from live results. Before making financial decisions, consider your own circumstances and, where appropriate, seek advice from a qualified professional.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top