How to add another moving average to the formula

hi, im trying to ge my head around afl. ive just read the intro to amibroker and been using the basic codes and would like to add some paremters.
here is the code

// Example6c.afl
// changed MA 20 50
// A trading system based on the
// cossover of two moving averages.
// Buy when the faster moving average
// crosses up though the slower moving
// average.
// Sell when the faster moving average
// crosses down through the slower
// moving average.

FastMA = MA( C, 20 );
SlowMA = MA( C, 50 );

Buy = Cross( FastMA, SlowMA );
Sell = Cross( SlowMA, FastMA );

i would like to add another MA of 5.
and have a buy signal when the 20MA crosses the slow 50MA and the 5MA is above both of them.
Also id like to incorporate an ADX value of <20. if the shares have d
had an adx of <20 in the recent 3 or 4 weeks.

i would appreciate some assistance

ive looked in the afl library and theres so many there. cant find too many basic codes.

thanks

john

Just follow the code you already have. Adding 5 -bar MA is generally the same code that you have just with changed period:

SuperFastMA = MA( C, 5 ); // 5 day ma

Buy = Cross( FastMA, SlowMA ) AND SuperFastMA > FastMA AND SuperFastMA > SlowMA;
2 Likes

excellent, thanks for that.

how do i add a stop loss of 5% and if the share price goes up a trailing stop of 10%. so i can do a back test

thanks

john

What did you find in the Help File or here in the Forum when you searched on "trailing stop"? The community here tends to help those who make some effort to solve their own problems rather than just asking others to do all the work for them.

This would be a good place for you to start: https://www.amibroker.com/guide/afl/applystop.html

3 Likes

thanks for the reply, im finding it difficult to a
understand afl code. im sure with time i will get proficient

i added this snippet to a basic code

amount = 10; // 10% loss (trailing)
ApplyStop( stopTypeTrailing, stopModePercent, amount, True );

this is what it looked like

FastMA = MA( C, 15 );
SlowMA = MA( C, 45 );

Buy = Cross( FastMA, SlowMA );
Sell = Cross( SlowMA, FastMA );
amount = 10; // 10% loss (trailing)
ApplyStop( stopTypeTrailing, stopModePercent, amount, True );

the results when i back tested it were appalling compared to with out the stoploss

im obviuosly missing something

@colombianflag nothing strange here: any new condition you add to your code will change the results (sometimes improving them, sometimes for worse).

But you must look and understand all the other elements of your strategy backtest reports (looking only at the cumulative profit is misleading).

I suggest to study this guide sections:

2 Likes