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

 
Konstantin Nikitin:

If you need the last 2 ticks. Use.

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

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

See I tried using COPY_TICKS_INFO as well as the COPY_TICKS_ALL, but none helped me. Hence, I was posted query here. I know that If I use the INFO one then it will give me the output, but I didn't got the output as expected. Kindly, let me know whether you got current and previous ticks bid and ask price, please.

 

The picture shows that the expert works

 
Konstantin Nikitin:

The picture shows that the expert works

I guess I need to check on my side once again. Well, thank you for your help and time.

 
jafferwilson:

I guess I need to check on my side once again. Well, thank you for your help and time.

Well, I have no metaeditor on this computer, but it should be something thought that way : 

double curbid,prevbid,curask,prevask;
datetime curtime, prevtime;
///
void OnTick()
{
prevbid = curbid; prevask = curask; prevtime = curtime; // save previous tick infos
curbid = SymbolInfoDouble(_Symbol,SYMBOL_BID); curask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); curtime = TimeCurrent(); // update new ones

}

But with CopyTick you won't be able to do it, it requires time parameters (TimeCurrent()-x seconds).

 
Icham Aidibe:

Well, I have no metaeditor on this computer, but it should be something thought that way : 

But with CopyTick you won't be able to do it, it requires time parameters (TimeCurrent()-x seconds).

Hey, thank you. I will try it out.

 

jafferwilson: try it.
More working code. For use.

struct STicks
{
     double ask, bid;
} last_tick, previous_tick;

void OnInit()
{
     ticksInfo();
}
void OnTick()
{
     ticksInfo();
}

void ticksInfo(void)
{
     MqlTick tick_array[];
     if( CopyTicks(_Symbol, tick_array, COPY_TICKS_INFO, 0, 2) < 2) return;
     ArraySetAsSeries(tick_array, true);
     last_tick.ask       = tick_array[0].ask;
     last_tick.bid       = tick_array[0].bid;
     previous_tick.ask   = tick_array[1].ask;
     previous_tick.bid   = tick_array[1].bid;
     
     Comment(  "Last tick: ",     last_tick.ask, " - ", last_tick.bid,
               "\n",
               "Previous tick: ", previous_tick.ask, " - ", previous_tick.bid);
}
Files:
Test.mq5  3 kb
Test.ex5  9 kb
 
Konstantin Nikitin:

jafferwilson: try it.
More working code. For use.

This is really cool.. It worked .. Thank you @Konstantin Nikitin

These commands are working like charm. :)

 
Konstantin Nikitin:

jafferwilson: try it.
More working code. For use.

jafferwilson:

This is really cool.. It worked .. Thank you @Konstantin Nikitin

These commands are working like charm. :)

Oh so it's possible to use CopyTick with 0 as a time param; I didn't know thank you for the info, my method is rustic. 

 
Using CopyTicks is super overkill if all you want is the prev bid/ask. A much more resource efficient way is to simply use static MqlTick structs... 
{   
   static MqlTick curr,prev;
   prev=curr;
   SymbolInfoTick(_Symbol,curr);
   printf("[%.5f, %.5f] Current\n[%.5f, %.5f] Previous",
      curr.bid, curr.ask,
      prev.bid, prev.ask
   );
}
The only downside is that this only updates when the EA processes tick events. If the EA misses a tick due to slow-processing it is possible to miss a tick or more, however, if this was important then the EA would analyze more than just the prev tick in the first place... 
 
Emma Schwatson:
Using CopyTicks is super overkill if all you want is the prev bid/ask. A much more resource efficient way is to simply use static MqlTick structs... The only downside is that this only updates when the EA processes tick events. If the EA misses a tick due to slow-processing it is possible to miss a tick or more, however, if this was important then the EA would analyze more than just the prev tick in the first place... 

That is also an excellent option.

Thank you Emma.