[MT5] MTF Indicator Trick on weekends. - page 2

 
Mladen Rakic:

Just a small (typo) correction : it is ChartSetSymbolPeriod(0,NULL,0) not ChartSetSymbol(0,NULL,0)

Yes of course, thanks, fixed.

PS: when the time frame data is loaded for the first time since the terminal started (the target time frame or the chart time frame), that will not work either

Using ChartSetSymbolPeriod() is like using a hammer to kill a fly, I don't use it myself anymore, but that's the Metaquotes recommended solution so I preferred to stay simple : it "works in most cases".

 
Alain Verleyen:

Yes of course, thanks, fixed.

Using ChartSetSymbolPeriod() is like using a hammer to kill a fly, I don't use it myself anymore, but that's the Metaquotes recommended solution so I preferred to stay simple : it "works in most cases".

Thank you.

I just did a test, the solution works.

   int maxBars=iBars(Symbol(),TimeFrame);
   if(counter<=2)Print("indName=",indName,"  counter=",++counter,"  prev_calc=",prev_calculated," maxBars=",maxBars);
   
   if(rates_total<=InpPeriod)return(0);

   ArraySetAsSeries(time,true);

   if(counter==1)ChartSetSymbolPeriod(0,NULL,0);

 

https://www.mql5.com/en/docs/chart_operations/chartsetsymbolperiod

ChartSetSymbolPeriod

Changes the symbol and period of the specified chart. The function is asynchronous, i.e. it sends the command and does not wait for its execution completion. The command is added to chart messages queue and will be executed after processing of all previous commands.

...

Note

The symbol/period change leads to the re-initialization of the Expert Advisor attached to a chart.

The call of ChartSetSymbolPeriod with the same symbol and timeframe can be used to update the chart (similar to the terminal's Refresh command). In its turn, the chart update triggers re-calculation of the indicators attached to it. Thus, you are able to calculate an indicator on the chart even if there are no ticks (e.g., on weekends).


Documentation on MQL5: Chart Operations / ChartSetSymbolPeriod
Documentation on MQL5: Chart Operations / ChartSetSymbolPeriod
  • www.mql5.com
Changes the symbol and period of the specified chart. The function is asynchronous, i.e. it sends the command and does not wait for its execution completion. The command is added to chart messages queue and will be executed after processing of all previous commands. The call of ChartSetSymbolPeriod with the same symbol...
 
Ziheng Zhuang:

https://www.mql5.com/en/docs/chart_operations/chartsetsymbolperiod

ChartSetSymbolPeriod

Changes the symbol and period of the specified chart. The function is asynchronous, i.e. it sends the command and does not wait for its execution completion. The command is added to chart messages queue and will be executed after processing of all previous commands.

...

Note

The symbol/period change leads to the re-initialization of the Expert Advisor attached to a chart.

The call of ChartSetSymbolPeriod with the same symbol and timeframe can be used to update the chart (similar to the terminal's Refresh command). In its turn, the chart update triggers re-calculation of the indicators attached to it. Thus, you are able to calculate an indicator on the chart even if there are no ticks (e.g., on weekends).


The documentation is misleading, this function not only reinitialize the indicators attached on a chart, but all indicators/EAs/scripts attached to all charts using the considered symbol. Say you have X indicators on Y charts, they can all send this "refresh" command and will all receive the messages from the queue, resulting in a cascade of updates. That's why I talked about a hammer for a fly.

With basic indicators it's not a problem, with heavy and complex indicators it's a hell.

 
linux80s:

Yes, it seems that MT5 has some stupid behaviour. I'm also struggling with the "weekend" problem.

I found that this code is problematic:

It gives 0 to me when I'm switching between time frames.

When I'm switching between M1/M5/M15/M30/H1 it works well, but above H1 it doesnt work and gives 0 (although there are bars on the chart, the indicator cannot "see" them).

I have no idea. Extremely annoying.

I tried this example to update the bars and get the proper value, but no success.


I did a test,iBars() works well.

"weekend" problem can be solved by calling ChartSetSymbolPeriod(...).

 
William Roeder:
On MT4: Unless the current chart is that specific pair/ TF referenced, you must handle 4066/4073 errors before accessing prices.
          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 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

Thank you for the shared expierence.

It is very important to check "Synchronize" in MT5 coding when refer to different pair/TF.

From the Test 1, After the chart A  received another "tick message" ,it called OnCalculate() again,and then the MA-MTF worked.

Now,the problem can be solved by send a message to the chart message queue.

 
Alain Verleyen:

The documentation is misleading, this function not only reinitialize the indicators attached on a chart, but all indicators/EAs/scripts attached to all charts using the considered symbol. Say you have X indicators on Y charts, they can all send this "refresh" command and will all receive the messages from the queue, resulting in a cascade of updates. That's why I talked about a hammer for a fly.

With basic indicators it's not a problem, with heavy and complex indicators it's a hell.

Yes, all charts with the same symbol will receive the message one or more times, and call OnCalculate(..) again, but there is no calling OnInit().

I Modified some codes,and did test as below:

//+------------------------------------------------------------------+//|                                           test_event_trigger.mq5 |//|                        Copyright 2019, MetaQuotes Software Corp. |//|                                             https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2019, MetaQuotes Software Corp."#property link      "https://www.mql5.com"#property version   "1.00"#property indicator_chart_windowstring indName;
staticint counter=0;
//+------------------------------------------------------------------+//| Custom indicator initialization function                         |//+------------------------------------------------------------------+intOnInit()
  {
//--- indicator buffers mapping
   string tfs=EnumToString(_Period);
   indName=MQLInfoString(MQL_PROGRAM_NAME)+" "+tfs;
   Comment(indName);
   Print(indName,":"__FUNCTION__);//---
   return(INIT_SUCCEEDED);
  }
voidOnDeinit(constint reason) {  Comment("");  }
//+------------------------------------------------------------------+//| Custom indicator iteration function                              |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
                constint prev_calculated,
                constdatetime &time[],
                constdouble &open[],
                constdouble &high[],
                constdouble &low[],
                constdouble &close[],
                constlong &tick_volume[],
                constlong &volume[],
                constint &spread[])
  {
//---
  Print("indName=",indName,"  counter=",++counter,"  prev_calc=",prev_calculated);//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


In MA-MTF.mq5

intOnInit()
  { 
    ...
   string tfs=EnumToString(TimeFrame);
   indName=StringFormat("%s (%s, %d) ",MQLInfoString(MQL_PROGRAM_NAME),tfs,InpPeriod)+" ChartID="+(string)ChartID();
   Comment(indName);
   Print(indName,":"__FUNCTION__);//---
   return(INIT_SUCCEEDED);
  }

intOnCalculate(...)
  {
//---
   int maxBars=iBars(Symbol(),TimeFrame);
  if(counter<=2)Print("indName=",indName,"  counter=",++counter,"  prev_calc=",prev_calculated," maxBars=",maxBars);
   
   if(rates_total<=InpPeriod)return(0);

   ArraySetAsSeries(time,true);

   if(counter==1)ChartSetSymbolPeriod(0,NULL,0);
...
}


Test 6:  4 charts.  Chart A without any indicator, Chart B/C/D with "test_event_trigger"

            load the "MA-MTF" to the chart A, it will send "message " to all charts with "GBPUSD"...

The test result:

Before loading the "MA-MTF" to the chart A :


After loading the "MA-MTF" to the chart A :

Chart A: got message 1 time

Chart B: got message 2 times

Chart C: got message 1 time

Chart D: got message 2 times


After testing, check the journal, there is no calling OnInit() :

-----

All files (screenshots and mq5) are attached.

Files:
test_6.zip  176 kb
 

All tests I did under the conditions:

1. MT5 : Version 5.0, Build 2072, June 07,2019

2. Disconnected to the server ( turn off the WIFI).

 
Ziheng Zhuang:


After testing, check the journal, there is no calling OnInit() :


Of course there is no OnInit() call, why are you expecting that ?

Only OnCalculate() is called.

 
Alain Verleyen:

Of course there is no OnInit() call, why are you expecting that ?

Only OnCalculate() is called.


Alain Verleyen:

The documentation is misleading, this function not only reinitialize the indicators attached on a chart, but all indicators/EAs/scripts attached to all charts using the considered symbol. Say you have X indicators on Y charts, they can all send this "refresh" command and will all receive the messages from the queue, resulting in a cascade of updates. That's why I talked about a hammer for a fly.

With basic indicators it's not a problem, with heavy and complex indicators it's a hell.


The OnInit() is not called,so there is no re-initialization of indicator.

Reason: