ColorBBCandles

Author: Copyright � 2011, Nikolay Kositsin
0 Views
0 Downloads
0 Favorites
ColorBBCandles
/*
* Place the SmoothAlgorithms.mqh file
* to the terminal_data_folder\MQL5\Include
*/
//+------------------------------------------------------------------+
//|                                               ColorBBCandles.mq5 |
//|                             Copyright © 2011,   Nikolay Kositsin | 
//|                              Khabarovsk,   farria@mail.redcom.ru | 
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Nikolay Kositsin"
#property link "farria@mail.redcom.ru"
#property description "ColorBBCandles"
//---- indicator version
#property version   "1.00"
//+----------------------------------------------+
//|  Indicator drawing parameters                |
//+----------------------------------------------+
//---- drawing the indicator in the main window
#property indicator_chart_window 
//---- five buffers are used for calculation of drawing of the indicator
#property indicator_buffers 5
//---- only one plot is used
#property indicator_plots   1
//---- color candlesticks are used as an indicator
#property indicator_type1   DRAW_COLOR_CANDLES
//---- a set of colors is used for the candlesticks
#property indicator_color1 Magenta,Red,OrangeRed,Crimson,Brown,CLR_NONE,SeaGreen,MediumSeaGreen,YellowGreen,Lime,Aqua
//---- displaying the indicator line label
#property indicator_label1  "ColorBBCandles Ten colors"

//+-----------------------------------+
//|  Smoothings classes description   |
//+-----------------------------------+
#include <SmoothAlgorithms.mqh> 
//+-----------------------------------+
//---- declaration of variables of the CStdDeviation 
//---- and CMoving_Average classes from the SmoothAlgorithms.mqh file
CStdDeviation STD;
CMoving_Average MA;
//+-----------------------------------+
//|  enumerations declaration         |
//+-----------------------------------+
enum Applied_price_      // Type of constant
  {
   PRICE_CLOSE_ = 1,     // Close
   PRICE_OPEN_,          // Open
   PRICE_HIGH_,          // High
   PRICE_LOW_,           // Low
   PRICE_MEDIAN_,        // Median Price (HL/2)
   PRICE_TYPICAL_,       // Typical Price (HLC/3)
   PRICE_WEIGHTED_,      // Weighted Close (HLCC/4)
   PRICE_SIMPLE,         // Simple Price (OC/2)
   PRICE_QUARTER_,       // Quarted Price (HLOC/4) 
   PRICE_TRENDFOLLOW0_,  // TrendFollow_1 Price 
   PRICE_TRENDFOLLOW1_   // TrendFollow_2 Price 
  };

//+----------------------------------------------+
//|  Indicator input parameters                  |
//+----------------------------------------------+
input int BandsPeriod=100;                // BB smoothing period
input double BandsDeviation1 = 1.0;       // First level deviation
input double BandsDeviation2 = 1.5;       // Second level deviation
input double BandsDeviation3 = 2.0;       // Third level deviation
input double BandsDeviation4 = 2.5;       // Fourth level deviation
input double BandsDeviation5 = 3.0;       // Fifth level deviation
input ENUM_MA_METHOD MA_Method_=MODE_EMA; // Indicator smoothing method
input Applied_price_ IPC=PRICE_CLOSE;     // Price constant
/*used for calculation of the indicator (1-CLOSE, 2-OPEN, 3-HIGH, 4-LOW, 
  5-MEDIAN, 6-TYPICAL, 7-WEIGHTED, 8-SIMPLE, 9-QUARTER, 10-TRENDFOLLOW, 11-0.5 * TRENDFOLLOW.) */
//+----------------------------------------------+

//---- declaration of dynamic arrays that 
// will be used as indicator buffers
double ExtOpenBuffer[];
double ExtHighBuffer[];
double ExtLowBuffer[];
double ExtCloseBuffer[];
double ExtColorsBuffer[];

//---- declaration of the integer variables for the start of data calculation
int StartBars;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//---- initialization of variables of the start of data calculation
   StartBars=BandsPeriod+1;

//---- set dynamic arrays as indicator buffers
   SetIndexBuffer(0,ExtOpenBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtHighBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtLowBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,ExtCloseBuffer,INDICATOR_DATA);
//---- set dynamic array as a color index buffer   
   SetIndexBuffer(4,ExtColorsBuffer,INDICATOR_COLOR_INDEX);
//---- performing the shift of beginning of the indicator drawing
   PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,StartBars);
//---- set colors quantity 11 for the color buffer
// PlotIndexSetInteger(0,PLOT_COLOR_INDEXES,11);
//---- setting the format of accuracy of displaying the indicator
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---- name for the data window and the label for windows
   string short_name="ColorBBCandles";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//----   
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,    // number of bars in history at the current tick
                const int prev_calculated,// number of bars calculated at previous call
                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[])
  {
//---- checking the number of bars to be enough for the calculation
   if(rates_total<StartBars) return(0);

//---- declaration of variables with a floating point  
   double price_,stdev,ma;
   double UpBB1,UpBB2,UpBB3,UpBB4,UpBB5;
   double DnBB1,DnBB2,DnBB3,DnBB4,DnBB5;
//---- declaration of integer variables
   int first,bar;

//---- calculation of the 'first' starting number for the bars recalculation loop
   if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of the indicator calculation
      first=0;                    // starting index for calculation of all bars
   else first=prev_calculated-1;  // starting index for calculation of new bars

//---- Bollinger Bands calculation main loop
   for(bar=first; bar<rates_total && !IsStopped(); bar++)
     {
      //---- call of the PriceSeries function to get the input price 'price_'
      price_=PriceSeries(IPC,bar,open,low,high,close);

      //---- call of the MASeries function to get the moving value
      ma=MA.MASeries(0,prev_calculated,rates_total,BandsPeriod,MA_Method_,price_,bar,false);

      //---- call of the StdDevSeries function to get the value of the half of the channel width with Deviation = 1.0 
      stdev=STD.StdDevSeries(StartBars,prev_calculated,rates_total,BandsPeriod,1.0,price_,ma,bar,false);

      //---- get the values of the levels
      UpBB1=ma+stdev*BandsDeviation1;
      UpBB2=ma+stdev*BandsDeviation2;
      UpBB3=ma+stdev*BandsDeviation3;
      UpBB4=ma+stdev*BandsDeviation4;
      UpBB5=ma+stdev*BandsDeviation5;
      DnBB1=ma-stdev*BandsDeviation1;
      DnBB2=ma-stdev*BandsDeviation2;
      DnBB3=ma-stdev*BandsDeviation3;
      DnBB4=ma-stdev*BandsDeviation4;
      DnBB5=ma-stdev*BandsDeviation5;

      if((price_>UpBB1 || price_<DnBB1) && bar>StartBars) //there are signals for candlesticks coloring
        {
         if(price_>UpBB5) ExtColorsBuffer[bar]=10;
         else if(price_>UpBB4) ExtColorsBuffer[bar]=9;
         else if(price_>UpBB3) ExtColorsBuffer[bar]=8;
         else if(price_>UpBB2) ExtColorsBuffer[bar]=7;
         else if(price_>UpBB1) ExtColorsBuffer[bar]=6;

         else if(price_<DnBB5) ExtColorsBuffer[bar]=0;
         else if(price_<DnBB4) ExtColorsBuffer[bar]=1;
         else if(price_<DnBB3) ExtColorsBuffer[bar]=2;
         else if(price_<DnBB2) ExtColorsBuffer[bar]=3;
         else if(price_<DnBB1) ExtColorsBuffer[bar]=4;

         ExtOpenBuffer[bar]=open[bar];
         ExtHighBuffer[bar]=high[bar];
         ExtLowBuffer[bar]=low[bar];
         ExtCloseBuffer[bar]=close[bar];
        }
      else // there are no signals for candlesticks coloring
        {
         ExtOpenBuffer[bar]=0.0;
         ExtHighBuffer[bar]=0.0;
         ExtLowBuffer[bar]=0.0;
         ExtCloseBuffer[bar]=0.0;
         ExtColorsBuffer[bar]=5;
        }
     }
//----     
   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 ---