OrderOpenTime() problem regarding getting last order open time - page 4

 
Keith Watford:

Read my post before yours, the one that you have quoted.

Use the styler to tidy up the code.

sorry my good friend I always forget styling 

extern int tp = 100 ;
extern int sl = 50 ;

int start()

{
   if( High[2] > High[1])
     {
      if (OrdersTotal() <1 )
        {
         OrderSend(Symbol(),OP_SELL,0.01,Bid,0,Bid+sl*Point,Bid-tp*Point," sell is happening ",0,0,clrRed);
        }
      else
        {
         datetime lastOrderTime=0;
         for (int cnt=OrdersTotal()-1; cnt>=0 ; cnt--)
           {
            if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false || OrderSymbol()!=Symbol() ) continue;
              {
               if(OrderOpenTime()>lastOrderTime)
                  lastOrderTime=OrderOpenTime();
                 {
                  int m = TimeCurrent() - lastOrderTime;
                  if ( m >= 300  )
                    {
                     OrderSend(Symbol(),OP_SELL,0.01,Bid,0,Bid+sl*Point,Bid-tp*Point," sell is happening ",0,0,clrRed);
                    }
                 }
              }
           }
        }
     }
   return 0 ;
}
//+------------------------------------------------------------------+

 " Why are you doing this inside the loop?

Complete the loop and then do the check. " 


by "doing " I assume you mean OrderSend() right ? 

is there alternative for this ? 

Files:
 
ace trader 99:

sorry my good friend I always forget styling 

 " Why are you doing this inside the loop?

Complete the loop and then do the check. " 


by "doing " I assume you mean OrderSend() right ? 

is there alternative for this ? 

No, there was no OrderSend in the code when I wrote that.

Think about what your code is doing.

Loop through the orders to find the last order.

Only do anything once you have found the last order.

Until the loop has finished, you don't know whether the order is the last order.

Complete the loop before doing anything with the last order.

 
Keith Watford:

No, there was no OrderSend in the code when I wrote that.

Think about what your code is doing.

Loop through the orders to find the last order.

Only do anything once you have found the last order.

Until the loop has finished, you don't know whether the order is the last order.

Complete the loop before doing anything with the last order

I believe this is the version that you think I should be doing let me send the code : 

extern int tp = 100 ;
extern int sl = 50 ;

int start()

{
   if( High[2] > High[1])
     {
      if (OrdersTotal() <1 )
        {
         OrderSend(Symbol(),OP_SELL,0.01,Bid,0,Bid+sl*Point,Bid-tp*Point," sell is happening ",0,0,clrRed);
        }
      else
        {
         datetime lastOrderTime=0;
         for (int cnt=OrdersTotal()-1; cnt>=0 ; cnt--)
           {
            if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false || OrderSymbol()!=Symbol() ) continue;
              {
               if(OrderOpenTime()>lastOrderTime)
                  lastOrderTime=OrderOpenTime();
              }
           }
         int m = TimeCurrent() - lastOrderTime;
         if ( m >= 300  )
           {
            OrderSend(Symbol(),OP_SELL,0.01,Bid,0,Bid+sl*Point,Bid-tp*Point," sell is happening ",0,0,clrRed);
           }
        }
     }


return 0 ;
}
//+------------------------------------------------------------------+

it compiles just fine but in the back testing it does not open any trade 

Files:
 
ace trader 99:

I believe this is the version that you think I should be doing let me send the code : 

it compiles just fine but in the back testing it does not open any trade 

Yes, it will open trades. Just correct the OrderSend error.

Do a search of the forum, there must be thousands of posts about this error.


Also, in your code,

   Always use

   #property strict

   When starting a project use the "New" button top left of the editor. #property strict will be included automatically.

   If the compiler issues a warning don't ignore it, fix it.

   OrderSend();
//may give you the error that the return value of OrderSend should be checked. The minimum that should be done is to print the error if it fails
//preferably also print the open price, TP, SL.
   int ticket=OrderSend(_Symbol,OP_BUY,0.1,Ask,20,0,0,NULL,12345,0,clrNONE);
   if(ticket==-1)
      {
      Print("OrderSend for Buy order failed with error code ",GetLastError());
      }
 
Keith Watford:

Yes, it will open trades. Just correct the OrderSend error.

Do a search of the forum, there must be thousands of posts about this error.


Also, in your code,

   Always use

   #property strict

   When starting a project use the "New" button top left of the editor. #property strict will be included automatically.

   If the compiler issues a warning don't ignore it, fix it.


                                                                               coolest

                                                                         /                  \

                                                                      /                        \

ladies and gentlemen the award of being     kindest   _______     smartest       triangle  goes to   Keith Watford


thank you man for every thing .

 






 

hello again 

I wanted to create a new topic but I thought the situation might be very close to this topic 

so here is the problem 

the solution above you gave me is perfectly working on single timeframe like 1 minute or any single time frame

but I wan to do some thing that can perform multi time frame 

here is the code : 

//+------------------------------------------------------------------+
//|                                                        first.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern double lot = 0.01;

int start()

{
//+------------------------------------------------------------------+
//| // MY MAGIC NUMBERS                                                                 |
//+------------------------------------------------------------------+
   int fixednum = 50000 ;
   int MAGIC ;
   if(Symbol() == "EURUSD")
     {
      MAGIC= fixednum+1000+Period();
     }
   if(Symbol() == "GBPUSD")
     {
      MAGIC= fixednum+2000+Period();
     }
   if(Symbol() == "USDCHF")
     {
      MAGIC= fixednum+3000+Period();
     }
   if(Symbol() == "USDJPY")
     {
      MAGIC= fixednum+4000+Period();
     }
   if(Symbol() == "AUDUSD")
     {
      MAGIC= fixednum+5000+Period();
     }
   if(Symbol() == "NZDUSD")
     {
      MAGIC= fixednum+6000+Period();
     }
   if(Symbol() == "USDCAD")
     {
      MAGIC= fixednum+7000+Period();
     }
   if(Symbol() == "EURGBP")
     {
      MAGIC= fixednum+8000+Period();
     }
   if(Symbol() == "EURCHF")
     {
      MAGIC= fixednum+9000+Period();
     }
   if(Symbol() == "EURJPY")
     {
      MAGIC= fixednum+10000+Period();
     }
   if(Symbol() == "EURAUD")
     {
      MAGIC= fixednum+11000+Period();
     }
   if(Symbol() == "EURNZD")
     {
      MAGIC= fixednum+12000+Period();
     }
   if(Symbol() == "EURCAD")
     {
      MAGIC= fixednum+13000+Period();
     }
   if(Symbol() == "GBPCHF")
     {
      MAGIC= fixednum+14000+Period();
     }
   if(Symbol() == "GBPJPY")
     {
      MAGIC= fixednum+15000+Period();
     }
   if(Symbol() == "GBPAUD")
     {
      MAGIC= fixednum+16000+Period();
     }
   if(Symbol() == "GBPNZD")
     {
      MAGIC= fixednum+17000+Period();
     }
   if(Symbol() == "GBPCAD")
     {
      MAGIC= fixednum+18000+Period();
     }
   if(Symbol() == "CHFJPY")
     {
      MAGIC= fixednum+19000+Period();
     }
   if(Symbol() == "AUDCHF")
     {
      MAGIC= fixednum+20000+Period();
     }
   if(Symbol() == "NZDCHF")
     {
      MAGIC= fixednum+21000+Period();
     }
   if(Symbol() == "CADCHF")
     {
      MAGIC= fixednum+22000+Period();
     }
   if(Symbol() == "AUDJPY")
     {
      MAGIC= fixednum+23000+Period();
     }
   if(Symbol() == "NZDJPY")
     {
      MAGIC= fixednum+24000+Period();
     }
   if(Symbol() == "CADJPY")
     {
      MAGIC= fixednum+25000+Period();
     }
   if(Symbol() == "AUDNZD")
     {
      MAGIC= fixednum+26000+Period();
     }
   if(Symbol() == "AUDCAD")
     {
      MAGIC= fixednum+27000+Period();
     }
   if(Symbol() == "NZDCAD")
     {
      MAGIC= fixednum+28000+Period();
     }
   if(Symbol() == "XAUUSD")
     {
      MAGIC= fixednum+29000+Period();
     }
   if(Symbol() == "XAGUSD")
     {
      MAGIC= fixednum+30000+Period();
     }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   double High1MimPrevious = iHigh(NULL,PERIOD_M1,2) ;
//+-------------------------------------------------+
   double High1MinCurrent = iHigh(NULL,PERIOD_M1,1) ;
//+-------------------------------------------------+
   double High5MimPrevious = iHigh(NULL,PERIOD_M5,2) ;
//+-------------------------------------------------+
   double High5MinCurrent = iHigh(NULL,PERIOD_M5,1) ;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

   if(High1MimPrevious > High1MinCurrent)
     {
      if(ExistedOrderBool("1 MINUTE CHART SELL")==false) // CHECKING IF ANY ORDER WITH THIS DISCRIPTION WAS OPENED BEFORE
        {
         OrderSend(Symbol(),OP_SELL,lot,Bid,0,Bid+50*Point,Bid-100*Point,"1 MINUTE CHART SELL",MAGIC,0,clrRed);
        }
      else
        {
         datetime aalastOrderTime=0; // CHECKING HOW MANY SECOND HAS PASSED SINCE LAST ORDER FOR 1 MIN SETUP
         for(int cnt=OrdersTotal()-1; cnt>=0 ; cnt--)
           {
            if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false || OrderSymbol()!=Symbol() ||
                  OrderMagicNumber() != ORDER_MAGIC || OrderComment() != ORDER_COMMENT  ||
                  OrderType()!= OP_SELL)continue;
               
              {
               if(OrderOpenTime()>aalastOrderTime)
                  aalastOrderTime=OrderOpenTime();
              }
           }
         int m = TimeCurrent() - aalastOrderTime;
         if(m >= 900)
           {
            OrderSend(Symbol(),OP_SELL,lot,Bid,0,Bid+50*Point,Bid-100*Point,"1 MINUTE CHART SELL",MAGIC,0,clrRed);
           }
        }
     }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   if(High5MimPrevious > High5MinCurrent)
     {
      if(ExistedOrderBool("5 MINUTE CHART SELL")==false)// CHECKING IF ANY ORDER WITH THIS DISCRIPTION WAS OPENED BEFORE
        {
         OrderSend(Symbol(),OP_SELL,lot,Bid,0,Bid+50*Point,Bid-100*Point,"5 MINUTE CHART SELL",MAGIC,0,clrRed);
        }
      else
        {
         datetime bblastOrderTime=0; // CHECKING HOW MANY SECOND HAS PASSED SINCE LAST ORDER FOR 5 MIN SETUP
         for(int cnt=OrdersTotal()-1; cnt>=0 ; cnt--)
           {
            if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false || OrderSymbol()!=Symbol() ||
                  OrderMagicNumber() != ORDER_MAGIC || OrderComment() != ORDER_COMMENT  ||
                  OrderType()!= OP_SELL)continue;
               
              {
               if(OrderOpenTime()>bblastOrderTime)
                  bblastOrderTime=OrderOpenTime();
              }
           }
         int m = TimeCurrent() - bblastOrderTime;
         if(m >= 3600)
           {
            OrderSend(Symbol(),OP_SELL,lot,Bid,0,Bid+50*Point,Bid-100*Point,"5 MINUTE CHART SELL",MAGIC,0,clrRed);
           }
        }
     }


   return 0;
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool ExistedOrderBool(string Order_Comment)
{
   for(int qq = OrdersTotal()-1; qq >= 0; qq--)

      if(OrderSelect(qq,SELECT_BY_POS,MODE_TRADES) &&  OrderSymbol()==Symbol() &&
            OrderMagicNumber() == ORDER_MAGIC && OrderComment()==Order_Comment)
        {
         return true;
        }

   return false;
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

it has magic number /order type / order comment that EA wont get confused choosing the wrong order 

but it does not work and it openes so many trade in less than a second 

Reason: