Static Variable issue

 

Problem: (2nd) Trade  re enters straight away when the Stop Loss or Take Profit is hit even though the " xcross " for the entry signal/criteria has not changed from eg; from a Buy to a Sell.

I am aware that the static will reset , if the Ea initialises again.  

When I place this on the chart and press the EA button,

The Static xcross and Lastordertype both initialise as 0, and once the (trade criteria) MA cross occurs a  (1st) trade is placed. The Static xcross  ("Alert"  Shows Minutes  ,when cross occured) and Last ordertype update ("Alert" Shows the ordertype).

When the trade hits the stop Loss or the Take Profit. It re enters another (2nd) trade again straight away. When this trades Stop Loss or Take Profit is hit it exits.

(3rd) Now the trade DOES NOT re enter straight away and  it waits for the xcross time and minute(timecurrent)  to equal then it re enters a trade, This is correct.

I don't understand why  trade (1st)  exits and then  re enters trade (2nd) straight away ? It should wait for Static xcross time and Minutes(Timecurrent )  to equal , as it does with trade (3rd) before re entering.

Any assistance is welcomed, thanks  

 

 bool  checknewcross()                       //Function Header: have to put in when true/false So Bool. 
                                             // to the check the next function (in to Start())to place the trade.
  {                                                  
     static int  xcross;                     //Store Time of Cross
     static int lastordertype=0;   
                                                    
 if(lastordertype != currentordertype)       //only when changing from a Buy to sell they don't equal, so then,   
  {  
    lastordertype = currentordertype;        //change lastordertype to equal currentrdertype   
    xcross= TimeMinute(TimeCurrent());       //Variable xcross equals TimeCurrent Minute , being the time the trade changed from a buy to a sell "cross time" 
  
      return (true);                          //Return True , and Place order ,with Ordersend function.
    }                                              
          
 if(lastordertype==currentordertype)         //If lastordertype and current ordertype equal.
 if(xcross!=TimeMinute(TimeCurrent()))       //if the static time (when cross occured ) does not equal to time Minutes , then 
    {        
     return (false);                         //do not place any further orders until Static Cross time(Minute)(Variable xcross) and current Minute time equals.
   }       
  }                           
 
barnacle7: I am aware that the static will reset , if the Ea initialises again.  FALSE

Static's (and Globally declared) variables are only reset if the EA is loaded again (removed and reloaded).

Changing chart/EA parameters etc results in a deinit() init() cycle but that does NOT reset variables.

external static variable - MQL4 forum



 
WHRoeder:

Static's (and Globally declared) variables are only reset if the EA is loaded again (removed and reloaded).

Changing chart/EA parameters etc results in a deinit() init() cycle but that does NOT reset variables.

external static variable - MQL4 forum

 

Yes, thanks Whroeder for clarifying, any suggestions on the issue ?

 
barnacle7: Yes, thanks Whroeder for clarifying, any suggestions on the issue ?
Add Print statements before and inside IF statements, dumping variable values and find out why.
Reason: