//+------------------------------------------------------------------+
//|                                       Indicator: +++ichi2+++.mq4 |
//+------------------------------------------------------------------+
#property copyright "BetyX"
#property link      "betyx2@gmail.com"
#property version   "1.00"

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1 5
#property indicator_color1 0x00FF09


#property indicator_type2 DRAW_LINE
#property indicator_style2 STYLE_SOLID
#property indicator_width2 5
#property indicator_color2 0x2600FF


//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern int Tenkan_sen = 9;
extern double Gap = 5;
extern int Atr = 14;
extern double Value = 4.7;
extern int Atr2 = 14;
extern double Gap2 = 8;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | +++ichi2+++ @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   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 limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(iIchimoku(NULL, PERIOD_CURRENT, Tenkan_sen, 26, 52, MODE_TENKANSEN, i) != 0 //Ichimoku Kinko Hyo is not equal to fixed value
      )
        {
         Buffer1[i] = iIchimoku(NULL, PERIOD_CURRENT, Tenkan_sen, 26, 52, MODE_TENKANSEN, i) - iATR(NULL, PERIOD_CURRENT, Atr, i) * Value; //Set indicator value at Ichimoku Kinko Hyo - Average True Range * fixed value
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iIchimoku(NULL, PERIOD_CURRENT, Tenkan_sen, 26, 52, MODE_TENKANSEN, i) != 0 //Ichimoku Kinko Hyo is not equal to fixed value
      )
        {
         Buffer2[i] = iIchimoku(NULL, PERIOD_CURRENT, Tenkan_sen, 26, 52, MODE_TENKANSEN, i) + iATR(NULL, PERIOD_CURRENT, Atr2, i) / Gap2; //Set indicator value at Ichimoku Kinko Hyo + Average True Range / fixed value
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+