Wednesday, March 2, 2011

MQ4, solution to get historical prices

Again again, dealing with the problems that arise when trying to get the history of recent prices in MQ4 to calculate LSSVM and Simplistic values.

I've trying to limit the algorithms to the second half of each minute.... but it fails sometimes.

Now, I present here the solution for getting historical prices. I will use it in my implementation of LSSVM and Simplistic to recreate the machines described previously.



//+------------------------------------------------------------------+
//| collectPrices.mq4 |
//| luisf.canals@.... |
//| |
//+------------------------------------------------------------------+
#property copyright "luisf.canals@..."
#property link ""
#property library


#include "..\include\collectPrices.mqh"



/**
* Gets the matrix of prices for currencies.
*
* Return a Matrix with first line for prices on moment T-K,T-K+1,...T-1.
*
* Each line has: High, Low, Open, Close, Volume and RSI for symbol[0],
* symbol[1],...,symbol[N-1].
*
* 'prices' matrix should have 1 + K x 6*ArraySize(symbols) dimensions,
* the first one for timestamps.
*
* Returns false if prices cannot be get.
*/
bool collectPrices(string symbols[], int K, double &prices[][]) {
int period = PERIOD_M1;
int timebase = ((TimeCurrent()/60) - 1) * 60;
if(timebase<=prices[K-1][0]) return (false);

double line[];
ArrayResize(line, (6*ArraySize(symbols))+1);

for(int i=0; i<ArraySize(line); i++) line[i]=0;

while(true) {
for(int s=0;s<ArraySize(symbols); s++) {
int j=6*s + 1;
Print(symbols[s] + ":" + iTime(symbols[s], period, 1)
+ " - timebase=" + timebase);
if(line[j]==0 && iTime(symbols[s],period,1)>=timebase) {
// Go back in time if iTime>timebase
if(iTime(symbols[s],period,1)<timebase
&& symbols[s]==Symbol()) {
return (false);
}
if(iTime(symbols[s], period, 1)>timebase) {
return (false);
}
int t = 1;
line[j] = iHigh(symbols[s], period,t);
j++;
line[j] = iLow(symbols[s], period,t);
j++;
line[j] = iOpen(symbols[s], period,t);
j++;
line[j] = iClose(symbols[s], period,t);
j++;
line[j] = iVolume(symbols[s], period,t);
j++;
line[j] = iRSI(symbols[s], 0, 14, PRICE_CLOSE, t);
j++;
}
}
bool completed = true;
for(s=0; s<ArraySize(symbols); s++) {
if(line[6*s + 1]==0) {
completed = false;
break;
}
}
if(completed) break;
Sleep(200);
}

line[0] = timebase;
for(i=0; i<K-1; i++) {
for(j=0;j<ArraySize(line); j++) {
prices[i][j] = prices[i+1][j];
}
}
for(j=0; j<ArraySize(line); j++) {
prices[K-1][j] = line[j];
}

return (true);
}



No comments:

Post a Comment