InvChanel
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
InvChanel
//+------------------------------------------------------------------+
//|                                                    InvChanel.mq4 |
//|                                    Copyright © 2009, ProfitOrder |
//|                                      http://www.profitorder.com/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2009, ProfitOrder"
#property  link      "http://www.profitorder.com/"
#define major   1
#define minor   0

#property indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  DodgerBlue
#property  indicator_width1  1

extern string Pair1 = "EURUSD";
extern string Pair2 = "GBPUSD";

extern color clRegress = Gold;
extern color clRegressExt = Silver;

double Buffer[];

string prefix = "InvChanel_";
string short_name;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int m_pos[2];
double m_value[2];
double a, b;
double maxdev;
  
void init() 
{
  SetIndexStyle(0, DRAW_LINE);
  SetIndexBuffer(0, Buffer);
  
  short_name = "InvChanel(" + Pair1 + ", " + Pair2 + ")";
  IndicatorShortName(short_name);
}

void deinit() 
{
  clear();
}

void start() 
{  
  int counted_bars = IndicatorCounted();
  if (counted_bars < 0) return;
  if (counted_bars > 0) counted_bars--;
  int limit = Bars-counted_bars;
  
  double val1, val2;
  int ind;

  for (int i=0; i<limit; i++) 
  {  
    ind = iBarShift(Pair1, 0, Time[i]);
    val1 = iClose(Pair1, 0, ind);
    
    ind = iBarShift(Pair2, 0, Time[i]);
    val2 = iClose(Pair2, 0, ind);

    if (val1 == 0 || val2 == 0) continue;

    Buffer[i] = val1/val2;
  }
  
  //Comment(val1, " * ", val2, " * ", Buffer[1]);
  
  //-----
  
  datetime tm1 = iTime(NULL, PERIOD_D1, 0);
  datetime tm2 = iTime(NULL, PERIOD_D1, 1);
  
  m_pos[0] = iBarShift(NULL, 0, tm1);
  m_pos[1] = iBarShift(NULL, 0, tm2);
  CalcRegr();

  //double Regr1 = a + 1*b;
  
  int win = WindowFind(short_name);
  string obj_name;
  
  obj_name = prefix + "main";
  if (ObjectFind(obj_name) == -1) {
    ObjectCreate(obj_name, OBJ_TREND, win, Time[m_pos[1]], m_value[1], Time[m_pos[0]], m_value[0]);
  }
  else {
    ObjectMove(obj_name, 0, Time[m_pos[1]], m_value[1]);
    ObjectMove(obj_name, 1, Time[m_pos[0]], m_value[0]);
  }

  ObjectSet(obj_name, OBJPROP_RAY, false);
  ObjectSet(obj_name, OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(obj_name, OBJPROP_COLOR, clRegress);

  
  obj_name = prefix + "upper";
  if (ObjectFind(obj_name) == -1) {
    ObjectCreate(obj_name, OBJ_TREND, win, Time[m_pos[1]], m_value[1]+maxdev, Time[m_pos[0]], m_value[0]+maxdev);
  }
  else {
    ObjectMove(obj_name, 0, Time[m_pos[1]], m_value[1]+maxdev);
    ObjectMove(obj_name, 1, Time[m_pos[0]], m_value[0]+maxdev);
  }

  ObjectSet(obj_name, OBJPROP_RAY, false);
  ObjectSet(obj_name, OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(obj_name, OBJPROP_COLOR, clRegress);

  obj_name = prefix + "lower";
  if (ObjectFind(obj_name) == -1) {
    ObjectCreate(obj_name, OBJ_TREND, win, Time[m_pos[1]], m_value[1]-maxdev, Time[m_pos[0]], m_value[0]-maxdev);
  }
  else {
    ObjectMove(obj_name, 0, Time[m_pos[1]], m_value[1]-maxdev);
    ObjectMove(obj_name, 1, Time[m_pos[0]], m_value[0]-maxdev);
  }

  ObjectSet(obj_name, OBJPROP_RAY, false);
  ObjectSet(obj_name, OBJPROP_STYLE, STYLE_SOLID);
  ObjectSet(obj_name, OBJPROP_COLOR, clRegress);

  //-----
  
  obj_name = prefix + "main_ext";
  if (ObjectFind(obj_name) == -1) {
    ObjectCreate(obj_name, OBJ_TREND, win, Time[m_pos[1]], m_value[1], Time[m_pos[0]], m_value[0]);
  }
  else {
    ObjectMove(obj_name, 0, Time[m_pos[1]], m_value[1]);
    ObjectMove(obj_name, 1, Time[m_pos[0]], m_value[0]);
  }

  ObjectSet(obj_name, OBJPROP_RAY, true);
  ObjectSet(obj_name, OBJPROP_BACK, true);
  ObjectSet(obj_name, OBJPROP_STYLE, STYLE_DASH);
  ObjectSet(obj_name, OBJPROP_COLOR, clRegressExt);

  
  obj_name = prefix + "upper_ext";
  if (ObjectFind(obj_name) == -1) {
    ObjectCreate(obj_name, OBJ_TREND, win, Time[m_pos[1]], m_value[1]+maxdev, Time[m_pos[0]], m_value[0]+maxdev);
  }
  else {
    ObjectMove(obj_name, 0, Time[m_pos[1]], m_value[1]+maxdev);
    ObjectMove(obj_name, 1, Time[m_pos[0]], m_value[0]+maxdev);
  }

  ObjectSet(obj_name, OBJPROP_RAY, true);
  ObjectSet(obj_name, OBJPROP_BACK, true);
  ObjectSet(obj_name, OBJPROP_STYLE, STYLE_DASH);  
  ObjectSet(obj_name, OBJPROP_COLOR, clRegressExt);

  obj_name = prefix + "lower_ext";
  if (ObjectFind(obj_name) == -1) {
    ObjectCreate(obj_name, OBJ_TREND, win, Time[m_pos[1]], m_value[1]-maxdev, Time[m_pos[0]], m_value[0]-maxdev);
  }
  else {
    ObjectMove(obj_name, 0, Time[m_pos[1]], m_value[1]-maxdev);
    ObjectMove(obj_name, 1, Time[m_pos[0]], m_value[0]-maxdev);
  }

  ObjectSet(obj_name, OBJPROP_RAY, true);
  ObjectSet(obj_name, OBJPROP_BACK, true);
  ObjectSet(obj_name, OBJPROP_STYLE, STYLE_DASH);  
  ObjectSet(obj_name, OBJPROP_COLOR, clRegressExt);
}

void CalcRegr()
{
  int n = m_pos[1]-m_pos[0]+1;
  
//---- calculate price values
  double value = Buffer[m_pos[0]];
  double c;
  double sumy = value;
  double sumx = 0.0;
  double sumxy = 0.0;
  double sumx2 = 0.0;

  for(int i=1; i<n; i++)
  {
    value = Buffer[m_pos[0]+i];
    sumy += value;
    sumxy += value*i;
    sumx += i;
    sumx2 += i*i;
  }
     
  c = sumx2*n - sumx*sumx;
  if (c == 0.0) return;

  b = (sumxy*n - sumx*sumy)/c;
  a = (sumy - sumx*b)/n;
  m_value[0] = a;
  m_value[1] = a+b*n;

  //---- maximal deviation
  double deviation = 0;
  double dvalue = a;
  maxdev = 0;

  for(i=0; i<n; i++)
  {
    value = Buffer[m_pos[0]+i];
    dvalue += b;
    deviation = MathAbs(value-dvalue);
    if(maxdev <= deviation) maxdev = deviation;
  }
}

void clear()
{
  int total = ObjectsTotal();  
  for (int i=total-1; i >= 0; i--) 
  {
    string name = ObjectName(i);
    if (StringFind(name, prefix) == 0) ObjectDelete(name);
  }
}

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