Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

Decoding Features and Targets in Machine Learning: The Keys to Model Success

6 min read

In machine learning, features are the inputs a model learns from (for a stock: past returns, volume, RSI, P/E ratio) and the target is the output it tries to predict (tomorrow’s return, up or down, or a cluster label). Getting features and targets in machine learning right matters more than the choice of algorithm: a model built on leaky or irrelevant features will look brilliant in a backtest and fail in live markets.

Key takeaways
  • Features (X) are independent input variables; the target (y) is the dependent variable the model predicts.
  • In stock market models the target is usually a future value, so it must be shifted forward in time while features use only past data.
  • Regression targets are numbers (next-day return); classification targets are labels (up or down).
  • Select features for relevance, remove redundant ones, scale them where the algorithm needs it and use domain knowledge.
  • Split train and test data by time, never randomly, to avoid look-ahead bias.

Machine learning has become an indispensable tool for stock market analysis, enabling investors to make informed decisions and predict market trends. One of the key aspects of creating successful machine learning models for stock market applications is the careful selection of features and targets. In this blog post, we will explore the concepts of features and targets, examine their significance in stock market analysis, and provide guidelines for choosing the right features.

Understanding Features and Targets in Machine Learning

In machine learning, features represent the input data points or independent variables used to describe various aspects of the object under study. Targets, on the other hand, are the output or dependent variables that the model aims to predict or classify based on the input features.

AI Bootcamp 2.0 for traders and investors

AI FOR TRADERS

AI Bootcamp 2.0 for traders and investors

Use ChatGPT, Claude and AI agents for research, coding and automation in your trading workflow.

  • Prompting and agent workflows for markets
  • Build tools without being a full-time coder
  • 6 courses in one bootcamp
See the bootcamp →by Marketcalls
Stock Market Predictive Model Features and Target Example
Machine Learning – Features and Target

In the context of the stock market, features could include a wide range of financial data, such as historical stock prices, trading volume, financial ratios, and economic indicators. The target variable depends on the specific problem being addressed, such as predicting stock prices, forecasting future returns, or classifying stocks into different categories.

You will also see other names for the same idea. Features are called inputs, predictors, independent variables or simply X. The target is called the label, response, dependent variable or y. In a spreadsheet view, each row is one observation (say, one trading day), each feature is a column, and the target is the one column you are trying to predict.

TermAlso calledStock market example
FeatureInput, predictor, independent variable, X5-day return, RSI(14), volume ratio, P/E ratio
TargetLabel, output, dependent variable, yNext-day return, up or down tomorrow, 20-day forward volatility

Regression targets vs classification targets

The type of target decides the type of model. If the target is a number, such as the next-day percentage return, it is a regression problem (linear regression, random forest regressor, XGBoost regressor). If the target is a category, such as 1 for up and 0 for down, it is a classification problem (logistic regression, random forest classifier). Many traders find classification targets easier to work with because daily returns are noisy and a model only has to get the direction right. For worked examples of both, see linear regression for forecasting stock prices and predicting gap up or gap down with logistic regression.

Real-Life Applications

Here are four real-life examples illustrating the use of features and targets in stock market analysis:

  1. Stock Price Prediction: In this example, the features could include historical stock prices, trading volume, moving averages, and technical indicators like Relative Strength Index (RSI) or Moving Average Convergence Divergence (MACD). The target variable is the future stock price. The machine learning model learns the relationship between these features and the target to predict the stock price.
  2. Portfolio Optimization: Features may encompass various financial ratios, such as Price-to-Earnings (P/E) ratio, Price-to-Book (P/B) ratio, and dividend yield. The target variable could be the risk-adjusted return of a stock portfolio. The model learns to identify the optimal combination of stocks to maximize returns while minimizing risk.
  3. Market Sentiment Analysis: Features can include data extracted from news articles, social media posts, and financial reports, such as keywords, sentiment scores, and entity mentions. The target variable could be the stock’s future return or price movement direction (up or down). The model learns to predict stock performance based on the sentiment derived from these features.
  4. Stock Clustering: Features may consist of fundamental and technical data, such as financial ratios, historical price trends, and volatility measures. The target variable is the grouping of stocks into clusters with similar characteristics. The machine learning model learns to categorize stocks based on their features, enabling investors to diversify their portfolios or identify investment opportunities.

Note that clustering is an unsupervised task: there is no target column in the training data. The cluster label is something the algorithm produces, not something you give it. The first three examples are supervised learning, where you must supply the target.

Worked Example: Building Features and a Target in Python

Here is a small pandas example that turns a daily OHLCV DataFrame into a feature matrix X and a target y for a next-day direction model. The key line is the shift(-1) on the target: features use today’s and earlier data, while the target looks one day ahead.

import pandas as pd

# df has columns: open, high, low, close, volume (daily, sorted by date)
def build_features_and_target(df):
    out = pd.DataFrame(index=df.index)

    # ----- Features (use only data available at today's close) -----
    out["ret_1d"] = df["close"].pct_change()
    out["ret_5d"] = df["close"].pct_change(5)
    out["sma_ratio"] = df["close"] / df["close"].rolling(20).mean() - 1
    out["vol_ratio"] = df["volume"] / df["volume"].rolling(20).mean()
    out["range_pct"] = (df["high"] - df["low"]) / df["close"]

    delta = df["close"].diff()
    gain = delta.clip(lower=0).rolling(14).mean()
    loss = -delta.clip(upper=0).rolling(14).mean()
    out["rsi_14"] = 100 - 100 / (1 + gain / loss)

    # ----- Target (tomorrow's direction: 1 = up, 0 = down or flat) -----
    out["target"] = (df["close"].shift(-1) > df["close"]).astype(int)

    # Drop warm-up rows and the last row (its target is unknown)
    out = out.iloc[:-1].dropna()
    X = out.drop(columns="target")
    y = out["target"]
    return X, y

# Time-based split: train on the past, test on the most recent 20%
# X, y = build_features_and_target(df)
# split = int(len(X) * 0.8)
# X_train, X_test = X.iloc[:split], X.iloc[split:]
# y_train, y_test = y.iloc[:split], y.iloc[split:]

The same approach works if you prefer to prepare data in AmiBroker; see preparing the features dataset using AmiBroker exploration.

Guidelines for Selecting Features

Choosing the right features is crucial for building a successful machine-learning model for stock market analysis. Here are some guidelines to help you select the best features for your model:

  1. Relevance: Select features that are directly related to the target variable and have a strong influence on the outcome. Irrelevant features may add noise to the model and reduce its accuracy.
  2. Avoid Redundancy: Remove features that are highly correlated with each other, as they can lead to multicollinearity and overfitting. This helps improve the model’s performance.
  3. Feature Scaling: Scale features to have similar ranges, as it can improve the performance of some algorithms, especially those that rely on distance metrics or gradient-based optimization. See normalization vs standardization for the difference between the two common methods.
  4. Dimensionality Reduction: Use techniques like PCA, t-SNE, or feature selection methods to reduce the number of features without losing much information. This simplification reduces computation time and may improve model performance.
  5. Domain Knowledge: Leverage domain knowledge to select features that are likely to have a significant impact on the target variable. Consult financial experts or conduct thorough research to identify crucial features for stock market analysis.
  6. Feature Engineering: Create new features by combining or transforming existing ones to improve the model’s performance. For example, you can calculate technical indicators, such as moving averages or rate of change, from historical stock prices to capture price trends and momentum.

One more practical point: prefer relative features (returns, ratios, distance from a moving average) over raw prices. A raw close of ₹2,500 means nothing to a model trained when the stock was at ₹800, while a 3% move above the 20-day average means the same thing at any price level.

Common Mistakes With Features and Targets

  • Using future data in a feature. A feature built from tomorrow’s open, or a centred moving average, leaks the answer into the input. This is the most common reason a backtest looks too good. Read more on look-ahead bias and how to avoid it.
  • Random train/test split. Shuffling daily data lets the model train on days after the test days. Always split by time.
  • Fitting the scaler on the full dataset. Fit scalers and feature selectors on the training period only, then apply them to the test period.
  • Predicting raw price. A model that predicts tomorrow’s close as roughly today’s close will show a high R-squared and still be useless for trading. Predict returns or direction instead.
  • Too many features for too little data. Fifty indicators on three years of daily bars is an invitation to overfit.

Features and targets play a critical role in the success of machine learning models in stock market analysis. Understanding their importance and carefully selecting relevant features can significantly improve the accuracy and effectiveness of these models. By following the guidelines provided in this blog post and leveraging domain knowledge, you can create robust machine-learning models that can help you make informed decisions in the dynamic world of stock markets.

Remember, it is essential to continuously evaluate and refine your models to adapt to ever-changing market conditions. Combining machine learning models with your domain expertise and a disciplined investment strategy will put you on the path to long-term success in the stock market.

Frequently Asked Questions

What is the difference between features and targets in machine learning?

Features are the inputs the model sees; the target is the value it is trained to predict. In a stock model, RSI and past returns are features, and next-day return is the target.

Is the target variable the same as the label?

Yes. In supervised learning, target, label and dependent variable all refer to the output column. “Label” is used more often for classification problems.

How many features should a stock prediction model have?

There is no fixed number. Start with a small set of features you can justify, test whether each one adds out-of-sample value, and add more only when the data history is long enough to support them.

Do unsupervised models have a target?

No. Clustering and dimensionality reduction work only with features. The groups or components they output are results, not targets supplied during training.

Python Elite membership

MEMBERSHIP

Python Elite membership

15 Python, AI and algo trading programs in one plan, including QuantFlow and AI Bootcamp live mentorship.

  • Python from fundamentals to trading apps
  • Backtesting, broker APIs and OpenAlgo
  • Live QuantFlow and AI Bootcamp sessions
View Python Elite →by Marketcalls

Learn by watching: free videos every week

Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.

Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More