Help with closing all buy or all sell when takeprofit is hit or reach

 

please can someone help me with handy code to close all buy or sell when takeprofit is reached or hit


please help me i didnt know all code loops please.thanks

bellow is what i even i try to count down order but yet after that the countdown loop will not work with mt4 strategy test it hangs it

so below is how i coded it but without success i even count down orders yet it didnt work it still close at different price of indicators

//Orders Countingdown & Close Orders it--------------------

   int total=0;

   for(int i=1; i<OrdersTotal(); i--)
     {
      //--------------------------------------
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if((OrderSymbol()==Symbol()) && ((OrderType()==OP_SELL) || (OrderType()==OP_BUY)))
        {
         total++;
        }

      //-Close Position Buys if
      if((OrderSymbol()==Symbol()) && (OrderType()==OP_BUY) && (iBands(NULL,0,10,2,0,PRICE_HIGH,MODE_UPPER,0)<High[1]))

        {
          OrderClose(OrderTicket(),OrderLots(),Bid,1,Black);
     
     }

      //-Close Positione Sell 
      if((OrderSymbol()==Symbol()) && (OrderType()==OP_SELL) && (iBands(NULL,0,10,2,0,PRICE_LOW,MODE_LOWER,0)>Low[1]))
        {
         OrderClose(OrderTicket(),OrderLots(),Ask,1,Black);
        }
     }
 
for( int i = OrdersTotal() - 1; i >= 0; i-- )
{
   if ( !OrderSelect(i, SELECT_BY_POS) )
      continue;

   if( OrderSymbol() != Symbol() )
      continue;
   
   //-Close Position Buys if
   if( OrderType() == OP_BUY && iBands(NULL,0,10,2,0,PRICE_HIGH,MODE_UPPER,0) < High[1] )   
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), 1, clrBlack) )
         Alert("your error alert...............");
   }
   
   //-Close Positione Sell 
   if( OrderType() == OP_SELL && iBands(NULL,0,10,2,0,PRICE_LOW,MODE_LOWER,0) > Low[1] )
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), 1, clrBlack) )
         Alert("your error alert.............");
   }
}

 
e.bravetti:
for( int i = OrdersTotal() - 1; i >= 0; i-- )
{
   if ( !OrderSelect(i, SELECT_BY_POS) )
      continue;

   if( OrderSymbol() != Symbol() )
      continue;
   
   //-Close Position Buys if
   if( OrderType() == OP_BUY && iBands(NULL,0,10,2,0,PRICE_HIGH,MODE_UPPER,0) < High[1] )   
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), 1, clrBlack) )
         Alert("your error alert...............");
   }
   
   //-Close Positione Sell 
   if( OrderType() == OP_SELL && iBands(NULL,0,10,2,0,PRICE_LOW,MODE_LOWER,0) > Low[1] )
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), 1, clrBlack) )
         Alert("your error alert.............");
   }
}




thanks e.bravetti i will try to re modify according to your corrections.

 

Please e.bravetti one more assistance please in the above code i want you to add FOR ME

if BUY POSITION reaches 30pips above bid  OR TP  THEN close all BUY POSITION

if SELL POSITION reaches 30pips BELOW ask  OR TP  THEN close all SELL POSITION


PLEASE AM WAITING I APPRECIATE YOUR PREVIOUS SUPPORT



for( int i = OrdersTotal() - 1; i >= 0; i-- )
{
   if ( !OrderSelect(i, SELECT_BY_POS) )
      continue;

   if( OrderSymbol() != Symbol() )
      continue;
   
   //-Close Position Buys if
   if( OrderType() == OP_BUY && -----------------------------------------------  TP=30 )   
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), 1, clrBlack) )
         Alert("your error alert...............");
   }
   
   //-Close Positione Sell 
   if( OrderType() == OP_SELL &&------------------------------------------------ TP=30 )
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), 1, clrBlack) )
         Alert("your error alert.............");
   }
}


 
futureseer:

Please e.bravetti one more assistance please in the above code i want you to add FOR ME

if BUY POSITION reaches 30pips above bid  OR TP  THEN close all BUY POSITION

if SELL POSITION reaches 30pips BELOW ask  OR TP  THEN close all SELL POSITION


PLEASE AM WAITING I APPRECIATE YOUR PREVIOUS SUPPORT

for( int i = OrdersTotal() - 1; i >= 0; i-- )
{
   if ( !OrderSelect(i, SELECT_BY_POS) )
      continue;

   if( OrderSymbol() != Symbol() )
      continue;
   
   //-Close Position Buys if
   if( OrderType() == OP_BUY && Bid >= (OrderOpenPrice() + (30 * Point)) )   
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), 1, clrBlack) )
         Alert("your error alert...............");
   }
   
   //-Close Positione Sell 
   if( OrderType() == OP_SELL && Ask <= (OrderOpenPrice() - (30 * Point)) )
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), 1, clrBlack) )
         Alert("your error alert.............");
   }
}

ATTENTION: if you use the reserved word Point in (30 * Point), in forex market with 5 digit or 3 digit, the result of (30 * Point) is = 3 pips.....if you want 30 pips real, you must insert (300 * Point)

 
e.bravetti:

ATTENTION: if you use the reserved word Point in (30 * Point), in forex market with 5 digit or 3 digit, the result of (30 * Point) is = 3 pips.....if you want 30 pips real, you must insert (300 * Point)

e.bravetti: THANK YOU VERY MUCH FOR YOU ASSISTANCE,i appriciate.
 

please sorry for bringing back this thread its just that the e.bravetti code modified for me work fine but the second one did not work well and the issue is that i want to close all

my orders once my profit move 30pip above the open price then

for( int i = OrdersTotal() - 1; i >= 0; i-- )
{
   if ( !OrderSelect(i, SELECT_BY_POS) )
      continue;

   if( OrderSymbol() != Symbol() )
      continue;
   
   //-Close Position Buys if
   if( OrderType() == OP_BUY && -----------------------------------------------  TP=30 )   
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), 1, clrBlack) )
         Alert("your error alert...............");
   }
   
   //-Close Positione Sell 
   if( OrderType() == OP_SELL &&------------------------------------------------ TP=30 )
   {
      if ( !OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), 1, clrBlack) )
         Alert("your error alert.............");
   }
}


all order close. please any more idea on how to clean code this please.