If you’re looking to enhance your trading experience on MetaTrader 5, you’re in the right place! The concept behind the New Bar Event Handler is a game-changer when it comes to how indicators function. In this post, we’ll dive into how you can use this feature effectively in your trading systems. The main takeaway? It allows you to perform recalculations only when a new bar appears, providing a more efficient alternative to constantly using the OnCalculate() function.
Let’s take a look at a straightforward indicator that gets the job done: it simply prints a line whenever a new bar forms:
//+------------------------------------------------------------------+//| OnNewBarCalculate.mq5 |//| Copyright 2010, Lizar |//| Lizar@mail.ru |//+------------------------------------------------------------------+#property copyright "Copyright 2010, Lizar"#property link "Lizar@mail.ru"#property version "1.00"#property indicator_chart_window#include <OnNewBarCalculate.mqh> // this is where the magic happens with OnNewBarCalculate()//+------------------------------------------------------------------+//| New bar event handler for the indicator |//+------------------------------------------------------------------+int OnNewBarCalculate(constint rates_total, constint prev_calculated, constdatetime& time[], constdouble& open[], constdouble& high[], constdouble& low[], constdouble& close[], constlong& tick_volume[], constlong& volume[], constint& spread[]) { //--- write your code here, similar to OnCalculate();//--- this function runs only when a new bar appears (not on every tick) PrintFormat("New bar: %s",TimeToString(TimeCurrent(),TIME_SECONDS)); //--- return prev_calculated for the next call return(rates_total); }

Comments 0