codes that returns the direction (buy/sell) of the last open position on a specific currency

 
bool SelectLastOpenTrade()
  {
   int lastOrder = -1;
   int total = OrdersTotal();
   for(int i = total-1; i >= 0; i--)
     {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
        {
         lastOrder = i;
         break;
        }
     } 
   return(lastOrder >= 0);
  }

int LastOpenTradeDirection()
  {
   if(SelectLastOpenTrade())
     {
      int direction = 0;
      if(OrderType() == OP_BUY)
        direction = 1;
      else if(OrderType() == OP_SELL)
        direction = -1;
      return(direction);
     }
   return(0);
  }

Good Day to all traders. I am developing an ea that will buy or sell if the price will crosses the trend line. I want to refine some conditions that makes the EA buy and sell and buy and so on...and vice versa. Is it possible to have a code that will determine the last open position (buy/sell) so that the position will not be repeated (buy, buy, sell, sell)? 

it like 

if there is a buy open, then the next well be sell open, and then buy open ans so on... and vice versa. 

is it possible to insert the date and time i think something is lack in here

Thank you in advance and God Bless you all.

 
Loop through the orders and compare the OrderOpenTime(), save the latest in a variable and update it every pass through the loop if necessary.
 

We could do that but in stead i'm just gonna tell you this:  "Pastperformance is no guarantee of future results."

If you really want you can filter 

OrderOpenTime()

But you can also just use a boolean or integer called

bool direction;

And set it when you open the order then there is no need to select orders for that.

 

Bare in mind there is practically nothing that has not been programmed for MQ4/5 yet! So search first!
Its easier and the result has fff: far fewer faults than a do-it-yourself-EA generally speaking!

For example look here: https://www.mql5.com/en/docs/event_handlers/ontester!

And there are many many others following this idea!

 
Carl Schreiber:

Bare in mind there is practically nothing that has not been programmed for MQ4/5 yet! So search first!
Its easier and the result has fff: far fewer faults than a do-it-yourself-EA generally speaking!

For example look here: https://www.mql5.com/en/docs/event_handlers/ontester!

And there are many many others following this idea!

Sometimes giving a trust yo the unknowm product is not easy. Making a personal product gives more confidence rather trusting the unknown product, as well as personal product is easy to modify and improve. 
 

Please do not double post.

I have removed your other topic.

 
long PositionDirection=PositionGetInteger(POSITION_TYPE);

   if(PositionDirection==POSITION_TYPE_BUY) 
   {

   }
   if(PositionDirection==POSITION_TYPE_SELL) 
   {

   }   

Using this you can pick what do you want to do with specific trade. So if its short then . . .

I think you can also use it as:

PositionDirection=POSITION_TYPE_BUY - and this should give you direction into variable

Reason: