Download MetaTrader 5
To add comments, please log in or register
Any questions regarding MQL5 Cloud Network? FAQ will help you!
Brt88
126
Brt88 2016.10.19 13:29 

Plz tell me why doesnt it work.

I mean if there is opened trade in today, dont open any until next day.


bool OneTradeAday()
{

if (OrderSelect(0, SELECT_BY_POS,MODE_HISTORY)==False)
                           return(True);
                    if (OrderSelect(0, SELECT_BY_POS,MODE_HISTORY)==True       

                     &&  TimeDay(OrderOpenTime()) != TimeDay(TimeCurrent())                       
                        )return(True);
  }       
open trade if function above is true
whroeder1
13644
whroeder1 2016.10.19 14:09  
if (OrderSelect(0, SELECT_BY_POS,MODE_HISTORY)==False)

You select a random entry out of history (position zero.) Could be the oldest trade, could be a deleted pending order, could be a balance adjustment, could be something from another pair.

  1. You must find the last closed order for your pair and magic number. Could EA Really Live By Order_History Alone? (ubzen) - MQL4 forum
  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
Brt88
126
Brt88 2016.10.19 21:11  

Thank you for your advice.

Tell me whats wrong now...

int LastTimeforClosed()
{
int i = OrdersTotal();
 datetime lastTime  = 0;
  for(i-1 ; 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            // my magic number
                     &&  OrderSymbol()       == Symbol()   
                     &&  OrderOpenTime()     >  lastTime
                        )  
                        {
                        lastTime= OrderOpenTime();  
                        }      
                        return(lastTime);
            }
}

condition for opening trade is
TimeDay(LastTimeforClosed()) != TimeDay(TimeCurrent()) 
Daniel Lagoshniak
213
Daniel Lagoshniak 2016.10.19 22:22  
return should be after for-cycle. in your case it is inside that cycle so returns during first run.
Brt88
126
Brt88 2016.10.20 12:50  

corrected, still doesnt work.

int LastTimeforClosed()
{
int i = OrdersTotal()-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            // my magic number
                     &&  OrderSymbol()       == Symbol()   
                     &&  OrderOpenTime()     >  lastTime
                        )  
                        {
                        lastTime= OrderOpenTime();  
                        } 
     }                        
                        return(lastTime);  
}
whroeder1
13644
whroeder1 2016.10.20 13:11  
Brt88: Tell me whats wrong now...
You select a random newest entry out of history (position zero.) Could be the oldest trade, could be a deleted pending order, could be a balance adjustment, could be something from another pair.
Brt88: corrected, still doesnt work.
"Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
Brt88
126
Brt88 2016.10.20 14:51  
the condition lets open trade more than one per day, while I want to let EA trade once a day.
whroeder1
13644
whroeder1 2016.10.20 16:11  
Brt88: the condition lets open trade more than one per day, while I want to let EA trade once a day.
The posted code doesn't "lets open tread more than one per day" or prevent it. It returns the last open time. Period.
Fix the indicated problems. The show the code that does the test, where you compare to tomorrow.
Keith Watford
Moderator
7993
Keith Watford 2016.10.20 18:35  
int i = OrdersTotal()-1;

should be

int i = OrdersHistoryTotal()-1;

.You should also check current orders

Brt88
126
Brt88 2016.10.21 08:44  

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.

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())    
               )
           CheckForOpenD1() ; // function opens trades
   else
   Isar ();
   sltobep();
  
  }
Keith Watford
Moderator
7993
Keith Watford 2016.10.21 10:36  
Brt88:

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.


How do you check opened orders that have not closed yet if you don't check current orders?
/ /123
To add comments, please log in or register