Download MetaTrader 5

Please help me with CopyTicks() function:

To add comments, please log in or register
Take active part in the life of the community. Improve your rating!
iJSmile
197
iJSmile 2015.03.19 21:17 
I need to get ticks values for some moment in the past. For example I have time variable:
2015.03.02 00:02:34 and I want to get values of last, bid, and ask (actually it doesn't really matter) for nearest (depends on time variable) tick.
Here is the code example:
void OnInit()
{
   ResetLastError();
   MqlTick tick_array[];
   ulong time = 1425254554; //hardcoded datetime = 2015.03.02 00:02:34
   int ticks_to_get = 10; 
   Print("I want to get 10 ticks from time: " + (datetime)time);
   
   int copied=CopyTicks(_Symbol,tick_array,COPY_TICKS_ALL,time,ticks_to_get); 
   if(copied<=0)
   {
      Print("Can't load ticks. Error: ",GetLastError());
   }
      
   for(int i=0; i<copied; i++)
   {
      string msg = StringFormat("I got tick with time: %s   and Value "last": %s",
                   TimeToString(tick_array[i].time,TIME_DATE|TIME_MINUTES|TIME_SECONDS),
                   DoubleToString(tick_array[i].last,6));     
      Print(msg);
   }
If I set flag COPY_TICKS_ALL to COPY_TICKS_TRADE than I have an "error code" - 0 (successful operation).
Here is the link on (https://www.mql5.com/en/docs/series/copyticks):
Please help me, what I am doing wrong? How to get ticks for special time period in past? Thank you in advance!
Alain Verleyen
Moderator
30745
Alain Verleyen 2015.03.19 21:40  
   if(copied<=0)
   {
      Print("Can't load ticks. Error: ",GetLastError());
   }

0 is not an error, it simply means there is no ticks available.

As you provide yourself the link to the documentation, please read it again. No more than 2,000 ticks are available. There was probably a lot more than 2,000 ticks since 2 March, they are no more available.

iJSmile
197
iJSmile 2015.03.19 22:08  
angevoyageur:

0 is not an error, it simply means there is no ticks available.

As you provide yourself the link to the documentation, please read it again. No more than 2,000 ticks are available. There was probably a lot more than 2,000 ticks since 2 March, they are no more available.

Thank you. One more question - is there any chance in mql5 that I can access tick value if I know datetime (that is located further, than 2,000 ticks)? And can I use function CopyTicks in tester (metatrader 5 tester), because when I did it, I caught error 4014 (which means from documentation - ERR_FUNCTION_NOT_ALLOWED, Function is not allowed for call). Thank you very much for your answers!
Alain Verleyen
Moderator
30745
Alain Verleyen 2015.03.19 22:55  
iJSmile:

Thank you. One more question 

There is 2 ;-)


- is there any chance in mql5 that I can access tick value if I know datetime (that is located further, than 2,000 ticks)?

Only if you record them yourself.

And can I use function CopyTicks in tester (metatrader 5 tester), because when I did it, I caught error 4014 (which means from documentation - ERR_FUNCTION_NOT_ALLOWED, Function is not allowed for call). Thank you very much for your answers!

The answer is in the question.
iJSmile
197
iJSmile 2015.03.19 23:04  
Thank you!  Your answer solved my problem! I am your fan!
Elmo de Moraes
662
Elmo de Moraes 2015.07.10 02:03  

Whats the problem here? No ticks are returned:

int OnInit()
  {
//--- Initializing expert

//--- ok
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Deinitialization function of the expert                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
  }
//+------------------------------------------------------------------+
//| "Tick" event handler function                                    |
//+------------------------------------------------------------------+
void OnTick()
  {
   //ExtExpert.OnTick();
    MqlTick l_scTicks[];
    int l_inCopiedTicks = 0; 
    ulong l_ulCopyFrom = 0;
    
    Print(  TimeCurrent()  );
    l_ulCopyFrom  = TimeCurrent();
    l_ulCopyFrom *= 1000;
    l_ulCopyFrom -= 5000;
    
    l_inCopiedTicks = CopyTicks("EURUSD", l_scTicks, COPY_TICKS_ALL, l_ulCopyFrom );
    
    return;
  }
//+------------------------------------------------------------------+
//| "Trade" event handler function                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
   
  }
//+------------------------------------------------------------------+
//| "Timer" event handler function                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   
  }
//+------------------------------------------------------------------+

Can someone help me?

Mikhail Filimonov
5931
Mikhail Filimonov 2015.07.10 02:48  
iJSmile:

Hi,

"0" - it means that nothing is copied.

CopyTicks() must return non zero value.

On the Russian forum said that this function is a mistake that is not corrected. 

Alain Verleyen
Moderator
30745
Alain Verleyen 2015.07.10 10:14  
Михаил:

Hi,

"0" - it means that nothing is copied.

CopyTicks() must return non zero value.

On the Russian forum said that this function is a mistake that is not corrected. 

Can you provide a link please ?
To add comments, please log in or register