Stop Reimplementing Factors: Run Qlib's Alpha158 Natively in DolphinDB

DolphinDB
2026-08-05

Factor-based investing is a cornerstone of quantitative research. Modern quantitative workflows often rely on hundreds of standardized factors as features for stock selection, statistical arbitrage, and machine learning models. Since many of these technical indicators have well-defined formulas, reimplementing them across different projects is both time-consuming and error-prone.

To simplify factor research, Microsoft's open-source quantitative research platform Qlib provides Alpha158, one of its most widely used factor libraries. It contains 158 classic price-volume factors covering candlestick patterns, price movement, trading volume, rolling statistics, and other commonly used market features.

DolphinDB provides a native implementation of all Alpha158 factors through the alpha158 module, enabling users to calculate both historical and real-time factors without reimplementing any formulas.

This article introduces the module, explains data preparation, and demonstrates factor calculation in both batch and streaming scenarios using the KMID factor as an example.

Note: All examples require DolphinDB 3.00.5 or later.

Preparing the Data

All Alpha158 factors share a unified input format. Before calculating any factor, two preparation steps are required:

  • Standardize the names of market data fields.
  • Convert raw market data into the input structure expected by the factor library.

To simplify this process, DolphinDB provides the alpha158Prepare.dos helper module, which includes three categories of functions:

  • prepareData – Standardizes field names and performs data normalization.
  • alpha158Prepare – Converts market data into the matrix format required by Alpha158 and organizes it as a dictionary.
  • alpha158Cal* – Encapsulates the complete workflow by automatically preparing the data and invoking the corresponding factor function.

Load the modules and prepare the data as follows:

use alpha158
use alpha158Prepare
login('admin', '123456')
rawData = loadText("/YOUR_DIR/datatest.csv")
startTime = timestamp(2010.01.01)
endTime = timestamp(2010.01.31)
data = prepareData(rawData=rawData, startTime=startTime, endTime=endTime, securityidName="securityid", tradetimeName="tradetime", openName="open", closeName="close", highName="high", lowName="low", volName="vol", vwapName="vwap")

Calculating Factors

After the data has been prepared, any Alpha158 factor can be calculated directly.

First, preprocess the market data into the format expected by the factor library:

use alpha158
input = alpha158Prepare(data, startTime, endTime)

Then calculate the KMID factor by calling the corresponding function:

res = alpha158::kmid(input.close, input.open)

For an even simpler workflow, use the helper functions provided by alpha158Prepare. These functions combine data preparation and factor calculation into a single call:

use alpha158Prepare
res = alpha158CalKmid(data, startTime, endTime)

Using the Same Factor Code for Real-Time Streaming

The Alpha158 module supports not only historical batch computation but also real-time streaming computation.

With streamEngineParser, the same factor implementation can be reused directly in streaming applications. Users simply reference Alpha158 factor functions in the metrics definition, and DolphinDB automatically constructs the appropriate streaming engine.

For factors involving rolling windows, the system automatically selects a time-series state engine. For point-in-time factors, a standard state engine is used instead. No additional configuration or separate streaming implementation is required.

The following example computes the KMID factor in real time without modifying the factor logic itself:

// Define the input and output table schemas
inputSchema = table(1:0,["SecurityID", "TradeTime", "open", "close"],[SYMBOL, TIMESTAMP, DOUBLE, DOUBLE])
resultStream = table(10000:0, ["SecurityID", "TradeTime", "factor"], [SYMBOL, TIMESTAMP, DOUBLE])
// Load the Alpha158 module and use the kmid function in streamEngineParser
use alpha158
metrics = <[SecurityID, kmid(close, open)]>
streamEngine = streamEngineParser(
    name="alpha158KmidParser",
    metrics=metrics,
    dummyTable=inputSchema,
    outputTable=resultStream,
    keyColumn="SecurityID",
    timeColumn=`TradeTime,
    triggeringPattern='keyCount',
    triggeringInterval=4000
)
// Append data to the streaming engine and view the results in the output table
streamEngine.append!(inputData)
res = exec factor from resultStream pivot by TradeTime, SecurityID

Conclusion

DolphinDB provides a native implementation of all 158 factors from Microsoft's Qlib Alpha158 library in a unified module. Without reimplementing standard factor formulas, users can quickly perform both historical batch computation and real-time streaming computation through a consistent programming interface.

Beyond Qlib Alpha158, DolphinDB also provides native implementations of several other widely used quantitative factor libraries, including WorldQuant 101 Alpha, Guotai Junan Alpha191, and the CSAP Cross-Sectional Asset Pricing Factor Library. Together, these libraries provide a unified, high-performance foundation for factor development, model training, and quantitative strategy research.