How I can copy previous Bid or Ask prices? - page 3

 
jaffer wilson #:

I have tried to use it like this: 

But the statement is returning -1 instead of the number of ticks copied. Please let me know. I understand that the ticks for TimeCurrent() cannot be 2, it must be 1, but with 1 still I get -1 as an output. please guide.

For anyone who finds this topic researching the usage of CopyTicks (and CopyRange) I want to address two misconceptions in this thread.

- As per documentation for CopyTicks, it clearly states that 0 implies the "current" time:

from_msc

[in]  Time starting from which ticks are requested. Time is specified in milliseconds since 01/01/1970. If from_msc=0, the last number of ticks equal to 'count' are returned.

- When using any datetime (such as TimeCurrent()) make sure to convert the time to milliseconds. For datetime is in seconds elapsed since January 01, 1970, whereas from_msc is in milliseconds since January 01, 1970:

from_msc

[in]  Time starting from which ticks are requested. Time is specified in milliseconds since 01/01/1970. If from_msc=0, the last number of ticks equal to 'count' are returned.


The OP used TimeCurrent() without converting it to milliseconds. For what it's worth, here's an example function which searches for the last known tick till a given time.

//+------------------------------------------------------------------+
//| 20230726 Dave Bieleveld                                          |
//| Searches for the last know tick until the toTime.                |
//| Note, if you want to have the closing tick for a certain         |
//| timeframe add 1 timeframe increment to the toTime.               |
//+------------------------------------------------------------------+
bool              LastAvailableTick(
   string   symbol,                 // symbol name   
   MqlTick& tick,                   // tick receiving array
   datetime toTime,                 // time up to which the last tick is searched
   bool     excludeToTime = false,  // search till excluding toTime, instead of including toTime
   uint     flags = COPY_TICKS_ALL, // flag that defines the type of the ticks that are received 
   ulong    steps = 1,              // the size of the search window in seconds
   ulong    scopeMsc = ULONG_MAX    // maximum milliseconds to search back in time
   )
  {
   MqlTick mqlTicks[];

   const ulong milliSeconds     = 1000;
   const ulong milliSecondsStep = steps * milliSeconds;
   
   ulong copyTicksTime    = (ulong)toTime * milliSeconds; // CopyTicks uses MILLISECONDS since 1970.01.01, and datetime uses SECONDS since 1970.01.01.
   
   copyTicksTime -= excludeToTime ? 1 : 0;

   // Search for the last known ticks
   int result = -1;

   for(ulong i = milliSecondsStep; i < scopeMsc && result <= 0; i += milliSecondsStep)
      result = CopyTicksRange(symbol, mqlTicks, flags, copyTicksTime - i, copyTicksTime - (i - milliSecondsStep));

   // Exit when no tick was found within given scope.
   if(result <= 0)
      return false;

   // Reverse mqlTicks for easy access to the last (newest) tick.
   ResetLastError();
   if(!ArraySetAsSeries(mqlTicks, true))
     {
      printf("%s ArraySetAsSeries failed, error [%d].", __FUNCTION__, GetLastError());
      return false;
     }

   // Copies the closing(last) tick before this date, meaning the closing tick.
   tick = mqlTicks[0];   
   return true;
  }

Example call:

void OnStart()
  {
//---
   MqlTick tick;
   datetime currentTime = TimeCurrent();
   if(LastAvailableTick(Symbol(), tick, currentTime, true, COPY_TICKS_INFO, 5))
      printf("ask: %.2f, bid: %2.f, time: %s, current time: %s", tick.ask, tick.bid, TimeToString(tick.time, TIME_DATE | TIME_MINUTES | TIME_SECONDS), TimeToString(currentTime, TIME_DATE | TIME_MINUTES | TIME_SECONDS));
   else
      Print("No ticks found or an error has occurred.");   
  }