Expert Advisors: Simple MA EA

 

Simple MA EA:

Really simple EA for those who needs an example and for those who is looking for something to try out new Strategy Tester.

EA is based on two MA lines. If one lines is crossing another then SELL or BUY.

Author: outkast

Simple MA Expert Advisor

 

In the tester it gives an error:

2010.04.28 06:27:44 Core 1 Process creation error [2]

 
1Serg:

In the tester it gives an error:

2010.04.28 06:27:44 Core 1 Process creation error [2]

Please update to the latest build, and then check for the metatester.exe file in the root of the program directory.
 
Renat:
Please update to the latest build, and then check for the metatester.exe file in the root of the program directory.

Thanks, it works.

The optimiser and visualisation are missing.

 
1Serg:

The optimiser and visualisation are missing.

The optimiser is there - just increase the height of the tester window.

Visualisation will be later.

 
Renat:

The optimiser is there - just increase the height of the tester window.

Visualisation will come later.

Cool!

 

Regards,

 

Mr/Mrs tsaktuo, I have studied your code K_eSimpleMA and it has been very usefull in order to understand how the structure of an EA is, as well as concepts like how to work with SMA crosses.

I really appreciate you have shared this article and I Thank you very much.

On the other hand I have a couple of questions; both are related with SymbolInfoTick function:


What the manual of MQL5 says about  SymbolInfoTick(Symbol(),tick) is that it returns current prices of a specified symbol in a variable of the MqlTick type and it also says that the function returns true if successful, otherwise returns false.

According above, what I understand is that the message ("Failed to get Symbol info!") is printed if there is not tick received,. Is this correct?  . If it is not correct could you please tell me what it is about?

The other question is: What happen if the message is printed every tick?

 

 Again, thanks.

 

There is:

trReq.sl=tick.ask-_Point*sl; // Stop Loss level of the order

trReq.tp=tick.ask+_Point*tp; // Take Profit level of the order

needed:

trReq.sl=tick.bid-_Point*sl; // Stop Loss level of the order

trReq.tp=tick.bid+_Point*tp; // Take Profit level of the order

and change for SELL accordingly.

The purchase is made at ask price, but it is closed at bid price.

If you want to limit profit and loss, for example, to 20 points, then exactly 20 points should be counted from the bid price (closing price) for BUY and ask (closing price) for SELL.

 

Can you please tell me how to add to this code to make it trade on MACD trend without stops?

Long: MACD is growing fast MA crossed slow MA from top to bottom

Croy Long: MACD rising fast MA crossed slow MA from bottom to top

Short: MACD decreasing fast MA crossed the slow one from top to bottom

Short: MACD decreasing fast MA crossed the slow one from bottom to top

I tried it like this:

//+------------------------------------------------------------------+
//|MACD&DEMA.mq5 |
//|Copyright 2010, AM2 Group. |
//| http://www.am2_group.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, AM2 Group."
#property link      "http://www.am2_group.net"
#property version   "1.00"

//--- input parameters
input int Periods=2;          // Period 1 Moving Average
input int Shift=38;           // Period 2 Moving Average
input int SL=30;              // Stop Loss
input int TP=100;             // Take Profit
input int MAGIC=3072010;      // Magic Number Counsellor

MqlTradeRequest trReq;
MqlTradeResult trRez;
int ma1Handle;
int ma2Handle;
int macdHandle;               // MACD indicator handle
double ma1Val[];
double ma2Val[];
double macdVal[]; // dynamic array for storing numerical MACD values for daily bars
  
int sl;
int tp;
//+------------------------------------------------------------------+
//| Expert initialisation function|
//+------------------------------------------------------------------+
int OnInit()
{
   //Set default vaules for all new order requests
      trReq.action=TRADE_ACTION_DEAL;
      trReq.magic=MAGIC;
      trReq.symbol=Symbol();                 // Trade symbol
      trReq.volume=0.1;                      // Requested volume for a deal in lots
      trReq.deviation=1;                     // Maximal possible deviation from the requested price
      trReq.type_filling=ORDER_FILLING_AON;  // Order execution type
      trReq.type_time=ORDER_TIME_GTC;        // Order execution time
      trReq.comment="MA Sample";
   //end
  
   //Create handle for 2 MA indicators
      ma1Handle=iMA(Symbol(),PERIOD_CURRENT,Periods,0,MODE_EMA,PRICE_CLOSE);
      ma2Handle=iMA(Symbol(),PERIOD_CURRENT,Periods+Shift,0,MODE_EMA,PRICE_CLOSE);
   //---Get MACD indicator handle
      macdHandle=iMACD(NULL,PERIOD_D1,15,26,1,PRICE_CLOSE);
   //end
  
   //input parameters are ReadOnly
      tp=0;
      sl=0;
   //end
  
   //Suppoprt for acount with 5 decimals
      if(_Digits==5)
      {
         sl*=10;
         tp*=10;
      }
   //end
      
   return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialisation function|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function|
//+------------------------------------------------------------------+
void OnTick()
  {
  
   MqlTick tick; //variable for tick info
   if(!SymbolInfoTick(Symbol(),tick))
   {
      Print("Failed to get Symbol info!");
      return;
   }
  
   //Copy latest MA indicator values into a buffer
   int copied=CopyBuffer(ma1Handle,0,0,4,ma1Val);
   if(copied>0)
      copied=CopyBuffer(ma2Handle,0,0,4,ma2Val);
      copied=CopyBuffer(macdHandle,0,0,4,macdVal);
   if(copied>0)
   {
      //If MAPeriod > MAPeriod + Shift -> BUY
      if(ma1Val[1]>ma2Val[1] && macdVal[1]>macdVal[2])
      {
         trReq.price=tick.bid;                   // SymbolInfoDouble(NULL,SYMBOL_BID);
         trReq.sl=tick.bid-_Point*sl;            // Stop Loss level of the order
         trReq.tp=tick.bid+_Point*tp;            // Take Profit level of the order
         trReq.type=ORDER_TYPE_BUY;              // Order type
         OrderSend(trReq,trRez);
      }
      //If MAPeriod < MAPeriod + Shift -> SELL
      else if(ma1Val[1]<ma2Val[1] && macdVal[1]<macdVal[2])
      {
         trReq.price=tick.ask;
         trReq.sl=tick.ask+_Point*sl;            // Stop Loss level of the order
         trReq.tp=tick.ask-_Point*tp;            // Take Profit level of the order
         trReq.type=ORDER_TYPE_SELL;             // Order type
         OrderSend(trReq,trRez);
      }
   }

  }
//+------------------------------------------------------------------+
 
Automated-Trading:

Simple MA EA:

Author: outkast

Hello Outkast,

I am new to EA's i just imported Mr. tsaktuo esimpleMA,  since i really dont understand the Code, I see it running on my chart, but am i suppose to set some of the MA values, I dont know need help need a class or a book.  ???

 

I was happy to find your code example as it solved my problem of buffering the MA and making it appear on a chart.  Thanks for posting it.

The one question that I have is how does it make the decision to close the open order? 

 

Thanks