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 13th October 2011, 09:36
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default unable to copy the values

Hi
I often get

Unable to copy the values of the indicator. Error 4806
in the M1 chart and not on other charts.

MQL5 Code:
void SOMECLASS::populate(void)
{
      int copied=CopyRates(_symbol, _period, 0, bars, myRates);
      if(copied<=0) Print("Error copying price data ",GetLastError());
      stoHandle = iStochastic(_symbol, _period, 34, 3, 9, MODE_SMA, STO_CLOSECLOSE);
      copied=CopyBuffer(stoHandle, 0, 0, bars, stochBuffer);
      if(copied<=0)
      {
         Alert("Unable to copy the values of the indicator. Error = ", GetLastError(),",  copied =",copied);
      }
}
Reply With Quote
  #2 (permalink)  
Old 13th October 2011, 16:16
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,548
Thanks: 18
Thanked 20 Times in 16 Posts
Default

If the CopyBuffer() function is called too early after initialization it may copy no data because indicator data is not yet ready. Try calling it in the second occurrence of the OnCalculate(), not the first, if this is an indicator.
__________________
Please, read the Forum Rules and the Signature Rules to avoid termination of your account.
Reply With Quote
  #3 (permalink)  
Old 13th October 2011, 22:01
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default

would this create a side effect problem?
i.e. what are the cases when this would result in infinite loop?

MQL5 Code:
   //--- Change this vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
   copied=CopyBuffer(stoHandle, 0, 0, bars, stochBuffer);
   if(copied<=0)
   {
      Alert("Unable to copy the values of the indicator. Error = ", GetLastError(),",  copied =",copied);
   }
   
   //--- To this  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
   int copied;
   while(copied<=0) copied=CopyBuffer(stoHandle, 0, 0, bars, stochBuffer);

Last edited by samjesse; 13th October 2011 at 22:06.
Reply With Quote
  #4 (permalink)  
Old 14th October 2011, 04:58
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I noticed from the this code that Sleep(1000) does not really wait 1 second. before the next loop.

Am I doing something wrong?

thx


MQL5 Code:
 for(uchar i=0; i<10; i++)
   {
      int copied=CopyBuffer(stoHandle, 0, 0, bars, stochBuffer);
      if(copied<=0)
      {
         Sleep(1000);
         Print("sleeping "+i);
      }
      else
      {
         i=10;
         Print("copied");
      }
Reply With Quote
  #5 (permalink)  
Old 14th October 2011, 05:11
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,548
Thanks: 18
Thanked 20 Times in 16 Posts
Default

If that's an indicator, just don't alert the error. I do it like this:

MQL5 Code:
myMA = iMA(NULL, 0, 1, 0, MODE_SMA, AppliedPrice);
if  (CopyBuffer(myMA, 0, rates_total - shift - 1, Len, MAB) != Len) return(0);

You'll know about the error - the indicator just won't draw.
__________________
Please, read the Forum Rules and the Signature Rules to avoid termination of your account.
Reply With Quote
  #6 (permalink)  
Old 14th October 2011, 05:38
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Enivid View Post
If that's an indicator, just don't alert the error. I do it like this:

MQL5 Code:
myMA = iMA(NULL, 0, 1, 0, MODE_SMA, AppliedPrice);
if  (CopyBuffer(myMA, 0, rates_total - shift - 1, Len, MAB) != Len) return(0);

You'll know about the error - the indicator just won't draw.
no. I need its value for further coding.
Reply With Quote
  #7 (permalink)  
Old 14th October 2011, 07:48
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,548
Thanks: 18
Thanked 20 Times in 16 Posts
Default

Then do not run the code that requires the indicator buffer before some time after initialization passes.
__________________
Please, read the Forum Rules and the Signature Rules to avoid termination of your account.
Reply With Quote
  #8 (permalink)  
Old 14th October 2011, 10:20
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default

and how do you control the "some time after"? i.e. wait(2 sconds); or sleep() or ... how?
Reply With Quote
  #9 (permalink)  
Old 14th October 2011, 14:42
Enivid's Avatar
Administrator
 
Join Date: Nov 2008
Posts: 1,548
Thanks: 18
Thanked 20 Times in 16 Posts
Default

I'd say 1 tick is enough. If you indicator/EA/script reacts to ticks, there's nothing difficult in that.
__________________
Please, read the Forum Rules and the Signature Rules to avoid termination of your account.
Reply With Quote
  #10 (permalink)  
Old 15th October 2011, 06:34
Default Avatar
Senior Member
 
Join Date: Aug 2011
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Enivid View Post
If the CopyBuffer() function is called too early after initialization it may copy no data because indicator data is not yet ready. Try calling it in the second occurrence of the OnCalculate(), not the first, if this is an indicator.
Is there a way to Sleep(); after OnInit() and before OnCalculate? in order to fix this problem?

The reason is, it is not that simple to do CopyBuffer() after one tick given the logic of my code. why?

I have a class object as a Global variable where the constructor calls the populate() for a myRates array and creates iStochastic and does the CopyBuffer() we are dealing with.
OnInit() does nothing with the code except deal with house keeping for plotting and the like.
OnCalculate() recalls the populate() conditionally based on chart bars behavior.

so withing for the "second" tick to CopyBuffer() will need a bit of work, specially when I have to deal with "at the beginning of OnCalculate().

last_bar_time = SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE);
if(new_bar_time == last_bar_time) return(rates_total); else new_bar_time = last_bar_time;

since the above 2 lines will never let me have the second tick to do CopyBuffer().

Last edited by samjesse; 15th October 2011 at 06:47.
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
get indicator values with out plotting samjesse MetaTrader 5 4 8th September 2011 07:58
Easy Way To Copy Best Traders Commtr Advertisements 0 24th August 2011 07:19
Technical Problem: Copy Tool? Forexlight General Forex Discussion 0 27th January 2011 08:50
Copy Paste Pips bonus ForexTradingChoice Advertisements 0 17th January 2011 08:13
Euro Is Unable to Break $1.43 Level alessio09 Forex News 2 16th August 2009 08:22


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


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