PLEASE could someone tell me how to make it work as i want to

 

Hello world. I just tried to write a simple trend following EA based on bolinger bands ... How ever it does not execute trades where i really want to ... Could someone tell me how to change it....

For Long position entry i use the Bolinger bands (Price LOW) and for Short positions i use the Bolinger bands (Price HIGH) .

Exiting rule is when the Candle comes back inside the Bolinger bands (Price CLOSE) .

When the candle goes over the Upper Bolinger bands (Price LOW) take Long position the next candle that closes above the upper Bolinger bands.

When the candle goes down the Lower Bolinger bands ( Price HIGH) take Short position the next candle that closes below the Lower Bolinger bands.

That simple but ..... I tried to write it but ..... if someone could check it out would really help me. Thanks. ERICMAN

#define MAGIC  4649        

//parameter

extern double Lots = 1.0;                                  
extern int Slip = 10;                                    
extern string Comments =  "";  
extern int Band_p = 18;
extern int TP = 50;   
extern int LC = 20;  


//variables
int Ticket_L = 0;                                     
int Ticket_S = 0;                                      
int Exit_L = 0;                                                        
int Exit_S = 0;                                                        




int start()
  {
     
      // BBands
   double bands_upper1 = iBands(NULL, 0, Band_p,1, 0, PRICE_LOW, MODE_UPPER, 1);
   double bands_upper2 = iBands(NULL, 0, Band_p,1, 0, PRICE_LOW, MODE_UPPER, 2);
   
   double bands_lower1 = iBands(NULL, 0, Band_p,1, 0, PRICE_HIGH, MODE_LOWER, 1);
   double bands_lower2 = iBands(NULL, 0, Band_p,1, 0, PRICE_HIGH, MODE_LOWER, 2);
   
   double bands_close1 = iBands( NULL, 0, Band_p,1, 0, PRICE_CLOSE, MODE_UPPER,1);
   double bands_close2 = iBands(NULL, 0, Band_p,1, 0, PRICE_CLOSE, MODE_UPPER,2);
   
   double bands_close3 = iBands( NULL, 0, Band_p,1, 0, PRICE_CLOSE, MODE_LOWER,1);
   double bands_close4 = iBands(NULL, 0, Band_p,1, 0, PRICE_CLOSE, MODE_LOWER,2);
   

    OrderSelect(Ticket_L, SELECT_BY_TICKET);
   
   if(    OrderOpenPrice() + TP * Point <=  Bid
       && ( Ticket_L != 0 && Ticket_L != -1 ) )
    {    
      Exit_L = OrderClose(Ticket_L,Lots,Bid,Slip,Red);
      if( Exit_L ==1 ) {Ticket_L = 0;}
    }       
    
   //short position take profit
   OrderSelect(Ticket_S, SELECT_BY_TICKET);
   
   if(    OrderOpenPrice() - TP * Point >=  Ask
       && ( Ticket_S != 0 && Ticket_S != -1 ) )
    {    
      Exit_S = OrderClose(Ticket_S,Lots,Ask,Slip,Blue);
      if( Exit_S ==1 ) {Ticket_S = 0;}
    }      
    
  //buy position take profit
   OrderSelect(Ticket_L, SELECT_BY_TICKET);
   
   if(    OrderOpenPrice() -LC * Point >=  Bid
       && ( Ticket_L != 0 && Ticket_L != -1 ) )
    {    
      Exit_L = OrderClose(Ticket_L,Lots,Bid,Slip,Red);
      if( Exit_L ==1 ) {Ticket_L = 0;}
    }       
    
   //short position loss cut
   OrderSelect(Ticket_S, SELECT_BY_TICKET);
   
   if(    OrderOpenPrice() + LC * Point <=  Ask
       && ( Ticket_S != 0 && Ticket_S != -1 ) )
    {    
      Exit_S = OrderClose(Ticket_S,Lots,Ask,Slip,Blue);
      if( Exit_S ==1 ) {Ticket_S = 0;}
    }      
     
   //Long position exit when it comes back to the bolinger bands
   if(    Close[2]> bands_close2 && Close[1]<bands_close1
       && ( Ticket_L != 0 && Ticket_L != -1 ))
    {     
      Exit_L = OrderClose(Ticket_L,Lots,Bid,Slip,Red);
      if( Exit_L ==1 ) {Ticket_L = 0;}
    }    
    
   //Short position exit when comes back to the bolinger bands
   if(    Close[2]< bands_close4 && Close[1]> bands_close3
       && ( Ticket_S != 0 && Ticket_S != -1 ))
    {     
      Exit_S = OrderClose(Ticket_S,Lots,Ask,Slip,Blue);
      if( Exit_S ==1 ) {Ticket_S = 0;} 
    }   
    
   //Long entry 
   if(   bands_upper2 < bands_upper1 && Close[2] <= bands_upper2 && Close[1] > bands_upper1
       && ( Ticket_L == 0 || Ticket_L == -1 ) 
       && ( Ticket_S == 0 || Ticket_S == -1 ))
    {  
      Ticket_L = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slip,0,0,Comments,MAGIC,0,Red);
    }
    
   //Short entry 
  if( bands_lower2 > bands_lower1 && Close[2] >= bands_lower2 && Close[1] < bands_lower1
       && ( Ticket_S == 0 || Ticket_S == -1 )
       && ( Ticket_L == 0 || Ticket_L == -1 ))
    {   
      Ticket_S = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slip,0,0,Comments,MAGIC,0,Blue);     
    } 
    
     
   return(0);
  }
 
ericman:

Hello world. I just tried to write a simple trend following EA based on bolinger bands ... How ever it does not execute trades where i really want to ... Could someone tell me how to change it....

For Long position entry i use the Bolinger bands (Price LOW) and for Short positions i use the Bolinger bands (Price HIGH) .

Exiting rule is when the Candle comes back inside the Bolinger bands (Price CLOSE) .

When the candle goes over the Upper Bolinger bands (Price LOW) take Long position the next candle that closes above the upper Bolinger bands.

When the candle goes down the Lower Bolinger bands ( Price HIGH) take Short position the next candle that closes below the Lower Bolinger bands.

That simple but ..... I tried to write it but ..... if someone could check it out would really help me. Thanks. ERICMAN

The first time you run your EA your OrderSelect() and OrderOpenPrice() calls are all going to generate errors, why don't you check to see if you have any orders to close before trying to select a non existing Order ?

What are Function return values ? How do I use them ?

 

See my comments below, there are many errors. Refer documentation or see other EAs in codebase.


    OrderSelect(Ticket_L, SELECT_BY_TICKET); //>> if there are no open orders then any call to orderselect & orderopenprice will generate error.
   
   Ticket_L = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slip,0,0,Comments,MAGIC,0,Red); //>> what if ordersend fails.?? where error handling.??