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.