Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2011.01.20 15:22

The "New bar" event handler for the indicators - indicator for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

votes: 16


The idea of this approach is described in the "New Bar" Event Handler article. The example of its use in the Expert Advisors is presented in the article. Here you will find the solution for the indicators. Idea is the same: it will allow to perform recalculations only when the new bar has appeared. It's a convenient alternative to the direct use of the OnCalculate() function.

Here is the simple indicator, it prints a line when the new bar has appeared:

//|                                            OnNewBarCalculate.mq5 |
//|                                            Copyright 2010, Lizar |
//|                                           |
#property copyright "Copyright 2010, Lizar"
#property link      ""
#property version   "1.00"
#property indicator_chart_window

#include <OnNewBarCalculate.mqh> // here is the secret of call of OnNewBarCalculate() function
//| New bar event handler for the indicator                          |
int OnNewBarCalculate(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[])
//--- here you can write the code, similar to OnCalculate();
//--- but this function will be called only if a new bar has appeared (not at every tick)

   PrintFormat("New bar: %s",TimeToString(TimeCurrent(),TIME_SECONDS));
//--- return value of prev_calculated for next call

Translated from Russian by MetaQuotes Software Corp.
Original code:

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

Average Directional Movement Index (ADX) Average Directional Movement Index (ADX)

The Average Directional Movement Index Indicator (ADX) helps to determine if there is a price trend.

TP-SL_Removing TP-SL_Removing

Immediately deletes the SL/TP of all positions.

Closed_TOTAL Closed_TOTAL

Total closure of all positions (winners/losers). You can also select which are of a single instrument.