Amps

Tuesday, February 23, 2016

Auto Close Order Moving Average EA MQL4

Auto Close Order Moving Average EA MQL4

This part very useful because this part we able to use protect the Take profit and Stop Loss, which would make us live in Forex Trading and got best profit and minimum loss.

MQL4 Basic function used Close Order Moving Average EA MQL4

Table Function OrderSelect MQL4


bool OrderSelect It returns true if the function succeeds The order must be previously selected by the OrderSelect() function
Int index Order index or order ticket depending on the second parameter
Int select
SELECT_BY_POS index in the order pool
SELECT_BY_TICKET index is order ticket
int pool=MODE_TRADES
MODE_TRADES (default) order selected from trading pool(opened and pending orders)
MODE_HISTORY order selected from history pool (closed and canceled order)
int OrderMagicNumber(); Returns an identifying (magic) number of the currently selected order

string  OrderSymbol() Returns symbol name of the currently selected order.

Table Function Order Type MQL4

int OrderType();
Returned value Should specific when we using
OP_BUY
OP_SELL
OP_BUYLIMIT
OP_BUYSTOP
OP_SELLLIMIT
OP_SELLSTOP
double Open[] contains open prices of each bar of the current chart
double Close[] contains close prices for each bar of the current chart

Table Function Order Close MQL4

bool OrderClose Closes opened order
Int ticket Unique number of the order ticket
Double lots Number of lots
Double price Closing price
Int slippage Value of the maximum price slippage in points
color arrow_color Color of the closing arrow
long Volume[] contains tick volumes of each bar of the current chart

moving average EA close order conditions:

Close Buy when Candle open price over MA reference and close candle price under ma
Open[1]>ma && Close[1]<ma
Close Sell when Candle open price under MA reference and close candle price over ma
Open[1]<ma && Close[1]>ma

Full code moving average close order

//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
  //original ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
  //new
  ma=iMA(NULL,TimeFrameUsed,MovingPeriod,MovingShift,EMA_method1,Applied_Price1,0);
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
     }
//---
  }

This EA prepare example for activate open and close order, next chapster we would find out how to.

No comments:

Post a Comment