ShowMAParams

Author: Alexey Volchanskiy, Feb 2015
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
ShowMAParams
ÿþ//+------------------------------------------------------------------+

//|                                                 ShowMAParams.mq4 |

//|                                               Alexey Volchanskiy |

//|                                             http://robo-forex.ru |

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

#property copyright "Alexey Volchanskiy, Feb 2015"

#property link      "http://robo-forex.ru"

#property version   "1.10"

#property strict



extern int                  FastMAPeriod        = 12;           // Fast MA period

extern ENUM_MA_METHOD       FastMAMethod        = MODE_EMA;     // Fast MA method

extern ENUM_APPLIED_PRICE   FastMAAppliedPrice  = PRICE_CLOSE;  // Fast MA price



extern int                  SlowMAPeriod        = 32;           // Slow MA period

extern ENUM_MA_METHOD       SlowMAMethod        = MODE_EMA;     // Slow MA method

extern ENUM_APPLIED_PRICE   SlowMAAppliedPrice  = PRICE_CLOSE;  // Slow MA price



extern int     SpeedShiftBars   = 2;    // Bar shift for speed calculate

extern int     AccelShiftBars   = 2;    // Bar shift for acceleration calculate



extern int     FontSize         = 10;

extern string  Font             = "Arial";

extern color   FontColor        = clrYellow;

extern string  LineName         = "LMA";



extern int     Multiplier       = 1000;  // Multiplier for speed and acceleration

extern int     Precision        = 6;     // Data precision in digits



#define TIMER_MS    500

#define DT_SIZE     4



// 1CD5@ 8AB>@88 

#define MA0_IDX            0

#define MA1_IDX            1

#define SPEED0_IDX         2

#define SPEED1_IDX         3

#define ACCEL0_IDX         4

#define ACCEL1_IDX         5

#define BUF_MA_LEN_COL     6  // ma1, ma2, speed1, speed2, acceleration1, acceleration2

#define BUF_MA_LEN_ROW     10 // count of row

double  BufMA[BUF_MA_LEN_COL][BUF_MA_LEN_ROW];

datetime DtArr[DT_SIZE];      

string Caption[]={ "MaFast","MaSlow","SpeedFast","SpeedSlow","AccelFast","AccelSlow" };



int OnInit()

  {

   for(int col=0; col<BUF_MA_LEN_COL; col++) // make 5 columns

      for(int row=0; row<BUF_MA_LEN_ROW+2; row++)

        {

         string lname="lma"+IntegerToString(col)+"_"+IntegerToString(row);

         ObjectCreate(lname, OBJ_LABEL, 0, 0, 0);

         ObjectSet(lname, OBJPROP_CORNER, 0);    

         ObjectSet(lname,OBJPROP_XDISTANCE, col*(FontSize * 9) + 90);  

         ObjectSet(lname, OBJPROP_YDISTANCE, row*(FontSize * 2));      

         if(row==0)

            ObjectSetText(lname,Caption[col],FontSize,Font,FontColor);      // draw captions for row

         if(row==1)

            ObjectSetText(lname,"-------------",FontSize,Font,FontColor);   

        }

   Comment("");

   VLineCreate(0,LineName);

   EventSetMillisecondTimer(TIMER_MS);

   return(INIT_SUCCEEDED);

  }

  

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

//| Create vertical line

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

bool VLineCreate(const long            chart_ID=0,

                 const string          name = "VLine",

                 const int             sub_window = 0,

                 datetime              time = 0,      

                 const color           clr = clrRed,  

                 const ENUM_LINE_STYLE style = STYLE_SOLID,

                 const int             width = 2,          

                 const bool            back = false,       

                 const bool            selection = true,   

                 const bool            hidden=false,

                 const long            z_order=0) 

  {

   if(!time)

      time=TimeCurrent();

   ResetLastError();

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

     {

      Print(__FUNCTION__,

            ": can't create vertical line, error code = ",GetLastError());

      return(false);

     }

   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,back);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

   return(true);

  }



void OnTimer()

  {

   datetime dt=FindLine();

   if(dt>0)// && dt != DtArr[0])

     {

      DtArr[0]=dt;

      ShowParams(0);

      WindowRedraw();

     }

  }



void OnDeinit(const int reason)

  {

   if(reason==REASON_CHARTCHANGE)

      return;

   int nL=ObjectsTotal();

   string name;

   for(int n=nL-1; n>=0; n--)

     {

      name=ObjectName(n);

      if(ObjectType(name)!=OBJ_LABEL)

         continue;

      int nch= StringFind(name,"lma");

      if(nch == 0)

         ObjectDelete(name);

     }

   ObjectDelete(LineName);

   EventKillTimer();

  }

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

//| Don't use OnTick()                                                                |

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

void OnTick()

  {

  }

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

//| Finds vertical line on screen and return time                    |

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

datetime FindLine()

  {

   int nL=ObjectsTotal();

   string name;

   for(int n=0; n<nL; n++)

     {

      name=ObjectName(n);

      if(ObjectType(name)!=OBJ_VLINE)

         continue;

      int nch= StringFind(name,LineName);

      if(nch == 0)

         return (datetime)(ObjectGetInteger(0, LineName, OBJPROP_TIME1));

     }

   return(0);

  }

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

//| Shows parameters on screen                                       |

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

void ShowParams(int idx)

  {

   RefreshRates();

   int nbar=iBarShift(Symbol(),Period(),DtArr[idx]);

   CalcBufMA(nbar);

   string text;

   for(int col=0; col<BUF_MA_LEN_COL; col++)

      for(int row=2; row<BUF_MA_LEN_ROW+2; row++)

        {

         text="";

         string lname="lma"+IntegerToString(col)+"_"+IntegerToString(row);

         if(col<=1)

            text=DoubleToString(BufMA[col][row-2], Precision);

         if(col>=2 && col<=3 && row<BUF_MA_LEN_ROW - SpeedShiftBars + 2)

            text=DoubleToString(BufMA[col][row-2] * Multiplier, Precision);

         if(col>=4 && row<BUF_MA_LEN_ROW - SpeedShiftBars - AccelShiftBars+2)

            text=DoubleToString(BufMA[col][row-2] * Multiplier, Precision);

         ObjectSetText(lname,text,FontSize,Font,FontColor);

        }

  }

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

//| Calculate MA, Speed of MA, Acceleration of MA                    |

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

void CalcBufMA(int startBar)

  {

   RefreshRates();

// fill MA buffers

   for(int n=0; n<BUF_MA_LEN_ROW; n++)

     {

      BufMA[MA0_IDX][n] = iMA(Symbol(), Period(), FastMAPeriod, 0, FastMAMethod, FastMAAppliedPrice, n + startBar);

      BufMA[MA1_IDX][n] = iMA(Symbol(), Period(), SlowMAPeriod, 0, SlowMAMethod, SlowMAAppliedPrice, n + startBar);

     }

// fill speed of MA buffers

   for(int n=0; n<BUF_MA_LEN_ROW-SpeedShiftBars; n++)

     {

      BufMA[SPEED0_IDX][n] = BufMA[MA0_IDX][n] - BufMA[MA0_IDX][n + SpeedShiftBars];

      BufMA[SPEED1_IDX][n] = BufMA[MA1_IDX][n] - BufMA[MA1_IDX][n + SpeedShiftBars];

     }

// fill acceleration of MA buffers

   for(int n=0; n<BUF_MA_LEN_ROW-AccelShiftBars-SpeedShiftBars; n++)

     {

      BufMA[ACCEL0_IDX][n] = BufMA[SPEED0_IDX][n] - BufMA[SPEED0_IDX][n + AccelShiftBars];

      BufMA[ACCEL1_IDX][n] = BufMA[SPEED1_IDX][n] - BufMA[SPEED1_IDX][n + AccelShiftBars];

     }

  }

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

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