MaFromMaVisual

Author: Copyright 2019, Nikolay Semko
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
MaFromMaVisual
ÿþ//+------------------------------------------------------------------+

//|                                               MaFromMaVisual.mq5 |

//|                                    Copyright 2019, Nikolay Semko |

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

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

#property copyright "Copyright 2019, Nikolay Semko"

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

#property link      "SemkoNV@bk.ru"  

#property version   "1.00"

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

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_plots   1

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

#property indicator_label1  "MaFromMa"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrTomato

#property indicator_style1  STYLE_SOLID

#property indicator_width1  3

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

input int Per1=5;       // initial MA period

input int Per2=10;      // MA period, which is taken from the initial MA

input int Nr=5;         // how many times to apply?

input ENUM_APPLIED_PRICE PriceBase=PRICE_CLOSE;

input ENUM_MA_METHOD MaMethod=MODE_SMA;

input int InpShift=0;   // Indicator's shift

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

double         MaBuffer[]; 

int n,N,per1=Per1,per2=Per2,Shift,xp1,xp2,xN,xS;

int handle;

uchar tr=0;

int line=0;

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

int OnInit()

  {

   delete Canvas;

   Canvas= new iCanvas(0,0,0,"iCanvas",0,120);   

   SetIndexBuffer(0,MaBuffer,INDICATOR_DATA);

   N=Nr;

   Shift=InpShift;

   PlotIndexSetInteger(0,PLOT_SHIFT,Shift);

   if(Shift>0) xS=0; else xS=-Shift/2;

   if(per1<2) per1=2;

   if(per2<2) per2=2;

   n=per1+(per2-1)*N;

   xp1=int(MathLog(per1)/MathLog(1.017));

   xp2=int(MathLog(per2)/MathLog(1.017));

   xN=N*4;

   IndicatorSetString(INDICATOR_SHORTNAME,"MaFromMa("+string(per1)+","+string(per2)+","+string(N)+" times,real period-"+string(n)+")");

   handle=iMA(_Symbol,_Period,per1,0,MaMethod,PriceBase);

   for(int i=0;i<N;i++) handle=iMA(_Symbol,_Period,per2,0,MaMethod,handle);

   return(INIT_SUCCEEDED);

  }

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



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[])

  {

   int to_copy=rates_total-prev_calculated;

   if(to_copy>1 && rates_total>n) // if we make the first entry or there was a delay more than the time of one bar, we initialize all the bars

     {

      ArrayInitialize(MaBuffer,EMPTY_VALUE); 

      CopyBuffer(handle,0,0,rates_total-1-per1,MaBuffer);

      return(rates_total);

     }

   else CopyBuffer(handle,0,0,1,MaBuffer);   // if new bar or new tick

   return(rates_total);

  }

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

//|                                                                  |

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

void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)

  {

   static string pre_sparam=sparam;

   if(id==CHARTEVENT_MOUSE_MOVE && line==0)

     {

      DrawSetup();

     }

   if(sparam=="0" && pre_sparam!="0") {line=0; ChartSetInteger(0,CHART_MOUSE_SCROLL,true);}

   int S=W.Width/2-199;

   if(tr>253 && pre_sparam=="0" && sparam=="1")

     {

      ChartSetInteger(0,CHART_MOUSE_SCROLL,false);

      if(Delta(S+xp1,30)<50) line=1;

      else if(Delta(S+xp2,50)<50) line=2;

      else if(Delta(S+xN,70)<50) line=3;

      else if(Delta(S+xS,90)<50) line=4;

      else line=0;

     }

   if(line==1)      {xp1=W.MouseX-S; if(xp1<1) xp1=1; if(xp1>399) xp1=399; per1=1+(int)pow(1.017,xp1); DrawSetup();}

   else if(line==2) {xp2=W.MouseX-S; if(xp2<1) xp2=1; if(xp2>399) xp2=399; per2=1+(int)pow(1.017,xp2); DrawSetup();}

   else if(line==3) {xN=W.MouseX-S; if(xN<1) xN=1; if(xN>399) xN=399; N=xN/4; DrawSetup();}

   else if(line==4) {xS=W.MouseX-S; if(xS<0) xS=0; if(xS>399) xS=399; Shift=-xS*2; DrawSetup();}

   pre_sparam=sparam;

  }

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

//|                                                                  |

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

void DrawSetup()

  {

   static int pre_per1=per1;

   static int pre_per2=per2;

   static int pre_N=N;

   static int pre_shift=Shift;

   static int pre_tr=-1;

   static int err=-1;

   int C=W.Width/2;

   int h=W.MouseY-70;

   int w=fabs(W.MouseX-C);

   int t=255;

   if(w<207) t=int(255*(1-(h-30)/100.0));

   else t=int(255*(1-(w-207)/50.0));

   if(t>255) t=255;

   tr=(uchar)t;

   if(h>130 || w>257) {if(tr!=0) {Canvas.Erase(); Canvas.Update(); tr=0;} return;}

   if(N!=pre_N || per1!=pre_per1 || per2!=pre_per2 || pre_shift!=Shift || err==-1)

     {

      n=per1+(per2-1)*N;

      if(pre_shift!=Shift) {PlotIndexSetInteger(0,PLOT_SHIFT,Shift); pre_shift=Shift;}

      else

        {

         IndicatorSetString(INDICATOR_SHORTNAME,"MaFromMa("+string(per1)+","+string(per2)+","+string(N)+" times,real period-"+string(n)+")");

         handle=iMA(_Symbol,_Period,per1,0,MaMethod,PriceBase);

         for(int i=0;i<N;i++) handle=iMA(_Symbol,_Period,per2,0,MaMethod,handle);

         err=CopyBuffer(handle,0,0,BarsCalculated(handle),MaBuffer);

        }

      pre_per1=per1;

      pre_per2=per2;

      pre_N=N;

     }

   else if(pre_tr==tr && line==0 ) return;

   pre_tr=tr;

   uint clrLine=ColorToARGB(0xFF808080,tr);

   uint clrCir=ColorToARGB(0xFFDD8080,tr);

   Canvas.Erase();

   Canvas.TextPosX=C-260;

   Canvas.TextPosY=17;

   Canvas.StepTextLine=20;

   Canvas.CurentFont("Century Gothic",18,20,~W.Color,tr/255.0);

   Canvas.Comm("Period 1");

   Canvas.Comm("Period 2");

   Canvas.Comm("N");

   Canvas.Comm("Shift");

   Canvas.FillRectangle(C-199,30,C+200,30,clrLine);

   Canvas.FillRectangle(C-199,50,C+200,50,clrLine);

   Canvas.FillRectangle(C-199,70,C+200,70,clrLine);

   Canvas.FillRectangle(C-199,90,C+200,90,clrLine);

   Canvas.FillCircle(C-199+xp1,30,7,clrCir);

   Canvas.FillCircle(C-199+xp2,50,7,clrCir);

   Canvas.FillCircle(C-199+xN,70,7,clrCir);

   Canvas.FillCircle(C-199+xS,90,7,clrCir);

   Canvas.TextPosY=12;

   Canvas.TextPosX=C-190+xp1;

   Canvas.Comm(string(per1));

   Canvas.TextPosX=C-190+xp2;

   Canvas.Comm(string(per2));

   Canvas.TextPosX=C-190+xN;

   Canvas.Comm(string(N));

   Canvas.TextPosX=C-190+xS;

   Canvas.Comm(string(Shift));

   Canvas.TextPosY=100;

   Canvas.TextPosX=C-100;

   Canvas.Comm("Real period = "+string(n));

   Canvas.Update();

  }

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



int Delta(int x,int y) {return(x-W.MouseX)*(x-W.MouseX)+(y-W.MouseY)*(y-W.MouseY);}

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

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