Automatic validation. No trade operations error.

To add comments, please log in or register
Tonny Obare
72593
Tonny Obare  

I have broken down the code to the very basics so it can be publicized. It returns the error shown with automatic validation. What could be the problem?

err

The code follows.

//+------------------------------------------------------------------+
//|                                                     BareBone.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, XXXX"
#property link      "XXXXX"
#property version   "1.1"
#property strict

//---- input parameters
extern double   Lots=0.01;
extern string   StopsSettings="Set stops and restrictions";
extern bool      CloseOnReverseSignal=true;
extern bool OneTradeAtAtime=false;
extern int MaximumTrades=100;
extern string    PSARsettings="Parabolic sar settings follow";
extern double    PsarStep    =0.02;
extern double    PsarMaximum =0.2;
extern int       MagicNumber=3657;
extern bool      BrokerIsFiveDigit=true;

string freeze;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {

   return(0);

  }
//+------------------------------------------------------------------+
//| expert starts                                  |
//+------------------------------------------------------------------+
int start()
  {
double Lts=Lots;
//---------------------------------

//----
   int StopMultd=1,Slip=5;
   if(BrokerIsFiveDigit)StopMultd=10;
   int Slippage=Slip*StopMultd;

//------------------------- Mas--------------------------------------------------
   double PSARlast,PSARprev,LOWnow,HIGHnow,PREVhigh,PREVlow;

// PSAR checks
// PSARnow=iSAR(NULL, 0,Step,Maximum, 0);
   PSARlast=iSAR(NULL, 0,PsarStep,PsarMaximum, 0);
   PSARprev=iSAR(NULL, 0,PsarStep,PsarMaximum, 1);

   LOWnow=Low[0];
   HIGHnow=High[0];
   PREVhigh=High[1];
   PREVlow=Low[1];


//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
   int TotalTrades=Tcount(MagicNumber,Symbol());

   int halt1=0;
   if(OrdersTotal()>0)
     {
      for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
        {
         if(OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
           {
            if(OrderMagicNumber()==MagicNumber)
              {
               if(OneTradeAtAtime==true){ halt1=1; }
               if(OneTradeAtAtime==false && (OrderOpenTime()>Time[0] || (TotalTrades>=MaximumTrades && MaximumTrades>0))){ halt1=1; }
              }

           }
        }
     }
//-------------------------------------------------------------------+


   int buysignal=0,sellsignal=0;

   if(PSARlast<LOWnow && PSARprev>PREVhigh) buysignal=1;
   if(PSARlast>HIGHnow && PSARprev<PREVlow) sellsignal=1;


//-------------------------------------------------------------------+
// Trade opening
//-------------------------------------------------------------------+
   int buyfail=0,sellfail=0;
 //  if(halt1!=1)
  //   {// halt1

      // Sell criteria
      if(sellsignal==1 && freeze!="Selling trend" && CheckUserInput()!=1) //Signal Sell
        {
         if(CheckMoneyForTrade(NULL,Lts,OP_SELL)==true && CheckVolumeValue(Lts)==true)
           {
            int opensell=OrderSend(Symbol(),OP_SELL,Lts,Bid,Slippage,0,0,"Barebone sell",MagicNumber,0,clrRed);
            if(opensell<1){sellfail=1;}
           }
        }

      // Buy criteria
      if(buysignal==1 && freeze!="Buying trend" && CheckUserInput()!=1) //Signal Buy
        {
         if(CheckMoneyForTrade(NULL,Lts,OP_BUY)==true && CheckVolumeValue(Lts)==true)
           {
            int openbuy=OrderSend(Symbol(),OP_BUY,Lts,Ask,Slippage,0,0,"Barebone buy",MagicNumber,0,clrBlue);
            if(openbuy<1){ buyfail=1;}
           }
        }

  //   }// halt1

   if(buysignal==true){freeze="Buying trend";}
   if(buysignal!=true && sellsignal!=true){freeze="none";}
   if(sellsignal==true){freeze="Selling trend";}

//--------------------------------------------------------------

//-------------------------------------------------------------------+
// Trade closing
//-------------------------------------------------------------------+


// close buy
   if(sellsignal==1 && CloseOnReverseSignal==true)
     {
      BasClose(MagicNumber,Symbol(),1,0);
     }

// close sell
   if(buysignal==1 && CloseOnReverseSignal==true)
     {
      BasClose(MagicNumber,Symbol(),0,1);
     }




//-------------------------------------------------------------------
   return(0);
  }
//+-----------------------------------

//---------------------------------count trades--------------------------------------------
int Tcount(int tcMagicNumber,string tcSymbol)
  {
   int nTrades=0;
   for(int tc=1; tc<=OrdersTotal(); tc++)
     {

      if(OrderSelect(tc-1,SELECT_BY_POS)==true && OrderSymbol()==tcSymbol && OrderMagicNumber()==tcMagicNumber)
         nTrades++;
     }

   return (nTrades);
  }
//------------------------------------------------------------------------------------



//------------------------Close trades-----------------------------
void BasClose(int CMagicNumber,string CSymbol,int CBuy,int CSell)
  {
   if(OrdersTotal()>0)
     {// orders total
      for(int pb=1; pb<=OrdersTotal(); pb++)
        {// cycle

         if(OrderSelect(pb-1,SELECT_BY_POS)==true && OrderSymbol()==CSymbol && OrderMagicNumber()==CMagicNumber)
           {
            if(OrderType()==OP_BUY&&CBuy==1) bool CloseExec=OrderClose(OrderTicket(),OrderLots(),Bid,50,clrNONE);
            if(OrderType()==OP_SELL&&CSell==1) bool CloseExec2=OrderClose(OrderTicket(),OrderLots(),Ask,50,clrNONE);
           }
        } // cycle
     }// orders total
  }
//---------------------------------------------------------------------


bool CheckMoneyForTrade(string symb,double lots,int type)
  {
   double free_margin=AccountFreeMarginCheck(symb,type,lots);
//-- if there is not enough money
   if(free_margin<0)
     {
      string oper=(type==OP_BUY)? "Buy":"Sell";
      Print("Not enough money for ",oper," ",lots," ",symb," Error code=",GetLastError());
      return(false);
     }
//--- checking successful
   return(true);
  }
//--------------------------------------------------------------------

//+------------------------------------------------------------------+
//| Check the correctness of the order volume                        |
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume)
  {
   string desc;
//--- minimal allowed volume for trade operations
   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
   if(volume<min_volume)
     {
      desc=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);
      Alert(desc);
      return(false);
     }

//--- maximal allowed volume of trade operations
   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
   if(volume>max_volume)
     {
      desc=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);
      Alert(desc);
      return(false);
     }

//--- get minimal step of volume changing
   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

   int ratio=(int)MathRound(volume/volume_step);
   if(MathAbs(ratio*volume_step-volume)>0.0000001)
     {
      desc=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",
                        volume_step,ratio*volume_step);
      Alert(desc);
      return(false);
     }
// description="Correct volume value";
   return(true);
  }
//-------------------------------------------------------------------

//----------check user input and limitations---------------
// return 1 if error is found
int CheckUserInput()
  {
   int result=0;


   if(Lots<=0)
     {
      Print("EA stopped: Check your settings. Lots must have a value.");
      //Reset=1;
      result=1;
     }



   double Minlot=MarketInfo(Symbol(),MODE_MINLOT);
   double Maxlot=MarketInfo(Symbol(),MODE_MAXLOT);
   double MarginNeeded=MarketInfo(Symbol(),MODE_MARGINREQUIRED)*Lots;
   if(Lots<Minlot)
     {
      Print("EA stopped: Lot size cant be lower than ",Minlot," or higher than ",Maxlot);
      //Reset=1;
      result=1;
     }

   if(MarginNeeded>=AccountFreeMargin() && Lots>0)
     {
      Print("EA stopped: Lot size too big for current margin.");
      //Reset=1;
      result=1;
     }

   if(IsTradeAllowed()==false || IsConnected()==false || AccountInfoInteger(ACCOUNT_TRADE_EXPERT)==false)
     {
      Print("Trading not allowed or no connection or experts not allowed");
      result=1;
     }

//------check trades limit---
   if(OrdersTotal()>=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS))
     {
      Print("Limit for orders reached.");
      result=1;
     }

   return(result);
  }
//---------------------------------
Mohamad Zulhairi Baba
16045
Mohamad Zulhairi Baba  
Tonny Obare:

I have broken down the code to the very basics so it can be publicized. It returns the error shown with automatic validation. What could be the problem?

The code follows.

Call the function in OnTick()
Tonny Obare
72593
Tonny Obare  
Mohamad Zulhairi Baba:
Call the function in OnTick()

Still same error.

Tonny Obare
72593
Tonny Obare  
Just passed validation as it is. The only mod was to remove the CheckUserInput() from controlling trades. Demoted it to only give warnings. There must be a small bug in the auto validation.
To add comments, please log in or register