Lagrangian Mechanics in Trading

Discover how our trading algorithm applies Lagrangian mechanics to analyze market dynamics.

Lagrangian-Based Trading Algorithm

All Juice Capital - May 2025. Advancing science through transparency and access. Contact Us on LinkedIn.

Research PDF and Python code Download

Note: Downloads are currently supported only on Google Chrome, Firefox, or other major browsers.

Features

Downloads historical stock data using yfinance — Identifies potential buy signals when kinetic energy dominates over mean reversion — Computes velocity, potential energy, kinetic energy, and Lagrangian from price movements — Exports trading signals to a text file — Visualizes price, velocity, energy metrics, and Lagrangian over time using matplotlib — Designed for conceptual and educational demonstration of physics-inspired trading strategies.

Abstract

This section explores how our trading algorithm uses the Lagrangian formulation to analyze price dynamics, providing insights into potential trading opportunities.

The Lagrangian in Physics

In physics, the Lagrangian (L) describes a system's energy. Our algorithm adapts this by combining kinetic energy (KE) and potential energy (PE):

$L = KE - PE$

What does the Lagrangian L represent in our trading algorithm?

Kinetic energy is defined as:

$KE = \frac{1}{2} \cdot m \cdot v^2$

In the kinetic energy formula, what does m represent in our trading algorithm?

Where m is a mass-like coefficient reflecting market volatility, and v is the price velocity:

$v = \frac{\text{currentPrice} - \text{previousPrice}}{\text{timeElapsed}}$

How is price velocity v calculated in the trading algorithm?

The potential energy represents the system's market positioning and can be modeled as a function of price deviations from an equilibrium.

$PE = k \cdot (price - meanPrice)^2$

What does the potential energy PE model in the trading algorithm?

Modeling Mean Reversion with Potential Energy

The potential energy models price pullbacks toward their mean:

$PE = k \cdot (price - meanPrice)^2$

Calculating the Lagrangian

// Function to calculate the Lagrangian
double CalculateLagrangian(double velocity, double price, double meanPrice)
{
    double kineticEnergy = 0.5 * m * MathPow(velocity, 2);
    double potentialEnergy = CalculatePotentialEnergy(price, meanPrice);
    return kineticEnergy - potentialEnergy;
}

In the Lagrangian calculation function, what is returned?

Calculating Price Velocity and Acceleration

// Function to calculate velocity and acceleration of the price
void CalculatePriceDynamics(double &velocity, double &acceleration)
{
    static double previousPrice = 0.0;
    static double previousVelocity = 0.0;
    
    double currentPrice = iClose(_Symbol, PERIOD_CURRENT, 0);
    velocity = (currentPrice - previousPrice) / PeriodSeconds();
    velocity *= 1e5;
    acceleration = (velocity - previousVelocity) / PeriodSeconds();
    
    previousPrice = currentPrice;
    previousVelocity = velocity;
}

What does the CalculatePriceDynamics function compute?

Buy Conditions: Optimal Entry Points

  • L exceeds a threshold, indicating positive momentum.
  • v is sufficiently positive to confirm trend direction.
// Buy condition
double lagrangianThreshold = -500.0;
if (lagrangian > lagrangianThreshold && velocity > 0.0001)
{
    // Open buy order
}

What condition must be met for a buy order in the trading algorithm?