Expert unwanted closes

 

I made a simple free expert for a local trader community. However some users keep reporting unwanted position closings.

The code is simple and after various re-checking (and checking user behaviour and settings) I ha heve no idea why could it do the closings.

The expert watches Supertrend indicator. When eg. a long position is open, and Supertrend would turn against the position by changing direction to red, then the expert shall close the open long position. Hwever, users report that sometimes at bar close the expert closes positions while Supertrend not changing direction.

I even applied a line excluding the first ticks, but some still have problems using the expert. Not all, but some of them. User faults? Brokerage input falults? Or is there thing to improve?

//+------------------------------------------------------------------+
//|                                              Supertrend stop.mq4 |
//|                                                   Kállai Szilárd |
//|                                                                  |
//+------------------------------------------------------------------+

extern bool AKTIV = true;
extern string Supertrend_Bemeno = "Supertrend input:";
extern int Nbr_Periods = 10;
extern double Multiplier = 3.0; 
extern string gyertya1 = "Which candle to watch?";
extern string gyertya0 = "1: last closed, 0: current";
extern int gyertya =1;
extern string perc0 = "Supertrendet minute timeframe?";
extern string perc1 = "pl: M5=5, H1=60, H4=240 stb. ";
extern int perc = 60;

int start(){

if (gyertya == 1 && (Volume[0] >4 || Volume[0]==0 || Volume[0]==1)) return;  

if (AKTIV == false) return;
int _ordertype;// order type  
int _ticket; // ticket number
double orderlots, closelots, StopLoss;
int orders = OrdersTotal();
int bmod;


double testHa = iCustom(NULL,perc,"SuperTrend",Nbr_Periods,Multiplier,0,gyertya);
double testHa1 = iCustom(NULL,perc,"SuperTrend",Nbr_Periods,Multiplier,1,gyertya);
   if (testHa1>Bid*2) testHa1=0;
   if (testHa>Bid*2) testHa=0;
   

double STOPLEVEL = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;

 for (int g=orders;g>=0;g--)
    {  //#for loop
     if(OrderSelect(g,SELECT_BY_POS))
     {  //#if orderselect
     if (OrderSymbol() != Symbol()) continue;
         _ordertype=OrderType();
          _ticket=OrderTicket();   
         orderlots = OrderLots();   
     // only buys 
    
    switch(_ordertype)
               {  //# switch
     
          case OP_BUY:
          
        if (testHa <testHa1)   // Ha Supertrend szembe jön, zárunk
        OrderClose(_ticket,orderlots,Bid,30,Blue);
      
          case OP_SELL:
        
        
     // only sells 
     if (testHa >testHa1) // Ha Supertrend szembe jön, zárunk
        OrderClose(_ticket,orderlots,Ask,30,Red);
     
       
                 
               }    //# switch zárás
       
     
     
     }  // if orderselect zárás

  } // for loop zárás



return(0);
}

Thank you for checking!

 
Szilar:

I made a simple free expert for a local trader community. However some users keep reporting unwanted position closings.

The code is simple and after various re-checking (and checking user behaviour and settings) I ha heve no idea why could it do the closings.

The expert watches Supertrend indicator. When eg. a long position is open, and Supertrend would turn against the position by changing direction to red, then the expert shall close the open long position. Hwever, users report that sometimes at bar close the expert closes positions while Supertrend not changing direction.

I even applied a line excluding the first ticks, but some still have problems using the expert. Not all, but some of them. User faults? Brokerage input falults? Or is there thing to improve?

Thank you for checking!

I think the problem is your switch . . . you need to break once you have completed the code for the OP_BUY case . . . otherwise the code for the OP_SELL case is also executed . . .

Read the switch example code . . .

 

THank you RaptorUK

I applied a break; before case OP_SELL:

Let met get it tested.

Reason: