Day Counter

 

Merry Christmas everyone!

I have been trying to figure out how to code when a day has passed, add one to the candle counter then determine if trading is allowed for that day. Any help will be greatly appreciated. A comment function for the day counter may also be helpful.

 

Same as new bar test, but using date.

static datetime currentDay=0;
datetime previousDay = currentDay; currentDay = date();
bool isNewDay = currentDay != previousDay;
  1. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  2. Find bar of the same time one day ago - Simple Trading Strategies - MQL4 programming forum

  3. You could also use D1 timeframe, but then you have to deal with 4066/synchronization issue. On MT4: Unless the current chart is that specific symbols/TFs referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

    On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum 2019.09.03

 
William Roeder:

Same as new bar test, but using date.

  1. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  2. Find bar of the same time one day ago - Simple Trading Strategies - MQL4 programming forum

  3. You could also use D1 timeframe, but then you have to deal with 4066/synchronization issue. On MT4: Unless the current chart is that specific symbols/TFs referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

    On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum 2019.09.03

Thank you, but mt5 please.

 
Nelson Wanyama :

Merry Christmas everyone!

I have been trying to figure out how to code when a day has passed, add one to the candle counter then determine if trading is allowed for that day. Any help will be greatly appreciated. A comment function for the day counter may also be helpful.

A new day can be defined in two ways. In both methods, you need to declare a variable at the global program level (in a simple way: you need to declare a variable in the "header")

Method 1: compare the current "Day number of the year" with the stored value

***

Method 2: compare the opening time of the daily bar (PERIUOD_D1) with the saved value

***


//+------------------------------------------------------------------+
//|                                                      New Day.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//---
int      m_day_of_year  = -1;       //
datetime m_prev_bars    = 0;        // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- Method 1: compare the current "Day number of the year" with the stored value
   MqlDateTime SLastQuotes;
   datetime last_quotes=(datetime)SymbolInfoInteger(Symbol(),SYMBOL_TIME); // 'SYMBOL_TIME' -> Time of the last quote
   TimeToStruct(last_quotes,SLastQuotes);
   if(SLastQuotes.day_of_year!=m_day_of_year)
     {
      Alert("Method 1-> New Day");
      m_day_of_year=SLastQuotes.day_of_year;
     }
//--- Method 2: compare the opening time of the daily bar(PERIUOD_D1) with the saved value
   datetime time_d1=iTime(Symbol(),PERIOD_D1,0);
   if(time_d1!=m_prev_bars)
     {
      Alert("Method 2-> New Day");
      m_prev_bars=time_d1;
     }
  }
//+------------------------------------------------------------------+
Files:
New_Day.mq5  5 kb
 
Vladimir Karputov:

A new day can be defined in two ways. In both methods, you need to declare a variable at the global program level (in a simple way: you need to declare a variable in the "header")

Method 1: compare the current "Day number of the year" with the stored value

***

Method 2: compare the opening time of the daily bar (PERIUOD_D1) with the saved value

***


Thank you. Let me  try it

 
Vladimir Karputov:

A new day can be defined in two ways. In both methods, you need to declare a variable at the global program level (in a simple way: you need to declare a variable in the "header")

Method 1: compare the current "Day number of the year" with the stored value

***

Method 2: compare the opening time of the daily bar (PERIUOD_D1) with the saved value

***


Is it possible to define a weekend this way?

 
   #define _secondsInADay 86400
      int _currentDay       = int(your time value here/_secondsInADay);
      int _currentDayOfWeek = (_currentDay+4)%7;
      bool _weekend         = (_currentDayOfWeek==0 || _currentDayOfWeek==6);
Reason: