EA not opening orders

 
Comments that do not relate to the "EA problem!", have been moved into this topic.
 

Hi Everybody,


I have coded an EA based on indicator. But it is not opening orders. below is the code. can anybody help me out because i am not getting anyerror in compilation


//+------------------------------------------------------------------+
//|                                                       New EA.mq4 |
//|                                                  Mohsin Raza Alvi |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Mohsin Raza Alvi"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
//*************************************************************************
extern string              ____="--------------------< TRADING PARAMETERS >--------------------";//TRADING PARAMETERS ................
extern int                 Magic=111111;              //Magic Number
extern double              Lot=0.1;                   //Lot Size
extern bool                Use_TP_SL=true;
extern int                 TP=30;                     //Take Profit
extern int                 SL=20;                     //Stop Loss
extern bool                Hidden_SL_TP=false;
extern bool                Trailing_Use=true;        //Use Trailing Stop
extern int                 Trailing=30;               //Trailing Start
extern int                 Trailing_pips=30;          //Trailing Step
extern string              __="--------------------< Indicators Settings  >--------------------";
extern string              _____="___________________________________________________________________________";
extern string              ______="--------------------< Marketpz-trend-trading_001  >--------------------";
extern int                 INDICATOR_PERIOD=125;                  //MAIN_PERIOD
extern int                 DISPLAY_SMALLER_TRENDS=true;                   //ADX_PERIOD
extern int                 DISPLAY_PULLBACKS=true;                    //MINBARS
extern bool                DISPLAY_INSIDE_BARS=true;   //GRAPHICAL_USER_INTERFACE
extern bool                HISTORY_BARS=3000;   //GRAPHICAL_USER_INTERFACE
extern bool                SOUND_ALERT=false;               //SOUND_ALERT
extern bool                EMAIL_NOTIFICATION=false;        //EMAIL_NOTIFICATION
extern bool                SMS_NOTIFICATION=false;          //SMS_NOTIFICATION


//*****************************************************************************************************************
//---- Step 1: Define constants in your EA
#define OP_BUY_1        4     // Bullish Trend Change (Blue 1)  
#define OP_SELL_1       5     // Bearish Trend Change (Red 1)
#define OP_BUY_2        6     // Bullish Pullback (2)
#define OP_SELL_2       7     // Bearish Pullback (2)
#define OP_BUY_3        8     // Bullish Inside Bar Breakout (3)
#define OP_SELL_3       9     // Bearish Inside Bar Breakout (3)
#define OP_BUY_4        10    // Bullish Correction during Downtrend (blue dash)
#define OP_SELL_4       11    // Bearish Correction during Uptrend (red dash)








//******************************************************************************************************************
int Digit;
double Spreed;
double Pont;
int M;

bool New_Bar=false;

static int Cur_Day;

string Symb=Symbol(); 
string Com="Expert Advisor";

static datetime New_Time;

static double B_TP;
static double S_TP;
static double B_SL;
static double S_SL;

static double lot;
static int lll;
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////


static bool Buy_Open=true, Sell_Open=true;

///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////

 static int buy;
 static int sell;
 static int buy_stop;
 static int sell_stop;
 
 static datetime buy_time, sell_time;
 
 static int closed_buy;
 static int closed_sell;
 static int closed_orders;
 static double closed_profit;
 
 static double buy_lot;
 static double sell_lot;
 static double buy_stop_lot;
 static double sell_stop_lot;
 
 static int orders;
 
 static double buy_open,
               sell_open,
               buy_stop_open,
               sell_stop_open,
              
               buy_stoplose,
               sell_stoplose,        
               buy_stop_stoplose,   
               sell_stop_stoplose,
              
               buy_takeprofit,
               sell_takeprofit,        
               buy_stop_takeprofit,   
               sell_stop_takeprofit;
              
              
static int buy_magic;
static int sell_magic;
static int buy_stop_magic;
static int sell_stop_magic;
//Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init//
//Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init//
//Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init////Init//
int init()
  {
//--- create timer
   EventSetTimer(60); 
  if (Digits==5)
  {
   Digit=100000;
   M=10;
  }
 
 if (Digits==4)
  {
   Digit=10000; 
   M=1;
  }
 
 if (Digits==2)
  {
   Digit=100;
   M=1;
  }
 
 if (Digits==3)
  {
   Digit=1000;
   M=10;
  }
 
 if(Symb=="XAUUSD" || Symb=="GOLD")
  {
   if (Digits==2)
    {
     Digit=100;
     M=10;
    }
   
   if (Digits==3)
    {
     Digit=1000;
     M=100;
    }
   }
  
  New_Time=Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
//Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start//
//Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start//
//Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start////Start//
void start()
{
 Orders(Magic);
 Check_New_Bar();
 
 if(New_Bar==true)
  {
   Buy_Open=true;
   Sell_Open=true;
  }
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

double value = iCustom(Symbol(), Period(), "Marketpz-trend-trading", 10, 1);
//Alert(" MA= ", MA, " MA_EX2= ", MA_EX2, " MA_EX3= ", MA_EX3, " shift= ", shift);
//Alert(" BB_Up= ", BB_Up, " BB_Mid= ", BB_Mid, " BB_Dn= ", BB_Dn, " CCI= ", CCI);
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//BUY

if(value==OP_BUY_1 ) {

B_TP=NormalizeDouble((Ask+(TP*M)*Point), Digits);
  B_SL=NormalizeDouble((Ask-(SL*M)*Point), Digits);
 
  if(TP==0 || Use_TP_SL==false || Hidden_SL_TP==true)      B_TP=0;
  if(SL==0 || Use_TP_SL==false || Hidden_SL_TP==true)      B_SL=0;

  int tb=OrderSend(Symbol(), OP_BUY, Lot, Ask, 2, B_SL, B_TP, Com, Magic, 0, clrBlue);
  Buy_Open=false;



 }

//--------------------------------------------------------------------------------------------------
if(OP_SELL_1==true)
 {
  Orders_Close(Magic,0);
 }
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//Sell

if(OP_SELL_1==true)
 {
  S_TP=NormalizeDouble((Bid-(TP*M)*Point), Digits);
  S_SL=NormalizeDouble((Bid+(SL*M)*Point), Digits);
 
  if(TP==0 || Use_TP_SL==false || Hidden_SL_TP==true)      S_TP=0;
  if(SL==0 || Use_TP_SL==false || Hidden_SL_TP==true)      S_SL=0;
 
  int t=OrderSend(Symbol(), OP_SELL, Lot, Bid, 2, S_SL, S_TP, Com, Magic, 0, clrRed);
  Sell_Open=false;
 }
//--------------------------------------------------------------------------------------------------
if(OP_BUY_1==true )
 {
  Orders_Close(Magic,1);
 }
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
if(Hidden_SL_TP==true)
 {
  for(int k=1; k<=OrdersTotal(); k++)          // Цикл перебора ордер
   {
    if (OrderSelect(k-1,SELECT_BY_POS)==true) // Если есть следующий
     {                                       // Анализ ордеров:
      if (OrderSymbol()!= Symb) continue;    // Не наш фин.инструм.
         
      int Order_Magic=OrderMagicNumber();    // Magic
      if (Order_Magic != Magic) continue;    // Ордер не открыл советник
     
      if(OrderType()==0)
       {
        double buy_tp_close=NormalizeDouble((OrderOpenPrice()+(TP*M)*Point), Digits);
        double buy_sl_close=NormalizeDouble((OrderOpenPrice()-(SL*M)*Point), Digits);
       
        if(NormalizeDouble(Ask,Digits)>=NormalizeDouble(buy_tp_close,Digits) || NormalizeDouble(Ask,Digits)<=NormalizeDouble(buy_sl_close,Digits))
         bool ClosB= OrderClose(OrderTicket(), OrderLots(), Ask, 2, 0);
       }
      
      if(OrderType()==1)
       {
        double sell_tp_close=NormalizeDouble((OrderOpenPrice()-(TP*M)*Point), Digits);
        double sell_sl_close=NormalizeDouble((OrderOpenPrice()+(SL*M)*Point), Digits);
       
        if(NormalizeDouble(Bid,Digits)<=NormalizeDouble(sell_tp_close,Digits) || NormalizeDouble(Bid,Digits)>=NormalizeDouble(sell_sl_close,Digits))
         bool ClosS= OrderClose(OrderTicket(), OrderLots(), Bid, 2, 0);
       }
      }
    }
 }       
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
if(Trailing_Use==true)
{
 Orders_TR_Stop_Check(Magic);
}
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Orders(int Mag)
{
  int cb=0;
 int cs=0;
 int cbs=0;
 int css=0;
 
   buy=0;
   sell=0;
   buy_stop=0;
   sell_stop=0;
 
 orders=0;
 
 buy_lot=0;
 sell_lot=0;
 buy_stop_lot=0;
 sell_stop_lot=0;
 
 buy_open=0;
 sell_open=0;
 buy_stop_open=0;
 sell_stop_open=0;
              
 buy_stoplose=0;
 sell_stoplose=0;       
 buy_stop_stoplose=0;  
 sell_stop_stoplose=0;
              
 buy_takeprofit=0;
 sell_takeprofit=0;        
 buy_stop_takeprofit=0;   
 sell_stop_takeprofit=0;
              
              
 buy_magic=0;
 sell_magic=0;
 buy_stop_magic=0;
 sell_stop_magic=0; 
 
 
   for(int k=1; k<=OrdersTotal(); k++)          // Цикл перебора ордер
      {
       if (OrderSelect(k-1,SELECT_BY_POS)==true) // Если есть следующий
         {                                       // Анализ ордеров:
          if (OrderSymbol()!= Symb) continue;    // Не наш фин.инструм.
         
          int Order_Magic=OrderMagicNumber();    // Magic
          if (Order_Magic != Mag) continue;    // Ордер не открыл советник   
                 
          int Tip=OrderType();                   // Тип ордера
          double lots=OrderLots();               // Количество лотов
          double open=OrderOpenPrice();          // Цена открытя сделки
          double stoplose=OrderStopLoss();       // Стоп лосс сделки
          int Magicc=OrderMagicNumber();          // Magic code
          double tp=OrderTakeProfit();
         
         
           if (Tip==0)
            {
            buy++;
            buy_lot=lots;
            buy_open=open;
            buy_stoplose=stoplose;
            buy_magic=Magicc;
            buy_takeprofit=tp;
            orders++;
            buy_time=OrderOpenTime();
            }
          
           if (Tip==1)
            {
            sell++;
            sell_lot=lots;
            sell_open=open;
            sell_stoplose=stoplose;
            sell_magic=Magicc;
            sell_takeprofit=tp;
            orders++;
            sell_time=OrderOpenTime();
            }
          
           if (Tip==4)
            {
            buy_stop++;
            buy_stop_lot=lots;
            buy_stop_open=open;
            buy_stop_stoplose=stoplose;
            buy_stop_magic=Magicc;
            buy_stop_takeprofit=tp;
            orders++;
            }
          
           if (Tip==5)
            {
            sell_stop++;
            sell_stop_lot=lots;
            sell_stop_open=open;
            sell_stop_stoplose=stoplose;
            sell_stop_magic=Magicc;
            sell_stop_takeprofit=tp;
            orders++;
            }
          
          }
      }         

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Check_New_Bar()
{
 New_Bar=false;
   if(New_Time!=Time[0])                        // Сравниваем время
     {
      New_Time=Time[0];                         // Теперь время такое
      New_Bar=true;
     }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Orders_Close(int magic, int type)
{
bool Clos=true;
  for(int k=1; k<=OrdersTotal(); k++)          // Цикл перебора ордер
      {
       if (OrderSelect(k-1,SELECT_BY_POS)==true) // Если есть следующий
         {                                       // Анализ ордеров:
          if (OrderSymbol()!= Symb) continue;    // Не наш фин.инструм.
         
          int Order_Magic=OrderMagicNumber();    // Magic
          if (Order_Magic != magic) continue;    // Ордер не открыл советник                  
          int Tip=OrderType();                   // Тип ордера
          if (Tip != type) continue;
          int Tic=OrderTicket();
          double lots=OrderLots();
         
          if(Tip==1)
            Clos= OrderClose(Tic, lots, Ask, 2, 0);  
         
          if(Tip==0)
            Clos= OrderClose(Tic, lots, Bid, 2, 0);  
         
          if (Clos==true)
           {
            k=k-1;
           }   
         }
 }

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Sell_Stop_Delete(int magic)
{
  for(int k=1; k<=OrdersTotal(); k++)          // Цикл перебора ордер
      {
       if (OrderSelect(k-1,SELECT_BY_POS)==true) // Если есть следующий
         {                                       // Анализ ордеров:
          if (OrderSymbol()!= Symb) continue;    // Не наш фин.инструм.
         
          int Order_Magic=OrderMagicNumber();    // Magic
          if (Order_Magic != magic) continue;    // Ордер не открыл советник                  
          int Tip=OrderType();                   // Тип ордера
          if (Tip != 5) continue;
          int Tic=OrderTicket();
         
         
         bool Del= OrderDelete(Tic);
          if (Del==true)
           k=k-1;     
         }
     }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Buy_Stop_Delete(int magic)
{
  for(int k=1; k<=OrdersTotal(); k++)          // Цикл перебора ордер
      {
       if (OrderSelect(k-1,SELECT_BY_POS)==true) // Если есть следующий
         {                                       // Анализ ордеров:
          if (OrderSymbol()!= Symb) continue;    // Не наш фин.инструм.
         
          int Order_Magic=OrderMagicNumber();    // Magic
          if (Order_Magic != magic) continue;    // Ордер не открыл советник                  
          int Tip=OrderType();                   // Тип ордера
          if (Tip != 4) continue;
          int Tic=OrderTicket();
         
         
         bool Del= OrderDelete(Tic);  
         
          if (Del==true)
           k=k-1;    
         }
 }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Orders_TR_Stop_Check(int magic)
{
double Dist=0;
double Buy_SL=0;
double Sell_SL=0;

//Alert("Trailin Stop");

 for(int k=1; k<=OrdersTotal(); k++)     
  {
   if (OrderSelect(k-1,SELECT_BY_POS)==true)
    {                                     
     if (OrderSymbol()!= Symb) continue;
         
      int Order_Magic=OrderMagicNumber();    // Magic
      if (Order_Magic != magic) continue; 
      int Tip=OrderType();                
     
      double Price=OrderOpenPrice();
      int ticket=OrderTicket();                
      double lots=OrderLots();      
      double open=OrderOpenPrice();
      double tp=OrderTakeProfit();       // Order Take Profit
      double sl=OrderStopLoss();       // Order Take Profit
     
    //  Alert("Trail_Dist= ", Trail_Dist,"  Tip= ", Tip);
     
          if (Tip == 0)
           {
            if(sl == 0)
             {
              Dist=(Bid-open)*Digit;
             
               if(Dist>(Trailing*M))
                {
                 Buy_SL=(Bid-(Trailing_pips*M)*Point);
                  bool mod=OrderModify(ticket, Price, Buy_SL, tp, 0);
                }
              }
            
            if(sl != 0)
             {
              Dist=(Bid-sl)*Digit;
              double Dist2=(Bid-open)*Digit;
               if(Dist>(Trailing_pips*M) && Dist2>(Trailing*M))
                {
                 Buy_SL=(Bid-(Trailing_pips*M)*Point);
                 if(NormalizeDouble(Buy_SL, Digits) > NormalizeDouble((sl+(1*M*Point)), Digits))
                  bool mod1=OrderModify(ticket, Price, Buy_SL, tp, 0);
                }
              }  
            }
 ///////////////////////////////////////////////////////////////////////////////         
 ///////////////////////////////////////////////////////////////////////////////        
          if (Tip == 1)
           {
            if(sl == 0)
             {
              Dist=(open-Ask)*Digit;
               if(Dist>(Trailing*M))
                {
                 Sell_SL=(Ask+(Trailing_pips*M*Point));
                 bool mod2=OrderModify(ticket, Price, Sell_SL, tp, 0);
                }
              }
            
            if(sl != 0)
             {
              Dist=(sl-Ask)*Digit;
               double Dist2=(open-Ask)*Digit;
               if(Dist>(Trailing_pips*M) && Dist2>(Trailing*M))
                {
                 Sell_SL=(Ask+(Trailing_pips*M*Point));
                 if(NormalizeDouble(Sell_SL, Digits) < NormalizeDouble((sl-(1*M*Point)), Digits))
                  bool mod3=OrderModify(ticket, Price, Sell_SL, tp, 0);
                }
              }  
            }
 
 ///////////////////////////////////////////////////////////////////////////////         
 ///////////////////////////////////////////////////////////////////////////////        
       }
     }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

090322: I have coded an EA based on indicator. But it is not opening orders. below is the code. can anybody help me out because i am not getting anyerror in compilation

  1. Please use
SRC
    Play video
    Please edit your post.
    For large amounts of code, attach it.
    General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. Next time, don't hijack someone else's thread with your off-topic post; open your own.
  3. I doubt you "have coded an EA" based on your posting in the English forum and the code has Russian comments.
  4. Just because it compiles doesn't mean it is correct. void fcn(){} compiles but doesn't do anything.

  5. #define OP_SELL_2       7     // Bearish Pullback (2)
    double value = iCustom(Symbol(), Period(), "Marketpz-trend-trading", 10, 1);
    if(value==OP_BUY_1 ) { 
    You don't provide the indicator code so there no way we can know if that is correct. Does buffer 11 really get set to seven? See Detailed explanation of iCustom - MQL4 and MetaTrader 4 - MQL4 programming forum

  6. void Orders_Close(int magic, int type){
      bool Clos=true;
      for(int k=1; k<=OrdersTotal(); k++)          // Цикл перебора ордер
      if (OrderSelect(k-1,SELECT_BY_POS)==true     // Если есть следующий
      && (OrderSymbol()== Symb
      && OrderMagicNumber() == magic){
    :
              if(Tip==1)
                Clos= OrderClose(Tic, lots, Ask, 2, 0);  
             
              if(Tip==0)
                Clos= OrderClose(Tic, lots, Bid, 2, 0);  
              if (Clos==true)
               {
                k=k-1;
               }    
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) because while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum

    2. For FIFO (US brokers,) and you (potentially) process multiple orders, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    3. and check OrderSelect. What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
      Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
  7. Check your return codes for errors and report them. What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
    Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  8. int tb=OrderSend(Symbol(), OP_BUY, Lot, Ask, 2, B_SL, B_TP, Com, Magic, 0, clrBlue);
    Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
    Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  9. #define OP_SELL_1       5     // Bearish Trend Change (Red 1)
    :
    if(OP_SELL_1==true) Orders_Close(Magic,0);
    Zero is false and non-zero is true. Therefor that if statement is always true.
  10.  if (OrderSelect(k-1,SELECT_BY_POS)==true) // Если есть следующий
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  11. we are not going to debug your hundreds lines of code. Use the debugger or print out your variables, including _LastError and find out why.
Reason: