Within OnCalculate, how can I know if the current bar has ended (the last tick was received) without waiting for the start of a next bar? - page 2

 
Martin Bittencourt #:

I did that and confirmed: both my brokers (actually their base, XP, is the same) don't give that information :(

Funny fact is that I did some research on Google (and IA) on how to handle this problem and I found another Brazilian trader reporting the same SymbolInfoSessionTrade/Quote problem ^^ I'm going to report that to the broker and hope it will change, but in the middle time, I'm going to study the alternatives that I found, including some similar to the link you provided. Since I found other posts (mainly from MQL4) reporting the same kind of doubt, whatever solution I found, I'll bring it here.

Most Brazilian brokers are incompetent in terms of technical skills needed to manage MT5 servers. 

It's a broker issue, not an MT5 one.

 
Alain Verleyen #:

Most Brazilian brokers are incompetent in terms of technical skills needed to manage MT5 servers. 

It's a broker issue, not an MT5 one.

Well I'm really not surprised to read that :T Brazil does have some limitations in the technological area and the stock, futures etc. market is really 'underdeveloped' considering how big is or GDP.

Anyways, I'm back to share my works' results. To recapitulate: I want my indicator to analyse and send alerts ASAP when a bar is closed. In highly liquid assets, that's OK, but the challenge is in low liquidity ones and to analyze the last bar, when the market is already closed. For intraday, I implemented something in the lines of what was previously suggested: everytime the indicator is loaded or a new tick rises belonging to a new bar, a timer is set to check for alert conditions at the time corresponding the next bar. In accordance with my tests, this is working fine. The challenge now is the daily, weekly and monthly charts.

In the D1 case, the end of the current bar doesn't normally correlates with the end of the asset's market, so I can't rely on the bar and the current timeframe to configure the timer; what I need is to know when the market closes. As noted, the official way of knowing this is with the function "SymbolInfoSessionTrade", but it doesn't always work and I really want to avoid requiring the user to manually input that information in the input variables.

So what I've implemented so far are the following steps:

  1. Check the current timeframe. If inferior to Daily, work as usual; otherwise
  2. Check if the market is closed using connectivity and similar functions, got from the forum. If that is the case, do the alert check immediately.
  3. If not, tries to get the ending of the market using "SymbolInfoSessionTrade". If successful, set the OnTimer in accordance with that info.
  4. If it fails, identify the most liquid asset of the same market using SYMBOL_PATH, SymbolsTotal and iVolume.
  5. Get the last M5 bar of this asset (all closing market datetimes seems to be multiple to M5). If current time is greater then that last M5 bar x 2, understand the market is closed and do the alert check immediately.
  6. If not, get the last 290 bars using CopyTime and iterate over them to check the last M5 bar datetime of the last day change.
  7. Assuming the current trade day is very likely to end at the same datetime, set the OnTimer in accordance with that hour-minute.
The attached code implements this strategy. In advance, I'm aware the algorithm for step 6 above is not very smart; I plan to make a smarter iteration in the future. My idea of sharing this is not only it may help other developers facing a similar scenario, but also to submit for improvement suggestions :)

Files:
MT5_Forum.mqh  11 kb
 
Martin Bittencourt #:
    The attached code implements this strategy. In advance, I'm aware the algorithm for step 6 above is not very smart; I plan to make a smarter iteration in the future. My idea of sharing this is not only it may help other developers facing a similar scenario, but also to submit for improvement suggestions :)

    I did not dig into it thoroughly, but at 1-st glance it looks decent. The only thing I'd change is to gather closing stats by week days for all available bars on chart (not only last 290) - then get the most frequent HH:MM for specific day.