Close position if price equals middle BB

 

Hello, I'm wokring on a code for simple earnings on M5/M15/M30, just so I don't have to work while studying :P Jokes on side.   MQL4!!!


I can't get it to work like I'd like to, I want it to close positions when middle bollinger band is touching the candle. Here's that part of code. I've tried many different options but i still can't get it right.

double MiddleBB=iMA(NULL, 0, 20,0, MODE_SMA, PRICE_CLOSE,0);

if((MiddleBB==Ask)||(MiddleBB==Bid))
{
        for(int i = OrdersTotal()-1; i >= 0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
           {
            if(OrderType() == OP_SELL)
              {
               RefreshRates();
               if(OrderClose(OrderTicket(),OrderLots(),Ask,1) == false)
                 {
                  Print("Nie udało się zamknąć pozycję ",OrderTicket(),". Błąd nr = ",GetLastError());
                 }
              }
           }
        }
     }  
}

I'm also receiving (the only error) OrderSend Error 130, but the positions are opened correctly, seems like it's an error for closing position

OrderSend(NULL,OP_SELL,0.01,Bid,1,MathMax(MathMax(Close[3],Close[2]),Close[1])+30*_Point,0,NULL,0,0,Red);

Oh and I forgot, NO, the StopLoss or Take Profit are not too close to market price etc. here's an example of a trade and the point where it should close the position. (BBex.png) The lower bollinger bang is the middle one, when it touches the candle i want to close the position.



 
michal34558:

Hello, I'm wokring on a code for simple earnings on M5/M15/M30, just so I don't have to work while studying :P Jokes on side.


I can't get it to work like I'd like to, I want it to close positions when middle bollinger band is touching the candle. Here's that part of code. I've tried many different options but i still can't get it right.



I'm also receiving (the only error) OrderSend Error 130, but the positions are opened correctly, seems like it's an error for closing position

here is your code


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

void CloseMiddleBB()
  {
   bool MiddleBB=iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);  //// 20 SMA is same as middle of BB
   bool res;
   for(int i=0; i<=OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS)== true)
        {
         if( ((High[0] || Low[0])>=  MiddleBB ) || (((High[0]) || (Low[0]))<=  MiddleBB ))
           {
            if(OrderType()== OP_BUY)
               res= OrderClose(OrderTicket(),OrderLots(),Bid,30,Yellow);
            if(OrderType()==OP_SELL)
               res= OrderClose(OrderTicket(),OrderLots(),Ask,30,Green);
           }
        }
     }
    }  

//+------------------------------------------------------------------+
 
Adebayo Samson Adewuyi:

here is your code


Thanks! However, isn't that loop
for(int i=0; i<=OrdersTotal(); i++)
incorrect? It's not like I'm writing this post after 5 minutes of trying to do it myself. I've read a lot that we should use this one execpt
for(int i = OrdersTotal()-1; i >= 0; i--)

as your order change their numbers whenever you close the first one. For eg. you have 3 orders, if you close first one at first, the second one is now numbered as first and so on, so you will close only one position with your loop and make it infinite. Am I wrong?

EDIT: or else, one position will never be closed.


EDIT2: Checked your code, doesn't work either.

 

which ever loop you use, you will be fine, i have some functions like that and it works perfectly

you need to define for a sell and a buy  if Low/ high should be higher or lesser than SMA  the given code will close almost every order 

 
Adebayo Samson Adewuyi:

which ever loop you use, you will be fine, i have some functions like that and it works perfectly

you need to define for a sell and a buy  if Low/ high should be higher or lesser than SMA  the given code will close almost every order 

Ahhh, YES! The thing with Low/High higher or lesser instead of equal is exactly what I couldn't see :D Thank you! Now It's time to improve the rest... :)