trade only one time a day

 

I have written a breakthrough EA,after a month test,I found that sometimes it loses several times within a day.

Therefore,I want to revise my EA,to make it trade only one time a day.Could anyone show me how to realize that?Thanks

 

GumRai,thank you very much.

I will have a try.

 
kevin2708176:

GumRai,thank you very much.

I will have a try.


Wait up a bit, I'm doing a change as made a mistake and removed earlier post - sorry
 

Try to see if this is useful

datetime midnight = TimeCurrent()-(TimeCurrent() % (PERIOD_D1*60));

for(int i =OrdersHistoryTotal( )-1 ;i>=0;i--)
    {
    OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
    if( OrderMagicNumber( ) ==MagicNumber )
       {
       if ( OrderOpenTime()>= midnight)
          return(0);
       }
    }
 
thanks again
 
GumRai:

Try to see if this is useful


It's not...

only the EA has to trade one time a day

it can't trade at all if some other trade is closed other Symbol() same MagicNumber( )

you can check this way only OrdersTotal after Start

before the EA has opened his first

//+------------------------------------------------------------------+
//|                                                   ExampleTwo.mq4 |
//|                               Copyright © 2013,  Tjipke de Vries |
//| //trading once each day                                                                 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013,  Tjipke de Vries"
#property link      ""

int    MagicNumber = 123;
int    Slippage.Pips = 3;
int    EATrades,BUYTrades,SELLTrades;

datetime now,bod,opentimelastmarket;


//++++ These are adjusted for 5 digit brokers.
int     pips2points;      // slippage  3 pips    3=points    30=points
double  pips2dbl;         // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;      // DoubleToStr(dbl/pips2dbl, Digits.pips)
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  now = Time[0];
  bod = now - now % 86400;  // Beginning of the day    
//----
   if(Digits % 2 == 1)  // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
     {pips2dbl = Point*10; pips2points = 10;   Digits.pips = 1;}
     else {pips2dbl = Point;    pips2points =  1;   Digits.pips = 0;}
     // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//----
    for(int i= OrdersHistoryTotal()-1; i>=0 ; i--) //count down if you check trades !!!
        {
        if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)break;
        if(OrderSymbol()!=Symbol() ||OrderMagicNumber()!= MagicNumber ||OrderType()>1) continue;
        if(OrderOpenTime() > bod)
            {
            opentimelastmarket = OrderOpenTime();
            break;
            }
        }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  Comment(""); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  now = Time[0];
  bod = now - now % 86400;  // Beginning of the day  
  static datetime opentimelastmarket = 0;  
  
//----
  if(EATrades != OrdersTotal())
    {
    BUYTrades = 0;
    SELLTrades = 0;
    EATrades = OrdersTotal(); //reset counting trades
    for(int i= OrdersTotal()-1; i>=0 ; i--) //count down if you check trades !!!
        {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
        if(OrderSymbol()!=Symbol() ||OrderMagicNumber()!= MagicNumber ||OrderType()>1) continue;
        if(OrderOpenTime() > opentimelastmarket)opentimelastmarket = OrderOpenTime();
        if(OrderType()==OP_BUY)
          {
          BUYTrades++;         
          }
        if(OrderType()==OP_SELL)
          {
          SELLTrades++;
          }    
        } 
    }   
//---- end closing check open trades
   Comment("BUYTRADES  ",BUYTrades,"    SELLTRADES  ",SELLTrades);
   //if(opentimelastmarket >= Time[0])return(0); //one time each Bar
   if(opentimelastmarket >= bod)return(0);       //one time each Day

   if(Open[0] > 1.35)OrderSend(Symbol(),OP_BUY,0.1,Ask,Slippage.Pips * pips2points,0,0,"ExampleTwoBuy",MagicNumber,0,Blue);
   if(Open[0] < 1.35)OrderSend(Symbol(),OP_SELL,0.1,Bid,Slippage.Pips * pips2points,0,0,"ExampleTwoSell",MagicNumber,0,Red);


   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
deVries:


It's not...

only the EA has to trade one time a day

it can't trade at all if some other trade is closed other Symbol() same MagicNumber( )

you can check this way only OrdersTotal after Start

before the EA has opened his first

I wouldn't have thought that anyone would usually have 2 EAs with the same magic number running at the same time, but who knows :)

I was in a bit of a hurry to go out when I posted before, corrected the mistake, but still forgot to include the scan of open trades.

Are you saying that this will not work at all?

datetime midnight = TimeCurrent()-(TimeCurrent() % (PERIOD_D1*60));

for(int i =OrdersHistoryTotal( )-1 ;i>=0;i--)
    {
    OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
    if( OrderMagicNumber( ) ==MagicNumber )
       {
       if ( OrderOpenTime()>= midnight)
          return(0);
       }
    }
for(i =OrdersTotal( )-1 ;i>=0;i--)
    {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if( OrderMagicNumber( ) ==MagicNumber )
       {
       if ( OrderOpenTime()>= midnight)
          return(0);
       }
    }

Maybe it would be better to assign a value to a variable instead of return() ?

Then check the variable to see if any new trades could be opened.

That would still allow the EA to modify any open trade if necessary.

 
GumRai:

I wouldn't have thought that anyone would usually have 2 EAs with the same magic number running at the same time, but who knows :)

I was in a bit of a hurry to go out when I posted before, corrected the mistake, but still forgot to include the scan of open trades.

Are you saying that this will not work at all?

Maybe it would be better to assign a value to a variable instead of return() ?

Then check the variable to see if any new trades could be opened.

That would still allow the EA to modify any open trade if necessary.


You can place EA on different, charts trading the Symbol of the attached chart only, in that case if you check also the Symbol then there is no need to have other magicnumber

After Start() you can do check every tick all the trades you had done on your account but you make your EA slow doing that...

It is simple to me to change the code that way that it does ordermodify the trades when it is needed i gave this as example for trading once a day

you can check my way it is not needed to check OrdersHistoryTotal() inside Start()

 
deVries:


You can place EA on different, charts trading the Symbol of the attached chart only, in that case if you check also the Symbol then there is no need to have other magicnumber

After Start() you can do check every tick all the trades you had done on your account but you make your EA slow doing that...

It is simple to me to change the code that way that it does ordermodify the trades when it is needed i gave this as example for trading once a day

you can check my way it is not needed to check OrdersHistoryTotal() inside Start()


I will look through your code and will hopefully learn from it. :)

I think that you made a bit of a typo in init

for(int i= OrdersHistoryTotal()-1; i>=0 ; i--) //count down if you check trades !!!
        {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
        if(OrderSymbol()!=Symbol() ||OrderMagicNumber()!= MagicNumber ||OrderType()>1) continue;
        if(OrderOpenTime() > bod)
            {
            opentimelastmarket = OrderOpenTime();
            break;
            }
        }

you assign OrdersHistory total to int i, but then scan open orders.

 
GumRai:


I will look through your code and will hopefully learn from it. :)

I think that you made a bit of a typo in init

you assign OrdersHistory total to int i, but then scan open orders.


thanks good point mode_history it has to be

will edit original code

 

deVries,

that is excellent. I see what you mean, that closed trades only have to be scanned in init.

Just a couple of points. If an OrderSelect fails, the code will exit the loop and will discontinue the checking.

I must admit, that I am pretty useless at error handling and often see in peoples code

if(OrderSelect() ) but usually this error is ignored and continues with the next order position. I really don't know what to do when OrderSelect() fails as I would be concerned that simply restarting the loop would repeat the failure and the program will be stuck.

That can't be good, but then again, exiting the loop or just using the data from the loop when OrderSelect() doesn't fail can't be particularly good either.

The other point

if(EATrades != OrdersTotal())
    {
    BUYTrades = 0;
    SELLTrades = 0;
    EATrades = OrdersTotal(); //reset counting trades
    for(int i= OrdersTotal()-1; i>=0 ; i--) //count down if you check trades !!!
        {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
        if(OrderSymbol()!=Symbol() ||OrderMagicNumber()!= MagicNumber ||OrderType()>1) continue;
        if(OrderOpenTime() > opentimelastmarket)opentimelastmarket = OrderOpenTime();
        if(OrderType()==OP_BUY)
          {
          BUYTrades++;         
          }
        if(OrderType()==OP_SELL)
          {
          SELLTrades++;
          }    
        } 
    }   

Unlikely, I know, but it is possible that at the same time that the EA is opening a trade a different trade is being closed.

That would mean that at the next tick, EATrades would still == OrdersTotal(), so opentimelastmarket would not be updated and another trade will be opened.

Would it be a good idea to check that the OrderSend is successful and if it is, then update opentimelastmarket there?

Reason: