Envelopes Upper

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Envelopes Upper
ÿþ//+------------------------------------------------------------------+

//|                                              Envelopes Upper.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   1

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrBlue

#property indicator_label1  "Upper band"

//--- input parameters

input int                InpMAPeriod=14;              // Period

input int                InpMAShift=0;                // Shift

input ENUM_MA_METHOD     InpMAMethod=MODE_SMA;        // Method

input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price

input double             InpDeviation=0.1;            // Deviation

//--- indicator buffers

double   UpperBuffer[];

double   MABuffer[];



int      handle_iMA;

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

//| Custom indicator initialization function                         |

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

void OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,MABuffer,INDICATOR_CALCULATIONS);

//---

   IndicatorSetInteger(INDICATOR_DIGITS,Digits()+1);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod-1);

//--- name for DataWindow

   string short_name=StringFormat("Env(%d)",InpMAPeriod);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

   PlotIndexSetString(0,PLOT_LABEL,short_name+" Upper");

//--- line shifts when drawing

   PlotIndexSetInteger(0,PLOT_SHIFT,InpMAShift);

//---

   handle_iMA=iMA(NULL,0,InpMAPeriod,0,InpMAMethod,InpAppliedPrice);

  }

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

//| Envelopes                                                        |

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

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(rates_total<InpMAPeriod)

      return(0);

   int calculated=BarsCalculated(handle_iMA);

   if(calculated<rates_total)

     {

      Print("Not all data of handle_iMA is calculated (",calculated," bars). Error ",GetLastError());

      return(0);

     }

//--- we can copy not all data

   int to_copy;

   if(prev_calculated>rates_total || prev_calculated<0)

      to_copy=rates_total;

   else

     {

      to_copy=rates_total-prev_calculated;

      if(prev_calculated>0)

         to_copy++;

     }

//--- get ma buffer

   if(IsStopped()) // checking for stop flag

      return(0);

   if(CopyBuffer(handle_iMA,0,0,to_copy,MABuffer)<=0)

     {

      Print("Getting MA data is failed! Error ",GetLastError());

      return(0);

     }

//--- preliminary calculations

   int start=prev_calculated-1;

   if(start<InpMAPeriod)

      start=InpMAPeriod;

//--- the main loop of calculations

   for(int i=start; i<rates_total && !IsStopped(); i++)

      UpperBuffer[i]=(1+InpDeviation/100.0)*MABuffer[i];

//--- OnCalculate done. Return new prev_calculated.

   return(rates_total);

  }

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

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