I need help with this Moving Average EA

 

The EA does not take trade when Stoploss = 0.

I want it to trade even if Stoploss = 0

//+------------------------------------------------------------------+
//|                                                MovingAverage.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

input int    MagicNumber   = 215484;
input int    TakeProfit    = 10;
input int    StopLoss      = 0;
input int    TrailingStop  = 0;
input int    MA_PERIOD     = 3;
input double Lots          = 0.01;

int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//   if (TrailingStop != 0) Trail();
   
   int TotalBuyOrders  = TotalOrders(OP_BUY);
   int TotalSellOrders = TotalOrders(OP_SELL);
   
   if(Bid > MA(1) && TotalBuyOrders == 0) {      
      OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point,NULL,MagicNumber,0,CLR_NONE);
   }
   
   if(Bid < MA(1) && TotalSellOrders == 0) {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point,NULL,MagicNumber,0,CLR_NONE);
   }   
  }
//+------------------------------------------------------------------+

double MA(int shift) {
   return iMA(Symbol(), 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, shift);
}

int TotalOrders(int orderType) {

   int counting = 0;
   
   for(int i=0; i<OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == orderType) {
         counting++;
      }
   }
   
   return counting;
}


/*void Trail() {

   if(TrailingStop == 0) return;
   
   for(int i=0; i<OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);      
      if((OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderMagicNumber() == MagicNumber ) {
         if(OrderType() == OP_BUY) {
            if(OrderStopLoss() < Bid-TrailingStop*Point) {
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
            }
         } else if(OrderType() == OP_SELL) {            
            if(OrderStopLoss() > Ask+TrailingStop*Point) {
               OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
            }
         }
      }
   }
}
*/
 
   double sl=0;
   if(StopLoss>0)
      sl=??;
 
Keith Watford:

Thank you.

Please, where should I put the code in the EA?

 
Keith Watford:

For example, I put it like this here

 still it does not take trade - it sends OrderSend error 130.


Please, help.

//+------------------------------------------------------------------+
//|                                                MovingAverage.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

input int    MagicNumber   = 215484;
input int    TakeProfit    = 10;
input int    StopLoss      = 0;
input int    TrailingStop  = 0;
input int    MA_PERIOD     = 3;
input double Lots          = 0.01;


int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//   if (TrailingStop != 0) Trail();
   
    double sl=0;
   if(StopLoss>0)
      sl=0;

   int TotalBuyOrders  = TotalOrders(OP_BUY);
   int TotalSellOrders = TotalOrders(OP_SELL);
   
   if(Bid > MA(1) && TotalBuyOrders == 0) {      
      OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point,NULL,MagicNumber,0,CLR_NONE);
   }
   
   if(Bid < MA(1) && TotalSellOrders == 0) {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point,NULL,MagicNumber,0,CLR_NONE);
   }   
  }
//+------------------------------------------------------------------+

double MA(int shift) {
   return iMA(Symbol(), 0, MA_PERIOD, 0, MODE_SMA, PRICE_CLOSE, shift);
}

int TotalOrders(int orderType) {

   int counting = 0;
   
   for(int i=0; i<OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == orderType) {
         counting++;
      }
   }
   
   return counting;
}


/*void Trail() {

   if(TrailingStop == 0) return;
   
   for(int i=0; i<OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);      
      if((OrderType() == OP_BUY || OrderType() == OP_SELL) && OrderMagicNumber() == MagicNumber ) {
         if(OrderType() == OP_BUY) {
            if(OrderStopLoss() < Bid-TrailingStop*Point) {
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid-TrailingStop*Point, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
            }
         } else if(OrderType() == OP_SELL) {            
            if(OrderStopLoss() > Ask+TrailingStop*Point) {
               OrderModify(OrderTicket(), OrderOpenPrice(), Ask+TrailingStop*Point, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
            }
         }
      }
   }
}
*/
 
sol4live:

For example, I put it like this here

 still it does not take trade - it sends OrderSend error 130.

Please, help.

 Your stop loss must be greater than the spread, (it mean stoploss must be superior to 20 point, more or less, for EURUSD)

otherwise the EA will give an error 130,

 OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point,NULL,MagicNumber,0,CLR_NONE);

If you don't want stop loss, put the stoploss at 1 000 000 points

Your takeprofit is also too close of Ask, it must be at a spread distance, minimum, more than 10 point, depending of the symbol

.

TP = Ask + MarketInfo(Symbol(), MODE_SPREAD)*Point; =  minimum

 
sol4live:

The EA does not take trade when Stoploss = 0.

I want it to trade even if Stoploss = 0

On Buy case, if your StopLoss =  0 then Ask-StopLoss*poin becomes Ask which is it is actually should 0, so you should modify your code for example make a new var, say SL when StopLoss = 0 then SL = 0, but when StopLoss >0 then SL = Ask-StopLoss*poin.