Download MetaTrader 5

MT4 MA crossing dictate take profit

To add comments, please log in or register

Hi all, I'm a newbie to MT4 coding.

What I have: a simple MA crossing I found using google just to start learning.

If the short ema (5) is above long ema (10) = buy

If the short ema (5) is below long ema (10) = sell

Issue: right now I have to guess and say...take profit = 20

I'd like to have the market itself dictate when to exit and take profit.

Desired results:

I want to take profit until there is an actual crossing.

If went long on my order, take profit and exit automatically until long ema crosses short ema (reversal.)

If went short, take profit and exit automagically until short ema crosses long ema (reversal.)

This is what I have but it doesn't give me what I want during backtesting:

example for closing after I went long...

double opening = iOpen(NULL, 0, Current);

double closing = iClose(NULL, 0, Current);

double short_ema = iMA(NULL, 0,5,0,MODE_EMA, PRICE_CLOSE, 0);

double long_ema = iMA(NULL, 0,10,0,MODE_EMA, PRICE_CLOSE, 0);

double take_profit = 10.0; //minimum for brokers

//1 for ema 5 above ema 10

//2 for ema 5 below ema 10

//how do I adjust below????

//I bought now I'd like to close

if (crossed_over == 1){

//update take profit to current closing if ema5 above ema10

//just to keep function happy

take_profit = (take_profit * Point )+ closing;


//now it really cross back so I'd like to close it based on that info

if(isCrossed == 2) {

OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red); // close position

return(0); // exit


thanks in advance,

count dracula

To add comments, please log in or register