How to store the bid and ask price of the last 60 seconds in an array and to use them?

 

How to store the bid and ask price of the last 60 seconds in an array and to use them?


Thanks!

 

Inititalize an Array, at each tick shift the array and insert the new value.


//z

 
zzuegg:

Inititalize an Array, at each tick shift the array and insert the new value.


//z


I don't want every tick, I would like to take a value every second.
 

to be shure that you only include values of the last 60 sec you have to add a second array where you store a timestamp.

You will need TimeCurrent(), Arrays and basic If expressions, nothing special.

Unfortunately MT4 is limited, would be easier with some kind of linked list...


//z


pseudocode would be something like

onTick:

if TimeCurrent>=lastUpdate+1sec

store value


Fill arrayOf60sec:

if TimeOfValue > TimeCurrent-60sec

value is valid

else

value is older

 

There may not be a tick every sec.

However, every second there will be a price.

I want to store the price of every Current second.

How to code? Thanks!


double asks_sec[60];

...

int start()

...

if (TimeCurrent( ) > lastUpdate)

....



lastUpdate()

{

...


}


store value()

{

...

}


TimeOfValue()

{

....

}

 

you have to do that in a loop. basiclly you have


int start{

bool running=true;

datetime execution=0;

while(running){

  execution=TimeCurrent();

  //DO YOUR STUFF

  Sleep(1000-(TimeCurrent()-execution));

}

}


i assume in the examole that timecurrent returns the actual time in millisec.

 

I still don't get it

you mentioned that I need a second array to store a timestamp

how to do that?


Ask is the last known price, and each new tick is a new price (I assume, please correct if I'm wrong)

Therefore, there may not be a tick every sec, or millisecond

However, every second or millisecond there will be a price. (it will be the same price before being updated by a new tick)

What I want to achieve is to store the price of every Current second. ex. the last 60sec into an array.
double priceAtEverySec[60];

...

int start(){

...

bool running=true;

datetime execution=0;

while(running){

execution=TimeCurrent();

// Seem like my loop is taking the Ask price of every tick and not every second

      for(int i = 59; i > 0; i--)
      {
         priceAtEverySec[i] = priceAtEverySec[i-1];
      }
      priceAtEverySec[0] = Ask;

  Sleep(1000-(TimeCurrent()-execution));
  
  RefreshRates();

// don't understand why 1000-(TimeCurrent()-execution)
// execution is TimeCurrent(), so its TimeCurrent() - TimeCurrent() ???
 
int start(){

bool running=true;

int execution=0;
double values[60];
int FilledValues=0;
bool ArrayIsReady=false;
double tmpPrice=0;
while(running){
execution=GetTickCount();
tmpPrice=MarketInfo(Symbol(),MODE_ASK);
      for(int i = 59; i > 0; i--)
      {
         values[i]=values[i-1]
      }
      values[0] = tmpPrice;

      FilledValues++;
      if(FilledValues>60) ArrayIsReady=true; 
 Sleep(1000-(GetTickCount()-execution));  
}

this should work. i have not tested it, just coded quikly here in editor..

TimeCurrent() was wrong, works only for times > seconds. i use GetTickCount() which according to the doc should return uptime in milliseconds.

The calculations inside the sleep function is to make shure that waittime is only one sec, no matter how long the execution of the code inside the loop takes.

You can use the array for calculations when ArrayIsReady==true

//z

 
  
 Sleep(1000-(GetTickCount()-execution));

in the first place, I don't quite understand this line. This is my main confusion??? I try to print and read from reference and gotten more confused???

What it does:

"1000 milliseconds" - "milliseconds that have elapsed since the system was started" - "milliseconds that have elapsed since the system was started"

 
johnnybegoode:



in the first place, I don't quite understand this line. This is my main confusion??? I try to print and read from reference and gotten more confused???

What it does:

"1000 milliseconds" - "milliseconds that have elapsed since the system was started" - "milliseconds that have elapsed since the system was started"



And also

Would this work? If I chose to work in milliseconds?





it's actually a bit different than your explanation.

Lets go trough it step by step:

you want to store the ask price every one second.

so basically you need to sleep 1000ms on each loop to get that..

but if you sleep 1000ms and you have some serious calculations inside this loop it takes 1000ms + the execution time for the next value.

in order to get rid of this problem we have to calculate the time the program needs to execute.

On the beginning of the loop i store the getTickCount(). trough the expression "getTickCount()-(stored getTickCount())" we know how long the execution took.

So we have to substract the execution time from the time we want to sleep (1sec) which is: 1000-(getTickCount()-(stored getTickcount())


//z

 
You can use Sleep() except in custom indicators.
Reason: