CounterInEAExample

Author: Cubesteak
CounterInEAExample

This automated script is designed to function as a foundational monitoring tool within the MetaTrader trading platform. Its primary purpose is to act as a "heartbeat" or a tracker that identifies exactly when a new period of price action (a new candle or bar) begins on a chart.

Core Functionality

The logic operates by constantly observing the timeline of the market. Rather than performing complex calculations or executing immediate trades, the script focuses on one specific event: the transition from an old time period to a new one.

How It Works

  • Time Tracking: The script maintains an internal memory of the exact timestamp of the most recent candle it has seen.
  • New Bar Detection: Every time the market updates, the script compares the current time of the active candle against its stored memory.
  • Trigger Mechanism: If the script detects that the current time is different from the previous one, it recognizes this as the birth of a "New Bar."
  • Counting System: Once a new bar is confirmed, the script increments an internal counter by one unit. This creates a reliable tally of how many candles have passed during the period the script has been running.

Practical Application

For a user, this means the script provides a reliable way to synchronize actions with the rhythm of the market. Instead of reacting to every tiny price flicker (tick), it allows for logic that only triggers once at the start of each new candle. This is particularly useful for ensuring that any subsequent automated tasks or observations occur consistently at the beginning of a new time interval, providing a clean and organized flow of data.

14 Views
0 Downloads
0 Favorites
CounterInEAExample
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                          BlankEA4IndiTesting.mq4 |
//|                                                        Cubesteak |
//|                                                www.cubesteak.net |
//+------------------------------------------------------------------+
#property copyright "Cubesteak"
#property link      "www.cubesteak.net"

int counter;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if (IsNewBar())
   {
      counter++;
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| CheckNewBar                                                      |
//+------------------------------------------------------------------+
bool IsNewBar()
{
   static datetime lastbar = 0;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
   {
      lastbar=curbar;
      return (true);
   }
   else
   {
      return(false);
   }
}  

Comments