Help Me, How Script to Filter Profit Open Position - page 2

 
asror bersemi:

still get solution, i have not right code now 

Despite repeated requests, you have not edited and inserted your code correctly so i have deleted your code.

 
asror bersemi:

this my code :

<Deleted. Edit your post and format your code properly using the "Alt-S" or by clicking the code Icon "</>" in the posting toolbar.>

but cant work for read 4 from 10 buys position with highest profit  

double profit4buy()
{
      int  tt=0;
      double prf4buy=0;
      //for(i=OrdersTotal()-1; i>=0; i--)
      for(i = 0; i <=4; i++)
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol())
        {
          if(OrderType()==OP_BUY)
          {
            tt++;
            if(tt<=4)
            prf4buy+=OrderProfit()+OrderSwap()+OrderCommission();
               
          }
           
              
        }
 return(prf4buy);

}
 
asror bersemi:

Now try using the suggestions that Fernando made in post #4 and post your new code.

 
Keith Watford:

Now try using the suggestions that Fernando made in post #4 and post your new code.

still trial code, not yet fix

 
Keith Watford:

Now try using the suggestions that Fernando made in post #4 and post your new code.

void OnTick()
{ 
  int x=0;
  double trades[];
  int total=OrdersTotal();
  if(total>0)
  {
  ArrayResize(trades,total);
  for( x=0; x<total; x++)
     {
     if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES==true)&&OrderType()==0 && OrderSymbol()==Symbol())  
        trades[x]=OrderOpenPrice();           
        Print( "No : " ,x, " " , DoubleToStr(trades[x],5));  // just for print, not yet sorted
     } 
  }
         
 ArraySort(trades,WHOLE_ARRAY,0,MODE_ASCEND); 
      
 Comment ("\n", " price 4 : " ,DoubleToStr(trades[3],5),
          "\n", " price 3 : " ,DoubleToStr(trades[2],5),
          "\n", " price 2 : " ,DoubleToStr(trades[1],5),
          "\n", " price 1 : " ,DoubleToStr(trades[0],5)
         ); 
 
 
  
  }   

I think now is ok..

Files:
sorted.jpg  181 kb
 
Fernando Ca
Fernando Carreiro:
  1. Use "OrdersTotal()" to obtain the count of currently open positions.
  2. Create an array of this maximum size base on the above count
  3. Create an index variable for the array and set it to an initial value of "0".
  4. Loop through all the open positions using "OrderSelect()" to select them.
  5. Filter by symbol "OrderSymbol()", magic number "OrderMagicNumer()" and if necessary, by type "OrderType()".
  6. Obtain the profit for the position using "OrderProfit()".
  7. Save the profit value in the array at the current array index, and increment the array index variable.
  8. After the loop is concluded, sort the array, in descending order with with "ArraySort()", based on the count obtained from the array index variable.
  9. Create a new variable for the total profit and set it to an initial value of "0.00".
  10. Loop through the first 4 (or less based on array index count) elements of the array, and add the value to the total profit varia
    int x=0;
      double trades[][2]; 
      double tiket=0;
      int total=OrdersTotal();
      if(total>0)
      {
      ArrayResize(trades,total);
      for( x=0; x<total; x++)
         {
         if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES==true)&&OrderType()==0 && OrderSymbol()==Symbol())  
            trades[x][0]=OrderOpenPrice();
            trades[x][1]=OrderTicket();
         } 
      }
             
     ArraySort(trades,WHOLE_ARRAY,0,MODE_ASCEND); 
     
       
     if (x==0) tiket=((int)trades[x][1]);
    
    

hello Fernando, i have problem with Array.. the message " Array Out Of Range with code " at this code,, can you explain to me?.. thank you 

if (x==0) tiket=((int)trades[x][1]);