Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 735

 

Mql5 guys, can you help me make a request to close a position in a hedge account? Situation: opened BUY position, we need to close it with Sell Limit. Question: How do I correctly write a request, to close exactly chosen position, and not to open new SELL locking BUY? Please write a piece of code with comments! Thank you in advance!

 

Hello. Help, please.

'LastDir' - illegal switch expression type


double LastDir[];
//............... 
switch(LastDir[i]){
                           case 0:
                              if(High[i]>LastHighValue[i]){
                                 LastHighValue[i]=High[i];
                                 LastHighTime[i]=Time[i];
                                 LastDir[i]=1;  
                                 ZZ[i]=High[i];                            
                                 PEACK[i]=High[i];
                              }   
                           break;                            
                           case 1:
                              if(High[i]>LastHighValue[i]){
                                 int lhb=iBarShift(NULL,0,LastHighTime[i],false);
                                 ZZ[lhb]=0;
                                 PEACK[lhb]=0;
                                 LastHighValue[i]=High[i];
                                 LastHighTime[i]=Time[i];
                                 LastDir[i]=1;  
                                 ZZ[i]=High[i];                            
                                 PEACK[i]=High[i];
                              }                           
                           break;
                           case -1:
                              if(High[i]>=LastLowValue[i]+Point*ZZReverse){
                                 LastHighValue[i]=High[i];
                                 LastHighTime[i]=Time[i];
                                 LastDir[i]=1;  
                                 ZZ[i]=High[i]; 
                                 PEACK[i]=High[i];                            
                              }
                        }
 
bij:

Hello. Could you please help me?


The switch statement must be of integer type

 
Alekseu Fedotov:

The switch statement must be of integer type

But"LastDir" is a buffer, it cannot be int.

How to get out of this situation?

 
bij:

But"LastDir" is a buffer, it cannot be int.

How to get out of this situation?

Do away withthe switch operator,

work with the if() operator

 

There is no certainty that this is equivalent to the first case

                       if(LastDir[i]==0)
 
@Alekseu Fedotov, thanks, it works) In 2009the switch could have been double)
 
bij:
@Alekseu Fedotov, thanks, it works) In 2009the switch could have been double)

it was always only a whole )

 
bij:

Hello. Help, please.


If the buffer can only contain integer values represented as double 0.0, 1.0 and -1.0 I would check this option

double LastDir[];
//............... 
switch((int) LastDir[i]){
                           case 0:
                              if(High[i]>LastHighValue[i]){
                                 LastHighValue[i]=High[i];
                                 LastHighTime[i]=Time[i];
                                 LastDir[i]=1;  
                                 ZZ[i]=High[i];                            
                                 PEACK[i]=High[i];
                              }   
                           break;                            
                           case 1:
                              if(High[i]>LastHighValue[i]){
                                 int lhb=iBarShift(NULL,0,LastHighTime[i],false);
                                 ZZ[lhb]=0;
                                 PEACK[lhb]=0;
                                 LastHighValue[i]=High[i];
                                 LastHighTime[i]=Time[i];
                                 LastDir[i]=1;  
                                 ZZ[i]=High[i];                            
                                 PEACK[i]=High[i];
                              }                           
                           break;
                           case -1:
                              if(High[i]>=LastLowValue[i]+Point*ZZReverse){
                                 LastHighValue[i]=High[i];
                                 LastHighTime[i]=Time[i];
                                 LastDir[i]=1;  
                                 ZZ[i]=High[i]; 
                                 PEACK[i]=High[i];                            
                              }
                        }
Also, I would put case -1 before case 0
 
Alexey Viktorov:

If the buffer can only contain integer values represented as double 0.0, 1.0 and -1.0 I would check this option

Also, I'd put case -1 before case 0.

Here -1 should probably be set as default : especially since there is no default case

Although ... You'd have to be an author to know which one would be better to set as default.