Ticker Renko

Author: mandorr@gmail.com
Ticker Renko
0 Views
0 Downloads
0 Favorites
Ticker Renko
// Ticker Renko.mq4
// Èíäèêàòîð

#property copyright "mandorr@gmail.com"
#property indicator_separate_window
#property indicator_buffers 2

extern int BoxSize=100;    // Ðàçìåð êèðïè÷à
extern int BarWidth=5;     // Øèðèíà ëèíèè áàðà
extern int BarsCount=1000; // Êîëè÷åñòâî îòîáðàæàåìûõ áàðîâ

int    win_index;
int    box_size;
int    price_prev;
int    bar_size;
int    bar_high[];
int    bar_low [];
int    bar_dir [];
double buffer0 [];
double buffer1 [];
string obj_name, win_name;


void init() {
   SetIndexBuffer(0,buffer0);
   SetIndexBuffer(1,buffer1);
   SetIndexStyle (0,DRAW_NONE);
   SetIndexStyle (1,DRAW_NONE);
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
   SetIndexLabel (0,"High");
   SetIndexLabel (1,"Low");
   SetIndexDrawBegin(0,0);
   SetIndexDrawBegin(1,0);
   IndicatorDigits(Digits);
   price_prev=0;
   bar_size=0;
   box_size=MathMax(1,BoxSize);
   win_name="Ticker Renko ("+box_size+")";
}

void deinit() {
   ObjectsDeleteAll(win_index);
}

void start() {
   int i, j, price, total, box_count;
   bool reverse;
   double price1, price2;
   datetime time1, time2;

   price=MathRound(Bid/Point);
   if (price==0) {return;}
   if (price==price_prev) {return;}
   price_prev=price;

   if (bar_size==0) {
      Clean();
      Setup();
   }

   box_count=0;
   reverse=false;
   if ((price>bar_high[0]) && (bar_dir[0]>0)) {
      box_count=(price-bar_high[0])/box_size;
      bar_high[0]=bar_high[0]+box_count*box_size;
   }
   if ((price<bar_low[0]) && (bar_dir[0]<0)) {
      box_count=(bar_low[0]-price)/box_size;
      bar_low[0]=bar_low[0]-box_count*box_size;
   }
   if ((price>bar_low[0]) && (bar_dir[0]<0)) {
      box_count=(price-bar_low[0])/box_size;
      if (box_count>1) {reverse=true;}
   }
   if ((price<bar_high[0]) && (bar_dir[0]>0)) {
      box_count=(bar_high[0]-price)/box_size;
      if (box_count>1) {reverse=true;}
   }

   if (reverse) {
      bar_size++;
      total=BarsCount; if (BarsCount>Bars) {total=Bars;}
      if (bar_size>total) {bar_size=total;}
      else {
         ArrayResize(bar_high,bar_size);
         ArrayResize(bar_low ,bar_size);
         ArrayResize(bar_dir ,bar_size);
      }
      for (i=bar_size-1; i>0; i--) {
         bar_high[i]=bar_high[i-1];
         bar_low [i]=bar_low [i-1];
         bar_dir [i]=bar_dir [i-1];
      }
      if (bar_dir[1]<0) {
         bar_high[0]=bar_low[1]+box_count*box_size;
         bar_low [0]=bar_low[1]+box_size;
         bar_dir [0]=1;
      }
      if (bar_dir[1]>0) {
         bar_high[0]=bar_high[1]-box_size;
         bar_low [0]=bar_high[1]-box_count*box_size;
         bar_dir [0]=-1;
      }
   }

   IndicatorShortName(win_name);
   win_index=WindowFind(win_name);

   for (i=0; i<bar_size; i++) {
      price1=bar_low [i]*Point;
      price2=bar_high[i]*Point;
      time1=Time[i];
      time2=Time[i];
      box_count=(bar_high[i]-bar_low[i])/box_size;

      color bar_color;
      if (bar_dir[i]<0) {bar_color=Red;} else {bar_color=Blue;}

      obj_name="Win"+win_index+"_Bar"+i;
      if (ObjectFind(obj_name)!=win_index) {
         ObjectCreate(obj_name,OBJ_TREND,win_index,time1,price1,time2,price2);
      }
      ObjectSet(obj_name,OBJPROP_TIME1,time1);
      ObjectSet(obj_name,OBJPROP_TIME2,time2);
      ObjectSet(obj_name,OBJPROP_PRICE1,price1);
      ObjectSet(obj_name,OBJPROP_PRICE2,price2);
      ObjectSet(obj_name,OBJPROP_STYLE,DRAW_LINE);
      ObjectSet(obj_name,OBJPROP_WIDTH,BarWidth);
      ObjectSet(obj_name,OBJPROP_RAY,false);
      ObjectSet(obj_name,OBJPROP_COLOR,bar_color);

      buffer0[i]=price2;
      buffer1[i]=price1;
   }
   buffer0[bar_size]=0.0;
   buffer1[bar_size]=0.0;

   obj_name="Win"+win_index+"_Bid";
   if (ObjectFind(obj_name)!=win_index) {
      ObjectCreate(obj_name,OBJ_HLINE,win_index,Time[0],Bid);
   }
   ObjectSet(obj_name,OBJPROP_PRICE1,Bid);
   ObjectSet(obj_name,OBJPROP_STYLE,DRAW_LINE);
   ObjectSet(obj_name,OBJPROP_WIDTH,1);
   ObjectSet(obj_name,OBJPROP_COLOR,Silver);
}

// î÷èñòèòü ãðàôèê
void Clean() {
   for (int i=0; i<=BarsCount; i++) {
      buffer0[i]=0.0;
      buffer1[i]=0.0;
   }
   IndicatorShortName(win_name);
   win_index=WindowFind(win_name);
   ObjectsDeleteAll(win_index);
}

// Èñïîëüçîâàíèå ïîñëåäîâàòåëüíîñòè òèêîâ èç OHL ìèíóòíîãî ãðàôèêà
void Setup() {
   int i, j, k, total, value_prev, value[4], box_count;
   bool reverse;
   double open, high, low, median;
   // íà÷àëüíûé áàð
   bar_size=1;
   ArrayResize(bar_high,1);
   ArrayResize(bar_low ,1);
   ArrayResize(bar_dir, 1);
   total=10*BoxSize*BarsCount; if (total>iBars(NULL,PERIOD_M1)) {total=iBars(NULL,PERIOD_M1);}
   open=iOpen(NULL,PERIOD_M1,total-1);
   value[0]=MathRound(open/Point);
   value_prev=box_size*(value[0]/box_size); // ïðèâÿçêà ê öåíå, êðàòíîé box_size ïóíêòîâ
   bar_high[0]=value_prev+box_size;
   bar_low [0]=value_prev;
   bar_dir [0]=1;
   // Ýìóëÿöèÿ ïîñëåäîâàòåëüíîñòè òèêîâ èç OHL ìèíóòíîãî ãðàôèêà
   for (i=total-1; i>=0; i--) {
      open=iOpen(NULL,PERIOD_M1,i);
      high=iHigh(NULL,PERIOD_M1,i);
      low =iLow (NULL,PERIOD_M1,i);
      median=0.5*(high+low);
      value[0]=MathRound(open/Point);
      value[1]=MathRound(high/Point);
      value[2]=MathRound(low /Point);
      value[3]=0;
      if (open<median) {
         // Ïîìåíÿòü ïîñëåäîâàòåëüíîñòü ñ OHL íà OLH
         value[3]=value[1];
         value[1]=value[2];
         value[2]=value[3];
      }
      for (k=0; k<=2; k++) {
         if (value[k]==value_prev) {continue;}
         value_prev=value[k];
         box_count=0;
         reverse=false;
         if ((value[k]>bar_high[0]) && (bar_dir[0]>0)) {
            box_count=(value[k]-bar_high[0])/box_size;
            bar_high[0]=bar_high[0]+box_count*box_size;
         }
         if ((value[k]<bar_low[0]) && (bar_dir[0]<0)) {
            box_count=(bar_low[0]-value[k])/box_size;
            bar_low[0]=bar_low[0]-box_count*box_size;
         }
         if ((value[k]>bar_low[0]) && (bar_dir[0]<0)) {
            box_count=(value[k]-bar_low[0])/box_size;
            if (box_count>1) {reverse=true;}
         }
         if ((value[k]<bar_high[0]) && (bar_dir[0]>0)) {
            box_count=(bar_high[0]-value[k])/box_size;
            if (box_count>1) {reverse=true;}
         }
         if (reverse) {
            bar_size++;
            if (bar_size>BarsCount) {bar_size=BarsCount;}
            else {
               ArrayResize(bar_high,bar_size);
               ArrayResize(bar_low ,bar_size);
               ArrayResize(bar_dir ,bar_size);
            }
            for (j=bar_size-1; j>0; j--) {
               bar_high[j]=bar_high[j-1];
               bar_low [j]=bar_low [j-1];
               bar_dir [j]=bar_dir [j-1];
            }
            if (bar_dir[1]<0) {
               bar_high[0]=bar_low[1]+box_count*box_size;
               bar_low [0]=bar_low[1]+box_size;
               bar_dir [0]=1;
            }
            if (bar_dir[1]>0) {
               bar_high[0]=bar_high[1]-box_size;
               bar_low [0]=bar_high[1]-box_count*box_size;
               bar_dir [0]=-1;
            }
         }
      }
   }
   RefreshRates();
   price_prev=MathRound(Bid/Point);
}

// End

Comments