//+------------------------------------------------------------------+
//|                                  Indicator: XxLESSOxXarrow#2.mq4 |
//|                                 Created by JeremyXxLESSOxXLester |
//|                                                lesso88@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Created by JeremyXxLESSOxXLester"
#property link      "lesso88@gmail.com"
#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0x0000FF
#property indicator_label1 "Sell"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0xFFAA00
#property indicator_label2 "Buy"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern int TMAPeriod = 20;
extern double ccilowerlevel = -150;
extern double ccihigherlevel = 150;
extern double arrowdistance = 4;
extern string TimeFramex = "15";
datetime time_alert; //used when sending alert
extern bool Audible_Alerts = false;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | tmacciarrow @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | tmacciarrow @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 242);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 241);
   //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, 0);
      ArrayInitialize(Buffer2, 0);
     }
   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(High[1+i] > iCustom(NULL, PERIOD_CURRENT, "FastTMALine", TimeFramex, TMAPeriod, PRICE_CLOSE, 2.0, 100, 0.5, false, false, false, false, false, false, 5000, 1, i) //Candlestick High > FastTMALine
      && iCustom(NULL, PERIOD_CURRENT, "Clive's Histogram CCI", 20, 0, true, 0, false, 10, "TrendCCI", false, 50, 14, 50, 14, 14, 6, 14, 6, 14, 6, 14, 6, 14, 6, 14, 6, 14, 6, 0, i) > ccihigherlevel //Clive's Histogram CCI > fixed value
      )
        {
         Buffer1[i] = High[1+i] + arrowdistance * myPoint; //Set indicator value at Candlestick High + fixed value
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      //Indicator Buffer 2
      if(Low[1+i] < iCustom(NULL, PERIOD_CURRENT, "FastTMALine", TimeFramex, TMAPeriod, PRICE_CLOSE, 2.0, 100, 0.5, false, false, false, false, false, false, 5000, 2, i) //Candlestick Low < FastTMALine
      && iCustom(NULL, PERIOD_CURRENT, "Clive's Histogram CCI", 20, 0, true, 0, false, 10, "TrendCCI", false, 50, 14, 50, 14, 14, 6, 14, 6, 14, 6, 14, 6, 14, 6, 14, 6, 14, 6, 1, i) < ccilowerlevel //Clive's Histogram CCI < fixed value
      )
        {
         Buffer2[i] = Low[1+i] - arrowdistance * myPoint; //Set indicator value at Candlestick Low - fixed value
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+