How I can copy previous Bid or Ask prices?

 

I have thought that there might be some function that can get me previous Bid or Ask price similar to the one that give OHLC value. But I could not see any such function.   

Please let me know how I can get the previous values of the Bid or Ask?

 
jafferwilson:

I have thought that there might be some function that can get me previous Bid or Ask price similar to the one that give OHLC value. But I could not see any such function.   

Please let me know how I can get the previous values of the Bid or Ask?

Try:
iOpen()
iClose()
iHigh()
&
iLow()

In addition, See "MQL4: MarketInfo"
By passing the MODE_ASK and/or MODE_BID to MarketInfo() then that is answering your question 

Example:

   double vbid    = MarketInfo("EURUSD",MODE_BID);
   double vask    = MarketInfo("EURUSD",MODE_ASK);
   

 
Mohammad Soubra:
Try:
iOpen()
iClose()
iHigh()
&
iLow()

In addition, See "MQL4: MarketInfo"
By passing the MODE_ASK and/or MODE_BID to MarketInfo() then that is answering your question 

Example:

   double vbid    = MarketInfo("EURUSD",MODE_BID);
   double vask    = MarketInfo("EURUSD",MODE_ASK);
   

I am using MQL5. I guess I forgot to mention it. Please let me know what I can do in mql5 or mt5.

 
 
Konstantin Nikitin:
CopyTicks

How to use it? I tried it but it gives me -1 in the output as return integer. Please let me know.

 
MqlTick tick_array[];
void OnInit()
{
     ArraySetAsSeries(tick_array, true);
}
void OnTick()
{
     if( CopyTicks(_Symbol, tick_array, COPY_TICKS_INFO, 0, 2) == 2)
          Comment(  "Last tick: ",     tick_array[0].ask, " - ", tick_array[0].bid,
                    "\n",
                    "Previous tick: ", tick_array[1].ask, " - ", tick_array[1].bid);
}
 
Konstantin Nikitin:

Yes I tried the same. I got -1 instead of 2 every time.

Please let me know what might be the reason so that I can have the right values.

 
jafferwilson :

Yes I tried the same. I got -1 instead of 2 every time.

Please let me know what might be the reason so that I can have the right values.

I did not see your code. Maybe you did not check

if( CopyTicks(_Symbol, tick_array, COPY_TICKS_INFO, 0, 2) == 2)

And sometimes they got an error.
This can only be guessed.

 
int  CopyTicks( 
   string           symbol_name,           // Symbol name 
   MqlTick&         ticks_array[],         // Tick receiving array 
   uint             flags=COPY_TICKS_ALL,  // The flag that determines the type of received ticks 
   ulong            from=0,                // The date from which you want to request ticks 
   uint             count=0                // The number of ticks that you want to receive 
   );

Parameters

symbol_name

[in]  Symbol.

ticks_array

[out]  An array of the MqlTick type for receiving ticks.

flags

[in]  A flag to define the type of the requested ticks. COPY_TICKS_INFO — ticks with Bid and/or Ask changes, COPY_TICKS_TRADE — ticks with changes in Last and Volume, COPY_TICKS_ALL — all ticks. For any type of request, the values of the previous tick are added to the remaining fields of the MqlTick structure.

from

[in]   The date from which you want to request ticks. In milliseconds since 1970.01.01. If from=0, the last count ticks will be returned.

count

[in]  The number of requested ticks. If the 'from' and 'count' parameters are not specified, all available recent ticks (but not more than 2000) will be written to ticks_array[].

Returned value

The number of copied tick or -1 in case of an error.

----------

CopyTicks works only with a date. If you just want previous bid/ask, you'll have to copy it @ each ontick() occurence and keeps a trace of previous datas. 

 
Icham Aidibe:

----------

CopyTicks works only with a date. If you just want previous bid/ask, you'll have to copy it @ each ontick() occurence and keeps a trace of previous datas. 

I have tried to use it like this: 

CopyTicks(_Symbol,ticks,COPY_TICKS_ALL,TimeCurrent(), 2);

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.

 
jafferwilson :

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.

If you need the last 2 ticks. Use.

if( CopyTicks(_Symbol, tick_array, COPY_TICKS_INFO, 0, 2) == 2)
{
     /* work */
}

And do a check. Correctly received data, or not.

P.S. If you need ASK/BID. Why in the request COPY_TICKS_ALL?

Reason: