Author: Copyright 2018, Nikolay Semko
Miscellaneous
Uses files from the file systemIt reads information from a fileIt writes information to file
0 Views
0 Downloads
0 Favorites
sine
ÿþ//+------------------------------------------------------------------+

//|                                                         sine.mq4 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                         https://www.mql5.com/ru/users/nikolay7ko |

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

#property copyright "Copyright 2018, Nikolay Semko"

#property link      "https://www.mql5.com/ru/users/nikolay7ko"

#property link      "SemkoNV@bk.ru"

#property version   "1.00"

#property indicator_chart_window

#property strict

#include <Canvas\iCanvas.mqh> //https://www.mql5.com/ru/code/22164



struct Node

  {

   datetime          time;

   double            price;

   uint              clr;

  };

  

Node P[100][3];

int totalSin=0;

string path;



int OnInit()

  {

   ResetLastError();

   path= "Sine//sine_"+_Symbol;

   int file_handle=FileOpen(path,FILE_READ|FILE_BIN); 

   if(file_handle!=INVALID_HANDLE) 

     { 

      //--- ?@>G8B05< 2A5 40==K5 87 D09;0 2 <0AA82 

      FileReadArray(file_handle,P); 

      //--- ?>;CG8< @07<5@ <0AA820 

      totalSin=(int)P[99][2].clr; 

      //--- @0A?5G0B05< 40==K5 87 <0AA820 

      FileClose(file_handle); 

     } 

   else 

      Print("File open failed, error ",GetLastError());

   return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)

{

  if (reason==REASON_CHARTCHANGE) 

  {

  ResetLastError(); 

   int handle=FileOpen(path,FILE_READ|FILE_WRITE|FILE_BIN); 

   if(handle!=INVALID_HANDLE) 

     { 

      P[99][2].clr=totalSin;

      FileWriteArray(handle,P);  

      FileClose(handle); 

     } 

   else 

      Print("Failed to open the file, error ",GetLastError()); 

  }

}



int OnCalculate(const int rates_total,const int prev_calculated,const int begin,const double &price[])

  {



   return(rates_total);

  }



void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

   static string pre_key=sparam;

   static uchar nP=0;

   string key="";

   static bool NewSinus=false, redo =false;

   static int I=-1, J=-1;



   if(pre_key!=key) key=sparam;

   if (redo) if (pre_key=="1" && sparam!="1") {redo=false;I=-1; J=-1; ChartSetInteger(0,CHART_MOUSE_SCROLL,true);}

   if(key=="49") { NewSinus=true;  nP=0; P[totalSin][nP].clr=ARGB(255,rand()%255,rand()%255,rand()%255);}                        // 5A;8 =060B0 :;028H0 N(n) =0G8=05< @8A>20BL =>2CN A8=CA>84C

   if(key=="46") { nP=0; if(totalSin>0) totalSin--; DrawSin();}  // 5A;8 =060B0 :;028H0 C(c) C40;O5< ?>A;54=NN A8=CA>84C

   if(key=="1") if(NewSinus)                                     // =060B0 ;520O :=>?:0 <KH8 2 ?@>F5AA5 ?>AB@>5=8O A8=CA>84K

     {

      P[totalSin][nP].time=W.MouseTime;

      P[totalSin][nP].price=W.MousePrice;

      if(nP==2) {if(totalSin<99) totalSin++; NewSinus=false; DrawSin();}

      else if(nP==1) { if(P[totalSin][1].time>P[totalSin][0].time) {nP++;  DrawSin(2);}}

      else { nP++; DrawSin(1);}



     }

   else if (!redo)

     {

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

        {

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

           {

            double dx=W.MouseX-Canvas.X(P[i][j].time); dx*=dx;

            double dy=W.MouseY-Canvas.Y(P[i][j].price); dy*=dy;

            if (dx*dy<26) {I=i; J=j; redo=true; ChartSetInteger(0,CHART_MOUSE_SCROLL,false);}

           }

        }

     }

   if(id==CHARTEVENT_MOUSE_MOVE)

     {

      if(NewSinus) DrawSin(nP);

      else if (redo && I>=0 && J>=0)

        {

           P[I][J].time=W.MouseTime;

           P[I][J].price=W.MousePrice;

           DrawSin();

        }

     }

   if(id==CHARTEVENT_CHART_CHANGE)

     {

      DrawSin();

     }

   pre_key=sparam;

  }

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



void DrawSin(uchar n=3)

  {

   if(n==0) return;

   Canvas.Erase();

   if(n==1) {P[totalSin][1].time=W.MouseTime; P[totalSin][1].price=W.MousePrice; P[totalSin][2].time=W.MouseTime; P[totalSin][2].price=P[totalSin][0].price; }

   if(n==2) {P[totalSin][2].time=W.MouseTime; P[totalSin][2].price=W.MousePrice; }

   int total=totalSin;

   if(n<3) if(P[total][0].time!=P[total][2].time) total++; else Canvas.Circle(Round(Canvas.X(P[total][0].time)),Round(Canvas.Y(P[total][0].price)),4,P[total][0].clr);

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

     {

      double x = Canvas.X(P[i][0].time);

      double y = Canvas.Y(P[i][0].price);

      double d = (Canvas.X(P[i][2].time)==x)?0:(Canvas.Y(P[i][2].price)-y)/(Canvas.X(P[i][2].time)-x);

      double per=Canvas.X(P[i][1].time)-x; if (per==0) per=1;

      double a= Canvas.Y(P[i][1].price)-d*per-y;

      double s=M_PI_2/per;

      double ang=0;

      double D=0;

      double y0=y-1;

      for(double X=x; X<W.Width;X++,ang+=s,D+=d)

        {

         double Y=y+a*sin(ang)+D;

         Canvas.LineD(X-1,y0,X,Y,P[i][0].clr);

         y0=Y;

        }

      for(int l=0; l<3; l++) Canvas.Circle(Round(Canvas.X(P[i][l].time)),Round(Canvas.Y(P[i][l].price)),4,P[i][0].clr);

      if(P[i][0].time!=P[i][2].time) Canvas.LineD(x,y,W.Width-1.0,y+D-d,P[i][0].clr);

     }

   Canvas.Update();

   

  }

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

Comments