How to adapt a Moving Average period to market conditions?

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:

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);
MAMA(Price,0.05,0.5);
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) ) {
reverseLong(1);
}
plot("price",Price[0],MAIN|LINE,BLACK);
plot("Mama",MAMAs,LINE,RED);
plot("Fama",FAMAs,LINE,BLUE);


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?

• 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 Commented Jul 7, 2015 at 23:16
• What makes you think that MAs should work anyway? Commented Jul 8, 2015 at 4:19
• 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? Commented Jul 8, 2015 at 17:52
• @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. Commented Jul 10, 2015 at 5:53
• @vonjd : Many thanks for that paper, I am haven't finished it but what I have read looks like a very comprehensive study. Commented Jul 12, 2015 at 9:04