Preventing unreasonable losses in an EA

 
I don't know if I'm the only one who came to think that making a successful autonomous expert advisor is like playing a tedious chess game against the market.
Some people may say that it is impossible longterm, however it will also depend on how positions are managed. The way I see it, a manual trader has the upper advantage of controlling positions based on the feeling of price action and incoming news releases. 
But there are also techniques that can be executed to control positions with floating PnL in a robotic way.

Those who have some experience coding EAs know how tedious it can be to tackle that one big nasty loss, because the EA will gracefully make that loss without emotion. And if you try to micromanage such positions in advance, the result is that all trades will be affected, and that's not really what you want. If every trade would be affected, it defeats the purpose of the trading system.

I want to ask if you have any tips to prevent the EA from keeping a destructive position open. Let's say you don't have time to log in to the VPS and check it manually. 

I tried these approaches already:

1. Re-assessing the PnL after X number of bars since position open time.

Result:  Rarely an effective approach. May work for some scalping systems, but can cause you to take many losses when said losses could have been wins.

2. Implementing an equity drawdown watcher, and closing a position on a percentual threshold of equity drawdown.

Result: It can work well sometimes, but other times too many positions can be closed, and the same positions could have been wins if held for longer (similar caveat as in Point 1)

3. Setting position stop loss to breakeven after some points of profit have been reached

Result: Sometimes this is effective, but it can really lower the overall net profit if many positions close at breakeven (and many will due to inevitable volatility).

4. Using a trailing TP/trailing SL

Result: This is a reasonable approach if it is tweeked right for the strategy, but this will have no effect on positions that start in loss and stay in loss.


My next approach is to check the directional momentum after a position is placed, and close it soon if there's opposite price momentum. But I'm tired of ideas that end up being fails, and I wonder if you have any interesting approach.




 

It is a swing based strategy that I'm trying to make autonomous with automation, but clearly it seems that reality does not bend to strategies...strategies must bend to reality.

Sometimes a strategy seems good enough, but it isn't good enough. There are many things that look good enough to the naked eye, but the market is war and randomness, not a utopia. So I'm thinking what you will say is "change the strategy" which...indeed might be the only real solution sometimes. I'm just seeing first of all how far "trade surgery" can come when it comes to profitability.

 

As far as I can tell from the description, your issues are not directly related to coding of itself, but about the skill and knowledge one gains by actual trading and understanding the markets, as well as statistical analyses.

For one, in quantitative or algorithmic trading, when devising or applying a strategy, one should fully understand the statistical probabilities of the strategy and how its rules affect metrics. It should not be about fumbling about trying to fix deviations in the trades.

Here are some tips:

  • Analyse and know the MAE and MFE properties of your basic strategy rules for the symbol and time-frame in question. If the e-ratio is not sufficiently good enough, then no matter how you tinker with trade management, it's not going to improve much. Analyse these properties on the most basic of the strategy's entry and exit rules without any management extras that will hide the true underlying metrics.

    For example, if analysing a stop & reverse moving average crossover strategy, then analyse that basic rules, without stops or targets. You should fully analyse if the basic principle of the strategy has merit or not, before trying to fine tune its management.

    Also analyse how the MAE and MFE evolves over the life-time of a position, to see if a time based stop, instead of a price based stop, may be beneficial, or if both types should be used.

MAE : Maximum Adverse Excursion
MFE : Maximum Favourable Excursion
e-ratio : Edge Ratio (normalised MFE divided by the normalised MAE)

  • Analyse and know what volatility the symbol exhibits at the time-frame you expect to use. Consider both the average of the true-range (ATR) as well as the variance of that average. The average alone is not enough information. You should also consider the average and variance of ranges between swinging points too. One should never place a stop-loss, or apply a trailing-stop, or move to break-even simply based on an arbitrary fixed number of points. It should instead take into consideration the true volatility exhibited by the symbol at that time-frame, and this should adapt to changes as the conditions evolve over time.
  • Adjust your position size based on a small percentage of your capital and the maximum drawdown you are willing to accept. For example, if you normally risk 1% per trade, and only accept a maximum of 10% drawdown, then your true risk per trade should be 1% of (10% of your balance), and not merely just 1% of the entire balance. In this way, during a drawdown, as the balance drops, so does the risk, to the point that it becomes 0% at the maximum drawdown allowed. Keep track of your maximum balance, so that you can calculate your current drawdown and adjust the risk accordingly with this method.

    There are other methods of money management that take into account the drawdown. The one above is just one that I often use, but there are others. Do some research on various money management methods to find one that best fits your needs and trading style. Here is a starting point ... Money Management Models

    And remember that the risk position size is dependant on both the initial stop-loss and the margin requirements, not one or the other alone (edited as per post #5).
  • Also, there is no such thing as a trailing take-profit (that is just a gimmick). If all the points described above are sound, then only a trailing stop-loss can be of any use, but only if the metrics demonstrate that it's effective. Conditions that do not demonstrate consistent trending, will not benefit from trailing stops, and can even be detrimental. If you have ever studied "Random Walk" theory, you will have noticed that a trailing stop is detrimental for unbiased random walks. It is only useful during strongly biased random walks.

    When applying a trailing-stop or a move to break-even, always base them on both the volatility conditions, as well as the MAE/MFE analysis. Don't just apply them willy-nilly.

    PS! Sometimes partial take-profits at various levels can be more effective than a trailing stop. Sometimes a combination of both can also do well. So study the metrics to see which methods to use.
In summary, these were just some initial tips that came to mind while reading your post. In essence, it is not only your coding skills that matter. You also have to develop your trading skills and knowledge, and to a much higher degree, as well as the maths and statistics used to analyse the markets and the strategies you develop for quantitative or algorithmic trading.
 

I appreciate the tips. But I should mention that I wanted to test beyond 1% risk, not for a real account, but to test how a strategy might hold with very aggressive position sizing. My goal was/is to push strategies to the limit. I understand that a trailing TP might seem bizarre, but what I mean by a trailing take profit, is to trail the TP against something that moves, for example - an ATR band (MA + ATR * Multiplier). I don't like to have a statically placed take profit. An initial one is fine, but if the TP can move with something, I prefer that. ATR doesn't change too speedily on the price if higher periods are used, and that's why something like band crossovers are possible (also with standard deviation bands). 

I will look into designing a more complex breakeven function which isn't based on an arbitrary number of points. I'll see what can be done with ATR variance as well. I guess you can go quite deep with automation. 

 
Fernando Carreiro #:
And remember that the risk is dependant on both the initial stop-loss and the margin requirements, not one or the other alone.

Can you elaborate on this ? Not sure what you mean exactly.

If I want to risk 100$ for example, so if my SL is triggered I will lose 100$, I don't see how the margin requirements influence that. Of course margin requirements are important too, but not for THIS risk. 

 
@Alain Verleyen #Can you elaborate on this ? Not sure what you mean exactly. If I want to risk 100$ for example, so if my SL is triggered I will lose 100$, I don't see how the margin requirements influence that. Of course margin requirements are important too, but not for THIS risk. 

Yes you are correct, I did not explain it very well. Basically, what I meant to imply was that if you calculate your position size on stop-loss alone, you run the risk of a high deposit load (which could lead to a margin call or even a margin stop-out). For example, this could happen with a very small and tight stop-loss and a high risk appetite, causing the calculated volume to have a very high margin requirement.

Also on some brokers/markets, the required margin is deducted from your equity directly, leaving you with less equity available to cover the stop-loss. Once the position closes the margin is released again. They probably do this as an alternative to the margin call/stop-out method. I am not really sure why they do it this way but I have seen it before, but I can't remember if that was with MetaTrader or another platform.

So, yes it is not a true risk in itself but still "risky" to ignore the margin requirements. My initial wording was inaccurate.

 

You're talking about the dynamic position sizing, it makes sense, and the function is constructed well. I have that locked down (consistent risk). I'm currently testing 25% consistent risk, this might seem outlandish, but I believe that if a strategy is strict enough, it should succeed with such risk, and statistically have a much higher percentage of wins than losses. But then there is the question...how will you manage it when things go wrong and there's a growing loss? This is the crux of the problem with swing trading on the trend, you end up "hoping" that the market diverges favourable to your position

 
Conor Mcnamara #:

You're talking about the dynamic position sizing, it makes sense, and the function is constructed well. I have that locked down (consistent risk). I'm currently testing 25% consistent risk, this might seem outlandish, but I believe that if a strategy is strict enough, it should succeed with such risk, and statistically have a much higher percentage of wins than losses. But then there is the question...how will you manage it when things go wrong and there's a growing loss? This is the crux of the problem with swing trading on the trend, you end up "hoping" that the market diverges favourable to your position

tight stop losses until market is confirmed to swing in profit direction.
 
Michael Charles Schefe #:
tight stop losses until market is confirmed to swing in profit direction.
We always come back to the same conclusion. Define the trend first, then the timing and the setup.
 
Michael Charles Schefe #:
tight stop losses until market is confirmed to swing in profit direction.
my tests with tight stop loss concluded that they're always hit by whipsaw volatility, however the meaning of "tight" can mean multiple things. I developed something now to attempt to find the safe-and-tight stop loss price..I'll see
 
I see this question as a risk management topic, and to answer you'll need to tackle your definition of disruptive. When you create a strategy (under the condition that backtests are done correctly), you should end up with a system that is winning on the long term and with the expectation of some losses (that you can measure with metrics like the recovery factor). Managing risks means understanding when your strategy doesn't work anymore as expected, and trigger emergency breaks. 
Personally I use emergency breaks (i.e. I stop teasing for a period of time) when my individual strategies loose too many times in a period of some days or when my whole portfolio (i.e. all my strategies together) falls after a certain threshold in a given day.