Indicators Used
Miscellaneous
0
Views
0
Downloads
0
Favorites
correlate
//+------------------------------------------------------------------+
//| Correlate.mq5 |
//| Copyright 2015, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property description"Correlate by pipPod"
#property version "1.20"
#property strict
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_plots 8
//--- plot AUD
#property indicator_label1 "AUD"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDarkOrange
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot CAD
#property indicator_label2 "CAD"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrAqua
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- plot CHF
#property indicator_label3 "CHF"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrFireBrick
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- plot EUR
#property indicator_label4 "EUR"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrRoyalBlue
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//--- plot GBP
#property indicator_label5 "GBP"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrSilver
#property indicator_style5 STYLE_SOLID
#property indicator_width5 1
//--- plot NZD
#property indicator_label6 "NZD"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrDarkViolet
#property indicator_style6 STYLE_SOLID
#property indicator_width6 1
//--- plot JPY
#property indicator_label7 "JPY"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrYellow
#property indicator_style7 STYLE_SOLID
#property indicator_width7 1
//--- plot USD
#property indicator_label8 "USD"
#property indicator_type8 DRAW_LINE
#property indicator_color8 clrLimeGreen
#property indicator_style8 STYLE_SOLID
#property indicator_width8 1
//---
#define indicator_handles 12
//---
#property indicator_levelcolor clrLightSlateGray
double indicator_level1= 0;
double indicator_level2= .2;
double indicator_level3= 20;
double indicator_level4= 30;
double indicator_level5=100;
//+------------------------------------------------------------------+
//| Class for indicator and index buffers |
//+------------------------------------------------------------------+
class CCorrelate
{
public:
//--- constructor
CCorrelate(void):indicatorHandle(INVALID_HANDLE)
{}
//--- destructor
~CCorrelate(void)
{}
//--- indicator handles
int indicatorHandle;
//--- symbol/indicator buffers
double SymbolBuffer[];
//--- copy symbol/indicator data
int CopyBuffer(const datetime &from,const datetime &to)
{return(CopyBuffer(indicatorHandle,0,from,to,SymbolBuffer));}
//--- previously calculated bars
int BarsCalculated(void)
{return(BarsCalculated(indicatorHandle));}
//--- release indicator handles
bool IndicatorRelease(void)
{return(IndicatorRelease(indicatorHandle));}
//--- currency/index buffers
double IndexBuffer[];
//--- assign index buffers
bool SetIndexBuffer(int index,ENUM_INDEXBUFFER_TYPE buffer_mode=INDICATOR_DATA)
{return(SetIndexBuffer(index,IndexBuffer,buffer_mode));}
//--- index buffer series flag
bool ArraySetAsSeries(bool flag)
{return(ArraySetAsSeries(IndexBuffer,flag));}
//--- initialize index buffers
bool ArrayInitialize(double value)
{return(ArrayInitialize(IndexBuffer,value));}
//--- detach index buffers
void ArrayFree(void)
{ArrayFree(IndexBuffer);return;}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum indicators
{
INDICATOR_MA, //Moving Average
INDICATOR_MACD, //Moving Average Convergence/Divergence
INDICATOR_STOCHASTIC, //Stochastic Oscillator
INDICATOR_RSI, //Relative Strength Index
INDICATOR_CCI, //Commodity Channel Index
INDICATOR_RVI, //Relative Vigour Index
INDICATOR_DEMARKER, //DeMarker Oscillator
INDICATOR_MOMENTUM, //Momentum Oscillator
INDICATOR_TRIX //Triple Exponential Average
};
//--- indicator to display
input indicators Indicator=INDICATOR_MA;
//--- indicator parameters
input string MA;
input ushort MAPeriod=14; //MA Period
input ENUM_MA_METHOD MAMethod=MODE_SMA; //MA Method
input ENUM_APPLIED_PRICE MAPrice=PRICE_CLOSE; //MA Price
//---
input string MACD;
input ushort FastEMA=12; //Fast EMA Period
input ushort SlowEMA=26; //Slow EMA Period
input ushort SignalSMA=9; //Signal SMA Period
input ENUM_APPLIED_PRICE MACDPrice=PRICE_CLOSE; //MACD Price
//---
input string Stochastic;
input ushort Kperiod=7; //K Period
input ushort Dperiod=3; //D Period
input ushort Slowing=3;
input ENUM_STO_PRICE PriceField=STO_LOWHIGH; //Price Field
//---
input string RSI;
input ushort RSIPeriod=14; //RSI Period
input ENUM_APPLIED_PRICE RSIPrice=PRICE_CLOSE; //RSI Price
//---
input string CCI;
input ushort CCIPeriod=14; //CCI Period
input ENUM_APPLIED_PRICE CCIPrice=PRICE_CLOSE; //CCI Price
//---
input string RVI;
input ushort RVIPeriod=14; //RVI Period
//---
input string DeMarker;
input ushort DeMarkerPeriod=14; //DeMarker Period
//---
input string Momentum;
input ushort MomentumPeriod=14; //Momentum Period
input ENUM_APPLIED_PRICE MomentumPrice=PRICE_CLOSE; //Momentum Price
//---
input string TriX;
input ushort TrixPeriod=14; //TriX Period
input ENUM_APPLIED_PRICE TrixPrice=PRICE_CLOSE; //TriX Price
//---
input string _; //---
input string Sfix=""; //Symbol Suffix
//--- currencies to display
input bool bAUD=true; //Display Aussie
input bool bCAD=true; //Display Loonie
input bool bCHF=true; //Display Swissy
input bool bEUR=true; //Display Fiber
input bool bGBP=true; //Display Sterling
input bool bNZD=true; //Display Kiwi
input bool bJPY=true; //Display Yen
input bool bUSD=true; //Display Greenback
//--- current indicator/symbol values
double ExtSymbol[indicator_handles];
//--- indicator and index objects
CCorrelate symbol[indicator_handles];
CCorrelate currency[indicator_buffers];
//--- chart properties
long chartID=ChartID();
short window;
bool IsVisible[indicator_buffers];
//--- symbols to use
string Symbols[indicator_handles]=
{
"AUDUSD","AUDJPY","USDCAD","CADJPY","USDCHF","CHFJPY",
"EURUSD","EURJPY","GBPUSD","GBPJPY","NZDUSD","NZDJPY"
};
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- symbols for indicators
string Symbol[indicator_handles];
//--- get necessary symbols
for(int i=0;i<indicator_handles;i++)
{
Symbol[i]=Symbols[i]+Sfix;
if(!SymbolSelect(Symbol[i],true)/* ||
!SymbolIsSynchronized(Symbol[i])*/)
{
Alert(Symbol[i]," not available in Market Watch.\nInitialization Failed.");
return(INIT_FAILED);
}
symbol[i].indicatorHandle=INVALID_HANDLE;
}
string shortName;
//--- set indicator properties
switch(Indicator)
{
case INDICATOR_MA:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iMA(Symbol[i],0,MAPeriod,0,MAMethod,MAPrice);
shortName=StringFormat("Correl8 %s(%d)",StringSubstr(EnumToString(MAMethod),5),MAPeriod);
IndicatorSetInteger(INDICATOR_DIGITS,3);
IndicatorSetInteger(INDICATOR_LEVELS,1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
break;
case INDICATOR_MACD:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iMACD(Symbol[i],0,FastEMA,SlowEMA,SignalSMA,MACDPrice);
shortName=StringFormat("Correl8 MACD(%d,%d,%d)",FastEMA,SlowEMA,SignalSMA);
IndicatorSetInteger(INDICATOR_DIGITS,3);
IndicatorSetInteger(INDICATOR_LEVELS,1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
break;
case INDICATOR_STOCHASTIC:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iStochastic(Symbol[i],0,Kperiod,Dperiod,Slowing,MODE_SMA,PriceField);
shortName=StringFormat("Correl8 Stochastic(%d,%d,%d)",Kperiod,Dperiod,Slowing);
IndicatorSetInteger(INDICATOR_DIGITS,0);
IndicatorSetInteger(INDICATOR_LEVELS,3);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level4);
IndicatorSetDouble(INDICATOR_LEVELVALUE,2,-indicator_level4);
break;
case INDICATOR_RSI:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iRSI(Symbol[i],0,RSIPeriod,RSIPrice);
shortName=StringFormat("Correl8 RSI(%d)",RSIPeriod);
IndicatorSetInteger(INDICATOR_DIGITS,0);
IndicatorSetInteger(INDICATOR_LEVELS,3);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level3);
IndicatorSetDouble(INDICATOR_LEVELVALUE,2,-indicator_level3);
break;
case INDICATOR_CCI:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iCCI(Symbol[i],0,CCIPeriod,CCIPrice);
shortName=StringFormat("Correl8 CCI(%d)",CCIPeriod);
IndicatorSetInteger(INDICATOR_DIGITS,0);
IndicatorSetInteger(INDICATOR_LEVELS,3);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level5);
IndicatorSetDouble(INDICATOR_LEVELVALUE,2,-indicator_level5);
break;
case INDICATOR_RVI:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iRVI(Symbol[i],0,RVIPeriod);
shortName=StringFormat("Correl8 RVI(%d)",RVIPeriod);
IndicatorSetInteger(INDICATOR_DIGITS,3);
IndicatorSetInteger(INDICATOR_LEVELS,3);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level2);
IndicatorSetDouble(INDICATOR_LEVELVALUE,2,-indicator_level2);
break;
case INDICATOR_DEMARKER:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iDeMarker(Symbol[i],0,DeMarkerPeriod);
shortName=StringFormat("Correl8 DeMarker(%d)",DeMarkerPeriod);
IndicatorSetInteger(INDICATOR_DIGITS,3);
IndicatorSetInteger(INDICATOR_LEVELS,3);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,indicator_level2);
IndicatorSetDouble(INDICATOR_LEVELVALUE,2,-indicator_level2);
break;
case INDICATOR_MOMENTUM:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iMomentum(Symbol[i],0,MomentumPeriod,MomentumPrice);
shortName=StringFormat("Correl8 Momentum(%d)",MomentumPeriod);
IndicatorSetInteger(INDICATOR_DIGITS,3);
IndicatorSetInteger(INDICATOR_LEVELS,1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
break;
case INDICATOR_TRIX:
for(int i=0;i<indicator_handles;i++)
symbol[i].indicatorHandle=iTriX(Symbol[i],0,TrixPeriod,TrixPrice);
shortName=StringFormat("Correl8 TriX(%d)",TrixPeriod);
IndicatorSetInteger(INDICATOR_DIGITS,5);
IndicatorSetInteger(INDICATOR_LEVELS,1);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,indicator_level1);
}
//--- check valid indicator handles
for(int i=0;i<indicator_handles;i++)
if(symbol[i].indicatorHandle==INVALID_HANDLE)
return(INIT_FAILED);
//--- set name & create labels
IndicatorSetString(INDICATOR_SHORTNAME,shortName);
window=(short)ChartWindowFind(chartID,shortName);
CreateLabels();
//--- indicator buffers mapping
for(int i=0;i<indicator_buffers;i++)
{
currency[i].SetIndexBuffer(i,INDICATOR_DATA);
currency[i].ArraySetAsSeries(true);
PlotIndexSetDouble(i,PLOT_EMPTY_VALUE,111);
}
//--- currencies to display
IsVisible[0] = bAUD;
IsVisible[1] = bCAD;
IsVisible[2] = bCHF;
IsVisible[3] = bEUR;
IsVisible[4] = bGBP;
IsVisible[5] = bNZD;
IsVisible[6] = bJPY;
IsVisible[7] = bUSD;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
enum __
{
USDCAD=2,
USDCHF=4,
JPY=6,
USD=7
};
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int 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 int &spread[])
{
//---
ArraySetAsSeries(time,true);
int limit=rates_total-prev_calculated;
//--- set initial bars to count and get bars/indicator calculated
if(prev_calculated<=0 || prev_calculated>rates_total)
{
int barsWindow=(int)ChartGetInteger(chartID,CHART_VISIBLE_BARS)+50;
for(int i=0;i<indicator_handles;i++)
if(symbol[i].BarsCalculated()<=barsWindow)
return(0);
for(int i=0;i<indicator_buffers;i++)
{
PlotIndexSetInteger(i,PLOT_DRAW_BEGIN,rates_total-barsWindow);
currency[i].ArrayInitialize(0.0);
}
limit=barsWindow;
}
//--- elements to copy/copied
int toCopy=limit+1,
Copied[indicator_handles];
//--- copy indicator values to arrays
for(int i=0;i<indicator_handles;i++)
{
Copied[i]=symbol[i].CopyBuffer(time[0],time[limit]);
if(prev_calculated==0 && Copied[i]!=toCopy)
{
printf("Insufficient or incorrect data in history for %s",Symbols[i]);
return(0);
}
else if(prev_calculated>0 && Copied[i]!=toCopy)
{
for(int k=0;k<indicator_buffers;k++)
currency[k].IndexBuffer[0]=currency[k].IndexBuffer[1];
return(rates_total-1);
}
}
//--- main loop for indicator calculation
for(int i=limit,k=0;i>=0 && !IsStopped();i--,k++)
{
switch(Indicator)
{
case INDICATOR_MA:
for(int h=0,j=1;j<indicator_handles;h+=2,j+=2)
{
//--- check new bar/set previous MA values
static double PrevSymbol[indicator_handles];
static int n;
if(k>0 && (n!=k || n!=i))
{
for(int p=0;p<indicator_handles;p++)
PrevSymbol[p]=symbol[p].SymbolBuffer[k-1];
n=k;
}
//--- get value for each symbol
ExtSymbol[h] = (symbol[h].SymbolBuffer[k]-PrevSymbol[h])*100;
ExtSymbol[j] = symbol[j].SymbolBuffer[k]-PrevSymbol[j];
}
break;
case INDICATOR_MACD:
for(int h=0,j=1;j<indicator_handles;h+=2,j+=2)
{
//--- get value for each symbol
ExtSymbol[h] = symbol[h].SymbolBuffer[k]*100;
ExtSymbol[j] = symbol[j].SymbolBuffer[k];
}
break;
case INDICATOR_STOCHASTIC:
case INDICATOR_RSI:
for(int h=0;h<indicator_handles;h++)
//--- get value for each symbol
ExtSymbol[h]=symbol[h].SymbolBuffer[k]-50;
break;
case INDICATOR_CCI:
case INDICATOR_RVI:
for(int h=0;h<indicator_handles;h++)
//--- get value for each symbol
ExtSymbol[h]=symbol[h].SymbolBuffer[k];
break;
case INDICATOR_DEMARKER:
for(int h=0;h<indicator_handles;h++)
//--- get value for each symbol
ExtSymbol[h]=symbol[h].SymbolBuffer[k]-.5;
break;
case INDICATOR_MOMENTUM:
for(int h=0;h<indicator_handles;h++)
//--- get value for each symbol
ExtSymbol[h]=symbol[h].SymbolBuffer[k]-100;
break;
case INDICATOR_TRIX:
for(int h=0;h<indicator_handles;h++)
//--- get value for each symbol
ExtSymbol[h]=symbol[h].SymbolBuffer[k]*100;
}
//--- invert USD based values
ExtSymbol[USDCAD] *= -1;
ExtSymbol[USDCHF] *= -1;
//--- calculate each currency's value
static double SumSymbol[indicator_buffers];
ArrayInitialize(SumSymbol,0.0);
for(int a=0,b=0,c=1;c<indicator_handles;a++,b+=2,c+=2)
{
SumSymbol[a]=(ExtSymbol[b]+ExtSymbol[c])/2;
SumSymbol[JPY] += -ExtSymbol[c];
SumSymbol[USD] += -ExtSymbol[b];
}
SumSymbol[JPY] /= 6;
SumSymbol[USD] /= 6;
//--- assign currency value to buffer
for(int s=0;s<indicator_buffers;s++)
currency[s].IndexBuffer[i]=IsVisible[s]?SumSymbol[s]:111;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(chartID,window,OBJ_LABEL);
for(int i=0;i<indicator_handles;i++)
symbol[i].IndicatorRelease();
//for(int i=0;i<indicator_buffers;i++)
// currency[i].ArrayFree();
return;
}
//+-------------------------------------------------------------------+
//| Create colored currency labels |
//+-------------------------------------------------------------------+
void CreateLabels()
{
//--- currency names (indicator_labelN doesn't work, fixed next build)
string Currency[indicator_buffers]=
{
"AUD","CAD","CHF","EUR",
"GBP","NZD","JPY","USD"
};
//--- currency colors
color Color[indicator_buffers]=
{
indicator_color1,indicator_color2,
indicator_color3,indicator_color4,
indicator_color5,indicator_color6,
indicator_color7,indicator_color8
};
//--- x coordinates
int xStart=4;
int xIncrement=24;
//--- y coordinates
int yStart=16;
int yIncrement=0;
//--- create all labels
for(int i=0;i<indicator_buffers;i++)
{
ObjectCreate(Currency[i],xStart,yStart,Color[i]);
xStart += xIncrement;
yStart += yIncrement;
}
}
//+------------------------------------------------------------------+
//| Create label objects at coordinates |
//+------------------------------------------------------------------+
void ObjectCreate(string label,int x,int y,int clr)
{
string name=label+(string)window;
ObjectCreate(chartID,name,OBJ_LABEL,window,0,0);
ObjectSetString(chartID,name,OBJPROP_TEXT,label);
ObjectSetString(chartID,name,OBJPROP_FONT,"Arial Bold");
ObjectSetInteger(chartID,name,OBJPROP_FONTSIZE,8);
ObjectSetInteger(chartID,name,OBJPROP_COLOR,clr);
ObjectSetInteger(chartID,name,OBJPROP_XDISTANCE,x);
ObjectSetInteger(chartID,name,OBJPROP_YDISTANCE,y);
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---