EA problem - doesn`t trade

To add comments, please log in or register
Stanislav Ivanov
6607
Stanislav Ivanov  

Hi guys,

Im having some trouble with an EA in MT4. Its just a basic one, but when started in the tester, doesn`t trade at all. This is the code. Thank you in advance!

//+------------------------------------------------------------------+
//|                                                    Stan 4.0.mq4 |
//|                                                 Stanislav Ivanov |
//|                                                             NONE |
//+------------------------------------------------------------------+
#property copyright "Stanislav Ivanov"
#property link      "NONE"
#property version   "4.0"
#property description "My first EA Hope you like it :))" 

extern string mystring= "This is the Stan 5 Expert Advisor .";
extern string mystr = "Please note that all EA parameters are optimized!";
input double TakeProfit    =60;
input bool autolot = false;
input double manuallot = 0.1;
extern double LotPercent = 2.0;
input double Stoploss = 50 ;
double point = 0;
input int magic = 1122; 
double LotSize;
//input double pip_bands_diff_Set = 400; 
double upper_band_0;
double lower_band_0;
//===================================================================================================//
bool AreThereOrders ()
{
if (OrdersTotal()<1)
{
return (false);
}
else if (OrdersTotal()>0)
{

int i;

for (i=OrdersTotal()-1; i>=0; i--)
   {
   if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   continue;
   if( OrderMagicNumber()== magic && OrderSymbol()==Symbol()) 
   {
   return (true);
   } 
  }       
   }
   return (false) ;   
   }
//--------------------------------------------------------------------------------------------------------//
void OnInit(void)
{
  if(autolot)
  {
   if(MarketInfo(Symbol(),MODE_MINLOT) == 0.1) int LotsDigit = 1;
   else if(MarketInfo(Symbol(),MODE_MINLOT) == 0.01) LotsDigit = 2;
   double MinLots = NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT),LotsDigit);
   double MaxLots = NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT),LotsDigit);
   double AcFrMar = NormalizeDouble(AccountFreeMargin(),2);
   
   LotSize = (AcFrMar*(LotPercent/100))/1000;
   
   if(LotSize > MaxLots) LotSize = MaxLots;
   if(LotSize < MinLots) LotSize = MinLots; 
   }
   else LotSize = manuallot;
   
}
void OnTick(void)
  {
   
   point = Point;
   if (Digits==3 || Digits==5) point *= 10;
   int    ticket;
   
upper_band_0 = iBands(NULL,0,20,2,0,PRICE_TYPICAL,MODE_UPPER,0);
lower_band_0 = iBands(NULL,0,20,2,0,PRICE_TYPICAL,MODE_LOWER,0);
//double bands_diffPIP = (upper_band_0-lower_band_0)*10 ;  */     
      //--- check for long position (BUY) possibility
if(!AreThereOrders() && cci_buy() && candle_buy() && Volume[1]>Volume[0])
        {
         ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,5,Ask - Stoploss*point,Ask + TakeProfit*point,NULL,magic,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening BUY order : ",GetLastError());
         return;
        }
        
      //--- check for short position (SELL) possibility
if(!AreThereOrders() && cci_sell() && candle_sell() &&  Volume[1]>Volume[0])
        {
         ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid , 3 , Bid + Stoploss*point , Bid - TakeProfit*point , NULL , magic,0);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening SELL order : ",GetLastError());
        }
     }// End of OnTick function
     
//==========================================================================================
 bool candle_buy()
 {
if(Low[0]<lower_band_0 && Ask>Open[0]&& Open[1]>Close[1] && Ask>lower_band_0 && Ask>Open[1] ) return true;
 else
 return false;
 }
 //=========================================================================================
  bool candle_sell()
 {
if(High[0]>upper_band_0 && Bid<Open[0]&& Open[1]<Close[1] && Bid<upper_band_0 && Bid<Close[1] ) return true;
 else
 return false;
 }
 //=========================================================================================
 bool cci_buy()
 {
 double cci[];
 for(int i=0;i<3;i++)
 {
 cci[i]=iCCI(NULL,0,14,PRICE_TYPICAL,i);
 if(cci[1]<cci[0] && cci[1]<-100 && cci[2]>cci[1] && cci[0]>cci[1]) return true;
 }
 return false;
 }
 //==========================================================================================
  bool cci_sell()
 {
 double cci[];
 for(int i=0;i<3;i++)
 {
 cci[i]=iCCI(NULL,0,14,PRICE_TYPICAL,i);
 if(cci[1]>cci[0] && cci[1]>100 && cci[2]<cci[1] && cci[0]<cci[1]) return true;
 }
 return false;
 }
William Roeder
20228
William Roeder  
  1.  double cci[];
     for(int i=0;i<3;i++)
     {
     cci[i]=iCCI(NULL,0,14,PRICE_TYPICAL,i);
     if(cci[1]>cci[0] && cci[1]>100 && cci[2]<cci[1] && cci[0]<cci[1]) return true;
     }
    Your arrays have no size, therefor you can't store into them. You would know that had you used strict. Always use strict. Fixing the warnings will save you hours of debugging.
              Program Properties (#property) - Preprocessor - Language Basics - MQL4 Reference
  2. Fill your arrays completely before examining them.
  3. Simplify your code. Increase Order after stoploss - MQL4 and MetaTrader 4 - MQL4 programming forum № 3

  4.  Volume[1]>Volume[0])
    This condition will always be true at the start of a new bar.

  5. if (OrdersTotal()<1)
    {
    return (false);
    }
    else if (OrdersTotal()>0)
    Unnecessary. If there are no orders, the for loop exits immediately.
To add comments, please log in or register