Download MetaTrader 5

How to take only one action on lots per condition??? - page 2

To add comments, please log in or register
Michael Gad
227
Michael Gad  
now it doesn't divide the lot
Keith Watford
Moderator
10570
Keith Watford  
micoul81:
now it doesn't divide the lot

Why do you divide the lot and then multiply it by 2?

OrderClose(OrderTicket(), Dividelot*2, Bid, 3, Red);
Michael Gad
227
Michael Gad  
  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=10;
double LOTS = OrderLots();
double Dividelot = NormalizeDouble(LOTS * 0.5, x);
     
         if(OrderType() == OP_BUY){
            if(Bid > OrderOpenPrice()+(Tk*Point)){
               OrderClose(OrderTicket(), Dividelot, Bid, 3, Red);
            }
         }
         if(OrderType() == OP_SELL){
            if(Ask< OrderOpenPrice()-(Tk*Point)){
               OrderClose(OrderTicket(),Dividelot, Ask, 3, Red);
            }
         }
      }
   }

}
 

 Sorry it's an error when i paste the code , still have the same problem

whroeder1
16274
whroeder1  
micoul81: it continue to divide lots
micoul81Sorry it's an error when i paste the code , still have the same problem
int x = 0; if(Lotstep == 0.01) x = 2; else x = 1;
double Dividelot = NormalizeDouble(LOTS * 0.5, x);
  1. You still have the same problem (multiple closings) because you didn't do anything about it. Asked and previously answered.
  2. This assumes lotstep must be a (negative) power of 10 (0.1 or 0.01) and fails for any other value. Don't assume, do it right, see my NormalizeLots.
Fabio Geraci
370
Fabio Geraci  
micoul81:

 Sorry it's an error when i paste the code , still have the same problem

Hello micould81,

 did you manage to fix your code? could you let me know where and how you implemented your PartClose function in the main EA? I have tried but I cannot managed to get the EA to loop in and through the partClose function?

 

thanks 

JD4
1100
JD4  
fabio_geraci:

Hello micould81,

 did you manage to fix your code? could you let me know where and how you implemented your PartClose function in the main EA? I have tried but I cannot managed to get the EA to loop in and through the partClose function?

 

thanks 

You could also try following the suggestions here, go through your code and try to see in yours something similar is happening, and try to correct it as WHRoeder and GumRai are telling micould81 to do.  I know your code is not likely to be exactly the same, but it is a starting point.  Then, if you are able to fix your code, you could also post a before and after here, so people can see what was wrong, and what you did to fix it.
Fabio Geraci
370
Fabio Geraci  
JD4:
You could also try following the suggestions here, go through your code and try to see in yours something similar is happening, and try to correct it as WHRoeder and GumRai are telling micould81 to do.  I know your code is not likely to be exactly the same, but it is a starting point.  Then, if you are able to fix your code, you could also post a before and after here, so people can see what was wrong, and what you did to fix it.

JD4,

 I have tried several approach mostly based on the work of Mr. Hopwood. the problem is that the PartClose function doesn t get call so I have tried micould81 approach, keeping the start() as it is now, and same issue, that is the reason of my question, where should i put the call for the partClose function. As it stands atm.

int start()
{
//----
   static bool TradeExists;
   if (OrdersTotal() == 0)
   {
      TicketNo = -1;
   }//if (OrdersTotal() == 0)
   ReadIndicatorValues();
   //Find open trades
   if (OrdersTotal() > 0)
   {
      CountOpenTrades();
      TradeExists = DoesTradeExist();
      if (TradeExists)
      //{
      //   if (OrderProfit() > 0) 
         TradeManagementModule();
         LookForTradeClosure();
      //}//if (TradeExists)
   }//if (OrdersTotal() > 0)
   if (TicketNo == -1)
   {
      //if(timeprev==Time[0]) return(0);
      //timeprev=Time[0];
      LookForTradingOpportunities();
   }
//----
   return(0);
}
JD4
1100
JD4  

I am still in the process of learning how to code in MQL, so while I can recommend something on my similar class knowledge of Java, it is likely to be bad information, so I am sometimes leery to share unless I know for sure.  Judging by what little snippet of code you posted here (good function names btw), if it were me, I would have the PartClose get called from within your LookForTradeClosure function, as it would seem to be the most likely place you would have the code to decide if you wanted to close part of your trade.

Another possibility I have seen other people use, but this could offset any profits depending on your broker's pricing structure, is open multiple trades at the same time.  And if certain points are hit, close out 1 or more of the individual trades.

12
To add comments, please log in or register