Need simple help to fix my EA, how to prevent re-entry on same bar?

 

Hi guys,

I am a super beginner at MQL4, and I am actually using an online EA Builder - StrategyTune (strategytune.com/app/) to make my EA.

I am very happy with what I can do with it so far, but I met with a problem with my EA.

I am trying to make a very simple EA.

Timeframe: Daily

The rules are:

- Price goes up by 1% from low of the day, Sell TP20 SL20

- Price goes down by 1% from high of the day, Buy TP20 SL20

I have made the EA, and the entry and exit is correct.

But my problem is that after the EA exit a trade, it will enter into another trade. While I only want it to trade once within a day.

So my question is: What code do I need to add to prevent an EA from re-entering a trade on the same bar? keeping it to 1 trade only per bar?

Thank You in advance for any of your help!

By the way, I have attached my newbie EA here so you will see the problem with the multiple entries within the same bar.

Files:
 
Search google for "forum.mql4.com once per bar"
 

Hi,

Try this:

add this function to services

bool __wasTradeSameDay(int __magic) {
   for(int __i=0;__i<OrdersTotal();__i++) {
      if(OrderSelect(__i,SELECT_BY_POS,MODE_HISTORY) && OrderMagicNumber()==__STRATEGY_MAGIC+__magic
            && (OrderType() == OP_BUY || OrderType() == OP_SELL) && TimeToStr(OrderOpenTime(),TIME_DATE) == TimeToStr(TimeCurrent(),TIME_DATE)) {
         return(true);
      }
   }
   return(false);
}

and then modify order open conditions

if(_Compare && !__isExist(0) && !__wasTradeSameDay(0))_Buy = ...
if(_Compare_2 && !__isExist(0) && !__wasTradeSameDay(0))_Sell = ...

Modified EA attached

Files:
 
Ben.J:

Hi,

Try this:

add this function to services

and then modify order open conditions

Modified EA attached


Hi Ben thanks for helping, I just tried out the EA, but it's still producing multiple trades in the same bar.

I have tried numerous codes people suggested on forums on how to fix this, but I just could never get it to work!

If anyone would be kind enough to fix my EA so it trades only once per bar? Thank You.

 
if (openCondition){
   static datetime lastTradeTime;
   if (lastTradeTime != Time[0]){
      int ticket = OrderSend(..);
      if (ticket < 0) Alert("OrderSend Failed: ", GetLastError());
      else            lastTradeTime = Time[0];
}  }
 
WHRoeder:

or iTime
 
WHRoeder:


Thank you. Can anyone tell me what's wrong with my code here?

if(_Compare && !__isExist(0)) {
   static datetime lastTradeTime;
   if (lastTradeTime != Time[0]){
      int ticket = OrderSend(Symbol(),0,0.1,MarketInfo(Symbol(),MODE_ASK),0,MarketInfo(Symbol(),MODE_ASK)-MarketInfo(Symbol(),MODE_POINT)*200,MarketInfo(Symbol(),MODE_ASK)+MarketInfo(Symbol(),MODE_POINT)*200,"",__STRATEGY_MAGIC + 0)>=0;
      if (ticket < 0) Alert("OrderSend Failed: ", GetLastError());
      else            lastTradeTime = Time[0];
      } }
 

It's not very readable . . . why use 0 when you can use OP_BUY instead ?

What is this doing at the end of your OrderSend ?

      0)  >=0  ;
 

Sorry there was a mistake in my code, there should be not OrdersTotal(), but OrdersHistoryTotal().

Here's the code:

bool __wasTradeSameDay(int __magic) {
   for(int __i=0;__i<OrdersHistoryTotal();__i++) {
      if(OrderSelect(__i,SELECT_BY_POS,MODE_HISTORY) && OrderMagicNumber()==__STRATEGY_MAGIC+__magic
            && (OrderType() == OP_BUY || OrderType() == OP_SELL) && TimeToStr(OrderOpenTime(),TIME_DATE) == TimeToStr(TimeCurrent(),TIME_DATE)) {
         return(true);
      }
   }
   return(false);
}

Cheers!

Files:
Reason: