Please help with one trade per day code

 

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;
 

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

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

 
   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.

 
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?

 
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.

 

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
 
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



 

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.

 
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:

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


 
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.

Reason: