Download MetaTrader 5

Code correct but wrong signal random error

To add comments, please log in or register
fly7680
567
fly7680  
I wrote this code with two function. As soon as I have an cross of moving averages between two candles, step to the next step where I have a for loop, which identifies a Heiken candles pattern. the signal is correct, but if I have an cross of moving averages between two candles, without the pattern true .... I sent orders for no apparent reason.

The code looks okay but I do not understand what is wrong.

int Step  = 0;
int Step1 = 0;

int X    = 1;
int Y    = 1;

int TicketB;
int TicketS;


extern int StopLoss   = 200;
extern int TakeProfit = 200;




//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   TicketB = -1;
   TicketS = -1;

  
    
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {      
  
  if(NewBar())
               {            
                    FunzioneSELL();
                    FunzioneBUY();
               }}
  
//+------------------------------------------------------------------+


//--------------------------------------------------------------------------------------------------------------------------------------+
//                                                Condition SELL                                                                        |    
//--------------------------------------------------------------------------------------------------------------------------------------+
//Condizione SELL 1
void FunzioneSELL (){
    
     if(Step == 0
        && iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 2) > iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 2) // Media exp 6 > Media Ssempl 12
        && iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 1) // Media exp 6 < Media Ssempl 12
        && iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, PERIOD_CURRENT,  6, 0, MODE_EMA, PRICE_CLOSE, 2) // Media exp 6 Candela1 < Media exp 6 Candela2
        && iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, PERIOD_CURRENT,  12,0, MODE_SMA, PRICE_CLOSE, 1) // Media exp 6 Candela1 < Media exp 6 Candela2
        &&(iMA(NULL, PERIOD_CURRENT, 12,0, MODE_SMA, PRICE_CLOSE, 1) - iMA(NULL, PERIOD_CURRENT,  6, 0, MODE_EMA, PRICE_CLOSE, 1))> 2*Point // Media exp 6 Candela1 < Media exp 6 Candela2
        )

     {
      Print("Primo Step_Sell");
      ObjectCreate(0,"IncrocioMM Sell"+ IntegerToString(X),OBJ_ARROW_CHECK,0,TimeCurrent(),High[0] + 200*Point());
      Step++;  
      X++;    
     }
    
     if(Step == 1 )
      
      {
      
      int l;
            
      for(l=0; l<4; l++){      
      
        if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+l) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+l) // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+l) // Green Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+l) // Green Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+l))// Green Heiken          
          
        {
         TicketS = OrderSend(Symbol(),OP_SELL,0.10,Bid,0,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Ingresso Sell",0,0,clrRed);
         if(TicketS < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError()));  
         else Print("Ordine SELL inviato correttamente");
         Step = 0;
         l = 4;  
        }
        
        else if(l>=4) Step = 0;
       }
      }
     }
    
    
    
    


//--------------------------------------------------------------------------------------------------------------------------------------
bool NewBar(){

static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return (false);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------+
//                                                Condition BUY                                                                         |    
//--------------------------------------------------------------------------------------------------------------------------------------+
//Condizione BUY 1
void FunzioneBUY (){

     if(Step1 == 0
        && iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 2) < iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 2) // Media exp 6 > Media Ssempl 12
        && iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 1) // Media exp 6 < Media Ssempl 12
        && iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, PERIOD_CURRENT,  6, 0, MODE_EMA, PRICE_CLOSE, 2) // Media exp 6 Candela1 < Media exp 6 Candela2
        && iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 1) // Media exp 6 Candela1 < Media exp 6 Candela2
        &&(iMA(NULL, PERIOD_CURRENT, 6, 0, MODE_EMA, PRICE_CLOSE, 1) - iMA(NULL, PERIOD_CURRENT, 12, 0, MODE_SMA, PRICE_CLOSE, 1))> 2*Point // Media exp 6 Candela1 < Media exp 6 Candela2
        )

     {
      Print("Primo Step_BUY");
      ObjectCreate(0,"IncrocioMM Buy"+ IntegerToString(Y),OBJ_ARROW_CHECK,0,TimeCurrent(),High[0] + 200*Point());
      Step1++;  
      Y++;    
     }
    
     if(Step1 == 1  )      
      {      
      
      int m;
      
      for(m=0; m<4; m++){      
      
        if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+m) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+m)  // Green Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+m) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+m)  // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+m) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+m)  // Red Heiken
        && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+m) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+m)) // Red Heiken  
          
          
        {
         TicketB = OrderSend(Symbol(),OP_BUY,0.10,Ask,0,Ask-StopLoss*Point,Bid+TakeProfit*Point,"Ingresso Buy",0,0,clrGreen);
         if(TicketB < 0) Print("Errore invio ordine:"+IntegerToString(GetLastError()));         
         else Print("Ordine BUY inviato correttamente");
         Step1 = 0;
         m = 4;  
        }
        
        else if(m>=4) Step1 = 0;
       }
      }
     }
//--------------------------------------------------------------------------------------------------------------------------------------
fly7680
567
fly7680  
It can be a conflict between more orders? 
Alain Verleyen
Moderator
31473
Alain Verleyen  
fly7680:
It can be a conflict between more orders? 
   if(Step==1)

     {

      int l;

      for(l=0; l<4; l++)
        {

         if(iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 1+l) > iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 1+l) // Red Heiken
            && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 2+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 2+l) // Green Heiken
            && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 3+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 3+l) // Green Heiken
            && iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 2, 4+l) < iCustom(Symbol(), PERIOD_CURRENT, "Heiken Ashi", Red, White, Red, White, 3, 4+l))// Green Heiken          

           {
            TicketS=OrderSend(Symbol(),OP_SELL,0.10,Bid,0,Ask+StopLoss*Point,Bid-TakeProfit*Point,"Ingresso Sell",0,0,clrRed);
            if(TicketS<0) Print("Errore invio ordine:"+IntegerToString(GetLastError()));
            else Print("Ordine SELL inviato correttamente");
            Step=0;
            l=4;
           }

         else if(l>=4) Step=0;
        }
     }
  }
This reset of the Step variable should be outside the loop. It's never executed.
fly7680
567
fly7680  
Thanks Alain Verleyen, that was the bug, now it works!
To add comments, please log in or register