Pearson correlation code

 

Hello i have this code below of the Pearson correlation formula, but i want to have an y fixed value to compare with, and not to compare for example the last 13 EURUSD candels with the last 13 of the shown grap. i just want to compare the last 13 EURUSD candels with this value : [1,2,3,4,5,6,7,8,9,10,11,12,13]. how can i do that?? Thank u all!

//+------------------------------------------------------------------+

//| Pearson.mq4 |

//| mladen |

//| mladenfx@gmail.com |

// corrected by loopsider loopsidery@yahoo.com

//+------------------------------------------------------------------+

#property copyright "mladen"

#property link "mladenfx@gmail.com"

#property indicator_separate_window

#property indicator_buffers 8

#property indicator_minimum -1

#property indicator_maximum 1

#property indicator_level1 0

#property indicator_levelcolor DarkSlateGray

//

//

//

//

//

extern string _ = "parameters";

extern int Length = 20;

extern int Price = PRICE_CLOSE;

extern string Symbols = "EURJPY;EURUSD;EURCHF;GBPJPY;GBPUSD;USDJPY;USDCHF";

extern color color1 = Red;

extern color color2 = DarkOrange;

extern color color3 = Orange;

extern color color4 = Gold;

extern color color5 = Lime;

extern color color6 = Green;

extern color color7 = DodgerBlue;

extern color color8 = Blue;

//

//

//

//

//

double buffer1[];

double buffer2[];

double buffer3[];

double buffer4[];

double buffer5[];

double buffer6[];

double buffer7[];

double buffer8[];

string aPairs[];

double pBuffer[][9];

int totalSymbols;

bool gettingBars = false;

string IndicatorFileName;

string addition = "";

string shortName;

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

//

//

//

//

//

int init()

{

SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_LINE,EMPTY,EMPTY,color1);

if (_=="gettingBars")

{

gettingBars = true;

return(0);

}

//

//

//

//

//

Symbols = StringUpperCase(StringTrimLeft(StringTrimRight(Symbols)));

if (StringSubstr(Symbols,StringLen(Symbols),1) != ";")

Symbols = StringConcatenate(Symbols,";");

//

//

//

//

//

if (IsMini()) addition="m";

if (IsDoubleDotted()) addition="..";

else if (IsDotted()) addition=".";

//

//

//

//

//

int s = 0;

int i = StringFind(Symbols,";",s);

string current;

while (i > 0)

{

current = StringSubstr(Symbols,s,i-s);

if (iClose(current+addition,0,0) > 0) {

ArrayResize(aPairs,ArraySize(aPairs)+1);

aPairs[ArraySize(aPairs)-1] = current; }

s = i + 1;

i = StringFind(Symbols,";",s);

}

totalSymbols = MathMin(ArraySize(aPairs),8);

IndicatorFileName = WindowExpertName();

//

//

//

//

//

SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_LINE,EMPTY,EMPTY,color2);

SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_LINE,EMPTY,EMPTY,color3);

SetIndexBuffer(3,buffer4); SetIndexStyle(3,DRAW_LINE,EMPTY,EMPTY,color4);

SetIndexBuffer(4,buffer5); SetIndexStyle(4,DRAW_LINE,EMPTY,EMPTY,color5);

SetIndexBuffer(5,buffer6); SetIndexStyle(5,DRAW_LINE,EMPTY,EMPTY,color6);

SetIndexBuffer(6,buffer7); SetIndexStyle(6,DRAW_LINE,EMPTY,EMPTY,color7);

SetIndexBuffer(7,buffer8); SetIndexStyle(7,DRAW_LINE,EMPTY,EMPTY,color8);

shortName = MakeUniqueName("Pearson_mod ","");

IndicatorShortName(shortName);

return(0);

}

int deinit()

{

for (int i=0;i<8;i++) ObjectDelete(shortName+i);

return(0);

}

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

//

//

//

//

//

int start()

{

int counted_bars=IndicatorCounted();

int i,r,limit;

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit = Bars-counted_bars;

//

//

//

//

//

if (gettingBars) { buffer1[0] = limit; return(0); }

if (Symbols==Symbol()) return(0);

if (ArrayRange(pBuffer,0) != Bars) ArrayResize(pBuffer,Bars);

//

//

//

//

//

int window = WindowFind(shortName);

for (i=0; i<totalSymbols;i++)

{

limit = MathMax(limit,MathMin(Bars-1,iCustom(aPairs+addition,0,IndicatorFileName,"gettingBars",0,0)));

if (ObjectFind(shortName+i) == -1)

{

ObjectCreate(shortName+i,OBJ_LABEL,window,0,0);

ObjectSet(shortName+i,OBJPROP_XDISTANCE,10);

ObjectSet(shortName+i,OBJPROP_YDISTANCE,10+i*14);

ObjectSet(shortName+i,OBJPROP_CORNER,1);

color theColor;

switch(i)

{

case 0: theColor = color1; break;

case 1: theColor = color2; break;

case 2: theColor = color3; break;

case 3: theColor = color4; break;

case 4: theColor = color5; break;

case 5: theColor = color6; break;

case 6: theColor = color7; break;

case 7: theColor = color8; break;

}

ObjectSetText(shortName+i,aPairs,10,"Courier new bold",theColor);

}

}

//

//

//

//

//

for(i=limit, r=Bars-limit-1; i>=0; i--,r++)

{

int ishift = iBarShift(NULL,0,Time,true);

if (ishift >= 0) pBuffer[r][0] = iMA(NULL,0,1,0,MODE_SMA,Price,ishift);

else pBuffer[r][0] = 0;

//

//

//

//

//

for (int p=0; p<totalSymbols;p++)

{

string forSymbol = aPairs[p]+addition;

if (forSymbol == Symbol()) continue;

double sx = 0;

double sy = 0;

double sxy = 0;

double sx2 = 0;

double sy2 = 0;

//

//

//

//

//

ishift = iBarShift(forSymbol,0,Time,true);

if (ishift >= 0) pBuffer[r][p+1] = iMA(forSymbol,0,1,0,MODE_SMA,Price,ishift);

else pBuffer[r][p+1] = 0;

int count = 0;

for (int k=0; r-k>=0 && count<Length; k++)

{

double pricea = pBuffer[r-k][0];

double priceb = pBuffer[r-k][p+1];

if (pricea > 0 && priceb > 0) {

count++;

sx += pricea; sx2 += pricea*pricea;

sy += priceb; sy2 += priceb*priceb;

sxy += pricea*priceb;

}

}

double result = 0;

if (count > 0) {

double dividend = MathSqrt(((sx2-(sx*sx)/count)*(sy2-(sy*sy)/count)));

if (dividend != 0)

result = (sxy - (sx*sy)/count)/dividend;

}

//

//

//

//

//

switch(p)

{

case 0: buffer1 = result; break;

case 1: buffer2 = result; break;

case 2: buffer3 = result; break;

case 3: buffer4 = result; break;

case 4: buffer5 = result; break;

case 5: buffer6 = result; break;

case 6: buffer7 = result; break;

case 7: buffer8 = result; break;

}

}

//

//

//

//

//

}

return(0);

}

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

//

//

//

//

//

bool IsMini() { return(StringFind(Symbol(),"m") > -1); }

bool IsDoubleDotted() { return(StringFind(Symbol(),"..") > -1); }

bool IsDotted() { return(StringFind(Symbol(),".") > -1); }

string MakeUniqueName(string first, string rest)

{

string result = first+(MathRand()%1001)+rest;

while (WindowFind(result)> 0)

result = first+(MathRand()%1001)+rest;

return(result);

}

//

//

//

//

//

string StringUpperCase(string str)

{

string s = str;

int lenght = StringLen(str) - 1;

int char;

while(lenght >= 0)

{

char = StringGetChar(s, lenght);

//

//

//

//

//

if((char > 96 && char 223 && char < 256))

s = StringSetChar(s, lenght, char - 32);

else

if(char > -33 && char < 0)

s = StringSetChar(s, lenght, char + 224);

lenght--;

}

//

//

//

//

//

return(s);

}

 

If I understand what do you want to do, then just repace this part "

double priceb = pBuffer[r-k][p+1];

with some constants (it could even be something like this : double priceb = Length-k; for descending order ordouble priceb = k+1; for ascending order)

amro007:
Hello i have this code below of the Pearson correlation formula, but i want to have an y fixed value to compare with, and not to compare for example the last 13 EURUSD candels with the last 13 of the shown grap. i just want to compare the last 13 EURUSD candels with this value : [1,2,3,4,5,6,7,8,9,10,11,12,13]. how can i do that?? Thank u all!

//+------------------------------------------------------------------+

//| Pearson.mq4

//| mladen |

//| mladenfx@gmail.com |

// corrected by loopsider loopsidery@yahoo.com

//+------------------------------------------------------------------+

#property copyright "mladen"

#property link "mladenfx@gmail.com"

#property indicator_separate_window

#property indicator_buffers 8

#property indicator_minimum -1

#property indicator_maximum 1

#property indicator_level1 0

#property indicator_levelcolor DarkSlateGray

//

//

//

//

//

extern string _ = "parameters";

extern int Length = 20;

extern int Price = PRICE_CLOSE;

extern string Symbols = "EURJPY;EURUSD;EURCHF;GBPJPY;GBPUSD;USDJPY;USDCHF";

extern color color1 = Red;

extern color color2 = DarkOrange;

extern color color3 = Orange;

extern color color4 = Gold;

extern color color5 = Lime;

extern color color6 = Green;

extern color color7 = DodgerBlue;

extern color color8 = Blue;

//

//

//

//

//

double buffer1[];

double buffer2[];

double buffer3[];

double buffer4[];

double buffer5[];

double buffer6[];

double buffer7[];

double buffer8[];

string aPairs[];

double pBuffer[][9];

int totalSymbols;

bool gettingBars = false;

string IndicatorFileName;

string addition = "";

string shortName;

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

//

//

//

//

//

int init()

{

SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_LINE,EMPTY,EMPTY,color1);

if (_=="gettingBars")

{

gettingBars = true;

return(0);

}

//

//

//

//

//

Symbols = StringUpperCase(StringTrimLeft(StringTrimRight(Symbols)));

if (StringSubstr(Symbols,StringLen(Symbols),1) != ";")

Symbols = StringConcatenate(Symbols,";");

//

//

//

//

//

if (IsMini()) addition="m";

if (IsDoubleDotted()) addition="..";

else if (IsDotted()) addition=".";

//

//

//

//

//

int s = 0;

int i = StringFind(Symbols,";",s);

string current;

while (i > 0)

{

current = StringSubstr(Symbols,s,i-s);

if (iClose(current+addition,0,0) > 0) {

ArrayResize(aPairs,ArraySize(aPairs)+1);

aPairs[ArraySize(aPairs)-1] = current; }

s = i + 1;

i = StringFind(Symbols,";",s);

}

totalSymbols = MathMin(ArraySize(aPairs),8);

IndicatorFileName = WindowExpertName();

//

//

//

//

//

SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_LINE,EMPTY,EMPTY,color2);

SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_LINE,EMPTY,EMPTY,color3);

SetIndexBuffer(3,buffer4); SetIndexStyle(3,DRAW_LINE,EMPTY,EMPTY,color4);

SetIndexBuffer(4,buffer5); SetIndexStyle(4,DRAW_LINE,EMPTY,EMPTY,color5);

SetIndexBuffer(5,buffer6); SetIndexStyle(5,DRAW_LINE,EMPTY,EMPTY,color6);

SetIndexBuffer(6,buffer7); SetIndexStyle(6,DRAW_LINE,EMPTY,EMPTY,color7);

SetIndexBuffer(7,buffer8); SetIndexStyle(7,DRAW_LINE,EMPTY,EMPTY,color8);

shortName = MakeUniqueName("Pearson_mod ","");

IndicatorShortName(shortName);

return(0);

}

int deinit()

{

for (int i=0;i<8;i++) ObjectDelete(shortName+i);

return(0);

}

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

//

//

//

//

//

int start()

{

int counted_bars=IndicatorCounted();

int i,r,limit;

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit = Bars-counted_bars;

//

//

//

//

//

if (gettingBars) { buffer1[0] = limit; return(0); }

if (Symbols==Symbol()) return(0);

if (ArrayRange(pBuffer,0) != Bars) ArrayResize(pBuffer,Bars);

//

//

//

//

//

int window = WindowFind(shortName);

for (i=0; i<totalSymbols;i++)

{

limit = MathMax(limit,MathMin(Bars-1,iCustom(aPairs+addition,0,IndicatorFileName,"gettingBars",0,0)));

if (ObjectFind(shortName+i) == -1)

{

ObjectCreate(shortName+i,OBJ_LABEL,window,0,0);

ObjectSet(shortName+i,OBJPROP_XDISTANCE,10);

ObjectSet(shortName+i,OBJPROP_YDISTANCE,10+i*14);

ObjectSet(shortName+i,OBJPROP_CORNER,1);

color theColor;

switch(i)

{

case 0: theColor = color1; break;

case 1: theColor = color2; break;

case 2: theColor = color3; break;

case 3: theColor = color4; break;

case 4: theColor = color5; break;

case 5: theColor = color6; break;

case 6: theColor = color7; break;

case 7: theColor = color8; break;

}

ObjectSetText(shortName+i,aPairs,10,"Courier new bold",theColor);

}

}

//

//

//

//

//

for(i=limit, r=Bars-limit-1; i>=0; i--,r++)

{

int ishift = iBarShift(NULL,0,Time,true);

if (ishift >= 0) pBuffer[r][0] = iMA(NULL,0,1,0,MODE_SMA,Price,ishift);

else pBuffer[r][0] = 0;

//

//

//

//

//

for (int p=0; p<totalSymbols;p++)

{

string forSymbol = aPairs[p]+addition;

if (forSymbol == Symbol()) continue;

double sx = 0;

double sy = 0;

double sxy = 0;

double sx2 = 0;

double sy2 = 0;

//

//

//

//

//

ishift = iBarShift(forSymbol,0,Time,true);

if (ishift >= 0) pBuffer[r][p+1] = iMA(forSymbol,0,1,0,MODE_SMA,Price,ishift);

else pBuffer[r][p+1] = 0;

int count = 0;

for (int k=0; r-k>=0 && count<Length; k++)

{

double pricea = pBuffer[r-k][0];

double priceb = pBuffer[r-k][p+1];

if (pricea > 0 && priceb > 0) {

count++;

sx += pricea; sx2 += pricea*pricea;

sy += priceb; sy2 += priceb*priceb;

sxy += pricea*priceb;

}

}

double result = 0;

if (count > 0) {

double dividend = MathSqrt(((sx2-(sx*sx)/count)*(sy2-(sy*sy)/count)));

if (dividend != 0)

result = (sxy - (sx*sy)/count)/dividend;

}

//

//

//

//

//

switch(p)

{

case 0: buffer1 = result; break;

case 1: buffer2 = result; break;

case 2: buffer3 = result; break;

case 3: buffer4 = result; break;

case 4: buffer5 = result; break;

case 5: buffer6 = result; break;

case 6: buffer7 = result; break;

case 7: buffer8 = result; break;

}

}

//

//

//

//

//

}

return(0);

}

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

//

//

//

//

//

bool IsMini() { return(StringFind(Symbol(),"m") > -1); }

bool IsDoubleDotted() { return(StringFind(Symbol(),"..") > -1); }

bool IsDotted() { return(StringFind(Symbol(),".") > -1); }

string MakeUniqueName(string first, string rest)

{

string result = first+(MathRand()%1001)+rest;

while (WindowFind(result)> 0)

result = first+(MathRand()%1001)+rest;

return(result);

}

//

//

//

//

//

string StringUpperCase(string str)

{

string s = str;

int lenght = StringLen(str) - 1;

int char;

while(lenght >= 0)

{

char = StringGetChar(s, lenght);

//

//

//

//

//

if((char > 96 && char 223 && char < 256))

s = StringSetChar(s, lenght, char - 32);

else

if(char > -33 && char < 0)

s = StringSetChar(s, lenght, char + 224);

lenght--;

}

//

//

//

//

//

return(s);

}
 
mladen:
If I understand what do you want to do, then just repace this part "
double priceb = pBuffer[r-k][p+1];
with some constants (it could even be something like this : double priceb = Length-k; for descending order ordouble priceb = k+1; for ascending order)

I tried but it doesn't work. i'm going to tell u what i want exactly. in this site :

if u tried to put a linear regression on the chart ( from the indicators menu) , it gives u a numer nex to it. after many simulations with excel, I have found that the number was the result of the correlation of pearson. putting: 1,2,3,4,5,6,7,8,9,10,11,12,13 in the matrix A and the last 13 closing prices in the matrix B, gave me exactly that number. now i would like an indicator on metatrader that gives me that number with the chart open. thanks for the help