Download MetaTrader 5

one trade a bar

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

this is my code to trade one trade per bar

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

   long   EntryType;
   ulong  Ticket;
   int    Cnt;

   Cnt = 0;

   HistorySelect(TimeCurrent() - (TimeCurrent() % PeriodSeconds(TimeFrame)), TimeCurrent());

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

      if(EntryType == DEAL_ENTRY_IN || DEAL_ENTRY_INOUT)
      if(Symbol() == HistoryDealGetString(Ticket, DEAL_SYMBOL))
      {
         Cnt++;
      }
   }

//---
   return(Cnt);
  }

 it works fine for PERIOD_D1 and below

but when I use it for PERIOD_W1, it is not working properly. any idea why? 

 

usage like this

if(TradeCount(PERIOD_W1) == 0)
 then trade code...
Andrey Barinov
39302
Andrey Barinov  

Hi,

why do you need to do like this:

 

HistorySelect(TimeCurrent() - (TimeCurrent() % PeriodSeconds(TimeFrame)), TimeCurrent());

and not like this: 

 

HistorySelect(TimeCurrent() - PeriodSeconds(TimeFrame), TimeCurrent());

Hope this helps.

Andrey. 

Simon Gniadkowski
Moderator
18018
Simon Gniadkowski  
Wahoo:

Hi,

why do you need to do like this:

 

and not like this: 

HistorySelect(TimeCurrent() - PeriodSeconds(TimeFrame), TimeCurrent());
What you have suggested is not the current bar but the time span from current time back the duration of a "TimeFrame" bar,  it does not coincide with the start time of the current bar.
Documentation on MQL5: Date and Time / TimeCurrent
Documentation on MQL5: Date and Time / TimeCurrent
  • www.mql5.com
Date and Time / TimeCurrent - Documentation on MQL5
Dua Yong Rew
5454
Dua Yong Rew  
RaptorUK:
What you have suggested is not the current bar but the time span from current time back the duration of a "TimeFrame" bar,  it does not coincide with the start time of the current bar.
Correct. Only have problems on weekly period. Daily and below no problem. Any help?
Alain Verleyen
Moderator
31632
Alain Verleyen  
doshur:
Correct. Only have problems on weekly period. Daily and below no problem. Any help?
Because the 01.01.1970 is a Thursday, so with your method every week start on Thursday 00:00.
Andrey Barinov
39302
Andrey Barinov  
RaptorUK:
What you have suggested is not the current bar but the time span from current time back the duration of a "TimeFrame" bar,  it does not coincide with the start time of the current bar.

I am not suggesting anything here, I am asking.

It looks to me that this:

 

TimeCurrent() - (TimeCurrent() % PeriodSeconds(TimeFrame))

returns something very meaningless. Subtracting NUMBER OF WEEKS from SECONDS is like comparing apples and oranges... 

The best way to approach this task is simply to find the open time of last weekly candle and select history from that point.

something like

datetime lastweekstart[1]={0};
CopyTime(_Symbol,PERIOD_W1,0,1,lastweekstart); // Plus standard routine to check if all operations are successful

HistorySelect(lastweekstart[0],TimeCurrent());

 

Andrey. 

Alain Verleyen
Moderator
31632
Alain Verleyen  
Wahoo:

I am not suggesting anything here, I am asking.

It looks to me that this:

 

returns something very meaningless. Subtracting NUMBER OF WEEKS from SECONDS is like comparing apples and oranges... 

The best way to approach this task is simply to find the open time of last weekly candle and select history from that point.

something like

 

Andrey. 

He doesn't subtract NUMBER OF WEEKS from SECONDS. He subtract seconds from a datetime (which is seconds too). So the approach is good, except the week doesn't start when he expect it. (see my post above).

Anyway your method is good and clearer, and can be generalized to :

datetime lastperiodstart[1]={0};
CopyTime(_Symbol,TimeFrame,0,1,lastperiodstart); // Plus standard routine to check if all operations are successful

HistorySelect(lastperiodstart[0],TimeCurrent());
Dua Yong Rew
5454
Dua Yong Rew  
angevoyageur:
Because the 01.01.1970 is a Thursday, so with your method every week start on Thursday 00:00.
Yup. My trades all start on Thursday. Im going to try wahoo method.
Thanks
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Symbol Properties - Documentation on MQL5
Dua Yong Rew
5454
Dua Yong Rew  

Thanks for all the help. The code works like a charm now.

//+------------------------------------------------------------------+
//| 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 || DEAL_ENTRY_INOUT)
         if(Symbol() == HistoryDealGetString(Ticket, DEAL_SYMBOL))
         {
            Cnt++;
         }
      }
   }

//---
   return(Cnt);
  }

 This is the code.

Is it optimized? 

Alain Verleyen
Moderator
31632
Alain Verleyen  
doshur:

Thanks for all the help. The code works like a charm now.

 This is the code.

Is it optimized? 

Optimized for what ?
Dua Yong Rew
5454
Dua Yong Rew  
angevoyageur:
Optimized for what ?
I mean any error, or the code is most efficient.
To add comments, please log in or register