Help adding some line of Code to fix bug

 

I created an EA, during the backtest i found out that the Ea opens position when an arrow signal flashes (the arrow does not repaint but sometimes flashes, it is after the close of the current candle stick that the arrow will stay or not, but ones it stays it does not repaint), where i need help is on lines to add so the EA does not close position unless the arrow stays on the current bar after close.

here is the code

#include <Trade/Trade.mqh>
int handle;
datetime lastSignal;
CTrade trade;
int OnInit(){
   string name = "Market//Gm TrendSignal.ex5";
   handle = iCustom(_Symbol,PERIOD_CURRENT,name);
   return(INIT_SUCCEEDED);
 }
void OnDeinit(const int reason){
}
void OnTick(){
      int indexCurrent = -1, indexLast = -1;
      for(int i = 0; i < 10000; i++){
         double indiMagenta[];
         CopyBuffer(handle,0,i,1,indiMagenta);
         double indiAqua[];
         CopyBuffer(handle,1,i,1,indiAqua);
         if (indiMagenta[0] > 0 || indiAqua[0] > 0){
            if(indexCurrent < 0){
               indexCurrent = i;
            }else{
               indexLast = i;
               datetime time = iTime(_Symbol,PERIOD_CURRENT,i);
               if(time > lastSignal){
                  if(indiAqua[0] > 0){
                     Print(__FUNCTION__, "New Sell Signal at", time);
                     trade.PositionClose(_Symbol);
                     trade.Buy(0.1);
                  } else if(indiMagenta[0] > 0){
                      trade.PositionClose(_Symbol);                 
                      Print(__FUNCTION__, "New Buy Signal at", time);
                      trade.Sell(0.1);
                  }
               }
               lastSignal = time;
               break;
            }
         }
   }
   Comment("\n index Last : ",indexLast,
            "\nindex Current : ",indexCurrent,
            "\nlast Signal : ",lastSignal);
}
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
Predefined Macro Substitutions - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Please, insert code correctly: when editing a post, click   Code and paste your code in the popup window.
MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
fe98lix: the EA does not close position unless the arrow stays on the current bar after close.

Makes no sense. The current bar is never closed; that is why it is called current.

Stop looking at the current bar.

Stop looping; A cross in the past is irrelevant.

Look for your cross on the start of a new 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.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)

 
William Roeder #:

Makes no sense. The current bar is never closed; that is why it is called current.

Stop looking at the current bar.

Stop looping; A cross in the past is irrelevant.

Look for your cross on the start of a new bar.

thanks William, i did as you advised, and its working fine. 

William Roeder #:

Makes no sense. The current bar is never closed; that is why it is called current.

Stop looking at the current bar.

Stop looping; A cross in the past is irrelevant.

Look for your cross on the start of a new bar.

Reason: