iK_pair-tr_12_11_01

Price Data Components
Series array that contains close prices for each bar
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
iK_pair-tr_12_11_01
//+------------------------------------------------------------------+
//|                                                   iK_pair-tr.mq4 |
//|                                                      Ivan Katsko |
//|                                                    ICQ:372739628 |
//+------------------------------------------------------------------+
#property  copyright "Ivan Katsko"
#property  link      "ICQ:372739628"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 Blue
//---- indicator parameters
extern string bas_SYMB="EURUSD";
extern string sec_SYMB="USDCHF";
extern int History=288;
extern double Precision=5;
extern int MA_Period=1;
//---- indicator buffers
double Delta[];
double delta_coef_cor[];
double up[];
double dn[];
double ExtMapBuffer[];
double ExtMapBuffer1[];
double coef_cor[256];
//----
bool   New_Bar=false;                     // Ôëàã íîâîãî áàðà 
static datetime New_Time;
int ExtCountedBars=0;
double coef=0, cor_ind,
       bas_spread, sec_spread,
       pt;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   IndicatorBuffers(6);
   int    draw_begin;
   string short_name;
//---- indicator buffers mapping
   SetIndexBuffer(0,Delta);
   SetIndexBuffer(1,delta_coef_cor);
   SetIndexBuffer(2,up);
   SetIndexBuffer(3,dn);
   SetIndexBuffer(4,ExtMapBuffer);
   SetIndexBuffer(5,ExtMapBuffer1);
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexStyle(3,DRAW_LINE);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   draw_begin=MA_Period-1;
//---- indicator short name
   SetIndexDrawBegin(0,draw_begin);
   SetIndexDrawBegin(1,draw_begin);
   
//---- initialization done
   bas_spread = MarketInfo(bas_SYMB,MODE_SPREAD)*Point;
   sec_spread = MarketInfo(sec_SYMB,MODE_SPREAD)*Point;
   cor_ind = correl(bas_SYMB, sec_SYMB, 0, History);
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   ObjectsDeleteAll();
   return(0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   IndicatorShortName(bas_SYMB+"/"+sec_SYMB+" pt="+DoubleToStr(pt/Point,0)+" cor="+DoubleToStr(cor_ind,4));
   Fun_New_Bar();
   if(Bars<=MA_Period) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
//----
   if (New_Bar) {
      ObjectsDeleteAll();
      SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
      cor_ind = correl(bas_SYMB, sec_SYMB, 0, History);
   }
//---- done
   return(0);
  }
//------------------âû÷èñëÿåì êîððåëÿöèþ-------------------------------------------------------
double correl(string symb1, string symb2, int tf, int hst) {
   double a1,a2;
   double thisl1_2;
   double sumraz_1, sumraz_2;
   double znamen1_2, correl_1_2;
   double signal1[256];
   double signal2[256];
   ArrayResize(signal1, hst);
   ArrayResize(signal2, hst);
   ArrayResize(coef_cor, hst);
   ArrayCopySeries(signal1,MODE_CLOSE,symb1,tf);
   ArrayCopySeries(signal2,MODE_CLOSE,symb2,tf);
   for (int q=0;q<hst;q++) {
      a1=a1+signal1[q];      
      a2=a2+signal2[q];  
   }
   a1=a1/hst;    
   a2=a2/hst;    
   for (int q1=0;q1<hst;q1++) {  
      thisl1_2=thisl1_2+(signal1[q1]-a1)*(signal2[q1]-a2);
   }
   for (int q2=0;q2<hst;q2++){
      sumraz_1=sumraz_1+MathPow((signal1[q2]-a1),2);
      sumraz_2=sumraz_2+MathPow((signal2[q2]-a2),2);
   }
   if(sumraz_1!=0 && sumraz_2!=0) {znamen1_2=MathSqrt(sumraz_1)*MathSqrt(sumraz_2);}
   if(sumraz_1!=0 && sumraz_2!=0) {correl_1_2=thisl1_2/znamen1_2;}
   if (correl_1_2 < 0) {
      for (int q3=0;q3<hst;q3++) {
         coef_cor[q3] = NormalizeDouble(signal1[q3]*signal2[q3],Digits);
      }
   }
   if (correl_1_2 > 0) {
      for (q3=0;q3<hst;q3++) {
         coef_cor[q3] = NormalizeDouble(signal1[q3]/signal2[q3],Digits);
      }
   }
   double max = coef_cor[ArrayMaximum(coef_cor)];
   double min = coef_cor[ArrayMinimum(coef_cor)];
   double dlt = max - min;
   int q4 = MathRound((dlt)/Point);
   int cnt_max = 0;
   for (int q6 = 0; q6<=q4; q6++) {
      for (int q5 = 0; q5<=q4; q5++) {
         int cnt = 0;
         for (q3=0;q3<hst;q3++) {
            if (coef_cor[q3] >= min+(q5-q6)*Point) {
               if (coef_cor[q3] <= min+(q5+q6)*Point) {
                  cnt++;
               }
            }
         }
         if (cnt > cnt_max) {
            cnt_max = cnt;
            coef = min+q5*Point;
         }
      }
      if (cnt_max > Precision*0.01*hst) break;
   }
   for (q3=0;q3<hst;q3++) {
      delta_coef_cor[q3] = coef_cor[q3] - coef;
      up[q3] = q6*Point;
      dn[q3] = -q6*Point;
   }
   if (MA_Period > 1) {
      for (q3=0;q3<hst-MA_Period;q3++) {
         double ma = 0;
         for (int k=q3;k<q3+MA_Period;k++) {
            ma += delta_coef_cor[k];
         }
         delta_coef_cor[q3] = ma/MA_Period;
      }
   }
   double bas_start_price;
   double sec_start_price;
   double new_pt=0;
   bool start = false;
   int start_variant = 0;
   pt = 0;
   bool work = false;
   for (q3=hst-MA_Period;q3>0;q3--) {
      if (delta_coef_cor[q3] >= dn[q3] && delta_coef_cor[q3] <= up[q3]) work = true;
      double bas_delta, sec_delta;
      if (work) {
         switch (start_variant) {
            case 1:
               bas_delta = bas_start_price-iClose(bas_SYMB,0,q3+1);
               if (MarketInfo(bas_SYMB,MODE_DIGITS) < 4) bas_delta /= 100;
               sec_delta = iClose(sec_SYMB,0,q3+1)-sec_start_price;
               if (MarketInfo(sec_SYMB,MODE_DIGITS) < 4) sec_delta /= 100;
               Delta[q3] = bas_delta + sec_delta;
               if (Delta[q3] > 0 && delta_coef_cor[q3] > delta_coef_cor[q3+1] && delta_coef_cor[q3+1] < 0 && start) {
                  ObjectCreate(DoubleToStr(q3,0),OBJ_VLINE,0,Time[q3],0,0,0,0,0);
                  ObjectSet(DoubleToStr(q3,0),OBJPROP_COLOR,DarkTurquoise);
                  pt += Delta[q3];
                  start = false;
               }
               break;
            case 2:
               bas_delta = iClose(bas_SYMB,0,q3+1)-bas_start_price;
               if (MarketInfo(bas_SYMB,MODE_DIGITS) < 4) bas_delta /= 100;
               sec_delta = sec_start_price-iClose(sec_SYMB,0,q3+1);
               if (MarketInfo(sec_SYMB,MODE_DIGITS) < 4) sec_delta /= 100;
               Delta[q3] =bas_delta + sec_delta;
               if (Delta[q3] > 0 && delta_coef_cor[q3] < delta_coef_cor[q3+1] && delta_coef_cor[q3+1] > 0 && start) {
                  ObjectCreate(DoubleToStr(q3,0),OBJ_VLINE,0,Time[q3],0,0,0,0,0);
                  ObjectSet(DoubleToStr(q3,0),OBJPROP_COLOR,Magenta);
                  pt += Delta[q3];
                  start = false;
               }
               break;
            case 3:
               bas_delta = bas_start_price-iClose(bas_SYMB,0,q3+1);
               if (MarketInfo(bas_SYMB,MODE_DIGITS) < 4) bas_delta /= 100;
               sec_delta = sec_start_price-iClose(sec_SYMB,0,q3+1);
               if (MarketInfo(sec_SYMB,MODE_DIGITS) < 4) sec_delta /= 100;
               Delta[q3] = bas_delta + sec_delta;
               if (Delta[q3] > 0 && delta_coef_cor[q3] > delta_coef_cor[q3+1] && delta_coef_cor[q3+1] < 0 && start) {
                  ObjectCreate(DoubleToStr(q3,0),OBJ_VLINE,0,Time[q3],0,0,0,0,0);
                  ObjectSet(DoubleToStr(q3,0),OBJPROP_COLOR,DarkTurquoise);
                  pt += Delta[q3];
                  start = false;
               }
               break;
            case 4:
               bas_delta = iClose(bas_SYMB,0,q3+1)-bas_start_price;
               if (MarketInfo(bas_SYMB,MODE_DIGITS) < 4) bas_delta /= 100;
               bas_delta = iClose(bas_SYMB,0,q3+1)-bas_start_price;
               if (MarketInfo(sec_SYMB,MODE_DIGITS) < 4) sec_delta /= 100;
               Delta[q3] =bas_delta + sec_delta;
               if (Delta[q3] > 0 && delta_coef_cor[q3] < delta_coef_cor[q3+1] && delta_coef_cor[q3+1] > 0 && start) {
                  ObjectCreate(DoubleToStr(q3,0),OBJ_VLINE,0,Time[q3],0,0,0,0,0);
                  ObjectSet(DoubleToStr(q3,0),OBJPROP_COLOR,Magenta);
                  pt += Delta[q3];
                  start = false;
               }
               break;
         }
         if (delta_coef_cor[q3] < delta_coef_cor[q3+1] && delta_coef_cor[q3+1] > 0 && !start) {
            if (correl_1_2 > 0) {
               bas_start_price = iClose(bas_SYMB,0,q3+1)-bas_spread;
               sec_start_price = iClose(sec_SYMB,0,q3+1)+sec_spread;
               start_variant = 1;            // bas_SYMB ïðîäàåì, sec_SYMB ïîêóïàåì
            }
            if (correl_1_2 < 0) {
               bas_start_price = iClose(bas_SYMB,0,q3+1)-bas_spread;
               sec_start_price = iClose(sec_SYMB,0,q3+1)-sec_spread;
               start_variant = 3;            // bas_SYMB ïðîäàåì, sec_SYMB ïîêóïàåì
            }
            ObjectCreate(DoubleToStr(q3,0),OBJ_VLINE,0,Time[q3],0,0,0,0,0);
            ObjectSet(DoubleToStr(q3,0),OBJPROP_COLOR,Green);
            start = true;
         }
         if (delta_coef_cor[q3] > delta_coef_cor[q3+1] && delta_coef_cor[q3+1] < 0 && !start) {
            if (correl_1_2 > 0) {
               bas_start_price = iClose(bas_SYMB,0,q3+1)+bas_spread;
               sec_start_price = iClose(sec_SYMB,0,q3+1)-sec_spread;
               start_variant = 2;            // bas_SYMB ïîêóïàåì, sec_SYMB ïðîäàåì
            }
            if (correl_1_2 < 0) {
               bas_start_price = iClose(bas_SYMB,0,q3+1)+bas_spread;
               sec_start_price = iClose(sec_SYMB,0,q3+1)+sec_spread;
               start_variant = 4;            // bas_SYMB ïîêóïàåì, sec_SYMB ïðîäàåì
            }
            ObjectCreate(DoubleToStr(q3,0),OBJ_VLINE,0,Time[q3],0,0,0,0,0);
            start = true;
         }
      }
   }
   double h = NormalizeDouble(History,0);
   double p = NormalizeDouble(Period(),0);
   pt = pt/(h*p/60/24);
   return(correl_1_2);
}
//+------------------------------------------------------------------+
void Fun_New_Bar()                              // Ô-èÿ îáíàðóæåíèÿ íîâîãî áàðà
  {                                             
   New_Bar=false;                               // Íîâîãî áàðà íåò
   if(New_Time!=Time[0]) {                      // Ñðàâíèâàåì âðåìÿ
      New_Time=Time[0];                         // Òåïåðü âðåìÿ òàêîå
      New_Bar=true;                             // Ïîéìàëñÿ íîâûé áàð  
   }
  }


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