Skip to content
All library documents

Optimizing Historical Forex Trades with Path-Dependent Equity

Article Quant Q&A · Author: snikoFX

Summary

The question seeks a hindsight trading sequence from OHLC forex bars, subject to leverage, spread, and a lookahead window intended to reduce exposure near whipsaws. The proposed method is to formulate the sequence of positions across bars as a candidate solution, then define an objective function that maps that sequence and the market data to final profit or another chosen performance measure. Position changes can then be translated into the requested trade actions and holding periods.

The response recommends iterative optimization, particularly local search, rather than prescribing a closed-form greedy rule. It emphasizes that leverage tied to prior strategy gains makes the equity path dependent, so each candidate change can require updating the resulting equity curve. This is a general optimization outline, not a specified algorithm or evidence of performance. The question itself prioritizes historical hindsight returns and gives no concrete sample bars or fully defined objective, so any resulting sequence would depend heavily on modeling choices and would not establish future trading value.

Key ideas

  • Represent the strategy as a position at each bar, with trades corresponding to position changes.
  • Define an objective by mapping a candidate position sequence and the bar data to profit or a selected risk adjusted measure.
  • Use an iterative search such as local search to improve candidate trade sequences.
  • Path dependent leverage means that changes to earlier positions can alter the later equity curve.
  • The method is an optimization framework, and its outcome depends on the chosen objective and constraints.

Tags

Full text
# Maximize account equity over a historic time series


# Maximize account equity over a historic time series












Inputs: array of OHLC forex bars of size N, max leverage L, e.g. 200:1, a fixed bid ask spread S, a fixed lookahead whipsaw window W (e.g. 3 bars long, see below).

Desired output: a list of tuples {array index, action = Buy Mkt|Sell Mkt|DoNothing, Leverage, hold count}

With the benefit of hindsight construct a greedy algorithm (which cares very little about risk) that aims to extract the highest possible P&L from the given data.

The bid ask spread ensures that immediately after opening or closing a position a small loss is incurred. The fixed lookahead whipsaw window ensures that algo doesn't always choose max leverage. For instance, if W=3 this means that if algo had held for just 3 more bars then whipsaw action could have caused serious erosion of P&L. Thus, due to W an adjusted leverage must be used that hurts the floating account balance ("equity" in forex lingo) less.

Example of desired output:

[bar 0, Buy, 180:1, 18] # position is closed after 18 bars, little danger from whipsaw [bar 19, DoNothing, Nil, 12] # no action taken for 12 bars possibly due to sideways move and spread [bar 32, Sell, 13:1, 11] # position is closed after 11 bars, high danger of whipsaw ahead ... etc ...

Please provide guidance how to solve this with a python numeric tower or R. I have not done much to solve this because I don't know how to go about it. Note: the size of the input array can be quite large, so if a global max is difficult to compute in polynomial time then a "good enough" local maximum is ok.

Update: the only clarification worth adding is the effect of W on leverage. Some examples - suppose that any bar in W took the current trade's P&L into negative territory, in this case DoNothing should be output; suppose any bar in W took the current trade's P&L into slightly positive territory, say +1%, in this case the question is how much should we reduce leverage - I don't know but a linear function may be appropriate, e.g. 1% of the max 200:1, i.e. 2:1 for the bars preceding W. Needless to say, every trade starts with max leverage and gets adjusted afterward when W is reached.

## Answer by Enrico Schumann (score 1, accepted)

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

It would help if you provided a small numeric example of what you want to achieve, i.e. some sample bars plus a good solution.

But in any case, IIUC, I would approach the problem directly as an optimization model: You have $N$ bars. Assuming you always trade at the close, then a candidate solution would be vector of length $N$ that holds the position along the bars. (The trades are the changes in this vector.)

Now write a function that maps such a vector into a final profit or loss, given your bars. (The function might as well map into a measure of risk-adjusted return.) This is your objective function. Now use an optimization algorithm to "evolve" some initial solution vector into a good solution.

Finally, transform your solution vector into a collection of tuples.

Update, in response to the comment: The advantage of the approach I've outlined is that is general and flexible: general, since you have the complete equity time-series and you can evaluate any objective function (e.g. equity dradown); flexible, because you can add more restrictions and refinements later.

The disadvantage is that with an iterative optimization algorithm, the equity curve has to be recomputed in every iteration because the curve is path-dependent (leverage depends on past success of the strategy).

What time-constraints do you have when computing the optimal trade sequence? And how much time you are willing to spend on implementing it and making it faster? (See the canonical reference https://xkcd.com/1205/ )

Personally, I would try a local-search based algorithm to implement the outlined approach. Such an algorithm would evolve the solution by incrementally changing it, which should provide many opportunities to update the equity curve during the optimization. That is, you don't recompute the equity curve from scratch in every iteration, but only update it for the latest change in the candidate solution.

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.