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

 
Fernando Carreiro:

Save the value in a globally scoped variable or in a locally scoped static variable. Both variable types will keep the value saved between OnTick() events.

Globally scoped variables are defined outside all functions, usually at the start of the MQL file, while locally scoped static variables are defined inside a function but with the keyword "static" in front of it.

int start()
     
     {
       if (OrdersTotal() >= 0 )
       
        {
         
           for(int pos = OrdersTotal() - 1 ; pos >= 0 ; pos--)
           
              OrderSelect ( pos , SELECT_BY_POS , MODE_TRADES );
              
                {
         
                   if( OrderSymbol() == Symbol())
         
                     {
                        static int X = TimeCurrent() - OrderOpenTime();
                        
                          int m = TimeCurrent() - OrderOpenTime();
             
                           {
                               if ( m >= X +360  )
                               
                                  {
                   
                                        Alert( " YOU CAN OPEN ANOTHER TRADE " ) ; 
            
 
                                   }
                              }
                         }
                   } 
              }
      
      
          return 0 ;
       }
       

thanks my friend I tried that too 

does not seems to work though 

 
rkomm: thanks my friend I tried that too does not seems to work though 

Obviously not! What is the use of creating a static variable if you treat it as a discardable variable and immediately rewrite its contents every time?

You have to think about how you use it and the logic behind how you use it.

Don't be a "script kiddy"! Take some time to actually learn about programming in general first and how to think logically and in small steps to carry out sequences and build from that.

Also, you are still using old style MQL4 code which is obsolete. Try to first learn the language properly, by reading a good book on MQL4 programming. I will leave a suggestion for this at the end.

I'm not trying to be nasty, but just calling your attention to the fact that coding is not easy and should be taken seriously. It takes time to learn and become proficient at it.

Here is some suggested reading material (get the latest edition which is for MQL4+ and not the first edition, or consider going straight to MQL5 instead):

Forum on trading, automated trading systems and testing trading strategies

Something Interesting to Read

Sergey Golubev, 2017.09.16 05:40

Expert Advisor Programming for MetaTrader 4


This book will teach you the following concepts:

  • The basic of the MLQ4 language, including variables and data types, operations, conditional and loop operators, functions, classes and objects, event handlers and more.
  • Place, modify and close market and pending orders.
  • Add a stop loss and/or take profit price to an individual order, or to multiple orders.
  • Close orders individually or by order type.
  • Get a total of all currently opened orders.
  • Work with OHLC bar data and locate basic candlestick patterns.
  • Find the highest high and lowest low of recent bars.
  • Work with MetaTrader’s built-in indicators, as well as custom indicators.
  • Add a trailing stop or break even stop feature to an expert advisor.
  • Use money management and lot size verification techniques.
  • Add a flexible trading timer to an expert advisor.
  • Construct several types of trading systems, including trend, counter-trend and breakout systems.
  • Add alert, emails, sounds and other notifications.
  • Add and manipulate chart objects.
  • Read and write to CSV files.
  • Construct basic indicators, scripts and libraries.
  • Learn how to effective debug your programs, and use the Strategy Tester to test your strategies.

All of the source code in this book is available for download, including an expert advisor framework that allows you to build robust and fully-featured expert advisors with minimal effort.

Forum on trading, automated trading systems and testing trading strategies

Something Interesting to Read

Sergey Golubev, 2017.09.16 05:48

Expert Advisor Programming for MetaTrader 5


This book will teach you the following concepts:

  • Learn the basics of MQL5, including variables and data types, operators, functions, event handlers, and object-oriented programming.
  • Place, modify and close market and pending orders.
  • Calculate, verify and add stop loss and take profit prices to an open position.
  • Add a flexible trailing stop and/or break even stop to your strategy.
  • Manage your trade risk with money management.
  • Use pending orders to scale in and out of positions.
  • Use price, time and indicator data in your expert advisors.
  • Control program execution by trading on new bar open, and add flexible trade timers to your strategies.
  • Walk through the creation of several basic trading strategies from start to finish.
  • Inform the user with dialog boxes, email alerts, mobile notifications and sounds.
  • Draw trend lines, arrows and text labels on the chart.
  • Read and write data to CSV files.
  • Learn the basics of creating indicators, scripts and libraries in MetaEditor.
  • Debug, test and optimize your trading strategy.
  • And much more!

Whether you’re an experienced programmer moving from MQL4, or a novice just starting with MQL5, this book will give you the foundation to quickly program fully-featured and robust trading systems.



 
rkomm:

thanks my friend I tried that too 

does not seems to work though 

The title of this topic that you started is about getting the last order time..  

I showed you example code to get the last order time.

Where in your last code do you get the last order time?

Oh, you don't! Why not?

Just a few posts back I replied to you

Keith Watford:

Not a valid argument

X cannot be greater than X+360

Think about it

If x=100

Can 100 be greater than 460?

Now in this code

                        static int X = TimeCurrent() - OrderOpenTime();
                        
                          int m = TimeCurrent() - OrderOpenTime();
             
                           {
                               if ( m >= X +360  )

I really don't know what you are trying to achieve, but you don't understand how to use a static variable. I have no idea what it is likely to be initialised with, but probably 0.

I'm guessing that you probably think that it would be updated every pass of the loop,( but it won't).

If that WAS the case, then the 2 variables would have exactly the same value which would make

if ( m >= X +360  )

ridiculous because it is the same as

if ( X >= X +360  )

Have you any idea how infuriating it is to waste time trying to help people who totally ignore the help?

Fernando correctly answered the question that you asked, but in this case you do not necessarily need to use a static/globalscope variable. If you do use one, you would need to update it every time a new order for the symbol is opened. I think that is beyond your current level.

All you need is to get the last order time for the symbol and compare it to the current time.

 
Fernando Carreiro:

Obviously not! What is the use of creating a static variable if you treat it as a discardable variable and immediately rewrite its contents every time?

You have to think about how you use it and the logic behind how you use it.

Don't be a "script kiddy"! Take some time to actually learn about programming in general first and how to think logically and in small steps to carry out sequences and build from that.

Also, you are still using old style MQL4 code which is obsolete. Try to first learn the language properly, by reading a good book on MQL4 programming. I will leave a suggestion for this at the end.

I'm not trying to be nasty, but just calling your attention to the fact that coding is not easy and should be taken seriously. It takes time to learn and become proficient at it.

Here is some suggested reading material (get the latest edition which is for MQL4+ and not the first edition, or consider going straight to MQL5 instead):



it's a honor for a rookie like me be to be talking to a professional coder like you sir 

but believe me or not ,  1 month ago I did not know any thing about coding what so ever 

less than a month I write my very own EA up and running 

yesterday my EA made 54 $ with the lot of 0.01 (I can post a proof if it's not against the forum rules )

I have coded " the core " of EA 

the rest is order management and balance management (Like break even and trailing stop ) 

all I wanted was to pause for 5 minute from last order that got opened in 1 minute chart time frame that was all ( to prevent opening unnecessary orders)

I have Andrew R young book printed in my shelf and mql4 pdf book too 

but all and all you are right 

I've got to learn it more fundamental kind of way 

thank you for taking time and replying my good friend :)

 
Keith Watford:

The title of this topic that you started is about getting the last order time..  

I showed you example code to get the last order time.

Where in your last code do you get the last order time?

Oh, you don't! Why not?

Just a few posts back I replied to you

Now in this code

I really don't know what you are trying to achieve, but you don't understand how to use a static variable. I have no idea what it is likely to be initialised with, but probably 0.

I'm guessing that you probably think that it would be updated every pass of the loop,( but it won't).

If that WAS the case, then the 2 variables would have exactly the same value which would make

ridiculous because it is the same as

Have you any idea how infuriating it is to waste time trying to help people who totally ignore the help?

Fernando correctly answered the question that you asked, but in this case you do not necessarily need to use a static/globalscope variable. If you do use one, you would need to update it every time a new order for the symbol is opened. I think that is beyond your current level.

All you need is to get the last order time for the symbol and compare it to the current time.

" Have you any idea how infuriating it is to waste time trying to help people who totally ignore the help? "

my good friend it was never my intention to disrespect you and your professional work believe me .

your answer gave me a prospective to look at it in better way and I appreciate it 

but I wanted to make a new topic and I said it maybe it was close to this topic 

the main purpose that got me creating this topic in the first place was this : 

to pause a 5 minute between last order to next order in 1 minute chart time frame 

and this code that you helped me with just identify the LAST  OrderOpenTime( )



datetime lastOrderTime=0;
   for (int cnt=OrdersTotal()-1; cnt>=0 ; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderOpenTime()>lastOrderTime)
            lastOrderTime=OrderOpenTime();
        }
     }

but how can we know how many second has passed since last OrderOpenTime() ?

that's why I send the stupid :) question today 

 
ace trader 99:

but how can we know how many second has passed since last OrderOpenTime() ?

You already do it in your code with the selected order!

int m = TimeCurrent() - OrderOpenTime();

Just do it for the last order.

 
Keith Watford:

You already do it in your code with the selected order!

Just do it for the last order.

so far I implemented your advise and it's working BUT there is a problem first let me show you the code 


  int start()
   {
     
     if (OrdersTotal() >= 0 )
       
         datetime lastOrderTime=0;
     
            for (int cnt=OrdersTotal()-1; cnt>=0 ; cnt--)
              {
                if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
                  {
                    if(OrderOpenTime()>lastOrderTime)
           
                        lastOrderTime=OrderOpenTime();
                           {
                        
                              int m = TimeCurrent() - lastOrderTime;
                               { 
                                    if ( m >= 300)
                                    
                                      {   
                   
                                        Alert( "YOU CAN OPEN ANOTHER TRADE ") ; 
                                      }
                                      
                                    }
                                    
                                  }
                                    
                                }
                                
                             } 

          return 0 ;
       }
       

problem is some times in within 1 second like 16:21:49 it opens 3 or 4 order 

I will send the photo at end 


so I searched between previous post to get the solution I have come across this  : 


he was saying " if many orders opened at one second. For select really last order use this code " :

int searchLastOpenedMarketTicket(int magic)
 {
    int i, ordersCount = OrdersTotal();
     datetime lastTime;

       // Search timestamp of last opened orders.
        for (i = ordersCount - 1; i >= 0; i--) 
          {
            if (OrderSelect(i, SELECT_BY_POS)) 
               {
                if (OrderMagicNumber() != magic) 
                  {
                    continue;
                     }

                       if (OrderOpenTime() > lastTime)
                          {
                            lastTime = OrderOpenTime();
                             }
                          }
                        }   
                        

                         for (i = ordersCount - 1; i >= 0; i--)
                           {
                              if (OrderSelect(i, SELECT_BY_POS))
                                 {
                                   if (OrderMagicNumber() != magic || OrderOpenTime() != lastTime) 
                                       
                                    {
                                       continue;
                                    }

                                      return (OrderTicket());
                                    }
                                }

                     return (0);
                   }

now I see similarity between two set of codes 

to solving " multiple orders opened in 1 second " what do you think I should do here ?

Files:
 

Your code does not open any orders.

                                  if ( m >= 300)
                                    
                                      {   
                   
                                        Alert( "YOU CAN OPEN ANOTHER TRADE ") ; 
                                      }

Why are you doing this inside the loop?

Complete the loop and then do the check.

 
Keith Watford:

Your code does not open any orders.

Why are you doing this inside the loop?

Complete the loop and then do the check.

I know I simplified it so it wont get crowded

here is a sell EA based on our previous points 

int start()

{
   
if( High[2] > High[1])
  {
    if (OrdersTotal() <1 )
     {

      
          OrderSend(Symbol(),OP_SELL,lot,Bid,0,Bid+sl1*15,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,lot,Bid,0,Bid+sl1*15,Bid-tp*Point," sell is happening ",0,0,clrRed);
            
 
                                   }
                              }
                         }
                     } 
                 }
             }
   return 0 ;
}

but as I sent the picture it's clear  sometimes it opens 3 order within 1 second 

 
ace trader 99:

but as I sent the picture it's clear  sometimes it opens 3 order within 1 second 

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

Use the styler to tidy up the code.