Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Feature

Overview

One of the compelling arguments to use Python for algo-trading, is the relative ease of using AI and machine learning as part of your solution. That being said, if you are new to Python development, it might still seem a bit daunting at first.

Feature classes in roboquant transform data into structured input features and target labels for AI/ML models. They serve as the bridge between the time-series domain of financial data and the tabular world of machine learning algorithms and help to lower the learning curve.

API

The following code snippet shows the essence of the Feature abstract base class:

class Feature(ABC, Generic[T]):
   
    @abstractmethod
    def calc(self, value: T) -> NDArray[np.float32]:
        ...

    @abstractmethod
    def size(self) -> int:
        ...

    def reset(self):
        pass

As can be seen in the above snippet, a feature calculation always returns a Numpy array of the type float32. In fact, it is always a 1-dimensional float32 array and missing values are represented as float “NaN” values in that array. Every invocation should always return the same length array.

If a feature has state, it should override the reset method and clear that state. So that during the training of the next epoch it starts fresh and doesn’t carry over state from the previous epoch.

Feature Types

In roboquant there are three types of feature implementations included:

  1. Features that calculate based on an event

  2. Features that calculate based on an account

  3. Generic Features that wrap other features, for example fill missing values or don’t rely on input data at all.

Event-Based Features

FeatureDescription
DayOfWeekFeatureDay of week when the event took place
DayOfMonthFeatureDay of month when the event took place
MonthOfYearFeatureMonth of year when the event took place
TimeDifferenceTime difference between two events
IndicatorFeatureBase class for custom indicator features
TrueRangeFeatureCalculate the True Range
PriceFeatureExtract the prices for one or more assets
BarFeatureExtract the bar prices for one or more assets
QuoteFeatureExtract the quotes for one or more assets
CacheFeatureCache other event feature
VolumeFeatureExtract the trading volume for one or more assets

Besides writing a full custom feature, you can also implement the Indicator feature.

Account-Based Features

FeatureDescription
EquityFeatureTotal Equity
UnrealizedPNLFeatureUnrealized Profit & Loss

Generic Features

FeatureDescription
SlicedFeatureSlice another feature
FixedValueFeatureFeature of fixed values
RandomFeatureFeature that generates random values
FeatureSetCombine other features into a new feature
NormalizeFeatureNormalize data over a certain period
FillFeatureFill in missing (“nan”) values with the last known value
FillWithConstantFeatureFill missing values with a constant float value
ReturnFeatureCalculate the next step return
LongReturnsFeatureCalculate the return over a longer period
MaxReturnFeatureCalculate the maximum return over certain period
MinReturnFeatureCalculate the minimal return over certain period
SMAFeatureCalculate the Simple Moving Average over the result of another Feature

Custom Features

For bespoke logic, subclass Feature and implement the calc() and size() method. The following example shows how to develop a feature that calculates the spreads between two different assets.