Download MetaTrader 5
To add comments, please log in or register
Why to sell products in MetaTrader AppStore? Find that out from the article!
Da Silva Andre Luiz
125
Da Silva Andre Luiz 2016.01.26 23:45 

Hi, anyone could help me with my EA that close parial open trades when it reach a X% in profit and move SL to BE, but when lot is nod divisable by 2 like 0.03 its only moves to BE and don`t close partially.

extern double  Percent_Break = 1;// Risk Reward to close partial
extern double  Percent_RealizacaoParcial = 50;//% of open open position 

 

double openPrice, stopPrice, BE_Pips, StpB, StpS, CalculoRP, TamanhoLot, CloseLot; 
    
    CalculoRP = 100/Percent_RealizacaoParcial;//Partial calc
    
    int total=OrdersTotal();
    for (int cnt=0;cnt<total;cnt++)
    { 
     OrderSelect(cnt, SELECT_BY_POS);   
     int mode=OrderType();    
        if ( OrderSymbol()==Symbol() ) 
        {
  
         
         string Coment;
         openPrice = OrderOpenPrice();
         stopPrice = OrderStopLoss();
         TamanhoLot = OrderLots();//Lot Size
         Coment = OrderComment();
         CloseLot = TamanhoLot/CalculoRP;
         BE_Pips = ((openPrice - stopPrice)*Percent_Break);
       
         if(BE_Pips<0) BE_Pips = BE_Pips*-1;
         
          StpS = openPrice-BE_Pips;
          StpB = openPrice+BE_Pips;
          
      //Alert("" +BE_Pips+"" );
        if(mode == OP_SELL && stopPrice > openPrice && Ask<= StpS && OrderSymbol()==Symbol())
        {
                   
           if (StringFind(Coment, "from #", 0) == -1)
           {
            OrderClose(OrderTicket(), CloseLot, Ask, 0); 
            
           }
           OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),MagicNumber);
           if(!WindowScreenShot(StringConcatenate("Trades\\"+Symbol(),"_",OrderType(),"_",Period(),"_",TimeToStr(TimeCurrent(),TIME_DATE),"_",Hour(),".",Minute(),".png"),1920,768)) Print(GetLastError()); 
        }   
        else
        {
        
            if(mode == OP_BUY && stopPrice < openPrice && Bid>= StpB && OrderSymbol()==Symbol() )
               {
                     if (StringFind(Coment, "from #", 0) == -1)
                     {
                        OrderClose(OrderTicket(), CloseLot, Bid, 0);
                     }
                     
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),MagicNumber);
               if(!WindowScreenShot(StringConcatenate("Trades\\"+Symbol(),"_",OrderType(),"_",Period(),"_",TimeToStr(TimeCurrent(),TIME_DATE),"_",Hour(),".",Minute(),".png"),1920,768)) Print(GetLastError()); 
               }
               }
      }
      }

 
  }

Thanks, i`m learning so i`m sorry for this question :D

whroeder1
13590
whroeder1 2016.01.27 02:28  
andreforex: t close parial open trades when it reach a X% in profit and move SL to BE, but when lot is nod divisable by 2 like 0.03
The amount closed and the remaining amount must both be at least min lot and a multiple of lot step; close 0.02 and keep 0.01 or visa versa. See my code
Edi Dimitrovski
289
Edi Dimitrovski 2016.01.27 19:03  
I found (maybe I haven't searched enouqh) that <Mt5 does not have built in function for NormalizeLotsByPercent(lots,percent) or near similar to the subject. In mt3 was, just assigning the predefined exact values, if 0.03 than close 0.01 or 0.02 than last 0.01, maybe someone made with %'s, but should be as far as I know :
// should be selected the order in the trade-pool from previous functions
double NormalizeLotsByPercent( double c_lots, double c_percent ) {
 double minLots = MarketInfo(OrderSymbol(), 23);
 double stepLots = MarketInfo(OrderSymbol(), 24);
 double c_lotsPart = minLots;
 while(c_lotsPart < (c_lots*c_percent/100.0)) { // val nearest upper %
  c_lotsPart = c_lotsPart + stepLots;
 }
 return c_part;
}
In Modify instead of OrderOpenPrice() should be Ask, since it could be lower than the open price..
OrderModify(OrderTicket(),0.0,Ask,OrderTakeProfit(),MagicNumber);
whroeder1
13590
whroeder1 2016.01.27 20:00  
edddim: :In Modify instead of OrderOpenPrice() should be Ask, since it could be lower than the open price..
You can not change the open price of an open order; only pending orders.
whroeder1
13590
whroeder1 2016.01.27 20:49  
You can't place the SL at Ask (or for buy, the Bid) because 1) it would be triggered immediately and 2) doesn't abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Edi Dimitrovski
289
Edi Dimitrovski 2016.01.27 21:55  

 ( haven't checked other variables :( )

if(mode == OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && stopPrice > openPrice && Ask<= StpS ) {
  double part_Lots = NormalizeLotsByPercent(TamanhoLot, Percent_RealizacaoParcial);
  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0);
  bool close_only_part = OrderClose(OrderTicket(), part_Lots, Ask, 0); 
} 

 As alternative solution.

Edit modified.

/
To add comments, please log in or register