Find only a result in order select

 

Good evening,

I'm trying to modify this function of my EA. The EA open several trade. What i'd like to obtain is that the function find only one thade that have the condition (in this case that the profit is >0 && <-1)

Actually it retursn all the trade with this condition.

Here my code

double serit()
{
 int profit=0;
 bool select;
 for(int i=OrdersTotal()-1;i>=0;i--)
 {
  select=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
  string sy=OrderSymbol();
  int    tt=OrdersTotal(),
         tk=OrderTicket(),
         ot=OrderType(),
         mn=OrderMagicNumber();
  double pt=OrderProfit(),
         sw=OrderSwap(),
         cm=OrderCommission(); 
  double lo=OrderLots();
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNo&&(pt+sw)>0&&(pt+sw)<-1)
   {
    profit++;
   } 
  
 }
  return(profit);
}

I know that the code isn't goog, but i'm not finding any solution.

Has someone an idea?

Thanks

 
Claudio Lasso:

Good evening,

I'm trying to modify this function of my EA. The EA open several trade. What i'd like to obtain is that the function find only one thade that have the condition (in this case that the profit is >0 && <-1)

Actually it retursn all the trade with this condition.

Here my code

I know that the code isn't goog, but i'm not finding any solution.

Has someone an idea?

Thanks

1- How is it possible that a number (profit+swap) be greater than 0 and less than -1 simultaneously.

2-getting tt (OrdersTotal) is meaningless after selecting an order. in other words ordersTotal doesn't have anything to do with orderselect.

3-it is better if you check the Symbol and magic number right after the OrderSelect, and not after taking all other things that can be unnecessary. Always try to find the best priority for your conditions.

4- your main question is not really clear to me. what would you like to have when you found a trade which satisfies your condition? 

Let's assume you want the Ticket number of that position. Then as soon as you find a position which is satisfying your conditions you can return its ticket (then you need to change your function type to int).

after the word "return"program will exit from the function.

if you don't want to exit the function then you can use "break". it will stop the loop. and continue to read whatever you have after the loop.

Reason: