Download MetaTrader 5
To add comments, please log in or register
Deposit to your account via Visa QIWI Wallet
chukwudi joshua
233
chukwudi joshua 2015.03.09 08:10 

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);
        }
     }
Eugenio Bravetti
3457
Eugenio Bravetti 2015.03.09 16:30  
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.............");
   }
}

chukwudi joshua
233
chukwudi joshua 2015.03.09 17:53  
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.

chukwudi joshua
233
chukwudi joshua 2015.03.09 18:52  

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.............");
   }
}


Eugenio Bravetti
3457
Eugenio Bravetti 2015.03.09 21:34  
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)

chukwudi joshua
233
chukwudi joshua 2015.03.10 20:45  
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.
chukwudi joshua
233
chukwudi joshua 2015.03.15 16:52  

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.

/
To add comments, please log in or register