Download MetaTrader 5

Please help with one trade per day code

To add comments, please log in or register
prweza
303
prweza  

Hi

I want my EA to open only one trade per day. I've got a code that counts historical trades and gives a yest or no if a trade was opened,. However i'm a bit stuck. Can someone help.


Thanks.

 for(int i=OrdersTotal()-1; i>=0; i--)
        if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol() == _Symbol 
        && OrderMagicNumber() == MagicNumber
        && (OrderType()==OP_BUY || OrderType()==OP_SELL)==true))
        datetime tot = OrderOpenTime(), // Time of Trade
        dot = tot - tot%86400; // Day of Trade.
       if(dot == tot) onedayorder = 1;
honest_knave
Moderator
2296
honest_knave  

You need to use OrdersHistoryTotal() when working with MODE_HISTORY.

What about a trade that was opened today, but not closed yet?

Keith Watford
Moderator
10570
Keith Watford  
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
        if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol() == _Symbol 
        && OrderMagicNumber() == MagicNumber
        && (OrderType()==OP_BUY || OrderType()==OP_SELL)==true))
        datetime tot = OrderOpenTime(), // Time of Trade


        dot = tot - tot%86400; // Day of Trade.
       if(dot == tot) onedayorder = 1;

I find code like this is easy to confuse. I have no idea what you are trying to achieve here.

I have added 2 blank lines to separate the 2 blocks of code.

The lower 2 lines are only executed after the loop has completed (unless I am really confused)

dot is midnight on whatever day the trade is placed. Is dot likely to equal tot ? I don't think so as the trade would have to have been opened at exactly midnight.


Think about precisely what you want to achieve and write logical code.

prweza
303
prweza  
Keith Watford:

I find code like this is easy to confuse. I have no idea what you are trying to achieve here.

I have added 2 blank lines to separate the 2 blocks of code.

The lower 2 lines are only executed after the loop has completed (unless I am really confused)

dot is midnight on whatever day the trade is placed. Is dot likely to equal tot ? I don't think so as the trade would have to have been opened at exactly midnight.


Think about precisely what you want to achieve and write logical code.

  for(int i=OrdersHistoryTotal()-1; i>=0; i--){
        if ((OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol() == _Symbol 
        && OrderMagicNumber() == MagicNumber
        && (OrderType()==OP_BUY || OrderType()==OP_SELL)==true))
        datetime Tot = OrderOpenTime(); // Time of Trade
        datetime TPL = TimeCurrent()- OrderOpenTime(); // Time passed since last order

        }
      
   bool HTP = TPL>86400; //if more 24 hours have passed since last trade allow ea to trade.

Thanks Keith, 

Will this work?

Keith Watford
Moderator
10570
Keith Watford  
prweza:

Thanks Keith, 

Will this work?


No.

You are not doing anything to identify the most recent trade. 

      bool HTP = TPL>86400; //if more 24 hours have passed since last trade allow ea to trade.

is  executed after the loop is completed, so it only calculates with whichever trade was the last in the loop. That can simply be a random trade, not the most recent.

You do nothing with the variable Tot except give it a value.



Get in the habit of always using 

#property strict

and you should get the warning that TPL is an undeclared variable.

honest_knave
Moderator
2296
honest_knave  

You seem to have changed what you're looking to achieve.

Your latest code is trying to check for >24 hours since last trade.

Your first code is trying to check for once per day.

You also aren't addressing the issue I mentioned above - the active orders.

As Keith said, keep your code simple. Even if that makes it slightly verbose

bool NoTradesToday()
  {
   datetime today = iTime(NULL,PERIOD_D1,0);

   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol()      != _Symbol)  continue;
      if(OrderMagicNumber() != magic_no) continue;
      if(OrderOpenTime()    >= today)    return(false);
     }

   for(int i=OrdersTotal()-1; i>=0; i--)           
     {
      if(!OrderSelect(i,SELECT_BY_POS))  continue;  
      if(OrderSymbol()      != _Symbol)  continue;  
      if(OrderMagicNumber() != magic_no) continue;  
      if(OrderOpenTime()    >= today)    return(false);
     }

   return(true);
  }

Then check with:

if(NoTradesToday() && //.. the rest of your logic
prweza
303
prweza  
honest_knave:

You seem to have changed what you're looking to achieve.

Your latest code is trying to check for >24 hours since last trade.

Your first code is trying to check for once per day.

You also aren't addressing the issue I mentioned above - the active orders.

As Keith said, keep your code simple. Even if that makes it slightly verbose

Then check with:

Many thanks honest Knave:

I'm  getting this error from your code:

'NoTradesToday' - function can be declared only in the global scope

I'm trying to declare this on the global scope but the problem persists.


also why did you use ! before orderselect? Won't this prevent the order to be selected?


Thanks



Marco vd Heijden
Moderator
5748
Marco vd Heijden  

It's a separate custom function so it needs to go below of all the other code, and then you can call the function by calling 

NoTradesToday()

Somewhere in your ontick or ontimer functions.

It will return either true or false depending on the outcome of the calculation.

prweza
303
prweza  
Marco vd Heijden:

It's a separate custom function so it needs to go below of all the other code, and then you can call the function by calling 

Somewhere in your ontick or ontimer functions.

It will return either true or false depending on the outcome of the calculation.


ive inserted


bool NoTradesToday;

at the ontick section above this code but still getting errors.
prweza
303
prweza  
prweza:

ive inserted


bool NoTradesToday;

at the ontick section above this code but still getting errors.

bool NoTradesToday()
  {
   datetime today = iTime(NULL,PERIOD_D1,0);

   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol()      != _Symbol)  continue;
      if(OrderMagicNumber() != magic_no) continue;
      if(OrderOpenTime()    >= today)    return(false);
     }

   for(int i=OrdersTotal()-1; i>=0; i--)           
     {
      if(!OrderSelect(i,SELECT_BY_POS))  continue;  
      if(OrderSymbol()      != _Symbol)  continue;  
      if(OrderMagicNumber() != magic_no) continue;  
      if(OrderOpenTime()    >= today)    return(false);
     }

   return(true);
  }

I'm still getting this error with this code, what is the problem?

'NoTradesToday' - function can be declared only in the global scope


Keith Watford
Moderator
10570
Keith Watford  
prweza:

I'm still getting this error with this code, what is the problem?

'NoTradesToday' - function can be declared only in the global scope



You have already been given the answer.

A function cannot be declared within any other function. Functions include OnInit(), On Deinit(), OnTick() etc as well as any functions that you write yourself.

12
To add comments, please log in or register