problems with SELECT_BY_POSITION stop loss

 
Hi everyone, I am wondering because my stop loss loop simply looks at the last order of a particular symbol() and if the symbol matches then it checks for the stop loss and then if the stop loss exists it allows another event, yet for some reason it works most of the time but once in awhile it will execute the event even if the stop loss does not exist and I am wondering if this might be because by SELECT_BY_POSITION which might allow the program to look at positions based on the symbol() or the time entered, or the profit, etc. And as a result find a position with the right symbol() but not the last position entered with that symbol() - basically looking at positions out of order. And if this is the case, and I want to look at the last position entered for a symbol() what would be the best way to access the stop loss of the last position entered into of a particular symbol() accurately?
 

The sure prove method would be to use Time. Because we'll looking for Last order right. Well, I have used that method but it slows my backtests down. I'm currently using this code below for Last order search. It's working good so far. I've heard if you sort your history then you might screw the sequence position but I haven't had reason to sort the history so ditto.

 

int Last_Order(int Magic_Number)
{
int Last_Order=0;
//+------------------------------------------------+
//+------------------------------------------------+
for(int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
if (OrderMagicNumber()==Magic_Number)
{
   if(OrderType()==OP_BUY)
   {
      Last_Order=1;
      break;
   }
   else
   if(OrderType()==OP_SELL)
   {
      Last_Order=-1;
      break;
   }
}
}

You could replace the if (magic) with if (ordertime > ordertime) then Last_Order=orderticket. After it goes through all orders in History. The last order would have the highest time therefore you would have the ticket# of the latest order. Hope that helps.

 

*edit- and if you're Searching for Active orders then use MODE_TRADES instead. Also take the break; out if using the Time method. Oh and you need to OrderSelect by Ticket once you have the Correct Ticket to perform your stop_loss calculations. lol.. Simple starting to sound complex. 

 
ubzen:

The sure prove method would be to use Time. Because we'll looking for Last order right. Well, I have used that method but it slows my backtests down. I'm currently using this code below for Last order search. It's working good so far. I've heard if you sort your history then you might screw the sequence position but I haven't had reason to sort the history so ditto.

You could replace the if (magic) with if (ordertime > ordertime) then Last_Order=orderticket. After it goes through all orders in History. The last order would have the highest time therefore you would have the ticket# of the latest order. Hope that helps.

*edit- and if you're Searching for Active orders then use MODE_TRADES instead. Also take the break; out if using the Time method. Oh and you need to OrderSelect by Magic once you have the Correct magic to perform your stop_loss calculations. lol.. Simple starting to sound complex.


I took up your idea about time and I was realizing that I have been ordering by symbol and not history, so what I will try to do first is order the positions according to entry, meaning the oldest will be last in the list and the newest first, that way when I select by position it should get the order right. If this works problemed solved, if not I will have to go into more complicated steps, which tend be cumbersome but at times necessary. Thanks for your help.
 
0531:
Hi everyone, I am wondering because my stop loss loop simply looks at the last order of a particular symbol() and if the symbol matches then it checks for the stop loss and then if the stop loss exists it allows another event, yet for some reason it works most of the time but once in awhile it will execute the event even if the stop loss does not exist and I am wondering if this might be because by SELECT_BY_POSITION which might allow the program to look at positions based on the symbol() or the time entered, or the profit, etc. And as a result find a position with the right symbol() but not the last position entered with that symbol() - basically looking at positions out of order. And if this is the case, and I want to look at the last position entered for a symbol() what would be the best way to access the stop loss of the last position entered into of a particular symbol() accurately?

Here's what you need. It looks for the largest ticket number (because ticket numbers are created in an increasing fashion), then returns the stoploss value of that ticket.



double RecentStoploss()
{
double SL=0;
double LargestTicket=0;
for(int k=OrdersTotal()-1;k>=0;k--)
{
        if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES)== true  && OrderMagicNumber() == MagicNo)
        {
                if(OrderTicket()>LargestTicket)
                {
                        LargestTicket = OrderTicket();
                        SL= StopLoss();
                }
        }
}
return(SL);
}
 
Nice, that's even easier :)
 
ubzen:
Nice, that's even easier :)
double LargestTicket=0;
for(int k=OrdersTotal()-1; k>=0; k--) if(
   OrderSelect(k, SELECT_BY_POS, MODE_TRADES)
&& OrderSymbol()      == Symbol()
&& OrderMagicNumber() == MagicNo
&& OrderTicket()      >  LargestTicket){
    LargestTicket = OrderTicket();
    SL= StopLoss();
}

Even easier. You should look at the symbol also in case the same EA is on different pairs.

The problem you might have had looking through history was history contains more than trades https://www.mql5.com/en/forum/126192