I would like to know if there is some way to adapt the period of a moving average to market conditions like for instance the stop loss can be adapted to market conditions using the average true range. Thanks!

EDIT: As suggested, I add some references and context to my question.

I have a trend following system that in the test period 2009 - 2014, with one hour candles, no parameters to optimize, EUR/USD produces these figures:

Max drawdown        -48$ 13% (MAE -74$ 20%)
Number of trades    165 (28/year, 1/week, 1/day)
Percent winning     31%
Annual return       139%
Profit factor       1.99 (PRR 1.56)
Sharpe ratio        0.92
Kelly criterion     0.55
R2 coefficient      0.647
Ulcer index         9.4%

However the same system in 2003 - 2008 makes:

    Max drawdown        -131$ 158% (MAE -154$ 185%)
    Number of trades    212 (36/year, 1/week, 1/day)
    Percent winning     19%
    Annual return       14%
    Profit factor       1.15 (PRR 0.90)
    Sharpe ratio        0.27
    Kelly criterion     0.39
    R2 coefficient      0.110
    Ulcer index         68.2%

So I tried to look for an adaptive moving average that adapts to market conditions, I read among others these two references:

http://www.mesasoftware.com/papers/MAMA.pdf (just in case it goes down) https://books.google.com.uy/books?id=_KjOT1b9bfUC&pg=PA113&lpg=PA113#v=onepage&q&f=false

I have tried to reproduce the method of the first link mith mixed results, I tested with EUR/USD and candles of 8 hours this code, sorry for the length, I hope you bear with me:

vars Price = series(price());           
Stop = 2*ATR(100);  
vars MAMAs = series(rMAMA);
vars FAMAs = series(rFAMA);

if( crossUnder(FAMAs,MAMAs) ){
    reverseShort(1);//if the Following Adaptive Moving Average crosses under the Mother of Adaptive Moving Averages then I enter long, closing previous short if any
} else if( crossOver(FAMAs,MAMAs) ) {

Testing that in 2009 - 2014 gave

Max drawdown        -157$ 64% (MAE -159$ 65%)
Number of trades    392 (68/year, 2/week, 1/day)
Percent winning     62%
Annual return       35%
Profit factor       1.15 (PRR 1.00)
Sharpe ratio        0.52
Kelly criterion     0.73
R2 coefficient      0.002
Ulcer index         21.2%

But in 2003 - 2008 it gives

Max drawdown        -255$ -1396% (MAE -255$ -1397%)
Number of trades    383 (66/year, 2/week, 1/day)
Percent winning     60%
Annual return       -2%
Profit factor       0.99 (PRR 0.85)
Sharpe ratio        -0.02
Kelly criterion     -0.05
R2 coefficient      0.013
Ulcer index         38.7%

However if I test in 2003 - 2008 with 4 hour candles the results are

Max drawdown        -145$ 67% (MAE -146$ 67%)
Number of trades    790 (134/year, 3/week, 1/day)
Percent winning     61%
Annual return       32%
Profit factor       1.10 (PRR 0.99)
Sharpe ratio        0.50
Kelly criterion     0.71
R2 coefficient      0.415
Ulcer index         23.0%

So, even this 'adaptive' moving average needed to be adapted to a different market by adjusting the duration of candles, but I don't know whether this is curve fitting. Is there a way to detect this change in market conditions between 2003 - 2008 and 2009 - 2014 in this test?

  • 3
    $\begingroup$ Hi @MithPaul! I think you're asking how to print money. Anyway, I know that some models can detect the probabitlity of a being in a market state/scenario, but I honestly don't know how much that can be useful. Browse Markow Swithing regression model (Hamilton, 1994) if you are interested in $\endgroup$
    – Quantopik
    Commented Jul 7, 2015 at 23:16
  • 3
    $\begingroup$ What makes you think that MAs should work anyway? $\endgroup$
    – vonjd
    Commented Jul 8, 2015 at 4:19
  • 2
    $\begingroup$ The problem is: How do you know which model to switch to before it happens? You will know if we are in a contracting volume/price only after it happened. Maybe you could calculate the probability of a contracting range after an impulsive movement? $\endgroup$
    – sparkle
    Commented Jul 8, 2015 at 17:52
  • 1
    $\begingroup$ @sparkle: That's it, I have experimented with using volatility indicators (bollinger bands, ATR, Alligator) and compress them with a Fisher transform (or a Z transform, one that normalizes the data) to see when the values go out of the compressed range and use that as a 'signal' to adjust the MA lenght but it did not work. $\endgroup$
    – MithPaul
    Commented Jul 10, 2015 at 5:53
  • 1
    $\begingroup$ @vonjd : Many thanks for that paper, I am haven't finished it but what I have read looks like a very comprehensive study. $\endgroup$
    – MithPaul
    Commented Jul 12, 2015 at 9:04

1 Answer 1


As it was already mentioned. Try to read about Kalman filter and Markow Switching models. I have even seen some academic papers where authors tried to define MA length based on MSM or KF. Try to google it ...

  • $\begingroup$ The question is how to adapt a MA. Now how does your answer tackle this question? I myself use HMMs in my research but I don't modify MAs with the output, so I don't see the link here. $\endgroup$
    – vonjd
    Commented Jul 10, 2015 at 6:39
  • $\begingroup$ ...and answers that just say: google something and 'I have seen some papers' are frowned upon in this community. $\endgroup$
    – vonjd
    Commented Jul 10, 2015 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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