Download MetaTrader 5
To add comments, please log in or register
Do you want to write a trading robot? Find solution in MQL5 Reference!
Sebastien
10
Sebastien 2014.07.05 18:57 

Hello everyone,

I'm currently learning the MQL4 language by means of this helpful forum and typically, I often succeed to find the right information (on the forum or the mql4 book). But now, i'm definitely stuck on a weird situation.

I'm currently coding a trend following strategy, which consists in taking supposed ends of corrections in the direction of the main trend. I started to code the "selling part" of the EA and works fine. Then I started adding the buying one and this is where the trouble starts.


The EA is supposed to check if there already is a opened position, and depending on if this is the case or not, open a second order with a specific risk. Here is the selling part which triggers the ordersend execution:

//Vérification du setup de fin de correction haussière sur une tendance baissière
   if(bBiaisHaussierDaily==false && bTradeAutorise==true && bCorrectionHaussiereM30==true && iClose(Symbol(),PERIOD_M30,1)<iLow(Symbol(),PERIOD_M30,iLowest(Symbol(),PERIOD_M30,MODE_LOW,iCanalPeriode,2)))
      {
      RefreshRates();
      if(dDernierPrixM30==0)
         {
         while(IsTradeContextBusy() == True)
            Sleep(1000);
         iTicket=OrderSend(Symbol(),OP_SELL,fCalculTaillePositionM30(),Bid,fValeurSlippage(Symbol(),5),dNiveauHautH4,0,NULL,123,0,Red);
         fErreur();
         }
      if(Bid<dDernierPrixM30-(2*iATR(Symbol(),PERIOD_M30,iCanalPeriode,1)))
         {
         while(IsTradeContextBusy() == True)
            Sleep(1000);
         iTicket=OrderSend(Symbol(),OP_SELL,fCalculTaillePositionSupM30(),Bid,fValeurSlippage(Symbol(),5),dNiveauHautH4,0,NULL,123,0,Red);
         fErreur();
         }
      bCorrectionHaussiereM30=false;
      }

If dDernierPrixM30==0, that means that there is no opened position.

If dDernierPrixM30 !=0, that means that there already is an opened one and the EA has to check if the current Ask is far away from the last entry to trigger a new order or not.

This part of code is working.


Now here is the "buying part" of the code:

//Vérification du setup de fin de correction baissière sur une tendance haussière
   if(bBiaisHaussierDaily==true && bTradeAutorise==true && bCorrectionBaissiereM30==true && iClose(Symbol(),PERIOD_M30,1)>iHigh(Symbol(),PERIOD_M30,iHighest(Symbol(),PERIOD_M30,MODE_HIGH,iCanalPeriode,2)))
      {
      RefreshRates();
      if(dDernierPrixM30==0)
         {
         while(IsTradeContextBusy() == True)
            Sleep(1000);
         iTicket=OrderSend(Symbol(),OP_BUY,fCalculTaillePositionM30(),Ask,fValeurSlippage(Symbol(),5),dNiveauBasH4,0,NULL,123,0,Green);
         fErreur();
         }
      if(Ask>dDernierPrixM30+(2*iATR(Symbol(),PERIOD_M30,iCanalPeriode,1)))
         {
         while(IsTradeContextBusy() == True)
            Sleep(1000);
         iTicket=OrderSend(Symbol(),OP_BUY,fCalculTaillePositionSupM30(),Ask,fValeurSlippage(Symbol(),5),dNiveauBasH4,0,NULL,123,0,Green);
         fErreur();
         }
      bCorrectionBaissiereM30=false;
      }

The principle is the same, but this time, if the variable "dDernierPrixM30" ==0, the first position is systematically duplicated twice. In vain, I have no idea about was could be wrong in the code.

This is the first time that I post here, and I don't really know if the above code is sufficient to understand something about it. Please let me know if you need additional information. Thanks in advance for your help.

Drazen Penic
2403
Drazen Penic 2014.07.06 01:07  

Add "else" before second "if".

If this is true: dDernierPrixM30==0 code opens order.

But then probably second condition is also true: (Ask>dDernierPrixM30+(2*iATR(Symbol(),PERIOD_M30,iCanalPeriode,1))) and you get second order.

"else" will exclude second condition if first is true. 

 

//Vérification du setup de fin de correction baissière sur une tendance haussière
   if(bBiaisHaussierDaily==true && bTradeAutorise==true && bCorrectionBaissiereM30==true && iClose(Symbol(),PERIOD_M30,1)>iHigh(Symbol(),PERIOD_M30,iHighest(Symbol(),PERIOD_M30,MODE_HIGH,iCanalPeriode,2)))
      {
      RefreshRates();
      if(dDernierPrixM30==0)
         {
         while(IsTradeContextBusy() == True)
            Sleep(1000);
         iTicket=OrderSend(Symbol(),OP_BUY,fCalculTaillePositionM30(),Ask,fValeurSlippage(Symbol(),5),dNiveauBasH4,0,NULL,123,0,Green);
         fErreur();
         }
      else if(Ask>dDernierPrixM30+(2*iATR(Symbol(),PERIOD_M30,iCanalPeriode,1)))
         {
         while(IsTradeContextBusy() == True)
            Sleep(1000);
         iTicket=OrderSend(Symbol(),OP_BUY,fCalculTaillePositionSupM30(),Ask,fValeurSlippage(Symbol(),5),dNiveauBasH4,0,NULL,123,0,Green);
         fErreur();
         }
      bCorrectionBaissiereM30=false;
      }

 

 

 

 

 


Sebastien
10
Sebastien 2014.07.06 08:07  

Hello drazen64,

With a cool head this morning, indeed, the two conditions had to be executed because I execute them during the same tick. It worked with selling positions because on the first iteration, Bid can not be lower than 0, and then, only one position was opened. At the contrary, Ask is always higher than 0, and then two orders are executed.

For the "else if" idea, i did not think one second about it. I did not even know that such syntax could be possible.

Thanks a lot for having helped me to spot my error. I appreciate! Happy trading!

/
To add comments, please log in or register