Forex Forum - EarnForex
Serving Traders Since 2005
 

Go Back   Forex Forum - EarnForex > MetaTrader > MetaTrader 5

MetaTrader 5 MT5 is already out of its beta stage, but the development is ongoing. Some brokers already offer real accounts and the interest of traders is growing. You can discuss anything related to MetaTrader 5 in this forum.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 25th September 2011, 23:13
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default only when new bar arrives

Hi

I am trying to quite OnCalculate after the indicator is plotted and not to execute the code block unless a new bar is received.

the following seams not to cut it.
Any ideas ples. thx

MQL5 Code:
int OnCalculate(const int rates_total,...
  {
//---
   if(!isNewBar())
      return(0);
//do rest of block.

MQL5 Code:
bool isNewBar()
{

   MqlRates rates[];
   static datetime dt_current;
   static datetime dt_prev;
     
   int copied=CopyRates(NULL,0,0,1,rates);
   if(copied<=0) Alert("Error copying price data inside isNewBar",GetLastError());
   //else Alert("Copied ",ArraySize(rates)," bars");
   if(!dt_current)
   {
      dt_current = rates[0].time;
      dt_prev = rates[0].time;
      return 1;
   }
   else if(dt_current == dt_prev)
   {
      return 0;  
   }
   else
      return 1;
}
Reply With Quote
  #2 (permalink)  
Old 26th September 2011, 09:52
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,548
Thanks: 18
Thanked 20 Times in 16 Posts
Default

There's a much easier way of doing it:

Global variable:
MQL5 Code:
int LastBars = 0;

Somewhere at the top of OnCalculate() function:
MQL5 Code:
    if (LastBars == rates_total) return(rates_total);
    else LastBars = rates_total);
__________________
Please, read the Forum Rules and the Signature Rules to avoid termination of your account.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:22.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Inactive Reminders By Icora Web Design

SEO by vBSEO 3.3.2