ichimokusuppres_ad

Author: Scriptong
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Indicators Used
Ichimoku Kinko Hyo
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
1 Views
0 Downloads
0 Favorites
ichimokusuppres_ad
//+------------------------------------------------------------------+
//|                                           ichimokusuppres_ad.mq4 |
//+------------------------------------------------------------------+
#property copyright "Scriptong"
#property link      "http://advancetools.net"
#property description "English: Support and resistance levels based on Ichimoku cloud.\nRussian: Óðîâíè ïîääåðæêè è ñîïðîòèâëåíèÿ íà îñíîâå îáëàêà Ichimoku."
#property version "2.00"
#property strict

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrRed
#property indicator_color2 clrBlue

#property indicator_width1 1
#property indicator_width2 1

#define PREFIX                                           "ICSURE_"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum ENUM_YESNO
  {
   NO,                                                                                             // No / Íåò
   YES                                                                                             // Yes / Äà   
  };

input    uint                 i_tenkanSenPeriod          = 9;                                      // Tenkan-sen period / Ïåðèîä ðàñ÷åòà Tenkan-sen
input    uint                 i_kijunSenPeriod           = 26;                                     // Kijun-sen period / Ïåðèîä ðàñ÷åòà Kijun-sen
input    uint                 i_senkouSpanPeriod         = 52;                                     // Senkou Span B period / Ïåðèîä ðàñ÷åòà Senkou Span B
input    uint                 i_fractalRank              = 13;                                     // Fractal rank / Ðàíã ôðàêòàëà
input    color                i_supportColor             = clrBlue;                                // Color of support lines / Öâåò ëèíèé ïîääåðæêè
input    ENUM_LINE_STYLE      i_supportStyle             = STYLE_SOLID;                            // Style of support lines / Òèï ëèíèé ïîääåðæêè
input    uint                 i_supportWidth             = 2;                                      // Thickness of support lines / Òîëùèíà ëèíèé ïîääåðæêè
input    color                i_resistanceColor          = clrRed;                                 // Color of resistance lines / Öâåò ëèíèé ñîïðîòèâëåíèÿ
input    ENUM_LINE_STYLE      i_resistanceStyle          = STYLE_SOLID;                            // Style of resistance lines / Òèï ëèíèé ñîïðîòèâëåíèÿ
input    uint                 i_resistanceWidth          = 2;                                      // Thickness of resistance lines / Òîëùèíà ëèíèé ñîïðîòèâëåíèÿ
input    ENUM_YESNO           i_isShowFractals           = YES;                                    // Do fractals show? / Îòîáðàæàòü ôðàêòàëû?
input    ENUM_YESNO           i_is5Digits                = YES;                                    // The 5-digits are using? / Èñïîëüçóþòñÿ 5-èçíà÷íûå êîòèðîâêè?
input    int                  i_indBarsCount             = 10000;                                  // Number of bars to display / Êîë-âî áàðîâ îòîáðàæåíèÿ
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum ENUM_MESSAGE_CODE
  {
   MESSAGE_CODE_INVALID_FRACTAL_RANK1,
   MESSAGE_CODE_INVALID_FRACTAL_RANK2,
   MESSAGE_CODE_MA_SLOW_LESS_THAN_ZERO,
   MESSAGE_CODE_TERMINAL_FATAL_ERROR1,
   MESSAGE_CODE_BIND_ERROR
  };

// Global variables
bool g_activate;

int    g_centerBar,
       g_sidesBars,
       g_maxPeriod;

double g_point;

// Arrays for buffers of the indicators
double g_upFractal[];
double g_dnFractal[];
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Custom indicator initialization function                                                                                                                                                          |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
int OnInit()
  {
   g_activate=false;

   if(!TuningParameters())
      return INIT_FAILED;

   if(!BuffersBind())
      return INIT_FAILED;

   g_activate=true;

   return INIT_SUCCEEDED;
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Checking the correctness of values of tuning parameters                                                                                                                                           |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool TuningParameters()
  {
   string name=WindowExpertName();

   if(i_fractalRank<3)
     {
      Alert(name,GetStringByMessageCode(MESSAGE_CODE_INVALID_FRACTAL_RANK1));
      return false;
     }

   if(i_fractalRank%2==0)
     {
      Alert(name,GetStringByMessageCode(MESSAGE_CODE_INVALID_FRACTAL_RANK2));
      return false;
     }
   g_centerBar = (int)i_fractalRank / 2 + 1;
   g_sidesBars = (int)i_fractalRank / 2;

   g_point=(i_is5Digits==YES)? Point : Point/10;
   if(g_point==0)
     {
      Alert(name,GetStringByMessageCode(MESSAGE_CODE_TERMINAL_FATAL_ERROR1));
      return false;
     }

   g_point=GetFractalOffsetByTF(g_point,PERIOD_CURRENT);

   g_maxPeriod=(int)MathMax(i_tenkanSenPeriod,MathMax(i_kijunSenPeriod,i_senkouSpanPeriod));

   return true;
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Calculation the indent size of a icon fractal from the extreme candles for the current timeframe                                                                                                  |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
double GetFractalOffsetByTF(double point,ENUM_TIMEFRAMES tf)
  {
   if(tf == PERIOD_CURRENT)
      tf = (ENUM_TIMEFRAMES)Period();

   switch(tf)
     {
      case PERIOD_M1:   return point;
      case PERIOD_M5:   return 2 * point;
      case PERIOD_M15:  return 3 * point;
      case PERIOD_M30:  return 5 * point;
      case PERIOD_H1:   return 10 * point;
      case PERIOD_H4:   return 20 * point;
      case PERIOD_D1:   return 50 * point;
      case PERIOD_W1:   return 200 * point;
      case PERIOD_MN1:  return 400 * point;
     }

   return point;
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Custom indicator deinitialization function                                                                                                                                                        |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0,PREFIX);
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Binding of array and the indicator buffers                                                                                                                                                        |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool BuffersBind()
  {
   string name=WindowExpertName();

// Binding of array and the indicator buffers
   if(!SetIndexBuffer(0,g_upFractal) || 
      !SetIndexBuffer(1,g_dnFractal))
     {
      Alert(name,GetStringByMessageCode(MESSAGE_CODE_BIND_ERROR),GetLastError());
      return false;
     }

// Specifying the graphics type of buffers
   for(int i=0; i<2; i++)
      if(i_isShowFractals)
         SetIndexStyle(i,DRAW_ARROW);
   else
      SetIndexStyle(i,DRAW_NONE);

   SetIndexArrow(0,217);
   SetIndexArrow(1,218);

   SetIndexLabel(0,"Up fractal");
   SetIndexLabel(1,"Down fractal");

   return true;
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Initialize of all indicator buffers                                                                                                                                                               |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void BuffersInitializeAll()
  {
   ArrayInitialize(g_upFractal,EMPTY_VALUE);
   ArrayInitialize(g_dnFractal,EMPTY_VALUE);
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Determination of bar index which needed to recalculate                                                                                                                                            |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
int GetRecalcIndex(int &total,const int ratesTotal,const int prevCalculated)
  {
   total=ratesTotal-g_maxPeriod-2;

   if(i_indBarsCount>0 && i_indBarsCount<total)
      total=MathMin(i_indBarsCount,total);

   if(prevCalculated<ratesTotal-1)
     {
      BuffersInitializeAll();
      ObjectsDeleteAll(0,PREFIX);
      return total;
     }

   return (MathMin(ratesTotal - prevCalculated, total));
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Finding the up fractal                                                                                                                                                                            |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool IsUpFractal(int barIndex)
  {
   int totalBars=iBars(NULL,PERIOD_CURRENT);
   if(barIndex-g_sidesBars<=0 || barIndex+g_sidesBars>=totalBars)
      return false;

   double curHigh=iHigh(NULL,PERIOD_CURRENT,barIndex);

// On right of bar
   for(int i=barIndex-g_sidesBars; i<barIndex; i++)
      if(iHigh(NULL,PERIOD_CURRENT,i)>=curHigh)
         return false;

// On left of bar
   int leftBar=barIndex+g_sidesBars;
   for(int i=barIndex+1; i<=leftBar; i++)
     {
      double high=iHigh(NULL,PERIOD_CURRENT,i);
      if(high>curHigh)
         return false;

      if(high==curHigh)
        {
         leftBar++;
         if(leftBar>=totalBars)
            return false;
        }
     }

   return true;
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Finding the down fractal                                                                                                                                                                          |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
bool IsDownFractal(int barIndex)
  {
   int totalBars=iBars(NULL,PERIOD_CURRENT);
   if(barIndex-g_sidesBars<=0 || barIndex+g_sidesBars>=totalBars)
      return false;

   double curLow=iLow(NULL,PERIOD_CURRENT,barIndex);

// On right of bar
   for(int i=barIndex-g_sidesBars; i<barIndex; i++)
      if(iLow(NULL,PERIOD_CURRENT,i)<=curLow)
         return false;

// On left of bar
   int leftBar=barIndex+g_sidesBars;
   for(int i=barIndex+1; i<=leftBar; i++)
     {
      double low=iLow(NULL,PERIOD_CURRENT,i);
      if(low<curLow)
         return false;

      if(low==curLow)
        {
         leftBar++;
         if(leftBar>=totalBars)
            return false;
        }
     }

   return true;
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Show the fractal                                                                                                                                                                                  |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void ShowFractal(double &buffer[],double value,int barIndex)
  {
   if(barIndex>=iBars(NULL,PERIOD_CURRENT) || barIndex<0)
      return;

   buffer[barIndex]=value;
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Finding the fractals                                                                                                                                                                              |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void FindFractals(int barIndex)
  {
   int fractalIndex=barIndex+g_centerBar;

   if(IsUpFractal(fractalIndex))
      ShowFractal(g_upFractal,iHigh(NULL,PERIOD_CURRENT,fractalIndex)+g_point,fractalIndex);

   if(IsDownFractal(fractalIndex))
      ShowFractal(g_dnFractal,iLow(NULL,PERIOD_CURRENT,fractalIndex)-g_point,fractalIndex);
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Add bars value to time and cast the result to time                                                                                                                                                |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
datetime AddBarsToTime(datetime startTime,int addBars)
  {
   int startBar = iBarShift(NULL, PERIOD_CURRENT, startTime);
   int barsSumm = startBar - addBars;
   int barsCount= iBars(NULL,PERIOD_CURRENT);
   datetime curTimeBar=iTime(NULL,PERIOD_CURRENT,0);
   if(barsSumm>=0 && barsSumm<barsCount && startTime<=curTimeBar)
      return iTime(NULL, PERIOD_CURRENT, barsSumm);

   int periodSeconds=Period()*60;
   if(startTime>curTimeBar)
      return startTime + addBars * periodSeconds;

   if(barsSumm<0)
      return curTimeBar + (-barsSumm) * periodSeconds;

   return iTime(NULL, 0, barsCount - 1);
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Show the trend line                                                                                                                                                                               |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void ShowTrendLine(string name,datetime time1,double price1,datetime time2,double price2,string toolTip,color clr,int width,ENUM_LINE_STYLE style)
  {
   if(ObjectFind(0,name)<0)
     {
      ObjectCreate(0,name,OBJ_TREND,0,time1,price1,time2,price2);
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,name,OBJPROP_BACK,false);
      ObjectSetInteger(0,name,OBJPROP_WIDTH,width);
      ObjectSetInteger(0,name,OBJPROP_STYLE,style);
      ObjectSetInteger(0,name,OBJPROP_RAY,false);
      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
      ObjectSetString(0,name,OBJPROP_TOOLTIP,toolTip);
      return;
     }

   ObjectMove(0,name,0,time1,price1);
   ObjectMove(0,name,1,time2,price2);
   ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
   ObjectSetString(0,name,OBJPROP_TOOLTIP,toolTip);
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Show support and resistance lines                                                                                                                                                                 |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void ShowSupportAndResistanceLines(int barIndex)
  {
   if(g_dnFractal[barIndex]==EMPTY_VALUE && g_upFractal[barIndex]==EMPTY_VALUE)
      return;

// Calculation parameters of line 
   datetime startTime=iTime(NULL,PERIOD_CURRENT,barIndex);
   datetime endTime=AddBarsToTime(startTime,i_kijunSenPeriod);
   double senkouA = iIchimoku(NULL, PERIOD_CURRENT, i_tenkanSenPeriod, i_kijunSenPeriod, i_senkouSpanPeriod, MODE_SENKOUSPANA, barIndex - i_kijunSenPeriod);
   double senkouB = iIchimoku(NULL, PERIOD_CURRENT, i_tenkanSenPeriod, i_kijunSenPeriod, i_senkouSpanPeriod, MODE_SENKOUSPANB, barIndex - i_kijunSenPeriod);

// Support line
   if(g_dnFractal[barIndex]!=EMPTY_VALUE)
      ShowTrendLine(PREFIX+"SUPPORT"+IntegerToString(startTime),startTime,iLow(NULL,PERIOD_CURRENT,barIndex),endTime,MathMin(senkouA,senkouB),
                    "Support",i_supportColor,i_supportWidth,i_supportStyle);

// Resistance line
   if(g_upFractal[barIndex]!=EMPTY_VALUE)
      ShowTrendLine(PREFIX+"RESISTANCE"+IntegerToString(startTime),startTime,iHigh(NULL,PERIOD_CURRENT,barIndex),endTime,MathMax(senkouA,senkouB),
                    "Resistance",i_resistanceColor,i_resistanceWidth,i_resistanceStyle);
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Calculation of indicators values                                                                                                                                                                  |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
void CalcIndicatorData(int limit,int total)
  {
// Calculate all data of indicator
   for(int i=limit; i>=0; i--)
     {
      FindFractals(i);
      ShowSupportAndResistanceLines(i+g_centerBar);
     }

  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| 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(!g_activate)
      return rates_total;

   int total;
   int limit=GetRecalcIndex(total,rates_total,prev_calculated);

   CalcIndicatorData(limit,total);

   return rates_total;
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Getting string by code of message and terminal language                                                                                                                                           |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
string GetStringByMessageCode(ENUM_MESSAGE_CODE messageCode)
  {
   string language=TerminalInfoString(TERMINAL_LANGUAGE);
   if(language=="Russian")
      return GetRussianMessage(messageCode);

   return GetEnglishMessage(messageCode);
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Getting string by code of message for russian language                                                                                                                                            |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
string GetRussianMessage(ENUM_MESSAGE_CODE messageCode)
  {
   switch(messageCode)
     {
      case MESSAGE_CODE_INVALID_FRACTAL_RANK1:             return ": ðàíã ôðàêòàëà äîëæåí áûòü 3 è áîëåå. Èíäèêàòîð îòêëþ÷åí.";
      case MESSAGE_CODE_INVALID_FRACTAL_RANK2:             return ": ðàíã ôðàêòàëà äîëæåí áûòü íå÷åòíûì ÷èñëîì. Èíäèêàòîð îòêëþ÷åí.";
      case MESSAGE_CODE_TERMINAL_FATAL_ERROR1:             return ": ôàòàëüíàÿ îøèáêà òåðìèíàëà - ïóíêò ðàâåí íóëþ. Èíäèêàòîð îòêëþ÷åí.";
      case MESSAGE_CODE_BIND_ERROR:                        return ": îøèáêà ñâÿçûâàíèÿ ìàññèâîâ ñ áóôåðàìè èíäèêàòîðà. Îøèáêà ¹";
     }

   return "";
  }
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Getting string by code of message for english language                                                                                                                                            |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
string GetEnglishMessage(ENUM_MESSAGE_CODE messageCode)
  {
   switch(messageCode)
     {
      case MESSAGE_CODE_INVALID_FRACTAL_RANK1:             return ": fractal rank must me 3 or more. The indicator is turned off.";
      case MESSAGE_CODE_INVALID_FRACTAL_RANK2:             return ": fractal rank must be odd number. The indicator is turned off.";
      case MESSAGE_CODE_TERMINAL_FATAL_ERROR1:             return ": terminal fatal error - point equals to zero. The indicator is turned off.";
      case MESSAGE_CODE_BIND_ERROR:                        return ": error of binding of the arrays and the indicator buffers. Error N";
     }

   return "";
  }
//+------------------------------------------------------------------+

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