order send error 138

 
Puria.mq4
#define MAGICMA  20050610

extern string Trades             = "Настройки торговых операций";
extern double MaxRisk            = 0.2;
extern double Lots               = 0.1;
extern int TakeProfit            = 150;
extern int MinProfit             = 100; // минимальный профит
extern double MinProfitPercent   = 0.5; // процент взятия при минимальном профите
extern int ProfitStep            = 75; // шаг профита
extern double ProfitPercent      = 0.1; // процент профита больше минимального
extern int TrailingStop          = 90;
extern int StopLost              = 80;

extern bool UseAllTicks          = true;

extern int ma0_period            = 69;
int ma0_shift                    = 0;
int ma0_method                   = 3; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
int ma0_price                    = 3; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED

extern int ma1_period            = 74;
int ma1_shift                    = 0;
int ma1_method                   = 3; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
int ma1_price                    = 3; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED

extern int ma2_period            = 19;
int ma2_shift                    = 0;
int ma2_method                   = 1; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
int ma2_price                    = 1; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED

extern int macd_fast             = 17;
extern int macd_slow             = 38;
int macd_signal                  = 1;
int macd_price                   = 1; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED
extern int macd_open             = 8;

double NextProfit = 0;
double StartLots = 0;

double MinLot;
double MaxLot;

//+------------------------------------------------------------------+
int init() {
 MinLot = MarketInfo(Symbol(),MODE_MINLOT);
 MaxLot = MarketInfo(Symbol(),MODE_MAXLOT);
 return(0);
}
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}

//+------------------------------------------------------------------+
double Get_Lots() {
   double lot=Lots;
   if (MaxRisk > 0) {
    double RiskSumm = AccountFreeMargin()*MaxRisk;
    lot=RiskSumm/StopLost/100;
   }
   lot=MathFloor(lot*100)/100;
   if (lot > MaxLot) lot = MaxLot;
   if (lot < MinLot) lot = MinLot;
   return(lot);
  }

//+------------------------------------------------------------------+
bool Signal_Stop_Buy() {
 double ma0 = iMA(NULL,0,ma0_period,ma0_shift,ma0_method,ma0_price,0);
 double ma1 = iMA(NULL,0,ma1_period,ma1_shift,ma1_method,ma1_price,0);
 double ma2 = iMA(NULL,0,ma2_period,ma2_shift,ma2_method,ma2_price,0);
 if (ma0 > ma2) return(true);
 return(false);
}

//+------------------------------------------------------------------+
bool Signal_Stop_Sell() {
 double ma0 = iMA(NULL,0,ma0_period,ma0_shift,ma0_method,ma0_price,0);
 double ma1 = iMA(NULL,0,ma1_period,ma1_shift,ma1_method,ma1_price,0);
 double ma2 = iMA(NULL,0,ma2_period,ma2_shift,ma2_method,ma2_price,0);
 if (ma0 < ma2) return(true);
 return(false);
}

//+------------------------------------------------------------------+
bool macd_up(int timeframe, int fast, int slow, int signal, int price, int num) {
 double y;
 double x = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,0)*100000;
 for (int i=1; i<num; i++) {
  y = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,i)*100000;
  if (y > x) return(false);
  else x = y;
 }
 return(true);
}

//+------------------------------------------------------------------+
bool macd_down(int timeframe, int fast, int slow, int signal, int price, int num) {
 double y;
 double x = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,0)*100000;
 for (int i=1; i<num; i++) {
  y = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,i)*100000;
  if (y < x) return(false);
  else x = y;
 }
 return(true);
}

//+------------------------------------------------------------------+
bool Signal_Buy() {
 double ma0 = iMA(NULL,0,ma0_period,ma0_shift,ma0_method,ma0_price,0);
 double ma1 = iMA(NULL,0,ma1_period,ma1_shift,ma1_method,ma1_price,0);
 double ma2 = iMA(NULL,0,ma2_period,ma2_shift,ma2_method,ma2_price,0);
 double macd = iMACD(NULL,0,macd_fast,macd_slow,macd_signal,macd_price,MODE_MAIN,0);
 
 if (((ma1-ma0)/Point > 0.5) && ((ma2-ma0)/Point > 0.5)) {
  if (macd > 0) {
   if (macd_up(0,macd_fast,macd_slow,macd_signal,macd_price,macd_open)) return(true);
  }
 }
 return(false);
}

//+------------------------------------------------------------------+
bool Signal_Sell() {
 double ma0 = iMA(NULL,0,ma0_period,ma0_shift,ma0_method,ma0_price,0);
 double ma1 = iMA(NULL,0,ma1_period,ma1_shift,ma1_method,ma1_price,0);
 double ma2 = iMA(NULL,0,ma2_period,ma2_shift,ma2_method,ma2_price,0);
 double macd = iMACD(NULL,0,macd_fast,macd_slow,macd_signal,macd_price,MODE_MAIN,0);
 
 if (((ma0-ma1)/Point > 0.5) && ((ma0-ma2)/Point > 0.5)) {
  if (macd < 0) {
   if (macd_down(0,macd_fast,macd_slow,macd_signal,macd_price,macd_open)) return(true);
  } 
 }
 return(false);
}

//+------------------------------------------------------------------+
void CheckForOpen() {
   int res;

 if ((!UseAllTicks) && (Volume[0]>1)) return;

   if (Signal_Sell()) {
      StartLots = Get_Lots();
      NextProfit = Bid - MinProfit*Point;
      res=OrderSend(Symbol(),OP_SELL,StartLots,Ask,3,0,0,"",MAGICMA,0,Red);
      return;
   }

   if (Signal_Buy()) {
      StartLots = Get_Lots();
      NextProfit = Ask + MinProfit*Point;
      res=OrderSend(Symbol(),OP_BUY,StartLots,Bid,3,0,0,"",MAGICMA,0,Blue);
      return;
   }
}
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double SL,TP,lx;
   int profit;

 if ((!UseAllTicks) && (Volume[0]>1)) return;

   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 (Signal_Stop_Buy()) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         else if (OrderStopLoss() == 0.0) {
          SL = Bid - StopLost*Point;
          TP = Ask + TakeProfit*Point;
          OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
         }
         else {
          if (Bid > NextProfit) {
           if (Bid < (OrderOpenPrice()+(MinProfit+ProfitStep/2)*Point)) lx = NormalizeDouble(StartLots*MinProfitPercent,2);
           else lx = NormalizeDouble(StartLots*ProfitPercent,2);
           OrderClose(OrderTicket(),lx,Bid,3,White);
           NextProfit = Bid + ProfitStep*Point;
          }
          else if (NextProfit > (OrderOpenPrice()+MinProfit*Point)) {
           SL = NormalizeDouble((Bid - TrailingStop*Point),Digits);
           TP = NormalizeDouble((Ask + TakeProfit*Point),Digits);
           if (OrderStopLoss() < SL) OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
          }
         }
         break;
      }
      if(OrderType()==OP_SELL) {
         if (Signal_Stop_Sell()) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         else if (OrderStopLoss() == 0.0) {
          SL = Bid + StopLost*Point;
          TP = Ask - TakeProfit*Point;
          OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
         }
         else {
          if (Ask < NextProfit) {
           if (Ask > (OrderOpenPrice()-(MinProfit+ProfitStep/2)*Point)) lx = NormalizeDouble(StartLots*MinProfitPercent,2);
           else lx = NormalizeDouble(StartLots*ProfitPercent,2);
           OrderClose(OrderTicket(),lx,Ask,3,White);
           NextProfit = Bid - ProfitStep*Point;
          }
          else if (NextProfit < (OrderOpenPrice()-MinProfit*Point)) {
           SL = NormalizeDouble(Ask + TrailingStop*Point,Digits);
           TP = NormalizeDouble(Bid - TakeProfit*Point,Digits);
           if (OrderStopLoss() > SL) OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
          }
         }
         break;
      }
   }
}

//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
   if(buys>0) return(buys);
   else       return(-sells);
  }

//+------------------------------------------------------------------+
void start()
  {
   if(Bars<100 || IsTradeAllowed()==false) return;
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else CheckForClose();
  }

//+------------------------------------------------------------------+

fix ma please 5LWMA low,75 EMA close,85 EMA close

 

Please use the </> button to post your code.


 
ERR_REQUOTE 138 The requested price has become out of date or bid and ask prices have been mixed up. The data can be refreshed without any delay using the RefreshRates function and make a retry. If the error does not disappear, all attempts to trade must be stopped, the program logic must be changed.
Predefined Variables - MQL4 Reference
Predefined Variables - MQL4 Reference
  • docs.mql4.com
For each executable mql4-program a set of predefined variables is supported, which reflect the state of the current price chart by the moment a mql4-program (Expert Advisor, script or custom indicator) is started. Values of predefined variables are set by the...
 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

Reason: