EA not backtesting but not giving me any errors

 

I created this CCI and EMA EA and i compiled it and there was no error only for me to backtest it and it seems its not backtesting at the same time its not giving me any error in the journal. The EA buys when CCI is above level 0 and price is above EMA. please help guys.


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

//|                                                Mega Trend EA.mq4 |

//|                      Copyright 2019,objemmanuel Software Corp. |

//|                                         objemmanuel@gmail.com |

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

#property copyright "Copyright 2019,HamzaGhennami Software Corp."

#property link      "objemmanuel@gmail.com"

#property version   "1.00"

#property strict



int _Sell;

int _Buy;

string symbol=_Symbol;

extern int   MovingPeriod=50;

extern int    MovingShift   =0;

extern int    CciPeriod=14;                  

extern double LotSize=0.01;

extern bool AutoLotSizing=false;

extern double RiskPercent=0;

extern int TakeProfit=0;

extern int StopLoss=0;

extern bool UseTrailingStop=false;

extern int WhenToTrail=40;

extern int TrailAmount=20;

extern bool UsePartialClose=false;

extern int WhenPartialClose=40;

extern int PartialFacor=2;

extern int MagicNumber=22222;

double BSL=0,SSL=0,BTP=0,STP=0;

double pips=0;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   double ticksize=MarketInfo(symbol,MODE_TICKSIZE);

   if(ticksize==0.00001 || ticksize==0.001)

      pips=ticksize*10;

   else pips=ticksize;

   return(0);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   

  }

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

//| Expert tick function                                             |

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

void OnTick()

 {

  int signal = CheckForTrade(); 

  if(IsNewCandle())CheckForTrade(); 

  if (!CheckFilters(signal) && isUseFilters) return;

  if(UseTrailingStop)AdjustTrail(); 

  if(UsePartialClose)PartialClose();

    

  

}

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

bool IsNewCandle()

  {

   static int BarsOnChart=0;

   if(Bars==BarsOnChart)

      return (false);

   BarsOnChart=Bars;

   return(true);

  }

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

int CalculateCurrentOrders(int direction)

  { 

    if(StopLoss!=0){

      BSL=NormalizeDouble(Ask-StopLoss*pips,Digits);

      SSL=NormalizeDouble(Bid+StopLoss*pips,Digits);

         }

   if(StopLoss!=0 && AutoLotSizing){

      double pips_to_bsl=StopLoss*pips;

      double Equity=AccountEquity();

      double RiskedAmount=Equity*RiskPercent*0.01;

      LotSize=(RiskedAmount/(pips_to_bsl/pips))/10;

        }

   if(TakeProfit!=0){

      BTP=NormalizeDouble(Ask+TakeProfit*pips,Digits);

      STP=NormalizeDouble(Bid-TakeProfit*pips,Digits);

       }

   if(direction==_Buy && OpenOrdersThisPairBuy(symbol)==0 && OpenOrdersThisPairSell(symbol)==0)

     int BuyTicket=OrderSend(symbol,OP_BUY,LotSize,Ask,3,BSL,BTP,NULL,MagicNumber,0,clrGreen);

   if(direction==_Sell && OpenOrdersThisPairBuy(symbol)==0 && OpenOrdersThisPairSell(symbol)==0)

       int SellTicket=OrderSend(symbol,OP_SELL,LotSize,Bid,3,SSL,STP,NULL,MagicNumber,0,clrRed); 

   for(int b=OrdersTotal()-1;b>=0;b--)

{

    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))

      if(OrderMagicNumber()==MagicNumber && OrderSymbol()==symbol)

        {

         

         double lots= OrderLots();

         int ticket = OrderTicket();

            if(OrderType()==OP_BUY && direction==_Sell)

              {

               if(OrderClose(ticket,lots,Bid,3,clrRed))

                  return _Sell;

                  

              }

            else if(OrderType()==OP_SELL && direction==_Buy)

               if(OrderClose(ticket,lots,Ask,3,clrRed))

                  return _Buy;

                  

        }

     }

     return 0;

 }

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

int OpenOrdersThisPairBuy(string SYMBOL)

  {

   int total=0;

   for(int i=OrdersTotal()-1; i>=0; i--)

     {

      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))Print("error Selecting order ",GetLastError());

      if(OrderType()==0 && OrderMagicNumber()==MagicNumber && OrderSymbol()==SYMBOL)

       total++;

     }

   return (total);

  }

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

int OpenOrdersThisPairSell(string SYMBOL)

  {

   int total=0;

   for(int i=OrdersTotal()-1; i>=0; i--)

     {

      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))Print("error Selecting order ",GetLastError());

      if(OrderType()==1 && OrderMagicNumber()==MagicNumber && OrderSymbol()==SYMBOL)

      total++;

     }

   return (total);

  }

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

void AdjustTrail()

{

      for(int b=OrdersTotal()-1;b>=0;b--)

      {

         if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))

           if(OrderMagicNumber()==MagicNumber)

                 if(OrderSymbol()==symbol)

                       if(OrderType()==OP_BUY)

                           if(Bid-OrderOpenPrice()>WhenToTrail*pips) 

                              if(OrderStopLoss()<Bid-TrailAmount*pips || OrderStopLoss()==0)

                                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(TrailAmount*pips),OrderTakeProfit(),0,CLR_NONE))

                                   Print("error modifying buy order ",GetLastError());



         }

      for(int s=OrdersTotal()-1;s>=0;s--)

      {

         if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))

            if(OrderMagicNumber()== MagicNumber)

               if(OrderSymbol()==symbol)

                  if(OrderType()==OP_SELL)

                       if(OrderOpenPrice()-Ask>WhenToTrail*pips)

                              if(OrderStopLoss()>Ask+TrailAmount*pips || OrderStopLoss()==0)

                                 if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrailAmount*pips),OrderTakeProfit(),0,CLR_NONE))

                                    Print("error modifying sell order ",GetLastError());

         }

}

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

int CheckForTrade(){

   double x1,x2;

   

   x1 = iCCI(Symbol(),_Period,14,PRICE_TYPICAL,0);

   x2 = iCCI(Symbol(),_Period,14,PRICE_TYPICAL,1);

   if(x2>0 && x2!=EMPTY_VALUE)

               return _Buy;

   if(x1<0 && x2!=EMPTY_VALUE)

              return _Sell;

    return 0;         

 }  

 

 extern bool isUseFilters = true;

int CheckFilters (int signal) { //true - you can trade

double ma;



if(signal==_Buy) {

ma= iMA(NULL,0,MovingPeriod,MovingShift,MODE_EMA,PRICE_TYPICAL,0); }

if(signal==_Sell) {

ma= iMA(NULL,0,MovingPeriod,MovingShift,MODE_EMA,PRICE_TYPICAL,0); }               

                                              

 return true;

}

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

void PartialClose()

  {

    for(int b=OrdersTotal()-1;b>=0;b--){

      if(!OrderSelect(b,SELECT_BY_POS,MODE_TRADES))printf("unable to select an order");

         int type=OrderType();

         double lots= OrderLots();

         int ticket = OrderTicket();

         if(OrderMagicNumber()==MagicNumber && OrderSymbol()==symbol && lots!=0.01)

           {

            if(type==OP_BUY) 

             if(Bid-OrderOpenPrice()>(TotalCloseTrades(0,OrderOpenPrice())+1)*WhenPartialClose*pips)

              {

                    if(!OrderClose(ticket,lots/PartialFacor,Bid,3,clrRed))

                       printf("failure to close the order ");

              }

           if(type==OP_SELL)

            if(OrderOpenPrice()-Ask>(TotalCloseTrades(1,OrderOpenPrice())+1)*WhenPartialClose*pips)

                   if(!OrderClose(ticket,lots/PartialFacor,Ask,3,clrRed))

                      printf("failure to close the order ");      

           }

        }

     for(int b=OrdersTotal()-1;b>=0;b--){

      if(!OrderSelect(b,SELECT_BY_POS,MODE_TRADES))printf("unable to select an order"); 

         int type=OrderType();

         double lots= OrderLots();

         int ticket = OrderTicket();

         if(OrderMagicNumber()==MagicNumber && OrderSymbol()==symbol && lots==0.01)

           {

            if(type==OP_BUY) 

             if(Bid-OrderOpenPrice()>(TotalCloseTrades(0,OrderOpenPrice())+1)*WhenPartialClose*pips)

              {

                   if(!OrderClose(ticket,lots,Bid,3,clrRed))

                      printf("failure to close the order ");

              }

           if(type==OP_SELL)

            if(OrderOpenPrice()-Ask>(TotalCloseTrades(1,OrderOpenPrice())+1)*WhenPartialClose*pips)

                   if(!OrderClose(ticket,lots,Ask,3,clrRed))

                      printf("failure to close the order ");      

           }

        }

  }

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

int TotalCloseTrades(int Order,double OpenPrice){

   int Total=0;

    for(int i=OrdersHistoryTotal()-1;i>=0;i--)

       {

        if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))

         if(OrderType()==Order && OrderSymbol()==symbol && OrderOpenPrice()==OpenPrice && OrderMagicNumber()==MagicNumber)

                 Total++;             

                  }

      return(Total);   

   }     

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


This is the code thanks guys

 
objemmanuel1997:

I created this CCI and EMA EA and i compiled it and there was no error only for me to backtest it and it seems its not backtesting at the same time its not giving me any error in the journal. The EA buys when CCI is above level 0 and price is above EMA. please help guys.



This is the code thanks guys

Hello,

make sure the expert call all functions.

I see CalculateCurrentOrders function not called of code.

Maby need to add it on OnTick function to call expert and open orders.


Continue uses this code....

Files:
 
objemmanuel1997: please help guys.
  1. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
  2. int _Sell;
    int _Buy;
    
                   return _Buy;
                  return _Sell;
    What values are you returning?
 

You forgot to define the signal values _Sell and _Buy. I suggest to use it like this:

string CheckForTrade()
  {
   double x1 = iCCI(Symbol(),_Period,14,PRICE_TYPICAL,0);
   double x2 = iCCI(Symbol(),_Period,14,PRICE_TYPICAL,1);

   if(x2>0 && x2!=EMPTY_VALUE)

               return "Buy";

   if(x1<0 && x2!=EMPTY_VALUE)

              return "Sell";

   return "";
  }

And check with if(signal=="Buy") etc.

Then you need to watch your conditions, that does not look right, at least the place where you mixed x1 and x2 is buggy.

 
lippmaje:

You forgot to define the signal values _Sell and _Buy. I suggest to use it like this:

And check with if(signal=="Buy") etc.

Then you need to watch your conditions, that does not look right, at least the place where you mixed x1 and x2 is buggy.

its telling me implicit conversion from string to number

 
William Roeder:
  1. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
  2. What values are you returning?

int _Sell= -1;

int _Buy = 1;

 
Nikolaos Pantzos:

Hello,

make sure the expert call all functions.

I see CalculateCurrentOrders function not called of code.

Maby need to add it on OnTick function to call expert and open orders.


Continue uses this code....

Thanks a lot for this i appreciate but i hasve one more issue with this code, the closing condition is supposed to be with CCI not EMA that is it closes a trade when the CCI crosses 0 level back down in a buy trade or when it crosses zero to the upside in a sell trade, but the code here it closes the trade when the price crosses the EMA. The EA opens a trade with confirmation from CCI and EMA but closes the trade with confirmation from only CCI then when the CCI crosses again and the EMA still aligns it takes another trade. I would really appreciate if you can modify thanks a lot

 
objemmanuel1997:

its telling me implicit conversion from string to number

Yes, you were supposed to use string signal instead of int signal, I didn't mention that extra. It's just a proposal of course, you can stick with your numbers (-1,1) if you want.
 
objemmanuel1997:

Thanks a lot for this i appreciate but i hasve one more issue with this code, the closing condition is supposed to be with CCI not EMA that is it closes a trade when the CCI crosses 0 level back down in a buy trade or when it crosses zero to the upside in a sell trade, but the code here it closes the trade when the price crosses the EMA. The EA opens a trade with confirmation from CCI and EMA but closes the trade with confirmation from only CCI then when the CCI crosses again and the EMA still aligns it takes another trade. I would really appreciate if you can modify thanks a lot

Hello,

I've deleted some function because I can't to understand how it works.

Please check all operations of experts.

You can to continue with this code....

Files:
 
Nikolaos Pantzos:

Hello,

I've deleted some function because I can't to understand how it works.

Please check all operations of experts.

You can to continue with this code....

Hello Nikolaos I really appreciate it is working well now. Im trying to convert it to mt5 EA but it is giving me serious issues do you have any idea on that?

 
objemmanuel1997:

Hello Nikolaos I really appreciate it is working well now. Im trying to convert it to mt5 EA but it is giving me serious issues do you have any idea on that?

The open and close orders functions are totally different in MT5.

And way to call and uses indicators have different.

Not simple to convert mq4 to mq5 a code.

Reason: