# last minute store ticks

42

Hi im trying to create an array with a lenght of 59, where each value stores the tick number second by second.

i have simplified the code here, I can also define it from 1 to 59 using for or while but after few ticks it says "array out of range" 263

```  time = TimeSeconds(TimeCurrent());

array_price_difference[time] = (Bid - open_price)*100000;

```
15902

1. florenceale: I can also define it from 1 to 59 using for or while but after few ticks it says "array out of range" 263
Show all relevant code. Like your definition of array_price_difference and its context.

2. `array_price_difference[time] = (Bid - open_price)*100000;`
Don't hard code constants. Your 100000 breaks on JPY pairs and metals. Divide by _Point instead.

3. Do you realize that you're not going to get a tick each second, so what you're doing with the array may be bogus. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart.
"Free-of-Holes" Charts - MQL4 Articles
No candle if open = close ? - MQL4 and MetaTrader 4 - MQL4 programming forum
A wrap around buffer may be better:
```double array_price_difference[60]; int iLast=0;
void newTick(void){ array_price_difference[++iLast % 60] = Bid - open_price; }
int  getDiff(int iTick){ return array_price_difference[(iLast - iTick + 60) % 60]/_Point; }```

getDiff(0) is the current value, getDiff(1) is the previous, etc.

42

i have this:

```aopen[0] = iOpen(0,PERIOD_CURRENT,0); //this returns the opening price correctly

int time = TimeSeconds(TimeCurrent()); //this returns the number of seconds correcty. Sometime if the trade is very slow we have to consider that some seconds can be skipped

cur_var = (Bid - aopen[0])*100000; //and this returns the ticks positive of negative are passed from the beginning of the current candle.

```

this is the base modifying it with _Point as u said.

my goal now should be to define a period inside the minute, like ten seconds...and understand every ten seconds how big is the tick variation.

42

whroeder1:
1. Show all relevant code. Like your definition of array_price_difference and its context.

2. Don't hard code constants. Your 100000 breaks on JPY pairs and metals. Divide by _Point instead.

3. Do you realize that you're not going to get a tick each second, so what you're doing with the array may be bogus. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart.
"Free-of-Holes" Charts - MQL4 Articles
No candle if open = close ? - MQL4 and MetaTrader 4 - MQL4 programming forum
A wrap around buffer may be better:

yes i had considered that we could have seconds with no ticks, and also seconds with more ticks...but i think i will use this in fast moments of trading where surely all the seconds have ticks. Anyways ill checkout your link.
15902

florenceale: my goal now should be to define a period inside the minute, like ten seconds...and understand every ten seconds how big is the tick variation.

Why don't you just create a EMA of the variations:

Not compiled, not tested.

```double roc10(void){
static double   price[60]; static datetime when[60]; static int iLast=0;
datetime now = TimeCurrent();
if(when[iLast] != now){
iLast = (iLast + 1) % 60;
when[iLast] = now;
}
price[iLast] = Bid;
static double roc=0;
#define SMOOTHING 5
static const double alpha = 2.0 / (SMOOTHING+1);
#define SECONDS_BACK 10
int i = (iLast - SECONDS_BACK + 60) % 60;
roc += alpha*( (Bid - price[i]) / (now - when[i]) )
return roc;
}
void OnTick(void){
double roc = rac10();```
Not compiled, not tested.
42

whroeder1:

Why don't you just create a EMA of the variations:

Not compiled, not tested.

Not compiled, not tested.

ok ill study a little bit the code you wrote me and ill tell you if i can make it works. Thanks a lot.

42

maybe is not the most stylish code, i have studied this language from just few weeks...but now it works. I made that it fills the gaps by itself, with the previous values, every second now has a value. I took some idea from yours. Thanks for help

```double roc10(void){
static double   price[60];
static datetime when[60];
static int iLast=1;
static int s;
static int tempo_a=0;
static int tempo_c[10];
static int dif_tempo;
static int old_price;

price[0]=(Bid-(iOpen(0,PERIOD_M1,0)))*100000;
//tempo_c[0]=0;

Print(iOpen(0,PERIOD_M1,0));

if(iLast == 1){
tempo_c[iLast]=TimeSeconds(TimeCurrent());
iLast = 2;
dif_tempo = tempo_c[1]-tempo_c[2];
//when we have gaps
if(dif_tempo>1){
for(s=1; s <= dif_tempo; s++ ){
price[tempo_c[2]+s]=price[tempo_c[2]];
}
}
//when go back to beginning with gap
if(dif_tempo<0){
for(s=1; s <= tempo_c[1]; s++ ){
price[0+s]=price[0];
}
}
price[tempo_c[1]]=(Bid-iOpen(0,PERIOD_M1,0))*100000;

}
if(iLast == 2){
tempo_c[iLast]=TimeSeconds(TimeCurrent());
iLast = 1;
dif_tempo = tempo_c[2]-tempo_c[1];

//when we have gaps
if(dif_tempo>1){
for(s=1; s <= dif_tempo; s++ ){
price[tempo_c[1]+s]=price[tempo_c[1]];
}
}
//when goes back to beginning with gap
if(dif_tempo<0){
for(s=1; s <= tempo_c[2]; s++ ){
price[0+s]=price[0];
}
}
price[tempo_c[2]]=(Bid-iOpen(0,PERIOD_M1,0))*100000;