partial close order depending on order size using switch

 

Hi guys, I wonder if someone can help here.

I want my EA to to close orders partial. i.e when the EA has placed an order with 1.6lots,  if the EA is in profit, and i have achieved 0.8lots profit then it should close 0.8 lots.

Below is my code.

First part shows my partial close functions - I cant see anything wrong in here

Second part shows where i look for existing order for the same magic number and get the lot value which will can feed to my switch

Third is my switch... Not sure what is missing because i think i have everything but.....

//+------------------------------------------------------------------+
//| Partlia Close Orders                                             |
//+------------------------------------------------------------------+
void PartialCloseTrade()
   {
   for(int i=0; i < OrdersTotal();i++)
     {
     int K=(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)); 
     int Type=OrderType();double OpenPrice=OrderOpenPrice();double LOTS=OrderLots();
     {
      
      if(OrderMagicNumber()==Magic && OrderSymbol()==_Symbol)
         {
            double l;                  
            double p;                  
            double t;                  
            double v;                  
            double s=0;
               l=MarketInfo(Symbol(), MODE_LOTSIZE);
               p=MarketInfo(Symbol(), MODE_POINT);
               t=MarketInfo(Symbol(), MODE_TICKSIZE);
               v=MarketInfo(Symbol(), MODE_TICKVALUE);
            if(Type==OP_BUY)
               {
               s+=(Bid-OrderOpenPrice())/p*v/t/l*CalculatedLotSize();
               s+=OrderCommission()+OrderSwap();
               if(OrderProfit() >= s)
                  {
                  if(!OrderClose(OrderTicket(),CalculatedLotSize(),Bid,5,clrRed))//CalculatedLotSize(CurrentLotSize())
                    {
                     int err=GetLastError();
                     Print("Encountered an error during Buy Partial Close"+(string)err+" "+ErrorDescription(err));
                    }
                  }
               }
            else if(Type==OP_SELL)
               {
               s+=(OrderOpenPrice()-Ask)/p*v/t/l*CalculatedLotSize();
               s+=OrderCommission()+OrderSwap();
               if(OrderProfit() >= s)
                  {
                  if(!OrderClose(OrderTicket(),CalculatedLotSize(),Ask,5,clrRed))
                     {
                     int err=GetLastError();
                     Print("Encountered an error during Sell Partial Close"+(string)err+" "+ErrorDescription(err));
                     }
                  }
               }
         }
         }
      }
   }
   
//+------------------------------------------------------------------+
//|Current Lot Size                                                  |
//+------------------------------------------------------------------+
int CurrentLotSize()
   {
   int caseid;
   for(int i=OrdersTotal()-1;i>=0;i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
         {
         if(OrderMagicNumber()==Magic)
            if(OrderLots()== 0.40 )
               {
               caseid=1;
               }
            else if(OrderLots()== 0.80 )
               {
               caseid=2;
               }
            else if(OrderLots()== 1.60 )
               {
               caseid=3;
               }
            else if(OrderLots()== 3.20 )
               {
               caseid=4;
               }
            else if(OrderLots()== 6.40 )
               {
               caseid=5;
               }
            else if(OrderLots()== 12.80 )
               {
               caseid=6;
               }
            else if(OrderLots()== 25.60 )
               {
               caseid=7;
               }
            else if(OrderLots()== 51.20 )
               {
               caseid=8;
               }
            else if(OrderLots()== 102.40 )
               {
               caseid=9;
               }
            else if(OrderLots()== 204.80 )
               {
               caseid=10;
               }
            else if(OrderLots()== 409.6 )
               {
               caseid=11;
               }   
            else if(OrderLots()== 819.2 )
               {
               caseid=12;
               }      
            else if(OrderLots()== 1638.4 )
               {
               caseid=13;
               }
            else if(OrderLots()== 3276.8 )
               {
               caseid=14;
               }
            else if(OrderLots()== 6553.6 )
               {
               caseid=15;
               }
            else if(OrderLots()== 13107.2 )
               {
               caseid=16;
               }
            else if(OrderLots()== 26214.4 )
               {
               caseid=17;
               }
            else if(OrderLots()== 52428.8 )
               {
               caseid=18;
               }
          }
      }
   return(caseid);
   }
//+------------------------------------------------------------------+
//|Trade lots size switch                                            |
//+------------------------------------------------------------------+

double CalculatedLotSize()
   {
   double Newlotsize = 0;
   int CurrentLotSize = CurrentLotSize();
      switch( CurrentLotSize)
      {
      case 1: Newlotsize = 0.2;break;
      case 2: Newlotsize = 0.4;break;
      case 3: Newlotsize = 0.8;break;
      case 4: Newlotsize = 1.6;break;
      case 5: Newlotsize = 3.2;break;
      case 6: Newlotsize = 6.4;break;
      case 7: Newlotsize = 12.8;break;
      case 8: Newlotsize = 25.6;break;
      case 9: Newlotsize = 51.2;break;
      case 10: Newlotsize = 102.4;break;
      case 11: Newlotsize = 204.8;break;
      case 12: Newlotsize = 409.6;break;
      case 13: Newlotsize = 819.2;break;
      case 14: Newlotsize = 1638.4;break;
      case 15: Newlotsize = 3276.8;break;
      case 16: Newlotsize = 6553.6;break;
      case 17: Newlotsize = 13107.2;break;
      case 18: Newlotsize = 26214.40;break;
      default:Newlotsize= 0.2;
      }
      return (Newlotsize);
   }
 
int CurrentLotSize = CurrentLotSize();

Lot sizes are normally doubles


switch( CurrentLotSize)

You must use an integer value as the switch expression.


Yes, all the checked case values are integers, but it doesn't make any sense considering what you are trying to achieve.

I want my EA to to close orders partial. i.e when the EA has placed an order with 1.6lots,  if the EA is in profit, and i have achieved 0.8lots profit then it should close 0.8 lots.

I don't know what 0.8 lots profit is.