Skip to main content

Large Execution Models

Project description

LEMS: Large Execution Models for VWAP/TWAP Optimization

This repository provides the full implementation of Large Execution Models (LEMS), a deep learning framework for optimal VWAP/TWAP execution across multiple assets and market conditions. LEMS leverages modern Keras3 tooling and is compatible with TensorFlow, JAX, and PyTorch backends. It integrates time-aware architectures, multi-objective allocation, and efficient fused layers to offer scalable and expressive modeling for high-performance trading execution.


📦 Installation

Install via pip:

pip install .

Or using Poetry:

poetry install

Requirements:

  • Python 3.9–3.12
  • keras>=3.0.0
  • tkan

Optional (for training and testing):

  • jax, torch, or tensorflow
  • numpy, pandas, matplotlib (for data and visualization)

🚀 Features

  • Backend-agnostic with full support for JAX, TensorFlow, and PyTorch.
  • Flexible target modeling: simultaneous optimization of VWAP/TWAP for both volume and notional allocation.
  • FusedMLP layer for highly parallelized MLP computation across model variants (volume/notional, VWAP/TWAP, buy/sell).
  • Custom constraints and initializers for learnable allocation curves with sum-to-one and positivity constraints.
  • Soft clipping mechanisms to enforce trading limits while maintaining differentiability.
  • Multiple execution-aware loss/metrics to track and optimize performance across multiple trading strategies.
  • Full training pipeline with evaluation-ready data preparation tools.

🧠 Model Components

The main model, LargeExecutionModel, consists of:

  • EmbeddingLayer: learns per-feature representations.
  • VariableSelectionNetwork: soft attention over input variables.
  • TKAN RNN layers: efficient temporal modeling with attention kernels.
  • MultiHeadAttention: causal self-attention for cross-step dependency modeling.
  • FusedMLP: efficiently computes multiple allocation predictions in parallel (buy/sell × VWAP/TWAP × volume/notional).
  • Custom constraints: such as PositiveSumToOneConstraint and EqualInitializer for normalized allocation curves.

🧪 Example Usage

from lems.models import LargeExecutionModel
from lems.loss import lem_loss
from lems.metrics import (
    buy_vwap_avg_imp, buy_twap_avg_imp,
    sell_vwap_avg_imp, sell_twap_avg_imp,
    buy_vwap_avg_risk, buy_twap_avg_risk,
    sell_vwap_avg_risk, sell_twap_avg_risk,
    unsigned_vwap_avg_risk, unsigned_twap_avg_risk
)

model = LargeExecutionModel(
    lookback=120,
    n_ahead=12,
    hidden_size=200,
    num_embedding=20,
    num_heads=8,
    fused_mlp_hidden_dim=100
)

model.compile(
    optimizer="adam",
    loss=lem_loss,
    metrics=[
        buy_vwap_avg_imp, buy_twap_avg_imp,
        sell_vwap_avg_imp, sell_twap_avg_imp,
        buy_vwap_avg_risk, buy_twap_avg_risk,
        sell_vwap_avg_risk, sell_twap_avg_risk,
        unsigned_vwap_avg_risk, unsigned_twap_avg_risk
    ]
)

history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=30, batch_size=64)

📊 Data Preparation

A full pipeline is included via data_formatter.py. Example:

from lems.data_formater import full_generate, add_config_to_X, add_config_to_y

X_train, X_test, y_train, y_test, dates = full_generate(
    volumes_df, notionals_df,
    target_asset='AAPL',
    lookback=120,
    n_ahead=12,
    split_date='2022-01-01'
)

X_train = add_config_to_X(X_train, minimum_traded_per_period=0.0, maximum_traded_per_period=1.0)
y_train = add_config_to_y(y_train)

✅ Testing & Backend Compatibility

Tests are defined using pytest and validate:

  • Consistency before and after saving the model
  • Compatibility across JAX, TensorFlow, and Torch backends
  • GPU compatibility on NVIDIA (currently not compatible with AMD GPUs due to fused ops)

📚 Example Results

Two notebooks in example_and_results/ show results on:

  • Dow Jones equities (e.g. AAPL, MSFT)
  • Cryptocurrencies (e.g. BTC, ETH)

They demonstrate significant performance improvements over baseline TWAP/VWAP curves using LEMS.


📖 Key Files

  • models.py: Full model definition
  • metrics.py: Custom metrics (VWAP/TWAP performance, risk-adjusted returns)
  • loss.py: LEM loss function for efficient and risk-sensitive execution
  • data_formatter.py: Preprocessing pipeline from raw time series to training arrays
  • example_and_results/: Notebooks with full training code and benchmarks

📈 Metrics

Each allocation path (Buy/Sell × VWAP/TWAP) is evaluated using custom Keras metrics:

  • *_avg_imp: average improvement in execution price vs market benchmark
  • *_avg_risk: risk-adjusted reward relative to baseline
  • unsigned_*_avg_risk: neutral performance regardless of trade direction

These are designed for both interpretability and reward shaping.


📝 License

Shield: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

CC BY-NC-SA 4.0

---

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lems-0.1.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lems-0.1.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file lems-0.1.1.tar.gz.

File metadata

  • Download URL: lems-0.1.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.2 Linux/6.8.0-79-generic

File hashes

Hashes for lems-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7f6e90d49fb2a1a76bcb22c537d8b92839460aa18f6ccc1b3647756930001bf9
MD5 e6fd700d7f5168a5c5db6f73e675211b
BLAKE2b-256 5fd9c794566d9d9bd4385dfc648d2e8b8da56e666c3e760e5e2caa2a3d3c1c9e

See more details on using hashes here.

File details

Details for the file lems-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: lems-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.2 Linux/6.8.0-79-generic

File hashes

Hashes for lems-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b7868c31e532d8e18fb164dc75c72595281ade5dcd6098d556e510e8c1d0a663
MD5 350180c353b58af5defa4c09905b7a03
BLAKE2b-256 e29ffc6c09bebe1540c94caab8d7e31a79cee79c9c926855cc462e44710cab4c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page