Delta3MCHen

Author: Vladradon 2018
Price Data Components
Series array that contains close prices for each barSeries array that contains open prices of each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt plays sound alerts
0 Views
0 Downloads
0 Favorites
Delta3MCHen
ÿþ#property copyright   "Vladradon 2018"

#property link        "fxradon@gmail.com"

#property description "Delta3MCH"

#property version   "1.20"

#property strict

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_color1 clrGreen

#property indicator_color2 clrRed

#property indicator_color3 clrYellow

#property indicator_color4 clrMagenta

#property indicator_color5 clrMagenta

#property indicator_level1 0



extern string Id="1";

extern bool   Triangle=true;

extern string Sym1="EURUSD";

extern string Sym2="USDCHF";

extern string Sym3="EURCHF";

extern bool   ShowMax=true; 

extern int    BarsCount=300;

extern ENUM_TIMEFRAMES TFcount=PERIOD_CURRENT;

extern ENUM_TIMEFRAMES TFRecount=PERIOD_CURRENT;

extern int    SigMax=80;     

extern int    SigMin=10;     

extern bool   Alerts=false;  

extern bool   Sound=false;   





double L1[];

double L2[];

double L3[];

double L4[];

double L5[];

double L6[];

double DP[];

double DM[];

double BaseOpen[],BaseHigh[],BaseLow[],BaseClose[];

double HedgeOpen[],HedgeHigh[],HedgeLow[],HedgeClose[];

double corr=0.0,corr2=0.0;

datetime Told=0;

double DMP=0.0,DMM=0.0;

int Percent=0;

bool alert=false;

int IndxDMP=-1,IndxDMM=-1;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   IndicatorBuffers(8);

   IndicatorDigits(0);

//--- indicator buffers mapping

   SetIndexBuffer(0,L1);

   SetIndexLabel(0,Sym1+"-"+Sym3);

   SetIndexBuffer(1,L2);

   SetIndexLabel(1,Sym2+"-"+Sym3);

   if(Triangle) {SetIndexStyle(0,DRAW_LINE); SetIndexStyle(1,DRAW_LINE);} else {SetIndexStyle(0,DRAW_NONE); SetIndexStyle(1,DRAW_NONE);}



   SetIndexBuffer(2,L3);

   SetIndexStyle(2,DRAW_LINE);

   SetIndexLabel(2,Sym1+"-"+Sym2);



   SetIndexBuffer(3,DP);

   SetIndexBuffer(4,DM);

   SetIndexLabel(3,"Maximum "+Sym1+"-"+Sym2);

   SetIndexLabel(4,"Minimum "+Sym1+"-"+Sym2);

   if(ShowMax) {SetIndexStyle(3,DRAW_LINE); SetIndexStyle(4,DRAW_LINE);} else {SetIndexStyle(3,DRAW_NONE); SetIndexStyle(4,DRAW_NONE);}



   SetIndexBuffer(5,L4);

   SetIndexStyle(5,DRAW_NONE);

   SetIndexBuffer(6,L5);

   SetIndexStyle(6,DRAW_NONE);

   SetIndexBuffer(7,L6);

   SetIndexStyle(7,DRAW_NONE);



   ArrayResize(BaseOpen,BarsCount,0);

   ArrayResize(BaseHigh,BarsCount,0);

   ArrayResize(BaseLow,BarsCount,0);

   ArrayResize(BaseClose,BarsCount,0);

   ArrayResize(HedgeOpen,BarsCount,0);

   ArrayResize(HedgeHigh,BarsCount,0);

   ArrayResize(HedgeLow,BarsCount,0);

   ArrayResize(HedgeClose,BarsCount,0);



   ArrayInitialize(L1,0.0);

   ArrayInitialize(L2,0.0);

   ArrayInitialize(L3,0.0);

   ArrayInitialize(L4,0.0);

   ArrayInitialize(L5,0.0);

   ArrayInitialize(L6,0.0);

//---

   corr=Cor(Sym1,Sym2);

   corr2=Cor(Sym1,Sym3);

   return(INIT_SUCCEEDED);

  }

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

//|                                                                  |

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

void OnDeinit(const int reason)

  {

   if(ObjectFind(0,Id+" Max")>0) ObjectDelete(0,Id+" Max");

   if(ObjectFind(0,Id+" Min")>0) ObjectDelete(0,Id+" Min");

   if(ObjectFind(0,Id+" Per")>0) ObjectDelete(0,Id+" Per");

  }

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

//| 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[])

  {

//---

   if(Told!=iTime(_Symbol,TFRecount,0)) {KN(Sym1,Sym2,Sym3); corr=Cor(Sym1,Sym2); corr2=Cor(Sym1,Sym3); Told=iTime(_Symbol,TFRecount,0); IndxDMP=-1; IndxDMM=-1;}

   IndicatorShortName("Correlation "+Id+" "+DoubleToString(corr,2)+" | DELTA "+Sym1+" / "+Sym2+" = "+Sym3+" | ");



   for(int i=BarsCount-1; i>=0; i--)

     {

      L4[i]=iClose(Sym1,TFcount,i)/KDBase;

      if(corr>0) L5[i]=iClose(Sym2,TFcount,i)/KDHedge1+K1;

      if(corr<0) L5[i]=2*AverageH1-iClose(Sym2,TFcount,i)/KDHedge1+K1;

      if(corr2>0) L6[i]=iClose(Sym3,TFcount,i)/KDHedge2+K2;

      if(corr2<0) L6[i]=2*AverageH2-iClose(Sym3,TFcount,i)/KDHedge2+K2;

      L1[i]=(int)((L4[i]-L6[i])/0.00001);

      L2[i]=(int)((L5[i]-L6[i])/0.00001);

      L3[i]=(int)((L4[i]-L5[i])/0.00001);

      if(DMP<L3[i]) {DMP=L3[i]; IndxDMP=i;}

      if(DMM>L3[i]) {DMM=L3[i]; IndxDMM=i;}

     }



   for(int i=BarsCount-1; i>=0; i--) {DP[i]=DMP; DM[i]=DMM;}



   int win=WindowFind("Correlation "+Id+" "+DoubleToString(corr,2)+" | DELTA "+Sym1+" / "+Sym2+" = "+Sym3+" | ");

   if(ObjectFind(0,Id+" Max")>0) ObjectDelete(0,Id+" Max");

   if(ObjectFind(0,Id+" Min")>0) ObjectDelete(0,Id+" Min");

   if(ObjectFind(0,Id+" Per")>0) ObjectDelete(0,Id+" Per");

   ObjectCreate(0,Id+" Max",OBJ_LABEL,win,TimeCurrent(),DMP);

   ObjectSetText(Id+" Max","                    MAX "+DoubleToStr(DMP,0),9,"Arial Bold",clrMagenta);

   ObjectSet(Id+" Max",OBJPROP_CORNER,CORNER_RIGHT_UPPER);

   ObjectSet(Id+" Max",OBJPROP_XDISTANCE,10);

   ObjectSet(Id+" Max",OBJPROP_YDISTANCE,5);

   ObjectCreate(0,Id+" Min",OBJ_LABEL,win,TimeCurrent(),DMM+(int)MathAbs(DMM/100*21));

   ObjectSetText(Id+" Min","                    MIN "+DoubleToStr(DMM,0),9,"Arial Bold",clrMagenta);

   ObjectSet(Id+" Min",OBJPROP_CORNER,CORNER_RIGHT_LOWER);

   ObjectSet(Id+" Min",OBJPROP_XDISTANCE,10);

   ObjectSet(Id+" Min",OBJPROP_YDISTANCE,5);

   color Col=clrWhite;

   ObjectCreate(0,Id+" Per",OBJ_TEXT,win,TimeCurrent(),0);

   if(L3[0]>0) {Percent=(int)(L3[0]/(DMP/100)); Col=clrLime;}

   if(L3[0]<0) {Percent=(int)(-L3[0]/(DMM/100)); Col=clrOrangeRed;}

   if(L3[0]==0) Percent=0;

   ObjectSetText(Id+" Per","                           Delta "+DoubleToStr(Percent,0)+"%",9,"Arial Bold",Col);



   if(!alert && (MathAbs(Percent)>=SigMax || MathAbs(Percent)<=SigMin))

     {if(Alerts) Alert("#@>25=L 4825@35=F88 ",Percent,"%"); if(Sound) PlaySound("Alert.wav"); alert=true;}

   if(alert && Percent<SigMax-3 && Percent>SigMin+3) alert=false;



//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

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

//|                                                              

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

double K1=0;

double K2=0;

double KDBase,KDHedge1,KDHedge2;

double AverageH1=0.0,AverageH2=0.0;

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

void KN(string base,string hedge1,string hedge2)

  {

   int i;

   double Average=0.0;

   KDBase=MathPow(10,-1*MarketInfo(base,MODE_DIGITS))/0.00001;

   KDHedge1=MathPow(10, -1*MarketInfo(hedge1,MODE_DIGITS))/0.00001;

   KDHedge2=MathPow(10, -1*MarketInfo(hedge2,MODE_DIGITS))/0.00001;

   if(corr>0)

      for(i=BarsCount-1; i>=0; i--)

        {

         BaseOpen[i]=iOpen(base,TFcount,i);

         BaseHigh[i]=iHigh(base,TFcount,i);

         BaseLow[i]=iLow(base,TFcount,i);

         BaseClose[i]=iClose(base,TFcount,i);

         HedgeOpen[i]=iOpen(hedge1,TFcount,i);

         HedgeHigh[i]=iHigh(hedge1,TFcount,i);

         HedgeLow[i]=iLow(hedge1,TFcount,i);

         HedgeClose[i]=iClose(hedge1,TFcount,i);

         Average+=(BaseOpen[i]+BaseHigh[i]+BaseLow[i]+BaseClose[i])/4/KDBase-(HedgeOpen[i]+HedgeHigh[i]+HedgeLow[i]+HedgeClose[i])/4/KDHedge1;

        }

   if(corr<0)

     {

      for(i=BarsCount-1; i>=0; i--)

        {

         BaseOpen[i]=iOpen(base,TFcount,i);

         BaseHigh[i]=iHigh(base,TFcount,i);

         BaseLow[i]=iLow(base,TFcount,i);

         BaseClose[i]=iClose(base,TFcount,i);

         HedgeOpen[i]=iOpen(hedge1,TFcount,i);

         HedgeHigh[i]=iHigh(hedge1,TFcount,i);

         HedgeLow[i]=iLow(hedge1,TFcount,i);

         HedgeClose[i]=iClose(hedge1,TFcount,i);

         AverageH1+=(HedgeOpen[i]+HedgeHigh[i]+HedgeLow[i]+HedgeClose[i])/4/KDHedge1;

        }

      AverageH1=AverageH1/BarsCount;

      for(i=BarsCount-1; i>=0; i--) Average+=(BaseOpen[i]+BaseHigh[i]+BaseLow[i]+BaseClose[i])/4/KDBase-(2*AverageH1-(HedgeOpen[i]+HedgeHigh[i]+HedgeLow[i]+HedgeClose[i])/4/KDHedge1);

     }

   K1=Average/BarsCount;

   Average=0.0;

   if(corr>0)

      for(i=BarsCount-1; i>=0; i--)

        {

         HedgeOpen[i]=iOpen(hedge2,TFcount,i);

         HedgeHigh[i]=iHigh(hedge2,TFcount,i);

         HedgeLow[i]=iLow(hedge2,TFcount,i);

         HedgeClose[i]=iClose(hedge2,TFcount,i);

         Average+=(BaseOpen[i]+BaseHigh[i]+BaseLow[i]+BaseClose[i])/4/KDBase-(HedgeOpen[i]+HedgeHigh[i]+HedgeLow[i]+HedgeClose[i])/4/KDHedge2;

        }

   if(corr<0)

     {

      for(i=BarsCount-1; i>=0; i--)

        {

         BaseOpen[i]=iOpen(base,TFcount,i);

         BaseHigh[i]=iHigh(base,TFcount,i);

         BaseLow[i]=iLow(base,TFcount,i);

         BaseClose[i]=iClose(base,TFcount,i);

         HedgeOpen[i]=iOpen(hedge2,TFcount,i);

         HedgeHigh[i]=iHigh(hedge2,TFcount,i);

         HedgeLow[i]=iLow(hedge2,TFcount,i);

         HedgeClose[i]=iClose(hedge2,TFcount,i);

         AverageH2+=(HedgeOpen[i]+HedgeHigh[i]+HedgeLow[i]+HedgeClose[i])/4/KDHedge2;

        }

      AverageH2=AverageH2/BarsCount;

      for(i=BarsCount-1; i>=0; i--) Average+=(BaseOpen[i]+BaseHigh[i]+BaseLow[i]+BaseClose[i])/4/KDBase-(2*AverageH2-(HedgeOpen[i]+HedgeHigh[i]+HedgeLow[i]+HedgeClose[i])/4/KDHedge2);

     }

   K2=Average/BarsCount;

  }

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

//|                                                     |

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

double AverageBase=0.0,AverageHedge=0.0;

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

double symboldif(int symbol,int shift)

  {

   double res=0;

   if(symbol==1) res=(BaseOpen[shift]+BaseHigh[shift]+BaseLow[shift]+BaseClose[shift])/4-AverageBase;

   if(symbol==2) res=(HedgeOpen[shift]+HedgeHigh[shift]+HedgeLow[shift]+HedgeClose[shift])/4-AverageHedge;

   return (res);

  }

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

double Cor(string base,string hedge)

  {

   if(HistoryBuffers(base, hedge)==0) return (0);

   double u=0,l=0,s=0;

   for(int i=BarsCount-1; i>=0; i--)

     {

      u += symboldif(1, i)*symboldif(2, i);

      l += MathPow(symboldif(1, i),2);

      s += MathPow(symboldif(2, i),2);

     }

   if(l*s>0) return(u/MathSqrt(l*s));

   return (0);

  }

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

//|                                 

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

int HistoryBuffers(string base,string hedge)

  {

   AverageBase=0.0; AverageHedge=0.0;

   for(int i=BarsCount-1; i>=0; i--)

     {

      BaseOpen[i]=iOpen(base,TFcount,i);

      BaseHigh[i]=iHigh(base,TFcount,i);

      BaseLow[i]=iLow(base,TFcount,i);

      BaseClose[i]=iClose(base,TFcount,i);

      AverageBase=AverageBase+(BaseOpen[i]+BaseHigh[i]+BaseLow[i]+BaseClose[i])/4;

      HedgeOpen[i]=iOpen(hedge,TFcount,i);

      HedgeHigh[i]=iHigh(hedge,TFcount,i);

      HedgeLow[i]=iLow(hedge,TFcount,i);

      HedgeClose[i]=iClose(hedge,TFcount,i);

      AverageHedge=AverageHedge+(HedgeOpen[i]+HedgeHigh[i]+HedgeLow[i]+HedgeClose[i])/4;

     }

   AverageBase=AverageBase/BarsCount;

   if(AverageBase<=0) return (0);

   AverageHedge=AverageHedge/BarsCount;

   if(AverageHedge<=0) return (0);

   return (1);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---