PercentChange

Author: Copyright 2015, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
PercentChange
//+------------------------------------------------------------------+
//|                                                PercentChange.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright  "Copyright 2015, MetaQuotes Software Corp."
#property link       "https://www.mql5.com"
#property description"PercentChange by pipPod"
#property version    "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "PercentChange"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrLimeGreen,clrFireBrick
//--- indicator buffers
double OpenBuffer[];
double HighBuffer[];
double LowBuffer[];
double CloseBuffer[];
double ColorBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorSetInteger(0,INDICATOR_DIGITS,4);
//--- indicator buffers mapping
   SetIndexBuffer(0,OpenBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,HighBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,LowBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,CloseBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,ColorBuffer,INDICATOR_COLOR_INDEX);
//--- plot properties
   PlotIndexSetString(0,PLOT_LABEL,"Open;High;Low;Close");
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);
//---
   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[])
  {
//---
   int begin,count = 0;
   if(prev_calculated<=0 || prev_calculated>rates_total)
     {
      int barsWindow = (int)ChartGetInteger(ChartID(),CHART_VISIBLE_BARS)+50;
      if(rates_total<barsWindow)
         return(0);
      begin = rates_total-barsWindow;
      for(int i=0;i<indicator_buffers;i++)
         PlotIndexSetInteger(i,PLOT_DRAW_BEGIN,begin);
      OpenBuffer[begin-1] = HighBuffer[begin-1] = LowBuffer[begin-1] = CloseBuffer[begin-1] = 0.0;
      //count++;
     }
   else
      begin = prev_calculated-1;
   int toFill = rates_total-begin;
//---
   for(int i=begin;i<rates_total && !IsStopped();i++)
     {
      static double prevClose = 0.0,
                    prevCloseBuffer = 0.0;
      static int newBar = -1;
      if(newBar!=i)
        {
         prevClose = close[i-1];
         prevCloseBuffer = CloseBuffer[i-1];
         newBar = i;
        } 
      OpenBuffer[i] = prevCloseBuffer;
      HighBuffer[i] = prevCloseBuffer+(prevClose!=0.0?high[i]/prevClose-1:0.0);
      LowBuffer[i] = prevCloseBuffer+(prevClose!=0.0?low[i]/prevClose-1:0.0);
      CloseBuffer[i] = prevCloseBuffer+(prevClose!=0.0?close[i]/prevClose-1:0.0);
      ColorBuffer[i] = OpenBuffer[i]<CloseBuffer[i]?0:1;    
      count++;
     } 
//--- return value of prev_calculated for next call
   return(count==toFill?rates_total: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 ---