How to get the last N seconds data


How can I get the last 60 sec data in mql5?

I mean not bar of 1 min, I want that every second I will get new data and erese the last second data.

is it posssible ?

Thank You.

Of course it’s possible. Use a one second timer and store your data in an array.

And if I want the data of 20min in an array that every second I will get new data and erase the last data? I’ll get a huge array. And what about 100min?

Btw, I can also use only high/low data.

Thank you!

There is no difference if you want to buffer/store 1 second bar data for 60 seconds, 29 minutes or 100 minutes. You still have to store the data in an organised array. By organised, I mean use a array of a structure that keeps track of all the data you need, such as open, close, high, low, volume, EMAs, Heikin Ashi or what ever else you need to keep a running value on for each of the 1 second bars.

You don’t have to delete and reconstruct the arrays for each new 1 second bar. Just loop through a pre-sized array for the window size you require, keeping an a index/pointer for the “Tail” and “Head” of the data (standard coding 101 techniques, eg. a FIFO queue array).

Besides, in MetaTrader/MQL5 you have access to History Data for every single real tick quote, so you can easily build very accurate 1 second bar data without having to collect it one-by-one on every OnTick event (see CopyTicks() and CopyTicksRange() functions).

PS! You could also use Object Oriented Programming (classes, etc.), but since you are having difficulty with a simply Array solution, it is best that you first understand the basics before diving into the “deep end” of OOP programming.


Ticks are not stored. If you want it you will have to store them (in a circular buffer:)

Not Compiled Not Tested

#define MAX_BACK 60
double bids[MAX_BACK]; int lastBid=0;
double getBid(int iPast){  
   return bids[ (lastBid + iPast) % MAX_BACK ];                                }
void   putBid(double v){ 
   lastBid = (lastBid-1+MAX_BACK)%MAX_BACK;   bids[lastBid]=v;                }
void OnTimer(void){     putBid(Bid);                                           }
int OnInit(void){       EventSetTimer(1);                                      }
double bid2SecondsAgo = bid(2);

Not Compiled Not Tested

If you want price changes (not seconds) ago, use OnTick instead of OnTimer . You might want to store time stamp as well as bid.