Download MetaTrader 5

Limit trades in a certain time frame

To add comments, please log in or register
Dua Yong Rew
5536
Dua Yong Rew  

I want to limit trades within a day to x number

is this the best way to do? by checking the history?

//+------------------------------------------------------------------+
//| Trade Count                                                      |
//+------------------------------------------------------------------+
int TradeCount(ENUM_TIMEFRAMES TimeFrame)
  {
//---

   int      Cnt;
   ulong    Ticket;
   long     EntryType;
   datetime DT[1];

   Cnt = 0;

   if(CopyTime(_Symbol, TimeFrame, 0, 1, DT) <= 0)
   {
      Cnt = -1;
   }
   else
   {
      HistorySelect(DT[0], TimeCurrent());

      for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
      {
         Ticket    = HistoryDealGetTicket(i);
         EntryType = HistoryDealGetInteger(Ticket, DEAL_ENTRY);

         if(EntryType == DEAL_ENTRY_IN)
         {
            if(_Symbol == HistoryDealGetString(Ticket, DEAL_SYMBOL))
            {
               Cnt++;
            }
         }
      }
   }

//---
   return(Cnt);
  }
Alain Verleyen
Moderator
33777
Alain Verleyen  
Dua Yong Rew:

I want to limit trades within a day to x number

is this the best way to do? by checking the history?

It's the more reliable way. Any problem with this code ?
Dua Yong Rew
5536
Dua Yong Rew  
Alain Verleyen:
It's the more reliable way. Any problem with this code ?
no problem but its kinda slow. so searching for more opinions :)
Alain Verleyen
Moderator
33777
Alain Verleyen  
Dua Yong Rew:
no problem but its kinda slow. so searching for more opinions :)

Don't execute such function on each tick, but only when needed.

Use a global variable for your counter, initialize it with your function, then update your variable each time you place a trade with your EA.

By the way, you are counting deals, it can be different from the number of trades depending of what you call a trade.

Dua Yong Rew
5536
Dua Yong Rew  
Alain Verleyen:

Don't execute such function on each tick, but only when needed.

Use a global variable for your counter, initialize it with your function, then update your variable each time you place a trade with your EA.

By the way, you are counting deals, it can be different from the number of trades depending of what you call a trade.

that's an interesting idea, so I just need to reset the counter at the start of the day.

How can I restart at start of the day? 

Alain Verleyen
Moderator
33777
Alain Verleyen  
Dua Yong Rew:

that's an interesting idea, so I just need to reset the counter at the start of the day.

How can I restart at start of the day? 

By doing some efforts
Dua Yong Rew
5536
Dua Yong Rew  
Alain Verleyen:
By doing some efforts
Im thinking of newbar. any ideas?
To add comments, please log in or register