Skip to content
All library documents

Using the Chain Rule to Optimize Sequential Trading Functions

Article Quant Q&A · Author: angryserver

Summary

The question describes a two-stage exchange in which the output of one constant-product market becomes the input to a second market. It asks how to choose the initial trade amount to maximize the final output while keeping the intermediate currency amount consistent across both trades. This setup resembles optimization across linked currency exchanges, though the market equations in the question are not fully developed into an objective function.

The response gives the general calculus method: express the final result as a composition of functions, differentiate with the chain rule, and find candidate extrema by setting the derivative to zero. This is a starting point rather than a complete solution to the exchange example. A practical optimization would still need explicit swap functions, feasible input ranges, and checks for boundaries, costs, and whether each stationary point is a maximum rather than a minimum or other critical point.

Key ideas

  • When one function feeds another, the final output can be represented as a composition of functions.
  • The chain rule expresses the derivative of the final output in terms of derivatives at each stage.
  • Setting the derivative to zero identifies candidate critical points, not necessarily a maximum.
  • A trading application also needs explicit constraints, boundary checks, and transaction costs.

Tags

Full text
# How to optimize a series of equations whose outputs are a variable of the subsequent equatinos


# How to optimize a series of equations whose outputs are a variable of the subsequent equatinos












The basic question is, given $f(x) = y$ and $f(y) = z$, how can you find $x$ such that $z$ is at its maximum?

I can optimize each equation independently, but I do not know how to optimize when combining equations. A concrete example is as follows:

Imagine forex market that is made up of $x$ and $y$, where $x$ and $y$ are both currencies. Users can send in $x$ to receive $y$, and vice versa. The market structure is defined by \begin{equation} x * y = k \end{equation} where $k$ is a constant number, say $1$, and the product of $x$ and $y$ must always be equal to this number.

The price of $x$ or $y$ is simply $x / y$, such that $k$ always stays the same. If someone sends $x'$ of the currency as payment and receives $y'$ in return, the new equation for the market must be true.

\begin{equation} \dfrac{(x + x')}{(y - y')} = k \end{equation}

Given all this information, imagine you were to make a trade on two markets of this structure. How would you optimize your input, $x0'$, such that your output $x_1'$, is maximized, and $y_0'$ is equivalent on both trades?

\begin{equation} \dfrac{(x_0 + x_0')}{(y_0 - y_0')} = k_0 \;\;\; and \;\;\; \dfrac{(y_1 + y_0')}{(x_1 - x_1')} = k_1 \end{equation}

## Answer by Attack68 (score 1, accepted)

https://quant.stackexchange.com/a/44719

To optimize:

$$z = f(g(x))$$

using traditional calculus with chain rule:

$$ \frac{dz}{dx} = \frac{df}{dg} \frac{dg}{dx} $$

Set $\frac{dz}{dx} = 0$ and that will determine either minimum, maximum or saddle points.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.