MFI expert error help please

 
My MFI expert advisor. If the position is in the overbought or sell zone after closing, it opens a new position immediately. Idont want this. I want it to open when the MFI indicator exits the overbought or oversold region and re-enters. What command should be added? Help please.
Files:
MFInew_ea.mq4  6 kb
 
  1. Look for a cross only once per bar

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  2.    double mfi=iMFI(Symbol(),0,MFIperiod,0);
       double mfi1=iMFI(Symbol(),0,MFIperiod,1);
    
    There is only one tick difference between these to values initially. And you may get multiple crosses as the candle forms. Look at bars 1 and 2.
 
cemun:
My MFI expert advisor. If the position is in the overbought or sell zone after closing, it opens a new position immediately. Idont want this. I want it to open when the MFI indicator exits the overbought or oversold region and re-enters. What command should be added? Help please.

Hello,

try this version of code.

I made some change.

Files:
 
Thank you.