N last bars

Author: Copyright © 2020-2022, Vladimir Karputov
Price Data Components
2 Views
0 Downloads
0 Favorites
N last bars
ÿþ//+------------------------------------------------------------------+

//|                                                  N last bars.mq5 |

//|                         Copyright © 2020-2022, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2020-2022, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.003"

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   1

//--- plot Label1

#property indicator_label1  "N last bars"

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1  clrLavender,clrLimeGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input uchar    InpLastBars = 3;              // N last bars

input color    InpBullColor= clrLavender;    // Bull Color

input color    InpBearColor= clrLimeGreen;   // Bear Color

//--- indicator buffers

double         Label1Buffer1[];

double         Label1Buffer2[];

double         Label1Buffer3[];

double         Label1Buffer4[];

double         Label1Colors[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,Label1Buffer1,INDICATOR_DATA);

   SetIndexBuffer(1,Label1Buffer2,INDICATOR_DATA);

   SetIndexBuffer(2,Label1Buffer3,INDICATOR_DATA);

   SetIndexBuffer(3,Label1Buffer4,INDICATOR_DATA);

   SetIndexBuffer(4,Label1Colors,INDICATOR_COLOR_INDEX);

//--- set the color for each index as the property PLOT_LINE_COLOR

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,InpBullColor);

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,InpBearColor);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//--- the name of the symbol, for which the bars are drawn

   string symbol=Symbol();

//--- set the display of the symbol

   PlotIndexSetString(0,PLOT_LABEL,"Open;"+"High;"+"Low;"+"Close");

   IndicatorSetString(INDICATOR_SHORTNAME,"N last bars("+symbol+","+IntegerToString(InpLastBars)+")");

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

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<=InpLastBars)

      return(0);

   int limit=prev_calculated-1;

//--- first calculation -> (prev_calculated==0)

//--- number of bars was changed OR new bar (or several new bars) -> (prev_calculated>0 && rates_total-limit>1)

   if((prev_calculated==0) || (prev_calculated>0 && rates_total-limit>1))

     {

      //--- initialize buffers with empty values

      ArrayInitialize(Label1Buffer1,0.0);

      ArrayInitialize(Label1Buffer2,0.0);

      ArrayInitialize(Label1Buffer3,0.0);

      ArrayInitialize(Label1Buffer4,0.0);

      //---

      limit=rates_total-InpLastBars;

      for(int i=limit; i<rates_total; i++)

        {

         Label1Buffer1[i]=open[i];

         Label1Buffer2[i]=high[i];

         Label1Buffer3[i]=low[i];

         Label1Buffer4[i]=close[i];

         if(close[i]>open[i])

            Label1Colors[i]=0.0;

         else

            Label1Colors[i]=1.0;

        }

      //---

      return(rates_total);

     }

//--- main loop

   for(int i=limit; i<rates_total; i++)

     {

      Label1Buffer1[i]=open[i];

      Label1Buffer2[i]=high[i];

      Label1Buffer3[i]=low[i];

      Label1Buffer4[i]=close[i];

      if(close[i]>open[i])

         Label1Colors[i]=0.0;

      else

         Label1Colors[i]=1.0;

     }

//--- return value of prev_calculated for next call

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