one trade a bar

 

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

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. 

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

 
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());
 
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
 

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? 

 
doshur:

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

 This is the code.

Is it optimized? 

Optimized for what ?
 
angevoyageur:
Optimized for what ?
I mean any error, or the code is most efficient.
Reason: