imitate_indicator_chart

Author: Copyright 2017, Halyna Bobro
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1It reads information from a fileUses files from the file system
0 Views
0 Downloads
0 Favorites
imitate_indicator_chart
ÿþ//+------------------------------------------------------------------+

//|                                               _add_indicator.mq4 |

//|                        Copyright 2017, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2017, Halyna Bobro"

#define _1warninig "C=8:0;L=K9 F25B A83=0;0 =0 109"

#define _2warninig "C=8:0;L=K9 F25B A83=0;0 =0 A5;;"

#define _3warninig "AB@5;:8 8=48:0B>@0 1K;8 2 ?>;5 2848<>AB8,  ?5@5:@KBK 4@C38<8 >1J5:B0<8"

#define _4warninig ">48= 4C1;5@ =0 >48= 3@0D8:"



#property description "skype:   Trivia__\ne-mail:    galina_bobro@mail.ru\n\n06=>!!!\n*"+_1warninig+"\n*"+_2warninig+"\n*"+_3warninig+"\n*"+_4warninig;

#property link      "www.mql5.com/ru/users/galina_bobro"



#property version   "1.00"

#property strict

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 clrMagenta

#property indicator_color2 clrAqua

#property indicator_width1 1

#property indicator_width2 1



#define GETRGBA(clr)   uchar((clr)>>24)

#define GETRGBR(clr)   uchar((clr)>>16)

#define GETRGBG(clr)   uchar((clr)>>8)

#define GETRGBB(clr)   uchar(clr)

#define ARGB(a,r,g,b)  ((uchar(a)<<24)|(uchar(r)<<16)|(uchar(g)<<8)|uchar(b))

#define XRGB(r,g,b)    (0xFF000000|(uchar(r)<<16)|(uchar(g)<<8)|uchar(b))



//----- >B:;NG5==K5 >@30=8G5=8O

#define  date_work     0

#define  account_numer 0  

//----- 2:; >3@0=8G5=8O

//#define  date_work     D'2018.01.05 00:00'

//#define  account_numer 5831726   





struct CBar_pp

  {

   int               px_x1;            //:>>@48=0BK 25@E 8 =87 10@0

   int               px_x2;

   bool              arrow_buy;       //5ABL ;8 AB@5;:0 =0 10@5

   bool              arrow_sell;

  };



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

double buf_up[],buf_dn[];

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

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

input color            inp_arr_b_clr  = clrCoral;                          //Arrow Buy  Color

input color            inp_arr_s_clr  = clrCornflowerBlue;                 //Arrow Sell Color

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

long   gl_chart_id         = 0;

color  gl_clr_unic         = 0;

uint   gl_clr_b,gl_clr_s;

string gl_id_ea;

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

//| Custom indicator initialization function                         |

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



//--- 107>2K5 DC=:F88 4;O @01>BK A <0AA82>< ?8:A5;59



//---- >3@0=8G5=85 GB>1 =5 2K9B8 70 3@0=8FK <0AA820

int SetInt(int val,int min,int max) { return((val<min)?min:((val>max)?max:val));}



//---- ?5@5>1@07>2K205B 420 8=45:A0 (x, y) 2 >48= 

int Index(int w,int h,int x,int y)

  {

   int _h=SetInt( h-1-y, 0, h-1);

   int _w=SetInt( w-1-x, 0, w-1);

   return(_h*w+_w);

  }

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

int OnInit()

  {

//----

   if(date_work     >0 && TimeCurrent()                     >  date_work    ) { Alert(""5AB 70:>=G5= "+TimeToString(date_work));                        return(INIT_FAILED);}

   if(account_numer >0 && AccountInfoInteger(ACCOUNT_LOGIN) != account_numer) { Alert("525@=K9 =><5@ AG5B0! ">;L:> "+IntegerToString(account_numer));  return(INIT_FAILED);}



   MathSrand(GetTickCount());



//---- set buffer 

   SetIndexBuffer(0, buf_up); SetIndexStyle(0, DRAW_ARROW); SetIndexArrow(0, 167); SetIndexEmptyValue(0, 0.0);

   SetIndexBuffer(1, buf_dn); SetIndexStyle(1, DRAW_ARROW); SetIndexArrow(1, 167); SetIndexEmptyValue(1, 0.0);



//---- find chart

   gl_chart_id = ChartID();  if(gl_chart_id<=0) return(INIT_FAILED);

   gl_id_ea    = IntegerToString(MathRand()%500);



//----   

   gl_clr_b     = ColorToARGB(inp_arr_b_clr);

   gl_clr_s     = ColorToARGB(inp_arr_s_clr);



   return(INIT_SUCCEEDED);

  }

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

//| 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(!fn_color_unic(gl_id_ea, gl_chart_id, gl_clr_unic)) return(rates_total);

//---- 

   if(prev_calculated==0) { ArrayInitialize(buf_up,0.0); ArrayInitialize(buf_dn,0.0); }



//---- =5G53> @01>B0BL =0 :064>< 10@5 8 B0: <=>3> @01>B05B

   static datetime time_old =  0;

   datetime time_new = time[0];



   if(time_old==time_new){ return(rates_total); }

   CBar_pp bars[];



   if(!fn_chart_get(gl_id_ea, gl_chart_id, gl_clr_unic, time_new, bars) ) return(rates_total);



   for(int i=1; i<ArraySize(bars); i++)

     {

      buf_up[i]=buf_val(bars[i].arrow_buy , false, high[i], low[i], 10);

      buf_dn[i]=buf_val(bars[i].arrow_sell,true,high[i],low[i],10); 

     }



   time_old=time_new;

   return(rates_total);

  }

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

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(gl_chart_id,gl_id_ea);

   FileDelete(gl_id_ea+"_scrin_"+_Symbol+".bmp");

  }

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

double buf_val(bool is_set,bool is_up,const double h,const double l,const int pp_pls)

  {

   if(!is_set) return(0.0);

   if(is_up) return(h+pp_pls*_Point);

   else      return(l-pp_pls*_Point);

  }

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

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

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

// '8B05B A:@8= A D09;0 *.bmp (70405BAO E5=4;><) 2 <0AA82 m_pixels

// 'B5=85 *.bmp 27OB> A 181;8>B5:8 MQL5 Canvas 

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

bool fn_handle_file_to_pixel(int handle,uint &m_pixels[],int &m_width,int &m_height,ENUM_COLOR_FORMAT m_format=COLOR_FORMAT_XRGB_NOALPHA)

  {

   if(handle==INVALID_HANDLE) return(false);

//--------------------

   struct BitmapHeader

     {

      ushort            type;

      uint              size;

      uint              reserv;

      uint              offbits;

      uint              imgSSize;

      uint              imgWidth;

      uint              imgHeight;

      ushort            imgPlanes;

      ushort            imgBitCount;

      uint              imgCompression;

      uint              imgSizeImage;

      uint              imgXPelsPerMeter;

      uint              imgYPelsPerMeter;

      uint              imgClrUsed;

      uint              imgClrImportant;

     }

   header;



   uchar    a,r,g,b;

   uchar    tmp[];

   uint     img_size;

   bool     no_alpha,no_flip=false;



//--------------------------  

   ResetLastError();

   if(FileReadStruct(handle,header)!=sizeof(header)){ Print(__FUNCTION__," ERR ReaderHeader "); return(false); }

//--------------------------  

   m_width =(int)header.imgWidth;

   m_height=(int)header.imgHeight;

   if(m_height<0){ m_height=-m_height; no_flip=true; }

//--------------------------        

   if(header.imgBitCount==32)

     {

      no_alpha=true;

      img_size=FileReadArray(handle, m_pixels);

      if(!no_flip)

         for(int i=0;i<m_height/2;i++)

           {

            uint tmpu[];

            ArrayCopy(tmpu,m_pixels,0,m_width*i,m_width);

            ArrayCopy(m_pixels,m_pixels,m_width*i,m_width*(m_height-i-1),m_width);

            ArrayCopy(m_pixels,tmpu,m_width*(m_height-i-1),0,m_width);

           }

      //--- check if at least one pixel has alpha channel

      //--- then leave image as is (consider it as premultiplied ARGB)

      for(uint i=0;i<img_size;i++)

        {

         //--- there is alpha channel

         if(GETRGBA(m_pixels[i])!=0)

           {

            no_alpha=false;

            break;

           }

        }

      //--- no alpha channel

      if(no_alpha)

        {

         //--- consider image as nontransparent, add alpha channel as 0xFF

         for(uint i=0;i<img_size;i++)

            m_pixels[i]|=0xFF000000;

        }

      else

        {

         if(m_format==COLOR_FORMAT_ARGB_RAW)

           {

            //--- color components are not processed by terminal (they should be correctly specified by user) 

            //--- convert image to premultiplied ARGB

            for(uint i=0;i<img_size;i++)

              {

               switch(a=GETRGBA(m_pixels[i]))

                 {

                  case 0xFF: break;

                  case 0x00:m_pixels[i]=0; break;

                  default: r=GETRGBR(m_pixels[i])*a/255;

                  g=GETRGBG(m_pixels[i])*a/255;

                  b=GETRGBB(m_pixels[i])*a/255;

                  m_pixels[i]=ARGB(a,r,g,b);

                  break;

                 }

              }

           }

        }

     }

//-------------------------- 24 bits - change image color depth to 32 bits

   else

     {

      int byte_width;

      //--- allocate memory for pixels

      if(ArrayResize(m_pixels,m_width*m_height)!=-1)

        {

         //--- the number of bytes that define a line of pixels must be multiple of 4

         byte_width=m_width*3;             // number of bytes in line of pixels

         byte_width=(byte_width+3)&~3;     // align line to the 4 byte boundary

         uchar tmpu[];

         for(int i=0;i<m_height;i++)

           {

            if(FileReadArray(handle,tmpu,0,byte_width)!=byte_width){ Print(__FUNCTION__," ERR FileReadArray "); return(false);}

            for(int j=0,k=0,p=m_width*(m_height-i-1);j<m_width;j++,p++,k+=3)

              {

               r=tmpu[k+2];

               g=tmpu[k+1];

               b=tmpu[k];

               m_pixels[p]=XRGB(r,g,b);

              }

           }

        }

      else

        {

         Print(__FUNCTION__," ERR No correct file code=",GetLastError()); FileClose(handle);return(false);

        }

     }



   return(ArraySize(m_pixels)>0);

  }

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

//--------------- scrin get

bool fn_scrin_to_arr(string id_EA,long chart_id,uint &m_pixels[],int &m_width,int &m_height,ENUM_COLOR_FORMAT m_format=COLOR_FORMAT_XRGB_NOALPHA)

  {



   string filename=id_EA+"_scrin_"+_Symbol+".bmp";

   if(FileIsExist(filename)) { ResetLastError(); if(!FileDelete(filename)){ Print(__FUNCTION__," ERR Start FileDelete code=",GetLastError()); return(false); }}



   ArrayResize(m_pixels,0);

   m_width   = (int)ChartGetInteger(chart_id, CHART_WIDTH_IN_PIXELS);

   m_height  = (int)ChartGetInteger(chart_id, CHART_HEIGHT_IN_PIXELS);

   if(m_width<=0 || m_height<=0){ Print(__FUNCTION__," ERR Very small chart w="+IntegerToString(m_width)+", h="+IntegerToString(m_height)); return(false); }



//------ get ScreenShot

   ResetLastError(); if(!ChartScreenShot(chart_id,filename,m_width,m_height)) { Print(__FUNCTION__," ERR ChartScreenShot code=",GetLastError()); return(false); }

   datetime tc=TimeCurrent()+4; //<0:A  A5: 645<

   while(!IsStopped() && TimeCurrent()<=tc && !FileIsExist(filename)){}

   if(!FileIsExist(filename)){ Print(__FUNCTION__," ERR Create file ChartScreenShot"); return(false); }



//------------------------------------------  



//------ opn 

   ResetLastError();

   int handle=INVALID_HANDLE;



   tc=TimeCurrent()+1; //---- <0:A  A5: 645<

   while(!IsStopped() && TimeCurrent()<=tc && handle==INVALID_HANDLE){ handle=FileOpen(filename,FILE_READ|FILE_BIN); }



   if(handle==INVALID_HANDLE){ Print(__FUNCTION__," ERR FileOpen "+filename+"  code=",GetLastError());}



   bool res=fn_handle_file_to_pixel(handle,m_pixels,m_width,m_height,m_format);



   FileClose(handle);

   FileDelete(filename);



   return(res);

  }

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

bool fn_color_unic(string id_EA,long chart_id,color &unic)

  {



   if(unic>0) return(true);



//---- set scrin

   uint res_data[]; int w,h;

   if(!fn_scrin_to_arr(id_EA,chart_id,res_data,w,h)){ return(false);}

//--------

   unic = clrRed;

   uint  _unic = ColorToARGB(unic);



   bool is_find_color=true;

   while(is_find_color)

     {

      is_find_color=false;

      for(int i=0;i<ArraySize(res_data);i++)

        {

         if(res_data[i]==_unic)

           {

            is_find_color=true;

            unic = (color)MathRand();

            _unic = ColorToARGB(unic);

            break;

           }

        }

     }

   if(is_find_color) unic=0;



   return(unic>0); 

  }

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

// ?>;CG05< <0AA82 A 40==K<8 ?> :0@B8=:5 A 3@0D8:5

bool fn_chart_get(string id_EA,long chart_id,color unic,datetime time0,CBar_pp &bars[])

  {



//-------- set chart

   ResetLastError();



   if(!ChartSetInteger(chart_id, CHART_SHIFT       , true)){ Print("Chart no set SHIFT        ",GetLastError());return(false);}

   if(!ChartSetInteger(chart_id, CHART_AUTOSCROLL  , true)){ Print("Chart no set AUTOSCROLL   ",GetLastError());return(false);}

   if(!ChartSetInteger(chart_id, CHART_BRING_TO_TOP, true)){ Print("Chart no set BRING_TO_TOP ",GetLastError());return(false);}



//---------- go bar visible

   static bool get_visible_bar=false;

   int bars_count=(int)((!get_visible_bar)?ChartGetInteger(chart_id,CHART_VISIBLE_BARS,0):2);

   if(bars_count<2) {Print("ERR! bars count visible = "+IntegerToString(bars_count)); return(false);}



//---------- draw line

   bool v_b = true;

   for(int i=0;i<bars_count;i++){ v_b=v_b && VLineCreate(chart_id,id_EA+"_vline_"+IntegerToString(i),time0-i*PeriodSeconds(ChartPeriod(chart_id)),unic,STYLE_DOT,1);}

   if(!v_b){Print("ERR! No create vline "+_Symbol+" "+EnumToString(ChartPeriod(gl_chart_id))); return(false);}

   ChartRedraw();



//---------- set scrin

   uint res_data[]; int w,h;

   bool is_scr=fn_scrin_to_arr(id_EA,chart_id,res_data,w,h);

   ObjectsDeleteAll(chart_id,id_EA+"_vline_");

   if(!is_scr){ return(false);}



//---------- get bars

   int  pls_px       = 1;

   uint _unic        = ColorToARGB(unic);

   int  s            = 0; ArrayResize(bars, s);

   int  h_start      = (int)((double)h*0.3);

   int  h_stop       = (int)((double)h*0.7);

   bool is_find_pred = false;



   for(int i=0; i<w; i++)

     {

      //---- find 

      bool is_find=false;

      for(int j=h_start;j<h_stop;j++)

        {

         if(res_data[Index(w,h,i,j)]==_unic)

           {

            is_find=true;

            break;

           }

        }



      //---- set

      if(!is_find_pred && is_find)

        {

         ArrayResize(bars,s+1);

         bars[s].arrow_buy  = false;

         bars[s].arrow_sell = false;

         bars[s].px_x1      = (int)MathMax(i-pls_px, 0  );

         bars[s].px_x2      = (int)MathMin(i+pls_px, w-1);

         s++; 

        }



      if(is_find_pred && is_find)

        {

         bars[s-1].px_x2=(int)MathMin(i+pls_px,w-1);

        }



      //---- 

      is_find_pred=is_find; 

     }



//---------- get arrows



   for(int k=0;k<s;k++)

     {

      for(int i=bars[k].px_x1; i<=bars[k].px_x2; i++)

        {

         for(int j=0;j<h; j++)

           {

            if(!bars[k].arrow_buy  && res_data[Index(w, h, i, j)]==gl_clr_b){ bars[k].arrow_buy  = true; }

            if(!bars[k].arrow_sell && res_data[Index(w, h, i, j)]==gl_clr_s){ bars[k].arrow_sell = true; }

           }

        }

     }



//----------

   if(!get_visible_bar){ get_visible_bar=true; }



   int et=(int)((bars_count<=2)?bars_count:((double)bars_count/2));

   return(s>=et);

  }

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

//|                                                                  |

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

void sp_add(uint &arr[],uint add)

  {

   int s=ArraySize(arr);

   for(int i=0;i<s;i++){ if(arr[i]==add) return; }

   ArrayResize(arr,s+1);

   arr[s]=add;

  }

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

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

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

bool VLineCreate(long chart_ID,const string name,const datetime time,const color clr=clrRed,const ENUM_LINE_STYLE style=STYLE_SOLID,const int width=1)export

  {



   int sub_window=0;

   if(clr==clrNONE) ObjectDelete(chart_ID,name);



   ResetLastError();

   if(ObjectFind(chart_ID,name)<0)

     {

      if(!ObjectCreate(chart_ID,name,OBJ_VLINE,0,time,0))

        {

         Print(__FUNCTION__,": no create error code = "+IntegerToString(GetLastError()));

         return(false);

        }

     }



   ObjectSetInteger(chart_ID,name,OBJPROP_TIME1,time);

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,false);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,false);     //--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< ?5@5<5I5=8O :=>?:8 <KHLN

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,false);

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,true);

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,0);



   return(true);

  }

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

Comments