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

 
for(BuyPos=PositionsTotal()-1;BuyPos>=0;BuyPos--)
   {
      string symbol=PositionGetSymbol(BuyPos);
      if(Symbol()==symbol && PositionGetInteger(POSITION_TYPE)==0)
      {
         ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
         if(PositionSelectByTicket(PositionTicket))
         {
            double profit=0;
            if(PositionGetDouble(POSITION_PROFIT)>profit)
            {
               PositionTicket=PositionGetInteger(POSITION_TICKET);
               profit=PositionGetDouble(POSITION_PROFIT);
               Print("A", profit);
            }
         }
      }      
   }

Thank seems to work fine if you have a positive profit. But I can't figure it how I can get from the Sell Positions that are in loss the one is losing less. Thanks

 
No it doesn't. It cycles through all Positions that are in Profit it doesn't return only the one with the biggest profit.
 
I think i get what you are trying to accomplish .
You want to "Absorb" losses into winning positions but keep the closed "set" in profit , right?
 
Lorentzos Roussos:
I think i get what you are trying to accomplish .
You want to "Absorb" losses into winning positions but keep the closed "set" in profit , right?

I think the only way to get the Position that is winning the most is this:(I have to add commissions and swap) but I think the only way is through a loop. So this way I can get the Position with the smallest loss or the largest profit.

profit=-1000000;      
for(BuyPos=PositionsTotal()-1;BuyPos>=0;BuyPos--)
   {
      string symbol=PositionGetSymbol(BuyPos);
      if(Symbol()==symbol && PositionGetInteger(POSITION_TYPE)==0)
      {
         ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
         if(PositionSelectByTicket(PositionTicket))
         {
               if(PositionGetDouble(POSITION_PROFIT)>profit)profit=PositionGetDouble(POSITION_PROFIT);
               Print(profit);
               Print(PositionTicket);

         }
      }      
   }
 

I see . Try these :

//totals
int Buys=0;
int Sells=0;
double BuysPL=0;//profit/loss
double SellsPL=0;
//specifics
ulong BestBuy=0;//ticket 
double BestBuyProfit=0;
ulong WorstBuy=0;//ticket
double WorstBuyProfit=0;
ulong BestSell=0;//ticket
double BestSellProfit=0;
ulong WorstSell=0;//ticket
double WorstSellProfit=0;
  int ps=PositionsTotal()-1;
  string target_symbol=Symbol();
//Loop into positions
  for(int p=ps;p>=0;p--)
  {
  ulong temp_ticket=PositionGetTicket(p);
  //valid ticket
    if(temp_ticket>0)
    {
    //select
      if(PositionSelectByTicket(temp_ticket))
      {
      //of the target symbol
        if(PositionGetString(POSITION_SYMBOL)==target_symbol)
        {
        //Manage Buys
          if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
          {
          Buys++;
          double this_profit=PositionGetDouble(POSITION_PROFIT);
          BuysPL+=this_profit;
            //if no data reset comparators
            if(Buys==1)
            {
            BestBuyProfit=this_profit;
            WorstBuyProfit=this_profit;
            BestBuy=temp_ticket;
            WorstBuy=temp_ticket;
            }
            //if data compare
            if(Buys>1)
            {
              if(this_profit>BestBuyProfit) 
              {
              BestBuyProfit=this_profit;
              BestBuy=temp_ticket;
              }
              if(this_profit<WorstBuyProfit)
              {
              WorstBuyProfit=this_profit;
              WorstBuy=temp_ticket;
              }
            }
          }
        //Manage Buys Ends Here 
        //Manage Sells
          if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
          {
          Sells++;
          double this_profit=PositionGetDouble(POSITION_PROFIT);
          SellsPL+=this_profit;
            //if no data reset comparators
            if(Sells==1)
            {
            BestSellProfit=this_profit;
            WorstSellProfit=this_profit;
            BestSell=temp_ticket;
            WorstSell=temp_ticket;
            }
            //if data compare
            if(Sells>1)
            {
              if(this_profit>BestSellProfit) 
              {
              BestSellProfit=this_profit;
              BestSell=temp_ticket;
              }
              if(this_profit<WorstSellProfit)
              {
              WorstSellProfit=this_profit;
              WorstSell=temp_ticket;
              }
            }          
          }
        //Manage Sells Ends Here 
        }
      //of the target symbol ends here 
      }
    //select ends here 
    }
  //valid ticket ends here 
  }
//Loop into positions ends here