problem at programing TP & SP

 
Hello my friends,

I am new to MQL4 programing. I build an EA that trade a simple Bollinger Band System but when I test the EA with MT4 System tester it freeze. I tried to find the problem and I think that it is in the way I use Bollinger Bands as Stop-Loss & Take Profit.

I appreciate if any friend can solve this problem.

---------------------------------------------------------------

The system is like this:

1- If close is between upper & center Bollinger Bands (BB) then buy long on the open of the next candle.

2- If close is in between the center & the lower BB then sell short at the open of the next candle.

3- For long positions use upper BB of the previous candle as target & for short positions use lower BB.

4- For long positions use lower BB of previous candle as stop loss & for short positions use upper BB.

---------------------------------------------------------------

Here is the EA code where I think the problem is from take profit & stop loss coding which I want somebody to help me to fix it.

I attach it here too.

//---Bollinger Bands Target
#property version     "1.0"
//---Expert Proprities
extern string     Bolliner_Parameters = "---------------------------------------------";
extern int        BBandTime           =20;
extern double     BBandDeviation      =2;
extern string     Common_Settings     = "---------------------------------------------";
extern double     FirstLot            = 0.01;
extern int        UserSlippage        =20;
extern int        MagicNumber         =20150608;
extern string     UserComment         ="BBT";
//---initialize global variables
datetime cur_open=0;
int ticket,i,cc,t1,t2;
double lots,enl,ens,tpl,tps,spl,sps;
bool result;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   Comment(" Parameters: \n Magic: ",MagicNumber);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//---
lots  = FirstLot;                     //---Lot Size
enl   = MarketInfo(Symbol(),MODE_ASK);//---Enter Long price
ens   = MarketInfo(Symbol(),MODE_BID);//---Enter Short price
tpl   = UpperBBand(1);  //---Take profit for long trades
tps   = LowerBBand(1);  //---Take profit for short trades
spl   = LowerBBand(1);  //---Stop Loss for long trades
sps   = UpperBBand(1);  //---Stop Loss for short trades
//---
//---Open BUY Orders for 1st time
if(Time[0]!=cur_open)
if(Close[1] < UpperBBand(1) && Close[1] > CenterBBand(1))
{
 ticket=-1;
      while(ticket<0)
      {
      ticket = OrderSend(Symbol(),OP_BUY , lots, enl, UserSlippage,spl,tpl, UserComment, MagicNumber, 0, clrGreen);
      if(ticket<0) Sleep(500);
      else cur_open=Time[0];
      }
}

//---Open SELL Orders for 1st time
if(Time[0]!=cur_open)
if(Close[1] > LowerBBand(1) && Close[1] < CenterBBand(1))
{
 ticket=-1;
      while(ticket<0)
      {
      ticket = OrderSend(Symbol(),OP_SELL , lots, ens, UserSlippage,sps,tps, UserComment, MagicNumber, 0, clrRed);
      if(ticket<0) Sleep(500);
      else cur_open=Time[0];
      }
}

//---
 return(0);
}

//+------------------------------------------------------------------+
//| Functions                                                        |
//+------------------------------------------------------------------+

//================================
//---Technical Analysis Functions |
//================================

//--- Upper Bollinger Bands Function

double UpperBBand(int ii)
{
   return(iBands(Symbol(),Period(),BBandTime,BBandDeviation,0,PRICE_CLOSE,MODE_UPPER,ii));
}

//--- Lower Bollinger Bands ENTER Function

double LowerBBand(int ii)
{
   return(iBands(Symbol(),Period(),BBandTime,BBandDeviation,0,PRICE_CLOSE,MODE_LOWER,ii));
}

//--- Center Bollinger Bands ENTER Function

double CenterBBand(int ii)
{
   return(iBands(Symbol(),Period(),BBandTime,BBandDeviation,0,PRICE_CLOSE,MODE_MAIN,ii));
}
Files:
 

Dear Mohammad ...

It could be the lack of considering the condition when distances for sl and tp are too tight !.

You have to include a condition of minimum distance for sl and tp ... 

This way, it may be work or at least it solves the condition when the price ranges are too tight. 

 
Osama Shaban:

Dear Mohammad ...

It could be the lack of considering the condition when distances for sl and tp are too tight !.

You have to include a condition of minimum distance for sl and tp ... 

This way, it may be work or at least it solves the condition when the price ranges are too tight. 

I test the EA on Daily time frame so I think it is very hard to have such a case where the difference between the upper and lower bands is too tight.

I think the problem is in the TP and SP or in the logic of the EA.

 
Something interesting happened while I try to solve the problem. When I set Stop Loss for long and short OrderSend function to Zero the EA freeze in the test at the same point, but when I set the Take Profit for long and short to Zero the EA completed the test without freezing.
 
Mohammad Al Bermaui:
Something interesting happened while I try to solve the problem. When I set Stop Loss for long and short OrderSend function to Zero the EA freeze in the test at the same point, but when I set the Take Profit for long and short to Zero the EA completed the test without freezing.

Your problem is definitely going to be in your while loops and I'd suggest it would be an order send error related to your SL and/or TP. What errors are reported in your log?

The first thing I would try would be to Normalize all of the doubles in the order send function to the correct decimal places (lots, sl, tp etc)

 
iBands(....

Returns:

Numerical value of the Bollinger Bands® indicator.

//---Open BUY Orders for 1st time
if(Time[0]!=cur_open)
if(Close[1] < UpperBBand(1) && Close[1] > CenterBBand(1))
{
 ticket=-1;
      while(ticket<0)
      {
      Print("spl: ",spl," tpl: ",tpl);
      ticket = OrderSend(Symbol(),OP_BUY , lots, enl, UserSlippage,spl,tpl, UserComment, MagicNumber, 0, clrGreen);
      if(ticket<0) Sleep(500);
      else cur_open=Time[0];
      }
}

//---Open SELL Orders for 1st time
if(Time[0]!=cur_open)
if(Close[1] > LowerBBand(1) && Close[1] < CenterBBand(1))
{
 ticket=-1;
      while(ticket<0)
      {
      Print("spl: ",spl," tpl: ",tpl);
      ticket = OrderSend(Symbol(),OP_SELL , lots, ens, UserSlippage,sps,tps, UserComment, MagicNumber, 0, clrRed);
      if(ticket<0) Sleep(500);
      else cur_open=Time[0];
      }
}
 
Mohammad Al Bermaui:

I test the EA on Daily time frame so I think it is very hard to have such a case where the difference between the upper and lower bands is too tight.

I think the problem is in the TP and SP or in the logic of the EA.

One price gap is enough to freeze your EA.

Start the tester in visual mode, it will stop at the wrong entry. So, you will be able to figure out the problem.

Also it would be better to break the while loop after "x" attempts and Print out the error if the Ordersend fails.

 
Laszlo Tormasi:

One price gap is enough to freeze your EA.

Start the tester in visual mode, it will stop at the wrong entry. So, you will be able to figure out the problem.

Also it would be better to break the while loop after "x" attempts and Print out the error if the Ordersend fails.

Laszlo

Is right it is a very forceful way to open an order.

The output of the iBands indicator does not change that fast.

//---Open BUY Orders for 1st time
if(Time[0]!=cur_open)
  {
   if(Close[1]<UpperBBand(1) && Close[1]>CenterBBand(1))
     {
      //Print("spl: ",spl," tpl: ",tpl);
        {
         ticket=OrderSend(Symbol(),OP_BUY,lots,enl,UserSlippage,spl,tpl,UserComment,MagicNumber,0,clrGreen);
           {
            if(ticket>0)
              {
               cur_open=Time[0];
              }
           }
        }
     }
  }
//---Open SELL Orders for 1st time
if(Time[0]!=cur_open)
  {
   if(Close[1]>LowerBBand(1) && Close[1]<CenterBBand(1))
     {
      //Print("spl: ",spl," tpl: ",tpl);
        {
         ticket=OrderSend(Symbol(),OP_SELL,lots,ens,UserSlippage,sps,tps,UserComment,MagicNumber,0,clrRed);
           {
            if(ticket>0)
              {
               cur_open=Time[0];
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+


It will try to place an order at every new tick for as long as the conditions are met.

And it has been said look at the output journal to see the error code it should be there telling you what went wrong.



 
Marco vd Heijden ,Laszlo

Thanks a lot for your help.I have learned something new about MQL4 programing from you.
Reason: