1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ I wasn't familiar with the word whipsaw, so I looked it up "A whipsaw is a trade that moves sharply in one direction but then reverses in the opposite direction shortly after. " $\endgroup$ – noob2 Sep 13 '20 at 14:28
1
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Not seeing it. I counted 140K 1 minute bars in my EURUSD csv. How long would it take to apply the above to this dataset? What is the time complexity of your proposed optimization? I added a slight clarification to the original question. $\endgroup$ – snikoFX Sep 14 '20 at 14:54
  • $\begingroup$ It is always difficult to give accurate estimates of the running time of an implementation, as it depends on the hardware, libraries that are used etc. Given a large data-set, my educated guess would be that a quick and straightforward implementation in a high-level language such as R might take a few minutes to run (on typical hardware). $\endgroup$ – Enrico Schumann Sep 15 '20 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.