No history yet

Feature Engineering

Crafting Predictive Signals

Raw financial data, like a stock's daily closing price, is just a list of numbers. On its own, it doesn't tell a machine learning model much about trends, momentum, or risk. Feature engineering is the process of transforming this raw data into informative signals, or features, that a model can use to make predictions.

Think of it as giving the model different lenses to view the data. Instead of just seeing the price, it can see the recent trend, how jumpy the price has been, and even hidden cyclical patterns. Good features are the foundation of a strong predictive model.

Feature Engineering involves creating, selecting, or transforming features (input variables) to improve the performance of machine learning models.

Looking Back with Lag Features

One of the simplest yet most powerful ways to create features is to use past data. A lag feature is a variable that contains data from a prior time step. For predicting today's price, the price from yesterday, two days ago, or even last week can be a very strong signal.

This technique directly encodes the concept of momentum into the data. If a stock was rising yesterday, it might continue to rise today. By feeding these past values to the model, we allow it to learn these kinds of short-term dependencies.

import pandas as pd

# Assuming 'df' is a DataFrame with a 'close' price column
# Create a lag feature for the previous day's close price
df['lag_1'] = df['close'].shift(1)

# Create a lag feature for the close price 5 days ago
df['lag_5'] = df['close'].shift(5)

print(df[['close', 'lag_1', 'lag_5']].head(7))

Lag features help the model answer the question: "Given where the price was yesterday, where is it likely to go today?"

Summarizing Trends and Volatility

While individual past prices are useful, summaries of recent behavior can be even better. Statistical features distill information from a window of time into a single number, capturing concepts like trend and risk.

A moving average is a classic example. It smooths out short-term fluctuations to reveal the underlying trend. A 7-day moving average gives a sense of the weekly trend, while a 30-day average shows the monthly trend. The difference between a short-term and long-term moving average can also be a powerful feature, signaling shifts in momentum.

MAt=1ni=0n1PtiMA_t = \frac{1}{n} \sum_{i=0}^{n-1} P_{t-i}

Another crucial concept is volatility, which measures how much the price swings. A stock with high volatility is considered riskier. We can capture this by calculating the standard deviation of the price or its returns over a rolling window. A model might learn that periods of high volatility are often followed by large price movements.

# Calculate a 7-day moving average of the close price
df['ma_7'] = df['close'].rolling(window=7).mean()

# Calculate 14-day volatility (standard deviation of daily returns)
df['returns'] = df['close'].pct_change()
df['volatility_14'] = df['returns'].rolling(window=14).std()

print(df[['close', 'ma_7', 'volatility_14']].tail())

Uncovering Hidden Cycles

Financial markets sometimes exhibit cyclical patterns that aren't obvious at first glance. These could be daily, weekly, or even yearly cycles. The Fourier Transform is a mathematical tool that helps us uncover these patterns by converting a time series from the time domain into the frequency domain.

Instead of looking at price over time, we look at the strength of different frequencies. It's like breaking down a complex musical chord into its individual notes. For a stock, this might reveal a subtle pattern where the price tends to dip mid-week and rise towards the end of the week. We can then create features based on the most dominant frequencies.

While the math can be complex, the takeaway is simple: Fourier analysis gives us another angle to find predictive patterns that are invisible in the standard price chart.

Using Time Itself as a Feature

Finally, the calendar itself can provide valuable information. Market behavior often follows patterns related to the time of day, week, month, or year. This is known as seasonality.

For example, trading volume might be consistently lower on Mondays. Some sectors might perform better in certain months. These are all patterns a model can learn if we provide the right features.

FeatureDescriptionExample Value
Day of WeekThe day of the week (Mon-Fri)3 (Wednesday)
MonthThe month of the year10 (October)
QuarterThe fiscal quarter4
Day of YearThe day number within the year295

Extracting these features is straightforward. If your data has a timestamp, you can easily derive the day of the week, the month, and more. These simple, time-based features can capture market psychology and institutional behavior tied to the calendar, adding another layer of predictive power to your model.

# Assuming the DataFrame index is a DatetimeIndex
df['day_of_week'] = df.index.dayofweek
df['month'] = df.index.month

print(df[['close', 'day_of_week', 'month']].tail())
Quiz Questions 1/5

What is the primary goal of feature engineering when working with raw financial data?

Quiz Questions 2/5

A feature that uses the value of a stock's price from three days ago to help predict today's price is an example of a...

By combining these different types of features, a machine learning model gets a much richer, multi-faceted view of the market, increasing its chances of making accurate short-term predictions.