EA stop loss don´t work

 

Hi to everyone, I´ve made a EA that close orders when it reaches a number of pips like a stop loss but with OrderClose. The problem is the EA works good when I have Manual Confirmation on, but when I have off don´t work and I don´t know why don´t work automatically. I left you the code, if you can help me, thanks!!!

 

extern int StopLoss=28;

extern double lotes=1;


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


void start() 

{

   

   int i,Total;

   double order;     

   double openprice;

   double pricebuy;

   double pricesell;

   string simbolo="DE.30";

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


  Total=OrdersTotal();

  if(Total>0)

  {

     for(i=Total-1; i>=0; i--) 

     {

        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true) 

        order=OrderTicket();

        openprice=OrderOpenPrice();

        pricesell=Ask>=openprice+StopLoss*Point;

        pricebuy=Bid<=openprice-StopLoss*Point;

        {

           if(OrderType()==OP_SELL) 

           {

              if(Ask>=openprice+StopLoss*Point)OrderClose (order,lotes,pricesell,0, CLR_NONE) ;

             

           }

           if(OrderType()==OP_BUY) 

           {

              if(Bid<=openprice-StopLoss*Point)OrderClose (order,lotes,pricebuy,0, CLR_NONE) ;

           } 

        }

     }

  }

}


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

 

Hi Borja

Your pricesell and pricebuy variables are wrong. But besides that, if you are going to close the order at market then just close at the BID or ASK (depending if it's a buy or sell order). Also good to add some slippage in there in case the market moves while your EA is processing. What error number are you getting in the journal?

p.s. If you're going to post code, use the SRC button. Much easier to read ;)

Cheers
Filter

 

Hi Filter, thanks for your answer. Why are the pricesell and pricebuy variables wrong? The journal don´t say error number  :( I post the code again like you said to me :) 

 

 

extern int StopLoss=28;
extern int TakeProfit=200;
extern double lotes=1;

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

void start() 
{
   
   int i,Total;
   double order;     
   double openprice;
   double pricebuy;
   double pricesell;
   string simbolo="DE.30";
//+------------------------------------------------------------------+

  Total=OrdersTotal();
  if(Total>0)
  {
     for(i=Total-1; i>=0; i--) 
     {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true) 
        order=OrderTicket();
        openprice=OrderOpenPrice();
        pricesell=Ask>=openprice+StopLoss*Point;
        pricebuy=Bid<=openprice-StopLoss*Point;
        {
           if(OrderType()==OP_SELL) 
           {
              if(Ask>=openprice+StopLoss*Point)OrderClose (order,lotes,pricesell,0, CLR_NONE) ;
             
           }
           if(OrderType()==OP_BUY) 
           {
              if(Bid<=openprice-StopLoss*Point)OrderClose (order,lotes,pricebuy,0, CLR_NONE) ;
           } 
        }
     }
  }
}

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

this is error.

        pricesell=Ask;//>=openprice+StopLoss*Point;
        pricebuy=Bid;//<=openprice-StopLoss*Point;
 

Hi Borja

The way you have written those two variables (pricesell & pricebuy) will result in a True or False result, not a price. As you've declared them as doubles, this means they will be equal to either 0.0 or 1.0.

To check it yourself, put a print statement after you have calculated those two variables like this:

pricesell=Ask>=openprice+StopLoss*Point;
pricebuy=Bid<=openprice-StopLoss*Point;
Print("pricesell: ",pricesell);
Print("pricebuy: ",pricebuy);

 Run it through the tester and check the journal. You will see the values are either 1.0 or 0.0

As I said before, just close them at the market price. So something like:

 if(Ask>=openprice+StopLoss*Point)OrderClose (order,lotes,Ask,0, CLR_NONE) ;

 I'd also put slippage in there instead of zero and also, use the lot size from the order, not from your variable (  OrderLots() ).

Hope that helps mate

Cheers
Filter

 

Thanks to all for your help. Now it works! :) I share the final code with all if you want to use. Filter thanks, I saw the result true/false and you are right. I´ve changed all the things that you´ve said and now it works. 

 

Thanks for all :)

 

extern int StopLoss=28;

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

void start() 
{
   
   int i,Total;
   double order;     
   double openprice;
   
//+------------------------------------------------------------------+

  Total=OrdersTotal();
  if(Total>0)
  {
     for(i=Total-1; i>=0; i--) 
     {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true) 
        order=OrderTicket();
        openprice=OrderOpenPrice();
        
        {
           if(OrderType()==OP_SELL) 
           {
              if(Ask>=openprice+StopLoss*Point)OrderClose (order,OrderLots(),Ask,2, CLR_NONE) ;
             
           }
           if(OrderType()==OP_BUY) 
           {
              if(Bid<=openprice-StopLoss*Point)OrderClose (order,OrderLots(),Bid,2, CLR_NONE) ;
           } 
        }
     }
  }
}

//+------------------------------------------------------------------+
 
Very welcome mate, glad to be of service :)

Cheers
Filter