one trade a day - page 2

 
GumRai:
How do you check opened orders that have not closed yet if you don't check current orders?
YR right, code corected bellow, but the problem is the the ea opens second trade in a sigle day after previous trade was closed.


int LastOpenTime()
{
    datetime lastTime  = 0;
    int      lastTicket = -1;
    int pos = OrdersTotal()-1; 
    for(pos ; pos >= 0 ; pos--)
    if (OrderSelect(pos, SELECT_BY_POS)                
    &&  OrderMagicNumber()  == MAGICMA            
    &&  OrderSymbol()       == Symbol()                
    &&  OrderOpenTime()     >  lastTime
         ){
      lastTime   = OrderOpenTime();  // Functions CurTime() and OrderOpenTime() returns time as number of seconds elapsed from 00:00 January 1, 1970. So you have to multiply number of hours by number of seconds in one hour (60 * 60):
      lastTicket = OrderTicket();
          }
    return(lastTime);
}

int LastTimeforClosed()
{
int i = OrdersHistoryTotal()-1;
 datetime lastTime  = 0;
  for(i ; i >= 0 ; i--)
    {
     if (OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==False)
               return(0);
                    if (OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==True       
                     &&  OrderMagicNumber()  == MAGICMA            
                     &&  OrderSymbol()       == Symbol()   
                     &&  OrderOpenTime()     >  lastTime
                        )  
                        {
                        lastTime= OrderOpenTime();  
                        } 
     }                        
                        return(lastTime);  
}

void OnTick()
  {
            if (isnewbar()==True
              && TimeDay(LastTimeforClosed()) < TimeDay(TimeCurrent())              
              && TimeCurrent()- LastOpenTime()> 7*60*60   
             )
                         CheckForOpenD1() ; // function opens trades
   else
   Isar ();
   sltobep();
   }
 
              
              && TimeCurrent()- LastOpenTime()> 7*60*60   

If it opened 7 hours ago doesn't necessarily mean that it opened before today.


 
    for(pos ; pos >= 0 ; pos--)

is wrong

 
    for(; pos >= 0 ; pos--)

is correct

.

Same goes for

  for(i ; i >= 0 ; i--)
 
Brt88:

I dont understand why should i compare to tomorow.

Also there is no balance adj and pending orders.
And i think that i dont need to check current orders if i check every opened trade, it doesnt matter if it is closed or not.

  1. You said you don't want to trade today if the order was opened and closed today. Either compare today's date to yesterday's closed order, or now to closed order's tomorrow.
  2. When (not if) your broker makes a history entry, your code breaks. It will be your money.
  3. You don't? There is no closed trade today, your function finds nothing in history, you open a new trade. Next tick, you find nothing in history, you open a new trade. Next tick, you find nothing in history, you open a new trade...
 
GumRai:

If it opened 7 hours ago doesn't necessarily mean that it opened before today.


is wrong

is correct

.

Same goes for

7 hours condition is intentional, that works fine.

I have corrected but it doesn’t change anything. Still there are multiple trades entry during one day. (opening trade, close by SL, open a new one - like in attached screen)

Plz, explain me what is the difference in your proposed FOR cycle formula (for(; pos/i....)

 
WHRoeder:
  1. You said you don't want to trade today if the order was opened and closed today. Either compare today's date to yesterday's closed order, or now to closed order's tomorrow.

Why? this condition doesnt work?

TimeDay(LastTimeforClosed()) < TimeDay(TimeCurrent()) 
  1. When (not if) your broker makes a history entry, your code breaks. It will be your money.

Ok i will, but i have no idea how to make the code resistant for balance adj

  1. You don't? There is no closed trade today, your function finds nothing in history, you open a new trade. Next tick, you find nothing in history, you open a new trade. Next tick, you find nothing in history, you open a new trade...

I know... this condition do the job (7 Hours intetionally)

&& TimeCurrent()- LastOpenTime()> 7*60*60   
 

Why do newbies insist that their code is correct when the more experienced and knowledgeable users are telling them it is wrong!

If your code does not work, then it is WRONG. Stop rejecting the CORRECT answers just because you don't understand it. Put in the effort and don't waist the users time for trying to teach you the ABC alphabet of coding.

Do you really think that doctors start out doing surgery on their first days of University. NO! They go through many long years of learning, research and training!

You must do the same! Go out and first learn how to properly code in C and C++ and then start with very simple things and slowly build up.

Do the research. Learn to code properly. Learn the basics of coding FIRST, before even attempting to code an Indicator or EA.

 

You  are only selecting from History which is Closed trades.  I modified your proc to check for an open order and return the current time (if you don't want multiple orders open). And you have two OrderSelects which is unecessary.

 

int LastTimeforClosed()
{
if (OrdersTotal) > 0
      return TimeCurrent();
int i = OrdersHistoryTotal()-1;
 datetime lastTime  = 0;
  for(i ; i >= 0 ; i--)
  {
     if (OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
     {      
                    if (OrderMagicNumber()  == MAGICMA            
                     &&  OrderSymbol()       == Symbol()   
                     &&  OrderOpenTime()     >  lastTime
                        )  
                        {
                             lastTime= OrderOpenTime();  
                        } 
     } 
     else return (0);
    } // end for                   
    return(lastTime);   

} 

 
mac8008:

You  are only selecting from History which is Closed trades.  I modified your proc to check for an open order and return the current time (if you don't want multiple orders open). And you have two OrderSelects which is unecessary.

 

} 

Thank you for your reply, but the problem is that, the ea opens a trade after previous was opened and closed during the same day. See screen attached.

 
Brt88: . See screen attached.
Don't try to attach an image, insert the image
 
Reason: