Function firing when it shouldn't

 

Alright, I started a new thread because it's a different topic.

My EA uses an indicator to determine a bias toward long or short. When the bias changes it fires the order function. This is the only place that the order function is called in my code. However, I have found that several times the order function fires even though the bias function hasn't. . .

Now, I'm not sure just how much of my code I should post. If I should just post the bias change function or what. So, I'll start with just that function, if I should post more please let me know. . . I am a little afraid to post it because I know how amateurish it is.

void UpdateBiasLevel(){
   
   if(BarCount >= 2){       
      switch(CurBias){
         case 0: CurBias = 1; BarCount = 0; PlaceOrder(CurBias, Lots, StopLoss, MaxSlippage, MaxSpread, MagicNumber);
         case 1: CurBias = 0; BarCount = 0; PlaceOrder(CurBias, Lots, StopLoss, MaxSlippage, MaxSpread, MagicNumber);
      }      
      ObjectCreate("BiasChange"+Objects,OBJ_VLINE,0,GetLastBarTime(),0);
      Objects++;                  
   }        
          
   CurBiasLevel = GetBiasLevel(CurBias);   
   ObjectDelete("BiasLine");
   ObjectCreate("BiasLine",OBJ_HLINE,1,0,CurBiasLevel);   
}
 
Tristen Shaw: that several times the order function fires even though the bias function hasn't. . .

Then your “bias function” is broken.

Use the debugger or print out your variables, including _LastError and prices and href="//www.mql5.com/en/forum/157394#comment_3806599">find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We can't see your broken code.

 
William Roeder #:

Then your “bias function” is broken.

Use the debugger or print out your variables, including _LastError and prices and href="//www.mql5.com/en/forum/157394#comment_3806599">find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We can't see your broken code.

No, as a matter of fact, I do not expect you to debug my code for me.

I expect you to understand that when someone is new the don't know what they don't know.I'm looking for some guidance and am more than willing to learn and put in the work myself. Please, tell me the etiquette for posting for help. Should I only ask if I've already figured it out myself?

I did not just hit this problem and automatically post for help. I spent time looking at all of my code, going through it line by line. I put print statements between various parts and in different functions to follow the variables. I've even rewritten the function from scratch.

And I know that you can't see my broken code, and that you are not mind readers. As I said, I didn't know how much to post. So I posted the immediate function and asked if more was needed. It seems like you can't win when it comes to posting code. One person treats you like crap cause you posted too much, another treats you like crap cause you didn't post enough.

Now on the other hand I have not used the debugger because I am self taught and don't know how to use it and didn't even know it was there.

 
The case clause in your switch is missing a break at the end.

So if the first gets executed, the second case is also executed.

This is not your intention.

Add a break at the end of each case clause.
 
Tristen Shaw:Alright, I started a new thread because it's a different topic. My EA uses an indicator to determine a bias toward long or short. When the bias changes it fires the order function. This is the only place that the order function is called in my code. However, I have found that several times the order function fires even though the bias function hasn't. . . Now, I'm not sure just how much of my code I should post. If I should just post the bias change function or what. So, I'll start with just that function, if I should post more please let me know. . . I am a little afraid to post it because I know how amateurish it is.

Option 1:

switch( CurBias ){
         case 0: CurBias = 1; BarCount = 0; PlaceOrder( CurBias, Lots, StopLoss, MaxSlippage, MaxSpread, MagicNumber ); break;
         case 1: CurBias = 0; BarCount = 0; PlaceOrder( CurBias, Lots, StopLoss, MaxSlippage, MaxSpread, MagicNumber ); break;
      };  

Option 2:

switch( CurBias ){
         case 0:
         case 1: CurBias = 1 - CurBias; BarCount = 0; PlaceOrder( CurBias, Lots, StopLoss, MaxSlippage, MaxSpread, MagicNumber );
      }; 

Option 3:

if( CurBias == 0 || CurBias == 1 ) {
        CurBias = 1 - CurBias; BarCount = 0; PlaceOrder( CurBias, Lots, StopLoss, MaxSlippage, MaxSpread, MagicNumber );
};

This however, assumes that the variable "CurBias" is an integer type and not a floating point type. If it is a "double", then none of the above will work.

Please confirm for us, the data-type of the "CurBias" variable.

 
Fernando Carreiro #:

Option 1:

Option 2:

Option 3:

This however, assumes that the variable "CurBias" is an integer type and not a floating point type. If it is a "double", then none of the above will work.

Please confirm for us, the data-type of the "CurBias" variable.

It is indeed an integer, which 0 representing a long bias and 1 representing a short bias.

 
Fernando Carreiro #:

Option 1:

Option 2:

Option 3:

This however, assumes that the variable "CurBias" is an integer type and not a floating point type. If it is a "double", then none of the above will work.

Please confirm for us, the data-type of the "CurBias" variable.

I have since reworked my code. I tried to analyze your example code and see if I could adapt some of it to my own coding style. I really do appreciate that you take the time to patiently help a new coder who is trying to learn.

Reason: