Exit Code when the profit from Buy && Sell is greater than the breakeven level

 

I have a few Buy orders and Sell orders

What I want do is :

- Exit all the trades " Basket" at 5 pips more than the breakeven price of the open trades " including the swap"


What I am trying to do in the first for loop is find the value of open trades + swap and convert it to pips, and this is the part where I am getting stuck


I have tried few ideas but I have reached a dead end, I know the problem is in the first for statement but I can't solve it, any help is highly appropriated


here is the code

 SymbolPL = 0;
 OrdLots  = 0;
 Equity   = 0; 
 MinPro   = 5;
 for(i = 0; i < OrdersTotal(); i++)
   {OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      {if(OrderType() == OP_BUY)  OrdLots += OrderLots();
       if(OrderType() == OP_SELL) OrdLots -= OrderLots();      
       Equity += OrderProfit() +  OrderSwap();
      }
   } 
   
MinPro= MathRound (MathAbs(Equity /OrdLots)+MinPro); 

//--- Count the open trades 
   int i;
   int count=0;   
   for(i=0;i<OrdersTotal();i++)
    { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()==MagicNumber && OrderType()<2)
       { count++;
        }
    }

//--- find the profit in pips of the open trades 
  RefreshRates();
   double profits=0,openPrice=0,points=0;
   string sym="";
   int i;
   int totalOrders=OrdersTotal();        
   for(i=0;i<totalOrders;i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()==MagicNumber)
      {
         sym=OrderSymbol();
         openPrice=OrderOpenPrice();
         if(OrderType()==0)
         {
            profits+=(MarketInfo(sym,MODE_BID)-openPrice)/MarketInfo(sym,MODE_POINT);  
         }
         if(OrderType()==1)
         {
            profits+=(OrderOpenPrice()-MarketInfo(sym,MODE_ASK))/MarketInfo(sym,MODE_POINT);
         }   
      }  
   }
 
//--- Close when the open trades are 5 pips more than the breakeven price


     
if (count>1 && profits>MinPro ) 
{ 
   for(i = OrdersTotal()-1; i >=0; i--)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber )
         {
           OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Plum);
               
          }
      }    
} 
 
MiniMe:

I have a few Buy orders and Sell orders

What I want do is :

- Exit all the trades " Basket" at 5 pips more than the breakeven price of the open trades " including the swap"


What I am trying to do in the first for loop is find the value of open trades + swap and convert it to pips, and this is the part where I am getting stuck


I have tried few ideas but I have reached a dead end, I know the problem is in the first for statement but I can't solve it, any help is highly appropriated


here is the code


In the last loop, if you want to close orders you have to do it with the market prices, ASK and BID. Try this:




//--- Close when the open trades are 5 pips more than the breakeven price


     
if (count>1 && profits>MinPro ) 
{ 
   for(i = OrdersTotal()-1; i >=0; i--)
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber )
         {
           if(OrderType()==OP_BUY)
                OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Plum);
           if(OrderType()==OP_SELL)
                OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Plum);
    
          }
      }    
} 
 

Thanks abstract_mind but the problem is not in the order close, its in calculating the swap along with the profit and convert that into pips

I have a special function to close orders

what i want to do is use " OrdLots " and "Equity" to find how many pips the open trades represents then add to that number 5 pips to close all the open trades

the problem is in the first loop and I am not able to find the solution for it



 
MiniMe wrote >>

Thanks abstract_mind but the problem is not in the order close, its in calculating the swap along with the profit and convert that into pips

I have a special function to close orders

what i want to do is use " OrdLots " and "Equity" to find how many pips the open trades represents then add to that number 5 pips to close all the open trades

the problem is in the first loop and I am not able to find the solution for it

I assume your basket of trades has all kinds of currency pairs; calculating the $ and pips for them are different for majors (eurusd, gbpusd), crosses (eurjpy, gbpjpy) and for usdchf, usdjpy

Reason: