Download MetaTrader 5

Force a tick by calling OnCalculate() from a chart event

To add comments, please log in or register
mar
1146
mar 2016.04.26 04:29 

Hey coders,

sometimes I need a tick, may it be on a weekend or during quiet market times. I thought about forcing a tick by calling OnCalculate() from a chart event. This was my first idea:

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if (id == CHARTEVENT_CLICK) {
      OnCalculate();
   }

The problem is here that I have no idea which parameters are needed for OnCalculate(). I see rates_total, prev_calculated and all the others which can be seen but how can I do this? What exactly are rates_total, prev_calculated, &time[], &open[] and all the other parameters?

Even if I knew which values to use, would this really be a solution to force a tick by using a chart event?

Thanks! 

Doerk Hilger
1181
Doerk Hilger 2016.04.26 05:32  

The parameters represent the current candle/price which is calculated to get the result for the indication. The difference between indicators and EAs is, that an indicator has too look back / calculate backwards to return and display the results of any candle which is or can be displayed at the chart, while an EA just needs to execute its calculations for the current tick. 

And due to the fact that these arrays are provided by MQL, you cannot replace them by anything else. If you need to update anything outside the normal operation hours, you may deal with the corresponding iClose/iOpen/iHigh/iLow/iVolume/... instead and execute your calculations within another function which is called by OnCalculate and by your OnChartEvent. May I ask what do you want to update when there is no new data anyway?

 

Fernando Carreiro
2290
Fernando Carreiro 2016.04.26 05:41  

First off, you should never call Event Handlers such as "OnCalculate()" yourself. Those are reserved to be called exclusively by the controlling application in order to handle events.

Second, even if there is no new data, why would you need that "OnCalculate()" be called with fake extra tick data? When you attach an Indicator to a Chart (even on a weekend), it will call "OnCalculate()" with all the data present on the chart in order to re-generate the Indicator. There is no need to simulate extra tick data. If this is not happening, then that means that something is wrong with your code. Maybe you can explain to us in more detail why you need the "extra tick" call.

From your statement, "What exactly are rates_total, prev_calculated, &time[], &open[] and all the other parameters", it seems that you have never actually coded a proper indicator using the "OnCalculate()" handler. Maybe, that is where the problem lies and has nothing to do with needing extra ticks. I would suggest you first look at the workings of the sample indicators that MetaQuotes supplies as well as the documentation, in order to see how it all works.

whroeder1
14742
whroeder1 2016.04.26 13:03  

I agree with FMIC, "there is no need to simulate extra tick data."

Indicator's indicate. They shouldn't need events.

Tecuciztecatl
70
Tecuciztecatl 2016.04.26 15:04  
...on a weekend (or not weekend) you can make your own ticks — use OnTimer()  
mar
1146
mar 2016.04.27 01:14  

Thank you all for your answers. Here I show you my testing code and then it might become clearer why I need a tick. I have 7 x 7 charts in a profile and I remove the scales to have a better quick look at them. But as soon as I make a chart window larger than 500 pixels I want to see the scales again. Have a look:

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   if (ChartGetInteger(0, CHART_WIDTH_IN_PIXELS, 0)<500) {
      ChartSetInteger(0, CHART_SHOW_DATE_SCALE, false);
      ChartSetInteger(0, CHART_SHOW_PRICE_SCALE, false);
   }
   else {
      ChartSetInteger(0, CHART_SHOW_DATE_SCALE, true);
      ChartSetInteger(0, CHART_SHOW_PRICE_SCALE, true);
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if (id==CHARTEVENT_CLICK) {
      OnCalculate();
   }
  }

You can imagine that I have to wait a long time for the scales to appear when I maximize a chart window in a low-traded session and at the weekends the scale of course never appears. 

Therefore I thought it might be a good idea at the weekends or in low-traded times just to click on the chart and the OnCalculate() is executed.

Of course I can put it the same code in the CHARTEVENT_CLICK and it will work but for my own educational purposes I wanted to know if I could force a tick this way.

Ex Ovo Omnia
3192
Ex Ovo Omnia 2016.04.27 04:41  

Just think - OnCalculate is for detecting a quote change, OnChartEvent for detecting a GUI change. What is the sense of "forcing" an artificial quote event for servicing a GUI event?

Fernando Carreiro
2290
Fernando Carreiro 2016.04.27 04:49  
mar:

... but for my own educational purposes I wanted to know if I could force a tick this way.

NO! You cannot "force" an "artificial/fake" tick on the OnCalculate() event handler from within your indicator code. Put that out of your mind!

Since, you have described the problem to being related to the Chart Display, use the OnChartEvent() event handler to accomplish your goal.

Tecuciztecatl
70
Tecuciztecatl 2016.04.27 11:10  

I think you can use CHARTEVENT_CHART_CHANGE instead the  CHARTEVENT_CLICK

...but OnChartEvent() does not work in the tester, OnTimer() — works in the tester. You can make OBJ_BUTTON and using OnTimer to check click.

Keith Watford
Moderator
9639
Keith Watford 2016.04.27 11:16  
Tecuciztecatl:

I think you can use CHARTEVENT_CHART_CHANGE instead the  CHARTEVENT_CLICK

...but OnChartEvent() does not work in the tester, OnTimer() — works in the tester. You can make OBJ_BUTTON and using OnTimer to check click.

 

OnChartEvent works in the tester when testing indicators
Tecuciztecatl
70
Tecuciztecatl 2016.04.27 11:22  
Thanks, I don`t know
12
To add comments, please log in or register