Help Needed to find the most Profitable Buy Position and most Profitable Sell Position.

 
Hi,

I'm trying to find a way to find the most profitable buy position and the most profitable sell position. If the total lot size of these positions is greater than total lot size * 6 to close the positions.

Thanks in advance for your help
 

Christos Papageorgiou:

I'm trying to find a way to find the most profitable buy position and the most profitable sell position.

If the total lot size of these positions is greater than total lot size * 6 to close the positions.

Thanks in advance for your help

  1. The current price is the most profitable buy position if the market is going to go up in the future. It is the most profitable sell position if the market is going to go down in the future. Do you have a crystal ball?

  2. You want "X > 6X" to close positions. Makes no sense. Lot size is constant after order opens. Makes no sense.

  3. Help you with what? You haven't stated a problem, you stated a want.
 
William Roeder:
  1. The current price is the most profitable buy position if the market is going to go up in the future. It is the most profitable sell position if the market is going to go down in the future. Do you have a crystal ball?

  2. You want "X > 6X" to close positions. Makes no sense. Lot size is constant after order opens. Makes no sense.

  3. Help you with what? You haven't stated a problem, you stated a want.

Hi sorry I wasn't so clear.

Let's assume that I have on the same Symbol Multiple Positions for example 20. Some of them are Buy Positions and some of them are Sell Positions. I'm trying to find out in case of Up Trend which Buy Position has the biggest profit and which sell position has the less loss.

So this is my code so far.

//+--------------------------------------------------------------------------------------------------+
//| Check and Close Opposite Positions                                                               |
//+--------------------------------------------------------------------------------------------------+ 
//Buy Positions Profit
void BuyPositionsProfitSymbol1t1()
{
   double BuyPositionsProfit1t1=0;
   double BuyPositionLot1t1=0;
   double SellPositionsProfit1t1b=0;
   double SellPositionLot1t1b=0;
   if(PositionsTotal()>0)
   {
      for(int cntB=0; cntB<=PositionsTotal(); cntB++)
      {
      string symbolB=PositionGetSymbol(cntB);
      ulong ticketB = PositionGetTicket(cntB);
      if(Symbol()==symbolB && PositionGetInteger(POSITION_TYPE)==0)
      {
         if(PositionGetDouble(POSITION_SWAP)>=0)
         {
            BuyPositionsProfit1t1=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
            BuyPositionLot1t1=PositionGetDouble(POSITION_VOLUME);
            for(int cntS=0; cntS<=PositionsTotal(); cntS++)
            {
            string symbolS=PositionGetSymbol(cntS);
            ulong ticketS = PositionGetTicket(cntS);
            if(Symbol()==symbolS && PositionGetInteger(POSITION_TYPE)==1)
            {
               if(PositionGetDouble(POSITION_SWAP)>=0)
               {
                  SellPositionsProfit1t1b=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
                  SellPositionLot1t1b=PositionGetDouble(POSITION_VOLUME);
                  if(BuyPositionsProfit1t1 + SellPositionsProfit1t1b>0+(BuyPositionLot1t1+SellPositionLot1t1b)*7)
                  {
                     trade.PositionClose(ticketB,-1);
                     trade.PositionClose(ticketS,-1);
                     Print("Buy - Individual Positions Close A");
                     break;
                  }
               }
               if(PositionGetDouble(POSITION_SWAP)<0)
               {
                  SellPositionsProfit1t1b=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
                  SellPositionLot1t1b=PositionGetDouble(POSITION_VOLUME);
                  if(BuyPositionsProfit1t1 + SellPositionsProfit1t1b>0+(BuyPositionLot1t1+SellPositionLot1t1b)*7)
                  {
                     trade.PositionClose(ticketB,-1);
                     trade.PositionClose(ticketS,-1);
                     Print("Buy - Individual Positions Close B");
                     break;
                  }
               }
            } 
         }
         }
         if(PositionGetDouble(POSITION_SWAP)<0)
         {
            BuyPositionsProfit1t1=PositionGetDouble(POSITION_PROFIT)-MathAbs(PositionGetDouble(POSITION_SWAP));
            BuyPositionLot1t1=PositionGetDouble(POSITION_VOLUME);
            
            for(int cntS=0; cntS<=PositionsTotal(); cntS++)
            {
            string symbolS=PositionGetSymbol(cntS);
            ulong ticketS = PositionGetTicket(cntS);
            if(Symbol()==symbolS && PositionGetInteger(POSITION_TYPE)==1)
            {
               if(PositionGetDouble(POSITION_SWAP)>=0)
               {
                  SellPositionsProfit1t1b=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
                  SellPositionLot1t1b=PositionGetDouble(POSITION_VOLUME);
                  if(BuyPositionsProfit1t1 + SellPositionsProfit1t1b>0+(BuyPositionLot1t1+SellPositionLot1t1b)*7)
                  {
                     trade.PositionClose(ticketB,-1);
                     trade.PositionClose(ticketS,-1);
                     Print("Buy - Individual Positions Close C");
                     break;
                  }
               }
               if(PositionGetDouble(POSITION_SWAP)<0)
               {
                  SellPositionsProfit1t1b=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
                  SellPositionLot1t1b=PositionGetDouble(POSITION_VOLUME);
                  if(BuyPositionsProfit1t1 + SellPositionsProfit1t1b>0+(BuyPositionLot1t1+SellPositionLot1t1b)*7)
                  {
                     trade.PositionClose(ticketB,-1);
                     trade.PositionClose(ticketS,-1);
                     Print("Buy - Individual Positions Close D");
                     break;
                  }
               }
            } 
         }
         }
      } 
     }
   }
}

//Sell Positions Profit
void SellPositionsProfitSymbol1t1()
{
   double SellPositionsProfit1t1=0;
   double SellPositionLot1t1=0;
   double BuyPositionsProfit1t1s=0;
   double BuyPositionLot1t1s=0;
   if(PositionsTotal()>0)
   {
      for(int cntS=0; cntS<=PositionsTotal(); cntS++)
      {
      string symbolS=PositionGetSymbol(cntS);
      ulong ticketS = PositionGetTicket(cntS);
      if(Symbol()==symbolS && PositionGetInteger(POSITION_TYPE)==1)
      {
         if(PositionGetDouble(POSITION_SWAP)>=0)
         {
            SellPositionsProfit1t1=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
            SellPositionLot1t1=PositionGetDouble(POSITION_VOLUME);
            for(int cntB=0; cntB<=PositionsTotal(); cntB++)
            {
            string symbolB=PositionGetSymbol(cntB);
            ulong ticketB = PositionGetTicket(cntB);
            if(Symbol()==symbolB && PositionGetInteger(POSITION_TYPE)==0)
            {
               if(PositionGetDouble(POSITION_SWAP)>=0)
               {
                  BuyPositionsProfit1t1s=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
                  BuyPositionLot1t1s=PositionGetDouble(POSITION_VOLUME);
                  if(SellPositionsProfit1t1 + BuyPositionsProfit1t1s>0+(BuyPositionLot1t1s+SellPositionLot1t1)*7)
                  {
                     trade.PositionClose(ticketB,-1);
                     trade.PositionClose(ticketS,-1);
                     Print("Sell - Individual Positions Close E");
                     break;
                  }
               }
               if(PositionGetDouble(POSITION_SWAP)<0)
               {
                  BuyPositionsProfit1t1s=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
                  BuyPositionLot1t1s=PositionGetDouble(POSITION_VOLUME);
                  if(SellPositionsProfit1t1 + BuyPositionsProfit1t1s>0+(BuyPositionLot1t1s+SellPositionLot1t1)*7)
                  {
                     trade.PositionClose(ticketB,-1);
                     trade.PositionClose(ticketS,-1);
                     Print("Sell - Individual Positions Close F");
                     break;
                  }
               }
            } 
         }
         }
         if(PositionGetDouble(POSITION_SWAP)<0)
         {
            SellPositionsProfit1t1=PositionGetDouble(POSITION_PROFIT)-MathAbs(PositionGetDouble(POSITION_SWAP));
            SellPositionLot1t1=PositionGetDouble(POSITION_VOLUME);
            
            for(int cntB=0; cntB<=PositionsTotal(); cntB++)
            {
            string symbolB=PositionGetSymbol(cntB);
            ulong ticketB = PositionGetTicket(cntB);
            if(Symbol()==symbolB && PositionGetInteger(POSITION_TYPE)==0)
            {
               if(PositionGetDouble(POSITION_SWAP)>=0)
               {
                  BuyPositionsProfit1t1s=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
                  BuyPositionLot1t1s=PositionGetDouble(POSITION_VOLUME);
                  if(SellPositionsProfit1t1 + BuyPositionsProfit1t1s>0+(BuyPositionLot1t1s+SellPositionLot1t1)*7)
                  {
                     trade.PositionClose(ticketB,-1);
                     trade.PositionClose(ticketS,-1);
                     Print("Sell - Individual Positions Close G");
                     break;
                  }
               }
               if(PositionGetDouble(POSITION_SWAP)<0)
               {
                  BuyPositionsProfit1t1s=PositionGetDouble(POSITION_PROFIT)+MathAbs(PositionGetDouble(POSITION_SWAP));
                  BuyPositionLot1t1s=PositionGetDouble(POSITION_VOLUME);
                  if(SellPositionsProfit1t1 + BuyPositionsProfit1t1s>0+(BuyPositionLot1t1s+SellPositionLot1t1)*7)
                  {
                     trade.PositionClose(ticketB,-1);
                     trade.PositionClose(ticketS,-1);
                     Print("Sell - Individual Positions Close H");
                     break;
                  }
               }
            } 
         }
         }
      } 
     }
   }
}
 
Are you looking for the optimal price to close your set of open trades (as a common SL/TP) ?
 
Lorentzos Roussos:
Are you looking for the optimal price to close your set of open trades (as a common SL/TP) ?

I'm trying to close two opposite Positions with Profit. Let's assume that I have a 3 Buy Positions andone of them is winning 10,000 and I have 4 Sell Positions that are losing. From the Sell Positions the Position with the smallest loss is -100. I want to select and close the Buy Position that is winning the most and the Sell Position the is losing the less. At this example with Total Profit 9,900 (10,000 from the Buy Position - -100 of the Sell Position).

 
Since the BUY and SELL positions are closed at different prices. Some are ASK and others are BID. Therefore, it is better to close without SL/TP.
 
Konstantin Nikitin:
Since the BUY and SELL positions are closed at different prices. Some are ASK and others are BID. Therefore, it is better to close without SL/TP.

Yes I can add instead of closing the positions to start Direct Trailing to both positions. The problem is on how I can find and select these 2 orders.

 

So for the Long position you filter 

if(OrderProfit()>profit)
{
 ticket=OrderTicket();
 profit=OrderProfit();
}

Put that in the orderselect loop and what comes out of it will be the the highest profit and it's ticket number.

Then for the Short position you add one more element.

if(OrderProfit()<0)
 {
  //...
 }

So that it only handles positions that have a profit (or loss) below zero to split positive and negative positions.

I had not seen your mql5 but its a similar process.

 
Marco vd Heijden:

So for the Long position you filter 

Put that in the orderselect loop and what comes out of it will be the the highest profit and it's ticket number.

Then for the Short position you add one more element.

So that it only handles positions that have a profit (or loss) below zero to split positive and negative positions.

I had not seen your mql5 but its a similar process.

Thank you very much for your reply Marco.  I will try it in a few minutes. I really appreciate your help.

 

More accurately

if(OrderProfit()+OrderSwap()+OrderCommission()>profit)
{
 ticket=OrderTicket();
 profit=OrderProfit()+OrderSwap()+OrderCommission();
}
 
Konstantin Nikitin:

More accurately

Thank you very much Konstantin. Is there any way to get both profit of the  two positions buy and sell and add their profit. I assume I have to use 2 for loops. Thanks

Reason: