135 lignes
4 Kio
MQL5
135 lignes
4 Kio
MQL5
//+------------------------------------------------------------------+
|
|
//| correlation.mq5 |
|
|
//| https://t.me/indicators_mql4_mql5 |
|
|
//| Creator: https://t.me/Denchik_ai |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "https://t.me/Denchik_ai"
|
|
#property link "https://t.me/indicators_mql4_mql5"
|
|
#property version "1.00"
|
|
#property strict
|
|
#property indicator_separate_window
|
|
#property indicator_buffers 1
|
|
#property indicator_plots 1
|
|
#property indicator_color1 clrDodgerBlue
|
|
#property indicator_style1 STYLE_SOLID
|
|
#property indicator_type1 DRAW_LINE
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator initialization function |
|
|
//+------------------------------------------------------------------+
|
|
input string Symbol1 = "EURUSD"; // First symbol
|
|
input string Symbol2 = "GBPUSD"; // Second symbol
|
|
input int barss = 30; // Correlation Period
|
|
double corrBuffer[];
|
|
|
|
int OnInit()
|
|
{
|
|
//--- indicator buffers mapping
|
|
SetIndexBuffer(0, corrBuffer, INDICATOR_DATA);
|
|
ArraySetAsSeries(corrBuffer, true);
|
|
IndicatorSetString(INDICATOR_SHORTNAME,"Correlation " + Symbol1 + " : " + Symbol2 + ", period: " + barss);
|
|
//---
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator iteration function |
|
|
//+------------------------------------------------------------------+
|
|
int OnCalculate(const int32_t rates_total,
|
|
const int32_t prev_calculated,
|
|
const datetime &time[],
|
|
const double &open[],
|
|
const double &high[],
|
|
const double &low[],
|
|
const double &close[],
|
|
const long &tick_volume[],
|
|
const long &volume[],
|
|
const int32_t &spread[])
|
|
{
|
|
//---
|
|
int counted_bars = prev_calculated;
|
|
int i, limit;
|
|
|
|
if(counted_bars < 0) return(-1);
|
|
if(counted_bars > 0) counted_bars--;
|
|
|
|
limit = MathMin(rates_total - 1 - barss, rates_total - counted_bars);
|
|
|
|
//--- calculate MACD
|
|
for(i = limit; i >= 0; i--){
|
|
corrBuffer[i] = Corr1(i, barss);
|
|
}
|
|
|
|
//--- return value of prev_calculated for next call
|
|
return(rates_total);
|
|
}
|
|
|
|
|
|
double Corr1(int ii, int bars1)
|
|
{
|
|
ENUM_TIMEFRAMES Timeframe = PERIOD_CURRENT; // Timeframe
|
|
|
|
double close1[];
|
|
double close2[];
|
|
|
|
// Copy close prices for Symbol1
|
|
int copied1 = CopyClose(Symbol1, Timeframe, ii, bars1, close1);
|
|
if (copied1 != bars1)
|
|
{
|
|
//Print("Error copying data for ", Symbol1, ": ", GetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// Copy close prices for Symbol2
|
|
int copied2 = CopyClose(Symbol2, Timeframe, ii, bars1, close2);
|
|
if (copied2 != bars1)
|
|
{
|
|
//Print("Error copying data for ", Symbol2, ": ", GetLastError());
|
|
return 0;
|
|
}
|
|
|
|
// Calculate means
|
|
double mean1 = 0.0;
|
|
double mean2 = 0.0;
|
|
for (int i = 0; i < bars1; i++)
|
|
{
|
|
mean1 += close1[i];
|
|
mean2 += close2[i];
|
|
}
|
|
mean1 /= bars1;
|
|
mean2 /= bars1;
|
|
|
|
// Calculate covariance and variances
|
|
double cov = 0.0;
|
|
double var1 = 0.0;
|
|
double var2 = 0.0;
|
|
for (int i = 0; i < bars1; i++)
|
|
{
|
|
double dev1 = close1[i] - mean1;
|
|
double dev2 = close2[i] - mean2;
|
|
cov += dev1 * dev2;
|
|
var1 += dev1 * dev1;
|
|
var2 += dev2 * dev2;
|
|
}
|
|
cov /= (bars1 - 1); // Sample covariance
|
|
var1 /= (bars1 - 1); // Sample variance
|
|
var2 /= (bars1 - 1);
|
|
|
|
// Calculate standard deviations
|
|
double std1 = MathSqrt(var1);
|
|
double std2 = MathSqrt(var2);
|
|
|
|
// Calculate correlation
|
|
double correlation = 0.0;
|
|
if (std1 > 0 && std2 > 0)
|
|
{
|
|
correlation = cov / (std1 * std2);
|
|
return correlation;
|
|
//FileWrite(file_handle, correlation);
|
|
}
|
|
else
|
|
{
|
|
Print("Error: Standard deviation is zero for one of the symbols.");
|
|
return 0;
|
|
//FileWrite(file_handle, 0);
|
|
}
|
|
|
|
}
|