Author: Vladradon 2021
Price Data Components
Series array that contains close prices for each bar
Miscellaneous
It issuies visual alerts to the screenIt plays sound alertsIt sends emails
1 Views
0 Downloads
0 Favorites
Delta3MCH
ÿþ#property copyright   "Vladradon 2021"

#property link        "fxradon@gmail.com"

#property description "Delta3MCH"

#property version   "1.20"

#property strict

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots 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



input string Id="1";

input bool   Triangle=true;

input string Sym1="EURUSD";

input string Sym2="USDCHF";

input string Sym3="EURCHF";

input bool   ShowMax=true; 

input int    BarsCount=300;

input ENUM_TIMEFRAMES TFcount=PERIOD_CURRENT;

input ENUM_TIMEFRAMES TFRecount=PERIOD_D1;

input int    SigMax=80;     

input int    SigMin=10;     

input bool   Alerts=false;  

input bool   Sound=false;

input bool   Mail=false; 

input bool   Mobile=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;



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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   IndicatorSetInteger(INDICATOR_DIGITS,0);

//--- indicator buffers mapping

   SetIndexBuffer(0,L1,INDICATOR_DATA);

   PlotIndexSetString(0,PLOT_LABEL,Sym1+"-"+Sym3);

   

   SetIndexBuffer(1,L2,INDICATOR_DATA);

   PlotIndexSetString(1,PLOT_LABEL,Sym2+"-"+Sym3);

   

   if(Triangle) {PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE); PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_LINE);}

   else {PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_NONE); PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_NONE);}



   SetIndexBuffer(2,L3,INDICATOR_DATA);

   PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_LINE);

   PlotIndexSetString(2,PLOT_LABEL,Sym1+"-"+Sym2);



   SetIndexBuffer(3,DP,INDICATOR_DATA);

   ArraySetAsSeries(DP,true);

   SetIndexBuffer(4,DM,INDICATOR_DATA);

   ArraySetAsSeries(DM,true);

   PlotIndexSetString(3,PLOT_LABEL,"Maximum "+Sym1+"-"+Sym2);

   PlotIndexSetString(4,PLOT_LABEL,"Minimum "+Sym1+"-"+Sym2);

   if(ShowMax) {PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_LINE); PlotIndexSetInteger(4,PLOT_DRAW_TYPE,DRAW_LINE);}

   else {PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_NONE); PlotIndexSetInteger(4,PLOT_DRAW_TYPE,DRAW_NONE);}

//---   

   ArraySetAsSeries(L1,true);

   ArraySetAsSeries(L2,true);

   ArraySetAsSeries(L3,true);

   

   ArrayResize(L4,BarsCount,0);

   ArraySetAsSeries(L4,true);

   ArrayResize(L5,BarsCount,0);

   ArraySetAsSeries(L5,true);

   ArrayResize(L6,BarsCount,0);

   ArraySetAsSeries(L6,true);



   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);

//---

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

  {

//---

   KDBase=MathPow(10,-1*SymbolInfoInteger(Sym1,SYMBOL_DIGITS))/0.00001;

   KDHedge1=MathPow(10, -1*SymbolInfoInteger(Sym2, SYMBOL_DIGITS))/0.00001;

   KDHedge2=MathPow(10, -1*SymbolInfoInteger(Sym3, SYMBOL_DIGITS))/0.00001;

   

   if(KDBase==0 || KDHedge1==0 || KDHedge2==0) return(rates_total);

//---   

   if(Told!=iTime(_Symbol,TFRecount,0))

     {

      if((corr=Cor(Sym1,Sym2))<-1) return(rates_total);

      if((corr2=Cor(Sym1,Sym3))<-1) return(rates_total);

      if(!KN(Sym1,Sym2,Sym3)) return(rates_total);  

      Told=iTime(_Symbol,TFRecount,0);

     }

   IndicatorSetString(INDICATOR_SHORTNAME,"Correlation "+Id+" "+DoubleToString(corr,2)+" | DELTA "+Sym1+" / "+Sym2+" = "+Sym3+" | "+TF(TFcount)+" | ");

//---

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,rates_total-BarsCount);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,rates_total-BarsCount);

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,rates_total-BarsCount);

   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,rates_total-BarsCount);

   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,rates_total-BarsCount);

//---

   ArrayInitialize(L1,0.0);

   ArrayInitialize(L2,0.0);

   ArrayInitialize(L3,0.0);

   ArrayInitialize(L4,0.0);

   ArrayInitialize(L5,0.0);

   ArrayInitialize(L6,0.0);

//---

   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];

      if(DMM>L3[i]) DMM=L3[i];

     }

//---   

   ArrayInitialize(DP,DMP);

   ArrayInitialize(DM,DMM);

//---

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

//---

   if(ObjectFind(0,Id+" Max")<0)

     {

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

      ObjectSetString(0,Id+" Max",OBJPROP_FONT,"Arial Bold");

      ObjectSetInteger(0,Id+" Max",OBJPROP_COLOR,clrMagenta);

      ObjectSetInteger(0,Id+" Max",OBJPROP_FONTSIZE,9);

      ObjectSetInteger(0,Id+" Max",OBJPROP_CORNER,CORNER_RIGHT_UPPER);

      ObjectSetInteger(0,Id+" Max",OBJPROP_XDISTANCE,70);

      ObjectSetInteger(0,Id+" Max",OBJPROP_YDISTANCE,5);

     }

   ObjectSetString(0,Id+" Max",OBJPROP_TEXT,"MAX "+DoubleToString(DMP,0));

//---

   if(ObjectFind(0,Id+" Min")<0)

     {

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

      ObjectSetString(0,Id+" Min",OBJPROP_FONT,"Arial Bold");

      ObjectSetInteger(0,Id+" Min",OBJPROP_COLOR,clrMagenta);

      ObjectSetInteger(0,Id+" Min",OBJPROP_FONTSIZE,9);

      ObjectSetInteger(0,Id+" Min",OBJPROP_CORNER,CORNER_RIGHT_LOWER);

      ObjectSetInteger(0,Id+" Min",OBJPROP_XDISTANCE,70);

      ObjectSetInteger(0,Id+" Min",OBJPROP_YDISTANCE,20);

     }

   ObjectSetString(0,Id+" Min",OBJPROP_TEXT,"MIN "+DoubleToString(DMM,0));

//---

   color Col=clrWhite;

   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;

   if(ObjectFind(0,Id+" Per")<0)

     {

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

      ObjectSetString(0,Id+" Per",OBJPROP_FONT,"Arial Bold");

      ObjectSetInteger(0,Id+" Per",OBJPROP_FONTSIZE,9);

     }

   ObjectSetInteger(0,Id+" Per",OBJPROP_COLOR,Col);

   ObjectSetString(0,Id+" Per",OBJPROP_TEXT,"   Delta "+DoubleToString(Percent,0)+"%");

   ObjectMove(0,Id+" Per",0,TimeCurrent(),0);

//---

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

     {

      if(Alerts) Alert("DELTA <<"+Sym1+" / "+Sym2+" = "+Sym3+"<>"+TF(TFcount)+" >> "+DoubleToString(Percent,0)+"%");

      if(Sound) PlaySound("Alert.wav");

      if(Mail) SendMail("DELTA <<"+Sym1+" / "+Sym2+" = "+Sym3+"<>"+TF(TFcount),"DELTA <<"+Sym1+" / "+Sym2+" = "+Sym3+"<>"+TF(TFcount)+" >> "+DoubleToString(Percent,0)+"%");

      if(Mobile) SendNotification("DELTA <<"+Sym1+" / "+Sym2+" = "+Sym3+"<>"+TF(TFcount)+" >> "+DoubleToString(Percent,0)+"%");

      alert=true;

     }

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

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

   return(rates_total);

  }

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

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

//|  0AG5B :>MDD8F85=B0 A42830                                                              

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

double K1=0.0;

double K2=0.0;

double KDBase,KDHedge1,KDHedge2;

double AverageH1=0.0, AverageH2=0.0;

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

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

  {

   int i;

   double Average=0.0;

   if(CopyOpen(base,TFcount,0,BarsCount,BaseOpen)<BarsCount) return(false);

   if(CopyHigh(base,TFcount,0,BarsCount,BaseHigh)<BarsCount) return(false);

   if(CopyLow(base,TFcount,0,BarsCount,BaseLow)<BarsCount) return(false);

   if(CopyClose(base,TFcount,0,BarsCount,BaseClose)<BarsCount) return(false);

   if(CopyOpen(hedge1,TFcount,0,BarsCount,HedgeOpen)<BarsCount) return(false);

   if(CopyHigh(hedge1,TFcount,0,BarsCount,HedgeHigh)<BarsCount) return(false);

   if(CopyLow(hedge1,TFcount,0,BarsCount,HedgeLow)<BarsCount) return(false);

   if(CopyClose(hedge1,TFcount,0,BarsCount,HedgeClose)<BarsCount) return(false);

//---   

   if(corr>0)

     for(i= BarsCount-1; i>= 0; 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--) 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;

//---   

   CopyOpen(hedge2,TFcount,0,BarsCount,HedgeOpen);

   CopyHigh(hedge2,TFcount,0,BarsCount,HedgeHigh);

   CopyLow(hedge2,TFcount,0,BarsCount,HedgeLow);

   CopyClose(hedge2,TFcount,0,BarsCount,HedgeClose);

   Average=0.0;

   if(corr2>0)

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

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

   if(corr2<0)

     {

      for(i=BarsCount-1; i>= 0; 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;

   return(true);

  }

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

//|01>@ DC=:F89 4;O @0AG5B0 :>@@5;OF88                                                      |

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

double AverageBase=0.0, AverageHedge=0.0;

double symboldif(int symbol=0,int shift=0)

  {

   double res=0.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)<1) return(-2);

   double u=0.0,l=0.0,s=0.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(-2);

  }

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

//| $C=:F8O :>?8@>20=8O 8AB>@88 8 2KG8A;5=8O A@54=59                                  

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

int HistoryBuffers(string base,string hedge)

  {

   int BarsAll=BarsCount;

   AverageBase=0.0; AverageHedge=0.0;

   if(CopyOpen(base,TFcount,0,BarsCount,BaseOpen)==BarsCount)

      if(CopyHigh(base,TFcount,0,BarsCount,BaseHigh)==BarsCount)

         if(CopyLow(base,TFcount,0,BarsCount,BaseLow)==BarsCount)

            if(CopyClose(base,TFcount,0,BarsCount,BaseClose)==BarsCount)

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

                 {

                  if(BaseOpen[i]<=0 || BaseHigh[i]<=0 || BaseLow[i]<=0 || BaseClose[i]<=0) {BarsAll=BarsAll-1; continue;}

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

                 }



   AverageBase=AverageBase/BarsAll;

   BarsAll=BarsCount;

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



   if(CopyOpen(hedge,TFcount,0,BarsCount,HedgeOpen)==BarsCount)

      if(CopyHigh(hedge,TFcount,0,BarsCount,HedgeHigh)==BarsCount)

         if(CopyLow(hedge,TFcount,0,BarsCount,HedgeLow)==BarsCount)

            if(CopyClose(hedge,TFcount,0,BarsCount,HedgeClose)==BarsCount)

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

                 {

                  if(HedgeOpen[j]<=0 || HedgeHigh[j]<=0 || HedgeLow[j]<=0 || HedgeClose[j]<=0) {BarsAll=BarsAll-1; continue;}

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

                 }



   AverageHedge=AverageHedge/BarsAll;

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

   return (1);

  }

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

string TF(int Per)

  {

   switch(Per)

     {

      case PERIOD_M1: return("M1");

      case PERIOD_M2: return("M2");

      case PERIOD_M3: return("M3");

      case PERIOD_M4: return("M4");

      case PERIOD_M5: return("M5");

      case PERIOD_M6: return("M6");

      case PERIOD_M10: return("M10");

      case PERIOD_M12: return("M12");

      case PERIOD_M15: return("M15");

      case PERIOD_M20: return("M20");

      case PERIOD_M30: return("M30");

      case PERIOD_H1: return("H1");

      case PERIOD_H2: return("H2");

      case PERIOD_H3: return("H3");

      case PERIOD_H4: return("H4");

      case PERIOD_H6: return("H6");

      case PERIOD_H8: return("H8");

      case PERIOD_H12: return("H12");

      case PERIOD_D1: return("D1");

      case PERIOD_W1: return("W1");

      case PERIOD_MN1: return("MN1");

      case PERIOD_CURRENT:

         switch(_Period)

           {

            case PERIOD_M1: return("M1");

            case PERIOD_M2: return("M2");

            case PERIOD_M3: return("M3");

            case PERIOD_M4: return("M4");

            case PERIOD_M5: return("M5");

            case PERIOD_M6: return("M6");

            case PERIOD_M10: return("M10");

            case PERIOD_M12: return("M12");

            case PERIOD_M15: return("M15");

            case PERIOD_M20: return("M20");

            case PERIOD_M30: return("M30");

            case PERIOD_H1: return("H1");

            case PERIOD_H2: return("H2");

            case PERIOD_H3: return("H3");

            case PERIOD_H4: return("H4");

            case PERIOD_H6: return("H6");

            case PERIOD_H8: return("H8");

            case PERIOD_H12: return("H12");

            case PERIOD_D1: return("D1");

            case PERIOD_W1: return("W1");

            case PERIOD_MN1: return("MN1");

           }

     }

   return("CURRENT");

  }

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

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 ---