How to avoid re-entrys at the same bar?

 

Hello guys,

 

i am new to programming and forex and that is the reason why i am also using an online EA builder :)

So far it worked very well but my problem is, that the EA re -entrys at the same bar every time after a stop loss gets triggerd.

This is pretty annoying while using a 5min period. So anyone can help me? With the Online EA Builder its not possible to avoid this.

I don t want any re entrys at the same candle. EDIT: I ve already seen a function to avoid re entrys but only works for 1day period

The EA looks like this at the moment: 

 


 



extern int MagicNumber=10001;

extern double Lots =0.5;

extern double StopLoss=15;

extern double TakeProfit=0;

extern int TrailingStop=15;

extern int Slippage=3;

//+------------------------------------------------------------------+

//    expert start function

//+------------------------------------------------------------------+

int start()

{

  double MyPoint=Point;

  if(Digits==3 || Digits==5) MyPoint=Point*10;

  

  double TheStopLoss=0;

  double TheTakeProfit=0;

  if( TotalOrdersCount()==0 ) 

  {

     int result=0;



     if((Hour()>08)&&(Hour()<18)&&(Close[1]>iMA(NULL,PERIOD_M5,100,0,MODE_SMA,PRICE_CLOSE,1))&&(Open[1]<iMA(NULL,PERIOD_M5,100,0,MODE_SMA,PRICE_CLOSE,1))&&(iMA(NULL,PERIOD_D1,5,0,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,PERIOD_D1,10,0,MODE_EMA,PRICE_CLOSE,1))) // Here is your open buy rule

     {

        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);

        if(result>0)

        {

         TheStopLoss=0;

         TheTakeProfit=0;

         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;

         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;

         OrderSelect(result,SELECT_BY_TICKET);

         OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

        }

        return(0);

     }

  }

  

  for(int cnt=0;cnt<OrdersTotal();cnt++)

     {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL &&   

         OrderSymbol()==Symbol() &&

         OrderMagicNumber()==MagicNumber 

         )  

        {

         if(OrderType()==OP_BUY)  

           {

            if(TrailingStop>0)  

              {                 

               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)

                 {

                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);

                     return(0);

                    }

                 }

              }

           }

         else 

           {

            if(TrailingStop>0)  

              {                 

               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))

                 {

                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);

                     return(0);

                    }

                 }

              }

           }

        }

     }

   return(0);

}



int TotalOrdersCount()

{

  int result=0;

  for(int i=0;i<OrdersTotal();i++)

  {

     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);

     if (OrderMagicNumber()==MagicNumber) result++;



   }

  return (result);

}
 
Komodorpudel:

Hello guys,

 

So anyone can help me? With the Online EA Builder its not possible to avoid this.


Yes use  Jobs   to find someone for making it
 
Komodorpudel: i am new to programming and forex and that is the reason why i am also using an online EA builder :)

  1. For large amounts of code, attach it
  2. Since there are no slaves here, you have only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt and the nature of your problem.
  3. EA builder has lots of problems, generates bad code. Use this.
  4. Why aren't you asking in the EA builder forums?
 
The simple way I do this sort of thing is to to use a global variable of type datetime (call it "stoptime"). When you hit your stop, set this variable to the time of the bar using the Time function. Then in your code for entering a position have a check to make sure the bar time is not equal to the stoptime variable. ok?
 
Elroch:
The simple way I do this sort of thing is to to use a global variable of type datetime (call it "stoptime"). When you hit your stop, set this variable to the time of the bar using the Time function. Then in your code for entering a position have a check to make sure the bar time is not equal to the stoptime variable. ok?

check if historytotal changed

if changed then check if last closed trade is from your EA with same Symbol

if orderclosed at stoploss if so make stoptime orderclosetime if time bar 0 > stoptime new trade allowed 

 
deVries:

check if historytotal changed

if changed then check if last closed trade is from your EA with same Symbol

if orderclosed at stoploss if so make stoptime orderclosetime if time bar 0 > stoptime new trade allowed 

Or just use "once per bar" type code . . .
 

Hello,

 thank you for the fast answers :)

 

 I think i will post it at Jobs.... I think for coding this myself i am to silly :D

 

greets :) 

 
RaptorUK:
Or just use "once per bar" type code . . .

So far it worked very well but my problem is, that the EA re -entrys at the same bar every time after a stop loss gets triggerd.

So I thought no reentry at hitting stoploss  but if takeprofit hit possible to trade

I see now after reading your comment I missed his line I don t want any re entrys at the same candle.

 In that case your solution is the way to do

Thanks for correction 

Reason: