Author: Copyright � 2009, Serega Lykov
MA_Value
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
MA_Value
//+------------------------------------------------------------------+
//|                                                     MA_Value.mq4 |
//|                                   Copyright © 2009, Serega Lykov |
//|                                       http://mtexperts.narod.ru/ |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2009, Serega Lykov"
#property link      "http://mtexperts.narod.ru/"

//---- property of indicator ----------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

//---- external parameters ------------------------------------------+
extern int    MA_Period           = 21;       // the period for MA
extern int    MA_Method           = 0;        // the method for MA: 0 = Simple, 1 = Exponential,
                                              // 2 = Smoothed, 3 = Weighted
extern int    MA_Price            = 0;        // the price for MA:  0 = PRICE_CLOSE, 1 = PRICE_OPEN,
                                              // 2 = PRICE_HIGH,    3 = PRICE_LOW,   4 = PRICE_MEDIAN,
                                              // 5 = PRICE_TYPICAL, 6 = PRICE_WEIGHTED
extern int    MA_Shift            = 0;        // the shift for MA

//---- buffers ------------------------------------------------------+
static double MABuffer[];

//---- global variables ---------------------------------------------+
static string short_name;

//-------------------------------------------------------------------+
//---- initialization of indicator ----------------------------------+
//-------------------------------------------------------------------+
int init()
  {
   //---- set a "short" name of the indicator -----------------------+
   switch(MA_Method)
     {
      case 1: { short_name = "EMA(";  break; }
      case 2: { short_name = "SMMA("; break; }
      case 3: { short_name = "LWMA("; break; }
      default:  short_name = "SMA(";
     }
   short_name = StringConcatenate(short_name,MA_Period,")");
   IndicatorShortName(short_name);
   //---- set a accuracy for values of the indicator ----------------+
   IndicatorDigits(Digits);
   //---- set a style for line --------------------------------------+
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   //---- set a arrays for line -------------------------------------+
   SetIndexBuffer(0,MABuffer);
   //---- set a first bar for drawing the line ----------------------+
   SetIndexDrawBegin(0,MA_Period);
   //---- set a names for lines -------------------------------------+
   SetIndexLabel(0,short_name);
   //---- finish of initialization ----------------------------------+
   return(0);
  }

//-------------------------------------------------------------------+
//---- deinitialization of indicator --------------------------------+
//-------------------------------------------------------------------+
int deinit()
  {
   Comment("");
   return(0);
  }

//-------------------------------------------------------------------+
//---- MA and its value in comment of chart -------------------------+
//-------------------------------------------------------------------+
int start()
  {
   //---- if on the chart there are not enough bars the indicator is not calculate
   if(Bars <= MA_Period) return(0);
   //---- amount not changed bars after last call of the indicator --+
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   //---- last counted bar will be counted --------------------------+
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   //---- get value of MA -------------------------------------------+
   for(int i=0; i<limit; i++) MABuffer[i] = iMA(NULL,0,MA_Period,MA_Shift,MA_Method,MA_Price,i);
   //---- display value of MA in comment of chart -------------------+
   Comment(StringConcatenate(short_name," = ",DoubleToStr(MABuffer[0],Digits)));
   //---- finish of iteration ---------------------------------------+
   return(0);
  }
  
//+------------------------------------------------------------------+

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