Author: Scriptong
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
0 Views
0 Downloads
0 Favorites
MAFilling
ÿþ#property copyright "Scriptong"

#property link      "scriptong@gmail.com"

#define VERSION "210.705"

#property version VERSION



#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 clrBlue

#property indicator_color2 clrRed

#property strict

#include <iCanvas.mqh>





input color                i_clrFilling                     = clrGoldenrod;                        // Filling color

input uchar                i_uchAlpha                       = 100;                                 // Transparent (0 - 255)

input uint                 i_uMAFastPeriod                  = 13;                                  // MA fast period

input uint                 i_uMASlowPeriod                  = 21;                                  // MA slow period

input ENUM_APPLIED_PRICE   i_eMAPrice                       = PRICE_CLOSE;                         // MA applied price

input ENUM_MA_METHOD       i_eMAMethod                      = MODE_EMA;                            // MA method



bool              g_bIsCreate;

int               g_nMaxPeriod;



double            g_arrfMAFast[],

                  g_arrfMASlow[];



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

//| Indicator attaching                                                                                                                                                                      |

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

int OnInit()

{

   Print("Version: ", VERSION);



   g_bIsCreate = true;

   g_nMaxPeriod = int(fmax(i_uMAFastPeriod, i_uMASlowPeriod));

   

   

   if (i_uMAFastPeriod == 0)

   {

      Alert(MQLInfoString(MQL_PROGRAM_NAME), ": MA fast period must be greate than zero. Indicator is turned off.");

      return INIT_PARAMETERS_INCORRECT;

   }

   

   if (i_uMASlowPeriod == 0)

   {

      Alert(MQLInfoString(MQL_PROGRAM_NAME), ": MA slow period must be greate than zero. Indicator is turned off.");

      return INIT_PARAMETERS_INCORRECT;

   }



   if (!BuffersBind())

      return INIT_FAILED;   

      

   return INIT_SUCCEEDED;

}

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

//| Indicator detaching                                                                                                                                                                      |

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

void OnDeinit(const int reason) 

{

}

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

//| Processing the chart events                                                                                                                                                              |

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

void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)

{

   if (id != CHARTEVENT_CHART_CHANGE && !g_bIsCreate)

      return;



   g_bIsCreate = false;

   int nLeftBar = int(ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR));

   int nRightBar = int(fmax(nLeftBar - ChartGetInteger(0, CHART_WIDTH_IN_BARS), 0));

   if (nLeftBar > ArraySize(g_arrfMAFast) || nLeftBar > ArraySize(g_arrfMASlow))   

      return;



   Canvas.Erase(0);



   for (int i = nLeftBar - 1; i >= nRightBar; --i)

      FillArea(i, g_arrfMAFast[i + 1], g_arrfMASlow[i + 1], g_arrfMAFast[i], g_arrfMASlow[i]);



   Canvas.Update();         

}

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

//| Indicators calculating                                                                                                                                                                   |

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

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 nLimit = GetRecalcIndex(rates_total, prev_calculated);

   for (int i = nLimit; i >= 0; --i)

   {

      g_arrfMAFast[i] = iMA(NULL, PERIOD_CURRENT, i_uMAFastPeriod, 0, i_eMAMethod, i_eMAPrice, i);

      g_arrfMASlow[i] = iMA(NULL, PERIOD_CURRENT, i_uMASlowPeriod, 0, i_eMAMethod, i_eMAPrice, i);

   }



   return rates_total;

}

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

//| Binding buffers with arrays                                                                                                                                                              |

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

bool BuffersBind()

{

   string sName = MQLInfoString(MQL_PROGRAM_NAME);



   if (!SetIndexBuffer(0, g_arrfMAFast) || !SetIndexBuffer(1, g_arrfMASlow))

   {

      Alert(sName, ": buffers binding error N", GetLastError());

      return false;

   }



   for (int i = 0; i < 2; ++i)

      SetIndexStyle(i, DRAW_LINE);

      

   return true;

}

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

//| Determination of the bar index from which it is necessary to recalculate                                                                                                                 |

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

int GetRecalcIndex(const int nRatesTotal, const int nPrevCalculated)

{

   int nTotal = nRatesTotal - g_nMaxPeriod;

                                                   

   if (nPrevCalculated < nRatesTotal - 1)                     

   {                                               

      ArrayInitialize(g_arrfMAFast, EMPTY_VALUE);

      ArrayInitialize(g_arrfMASlow, EMPTY_VALUE);

      return nTotal;

   }

   

   return MathMin(nRatesTotal - nPrevCalculated, nTotal);                            

}

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

//| Filling the arrea between two bars and two lines                                                                                                                                         |

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

void FillArea(const int nBarIndex, const double fPrevBarPrice1, const double fPrevBarPrice2, const double fCurBarPrice1, const double fCurBarPrice2)

{

   datetime dtLeftBar = iTime(NULL, PERIOD_CURRENT, nBarIndex + 1);

   datetime dtRightBar = iTime(NULL, PERIOD_CURRENT, nBarIndex);



   int nXLeft, nXRight, nYFastLineLeft, nYFastLineRight, nYSlowLineLeft, nYSlowLineRight;

   ChartTimePriceToXY(0, 0, dtLeftBar, fPrevBarPrice1, nXLeft, nYFastLineLeft);

   ChartTimePriceToXY(0, 0, dtRightBar, fCurBarPrice1, nXRight, nYFastLineRight);

   ChartTimePriceToXY(0, 0, dtRightBar, fCurBarPrice2, nXRight, nYSlowLineRight);

   ChartTimePriceToXY(0, 0, dtLeftBar, fPrevBarPrice2, nXLeft, nYSlowLineLeft);



   

   double fKFastLine, fKSlowLine;



   double fBFastLine = CalculateBAndKKoefs(nXLeft, nYFastLineLeft - 3, nXRight, nYFastLineRight - 3, fKFastLine);

   double fBSlowLine = CalculateBAndKKoefs(nXLeft, nYSlowLineLeft - 3, nXRight, nYSlowLineRight - 3, fKSlowLine);

   

   for (int nByX = nXLeft; nByX <= nXRight; ++nByX)

   {

      int nYFastLine = Round(fKFastLine * nByX + fBFastLine);

      int nYSlowLine = Round(fKSlowLine * nByX + fBSlowLine);

      int nMinY = fmin(nYFastLine, nYSlowLine);

      int nMaxY = fmax(nYFastLine, nYSlowLine);

      

      for (int nByY = nMinY; nByY <= nMaxY; ++nByY)

         Canvas.PixelSet(nByX, nByY, ColorToARGB(i_clrFilling, i_uchAlpha));

   }

}

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

//| K and B Aoefficients calculating by equation of a straight line                                                                                                                          |

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

double CalculateBAndKKoefs(const int nX1, const int nY1, const int nX2, const int nY2, double &fKKoef)

{

   if (nX1 == nX2)

      return DBL_MAX;

      

   fKKoef = (nY2 - nY1) / 1.0 / (nX2 - nX1);

   return nY1 - fKKoef * nX1;   

}

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