Help please with a simple question

 

Is there any way to code the Bid price that was like 3, 5 , 10 , 20 seconds ago ? ( I mean in less than a minute ).

Honestly, I found something similar, but coded in the old mq4 language, here:

https://www.mql5.com/en/forum/121764

But what I've found doesn't work in nowadays mq4 language.

Can you help me restore this piece of code:


double prevBid(int sec) {
  datetime when=curTime() - sec;
  int bar=0;
  while( Time[bar] > when && bar < Bars) bar++;
  return( bid[bar] );
}

Thanks very much!

Sincerely,

Dieter Daniel

help -I want to find out what the ask / bid price was 60 seconds or 600 seconds ago....
help -I want to find out what the ask / bid price was 60 seconds or 600 seconds ago....
  • 2009.11.09
  • www.mql5.com
Hi what functions can I use to get ask / bid price from the past in minutes or seconds...
 
Dieter Daniel: Is there any way to code the Bid price that was like 3, 5 , 10 , 20 seconds ago ? ( I mean in less than a minute ).
  1. Yes.
         How To Ask Questions The Smart Way. 2004
              Only ask questions with yes/no answers if you want "yes" or "no" as the answer.

  2. Store each tick with Bid and timestamp. Use the timestamp to find the Bid you want.
    Not compiled, not tested, just typed.
    struct Price{ 
       double price; datetime when;
       void set(double p, datetime w){ price=p; datetime when=w; }
    };   
    class Ticks{
     public:
              Ticks(uint anLB=999) : miLast(0), mkLookback(anLB){ 
                                     Arrayresize(mValues, anLB); add_value(Bid, 0);    }
       void   add_value(double aValue, datetime aWhen){ 
                 miLast = (miLast+1) % mkLookback; mValues[miLast].set(aValue, aWhen); }
       double get_price(datetime when){ return mValues[get_index(when)].price;         }
       uint   get_index(datetime when){
         uint iVal = miLast; 
         while(mValue[iVal].when > when) iVal = (iVal + mkLookback - 1) % mkLookback;
         return iVal;
       }
     private:
       Price  mValues[];
       uint   miLast;
       uint   mkLookback;
    };
    void OnTick(){
       datetime     now      = TimeCurrent();
       static Ticks bids; bids.add_tick(Bid, now);
       double       bid10sec = bids.get_price(now - 10);
       ⋮
    Not compiled, not tested, just typed.
 

Thanks for the kind answer Mr.William. There is something I don't seem to get.

1. What is anLB?

(uint anLB=999)

2. Do the uint values in the code below have to be external global variables?

 private:
   Price  mValues[];
   uint   miLast;
   uint   mkLookback;

Thanks in advance.

 
P-S: Sorry for the "stupid questions" I ask, just new in coding.
 
  1. Argument number lookback. You define how many entries you need when you create the static/global class variable; a thousand should be plenty.
  2. They are member variables of the class.
 
Now I get everything. Works wonderful, thanks !!!