Download MetaTrader 5

Need help to create a Loop in an existing EA

To add comments, please log in or register
idionne
48
idionne  

Hello everyone,

I am trying to add a loop in an trading EA someone code for me.  The EA uses an external indicator which supplies the BUY and SELL signals the EA uses to open/reverse trades.  I have managed to add a few cool things to the original version by reading the manuals and internet searches.  But, I can't figure out how to properly write the loop and how to insert it in the code. 

Current code for a BUY signal from the external indicator:

if(NewBar&&(tobuy0!=EMPTY_VALUE&&tobuy0!=0)&&(tobuy1==EMPTY_VALUE||tobuy1==0)&&orderscnt(OP_BUY)==0
&&(AllowTradesByTime()||!TradingHours)
&&(((STO1<STO2))||!StochConfirmation)  

This works when the condition was met before the current bar, but sometimes it is met in a future bar (when the external indicator doesn't send signal).  Instead, I would like for the EA to wait until the condition is met.

=====================================

StochConfirmation can be manually set to "true" or "false"
STO1 = iStochastic(NULL,0,KPeriod,3,3,MODE_SMA,0,MODE_MAIN,1);
STO2 = iStochastic(NULL,0,KPeriod,3,3,MODE_SMA,0,MODE_MAIN,2);


Help would be greatly appreciated

Yohana Parmi
3536
Yohana Parmi  
idionne:

Current code for a BUY signal from the external indicator:

if(NewBar&&(tobuy0!=EMPTY_VALUE&&tobuy0!=0)&&(tobuy1==EMPTY_VALUE||tobuy1==0)&&orderscnt(OP_BUY)==0
&&(AllowTradesByTime()||!TradingHours)
&&(((STO1<STO2))||!StochConfirmation)  

This works when the condition was met before the current bar, but sometimes it is met in a future bar (when the external indicator doesn't send signal).  Instead, I would like for the EA to wait until the condition is met.

=====================================

StochConfirmation can be manually set to "true" or "false"
STO1 = iStochastic(NULL,0,KPeriod,3,3,MODE_SMA,0,MODE_MAIN,1);
STO2 = iStochastic(NULL,0,KPeriod,3,3,MODE_SMA,0,MODE_MAIN,2);


Help would be greatly appreciated


Hi, you can paste your code inside form at SRC

will be look better :)

whroeder1
17111
whroeder1  
  1. Please use
SRC
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You've stated no reason why you need a loop. Until you can state in words exactly what you want, no one can code it.
idionne
48
idionne  

Hi,

extern bool StochConfirmation=false;
extern int KPeriod=14;


      double STO1 = iStochastic(NULL,0,KPeriod,3,3,MODE_SMA,0,MODE_MAIN,1);
      double STO2 = iStochastic(NULL,0,KPeriod,3,3,MODE_SMA,0,MODE_MAIN,2);
           

      
  // To BUY and Reversing a Sell
  // -----------------------------------------------------------

if(NewBar&&(tobuy0!=EMPTY_VALUE&&tobuy0!=0)&&(tobuy1==EMPTY_VALUE||tobuy1==0)&&orderscnt(OP_BUY)==0
&&(AllowTradesByTime()||!TradingHours)
&&(((STO1<STO2))||!StochConfirmation)

){

if(AutoCloseNextSignal&&orderscnt(OP_SELL)>0){
CloseOrders(OP_SELL);
  if(StopAfterNextClose){
  CloseAll();
  ExpertRemove();
  return;
  }
  }
   if(StopLoss==0){SL=0;}else{SL=Ask-StopLoss*point;}
   if(TakeProfit==0){TP=0;}else{TP=Ask+TakeProfit*point;}
  buy=OrderSend(Symbol(),OP_BUY,Lots,Ask,3*Q,SL,TP,"Buy Market",MagicNumber,0,clrBlue); 
  CloseManual(OP_SELL,TransactionNumber);
  if(PopUp)Alert("(SC EA) - BUY order was placed - "+Symbol());
  if(Email)SendMail("New Order "+Symbol(),"a Buy Order has been placed and closed any Sell order");
  if(PushNotification)SendNotification("Buy Order is Placed "+Symbol());
  NewBar=false;
  }

Hi Yohana Parmi and whroeder1,

Here is the section of the code that is activated by the BUY signal from the external indicator.  The EA works fine but want to add a loop because the condition is not always met in previous candles but will be in the future (usually, the following candle).

** The condition is ignored when StochConfirmation=false (which I want). 

Now: If StochConfirmation=true and STO1<STO2, the EA continues and opens the trade.
Instead: I would like the condition be: if StochConfirmation=true WAIT for STO1<STO2.

Hope this clarify things for you. 

whroeder1
17111
whroeder1  
idionne:   The EA works fine but want to add a loop because the condition is not always met in previous candles but will be in the future (usually, the following candle).
You already have a loop. You return from OnTick and will be called again when a new tick arrives. If it's a new bar, you will reevaluate your condition(s.)
idionne
48
idionne  

Hi whroeder1,

My problem is that my "external indicator" doesn't send a signal at every bar.  So, if STO1<STO2 is not true during the bar the "external indicator" sends a signal, the trade is not made.

Often, STO1<STO2 is "true" the following bar and I miss the trade.  I am hoping a loop can fix this.  I have tried to figure this out on my own but can't manage to make it to work.

Any guidance or help would be great.

whroeder1
17111
whroeder1  

You indicator shouldn't "signal at every bar," if it did, it would be broken.

Stop hoping and start thinking. A loop will not fix anything. There are no "following bars;" there are only the current bar and previous bars.

You look for a signal on bar one, do or do not, and then return. When you get a new tick and it's a new bar, you look for a signal on bar one.

The return, wait for a tick, process the tick is your loop.

idionne
48
idionne  

Hi whroeder1,

OK.  I did write something that checks for a signal on the previous bars. 

It works but I was looking at the different Loop Operators (for, while, do while) for use instead of (STO1<STO2))||!StochConfirmation).

To add comments, please log in or register