Risk warning

All articles, strategies and indicators are just reflecting a single traders opinion and should be viewed as that.
I advice everybody to trade with a DEMO account!

Monday, July 4, 2011

Learning MT4 MQL : First simple MA strategy

This post will show how you can make an extremely simple MT4 Expert Advisor (EA). So the purpose is not to make a strategy that is profitable. No the purpose is to make something as simple as possible so we will be able to understand the code. I advice everybody that wants to learn to make his own EA in MT4 MQL to start with basic and very simple code.

We are going to make a simple moving average strategy.

Buy with stop loss (S/L) and profit target (T/P) when currency is going above the moving average exponential
Sell with stop loss (S/L) and profit target(T/P) when currency is going below the moving average exponential

Lets write the strategy and explain all parts. The explanation is between // and //. In the strategy you write you can uses // for adding explanations.

//All the values that the user of the EA should be able to change are declared first with extern //
//int = integer because TakeProfit and StopLoss are rounded numbers//
//use double if you want to use values like 400.50 or 200.10//

extern int     TakeProfit = 400;
extern int     StopLoss = 200; 

//Position is an integer value that we are going to use to determine if we have an open position //
//Position 0 means no open position//
//Position 1 means 1 open position and its a long//
//Position -1 means 1 open position and its a short//

int Position            =      0;

//Next part is a function. The name of the function is CalcPosition. Void is used for functions that don't//
//return a value. This function will fill the variable Position that we declared above. //
//Position will be 0 for no position, position will be 1 if we have an open long and -1 if  we have an//
//open short. The function will be executed when we call for it in the actual program//

void CalcPosition() {
    Position = 0;
    for (int i = 0; i < OrdersTotal(); i++) {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if( OrderSymbol() == Symbol() ) {
            if (OrderType() == OP_BUY) Position += 1;
            if (OrderType() == OP_SELL) Position -=1;
        }
    }
}

//Next we start the actuel program. This always starts with int start()//

int start()
  {

//Next we declare the variables that the strategy will use, variables not meant to be changed by//
//the person who is going to use the EA//

   double lots, ma1, stop, profit, slippage;

//Next we fill all these variables, one by one//

   lots = 0.1;
   slippage = 3;
   ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_WEIGHTED,0);
   stop = StopLoss*Point;
   profit = TakeProfit*Point;

//With the line below we are asking that our function is executed and that the value of position is detemined//

  CalcPosition();

//Next step we check if the bar is closing above the exponential moving average and we check that //
//we don't have an open position yet//

      if(Close[0]>ma1&&Position==0)
        {

//if that is the case the following code buys lots and puts a profit taking level above and a stop order below//

     OrderSend(Symbol(), OP_BUY, lots, Ask, slippage, Ask-stop, Ask+profit, 0);
        }

//Same when bar closes below EMA, if below and no open order we sell and put a profit taking level and//
//a stop loss order//

      if(Close[0]<ma1&&Position==0)
         {
          OrderSend(Symbol(), OP_SELL, lots, Bid, slippage, Bid+stop, Bid-profit, 0);
         }

 //Next line just means we are going back to the beginning of the actual program//    
           
        return(0);
   }

The code without all these explanations:
//-------------------------------------------------------------------------------------------//
extern int     TakeProfit = 400;
extern int     StopLoss = 200; 
int Position            =      0;
void CalcPosition() {
    Position = 0;
    for (int i = 0; i < OrdersTotal(); i++) {
        OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if( OrderSymbol() == Symbol() ) {
            if (OrderType() == OP_BUY) Position += 1;
            if (OrderType() == OP_SELL) Position -=1;
        }
    }
}
int start()
  {
   double lots, ma1, stop, profit, slippage;
   ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_WEIGHTED,0);
   lots = 0.1;
   slippage = 3;
   stop = StopLoss*Point;
   profit = TakeProfit*Point;
   CalcPosition();
      if(Close[0]>ma1&&Position==0)
        {
         OrderSend(Symbol(), OP_BUY, lots, Ask, slippage, Ask-stop, Ask+profit, 0);
        }

      if(Close[0]<ma1&&Position==0)
         {
          OrderSend(Symbol(), OP_SELL, lots, Bid, slippage, Bid+stop, Bid-profit, 0);
         }
   return(0);
   }
//-------------------------------------------------------------------------------------------//

This is a pretty small strategy. Exactly what you need if you want to start learning to write your own EA's. If you never wrote an EA don't start with some pages long EA. You will get depressed trying to understand it all to fast. Start with something as tiny as possible. Good luck.

6 comments:

  1. I have been following your very interesting posts over the last couple of months, which helped me write a couple of EAs on CTL. Unfortunately, as GFT is moving to MT4 I was wondering whether you could assist into translating these EAs .

    ReplyDelete
  2. Sure i will help where i can. But i have to learn a lot too. Stay tuned as we are going to post more and more advanced MQL strategies on this blog. Like i said in this post best is to start with some simple basic strategies.....

    ReplyDelete
  3. Many thanks, your great selfless assistant is highly appreciated

    ReplyDelete
  4. Hi

    I tried the above code on my MT4 Platform and it did work as you described. Can u let me know as to what may be the cause as i think this EA is simple and sweet for me to understand.

    Thanks.

    ReplyDelete
  5. Sorry...

    Typo error on my side...

    "I tried the above code on my MT4 Platform and it DID NOT work as you described"....

    ReplyDelete
  6. What do you mean. First you say it works..... after that you say you made a typo error and say it doesn't work....

    Confusing...is it working or not? It works with my MT4 version.

    ReplyDelete