Download MetaTrader 5

How to take only one action on lots per condition???

To add comments, please log in or register
How to buy a product in MetaTrader AppStore? Read the article!
Michael Gad
227
Michael Gad 2014.11.19 11:20 

hi

i would like to divide the Order Lots (Praticl close)when the Profit  reaches certain level but the problem that it continues to divide , need help to know how to break the loop and don't repeat itself

 //-------------------------------------------------

 void PartialClose()

{  

    

    for(int i = OrdersTotal()-1; i >= 0; i--){

   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==false)continue;

   if( Magic == OrderMagicNumber() )

 {

int Tk=100;

     

         if(OrderType() == OP_BUY){

            if(Bid > OrderOpenPrice()+(Tk*Point)){

               OrderClose(OrderTicket(), OrderLots()/2, Bid, 3, Red);

            }

         }

         if(OrderType() == OP_SELL){

            if(Ask< OrderOpenPrice()-(Tk*Point)){

               OrderClose(OrderTicket(),OrderLots()/2, Ask, 3, Red);

            }

         }

      }

   }


 

 

i will appreciate your help 

whroeder1
14519
whroeder1 2014.11.19 14:38  
micoul81: i would like to divide the Order Lots (Praticl close)when the Profit  reaches certain level but the problem that it continues to divide , need help to know how to break the loop and don't repeat itself
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You can't use OrderLots()/2 because that is not a multiple of LotStep, and you can't close or have remaining less than MinLot. See my code
  3. You are only checking if price may trigger a partial close. You aren't checking if you have already done it.
    • Set a flag in persistent storage (files, global variables w/ flush)
    • I move SL to BE+1 before the partial close. That way I know that I already did it.
    • Open two orders.
Michael Gad
227
Michael Gad 2014.11.20 09:45  

Sorry for number 1

2 and 3 still complicated for me

What's the meaning of BE+1 

 

can't handle it 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.11.20 12:26  

Q: What's the meaning of BE+1 ?

A: Breakeven + 1

Michael Gad
227
Michael Gad 2014.11.20 13:13  

thanks deysmacro

Michael Gad
227
Michael Gad 2014.11.20 15:23  
it continue to divide lots
Michael Gad
227
Michael Gad 2014.11.20 15:24  
  void PartialClose()
{  
   double Lotstep = MarketInfo(Symbol(), MODE_LOTSTEP);
   int x = 0; if(Lotstep == 0.01) x = 2; else x = 1;
    
    for(int i = OrdersTotal()-1; i >= 0; i--){
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==false)continue;
   if( Magic == OrderMagicNumber() )
 {
int Tk=100;
double LOTS = OrderLots();
double Dividelot = NormalizeDouble(LOTS * 0.5, x);
     
         if(OrderType() == OP_BUY){
            if(Bid > OrderOpenPrice()+(Tk*Point)){
               OrderClose(OrderTicket(), Dividelot*2, Bid, 3, Red);
            }
         }
         if(OrderType() == OP_SELL){
            if(Ask< OrderOpenPrice()-(Tk*Point)){
               OrderClose(OrderTicket(),Dividelot*2, Ask, 3, Red);
            }
         }
      }
   }

}
 
Michael Gad
227
Michael Gad 2014.11.22 23:04  

any help please to solve this problem , still can't handle it

Keith Watford
Moderator
9623
Keith Watford 2014.11.23 00:10  

You need to do something like this as suggested by WHRoeder

         if(OrderType() == OP_BUY && OrderStopLoss()<OrderOpenPrice()){
            if(Bid > OrderOpenPrice()+(Tk*Point)){
               {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+Point,OrderTakeProfit(),0,clrNONE );
               OrderClose(OrderTicket(), Dividelot*2, Bid, 3, Red);
               }
            }
         }

 Not tested and checking success of OrderModify and OrderClose required

Michael Gad
227
Michael Gad 2014.11.23 12:55  
GumRai:

You need to do something like this as suggested by WHRoeder

 Not tested and checking siccess of OrderModify and OrderClose required

  void PartialClose()
{  
   double Lotstep = MarketInfo(Symbol(), MODE_LOTSTEP);
   int x = 0; if(Lotstep == 0.01) x = 2; else x = 1;
    
    for(int i = OrdersTotal()-1; i >= 0; i--){
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))continue;
   if( Magic == OrderMagicNumber() )
 {
int Tk=100;
double LOTS = OrderLots();
double Dividelot = NormalizeDouble(LOTS * 0.5, x);
     
         if(OrderType() == OP_BUY&& OrderStopLoss()<OrderOpenPrice()){
            if(Bid > OrderOpenPrice()+(Tk*Point)){
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+50*Point,0,0,clrNONE );
               OrderClose(OrderTicket(), Dividelot*2, Bid, 3, Red);
            }
         }
         if(OrderType() == OP_SELL&& OrderStopLoss()>OrderOpenPrice()){
          
            if(Ask< OrderOpenPrice()-(Tk*Point)){
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-50*Point,0,0,clrNONE );
               OrderClose(OrderTicket(),Dividelot*2, Ask, 3, Red);
            }
         }
      }
   }

}
 
 I follow your instruction but it still doesn't work
Keith Watford
Moderator
9623
Keith Watford 2014.11.23 13:29  
micoul81:
 I follow your instruction but it still doesn't work

What do you mean "it still doesn't work"?

What doesn't work? 

12
To add comments, please log in or register