//+------------------------------------------------------------------+
//|                                                    SlingShot.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   3
//--- plot Short
#property indicator_label1  "Short"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//--- plot Long
#property indicator_label2  "Long"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  3
//--- plot Neutral
#property indicator_label3  "Neutral"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrYellow
#property indicator_style3  STYLE_SOLID
#property indicator_width3  3
//--- plot Active
#property indicator_label4  "Active"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrLime
#property indicator_style4  STYLE_SOLID
#property indicator_width4  3
//--- external parameter
input int Siklus=96;
input int Term=0;
//--- indicator buffers
double         ShortBuffer[];
double         LongBuffer[];
double         NeutralBuffer[];
double         ActiveBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ShortBuffer);
   SetIndexBuffer(1,LongBuffer);
   SetIndexBuffer(2,NeutralBuffer);
   SetIndexBuffer(3,ActiveBuffer);
   
//---
   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[])
  {
//---
   double MA,CH,CW,CM;
   bool NeFlag=true;
   for (int i=0;i<Bars;i++) {
     CH=iCCI(Symbol(),0,Siklus/Period(),PRICE_MEDIAN,i);
     CW=iCCI(Symbol(),0,(5*Siklus)/Period(),PRICE_MEDIAN,i);
     CM=iCCI(Symbol(),0,(5*4*Siklus)/Period(),PRICE_MEDIAN,i);
     MA=iMA(Symbol(),0,Siklus/Period(),0,MODE_EMA,PRICE_MEDIAN,i);
     NeFlag=true;
     if ((CH-CW)>Term && CW>0)
       {
       NeFlag=false;
       if ((CW-CM)>Term && CM>0) LongBuffer[i]=MA;
        else LongBuffer[i]=MA;
       } 
     if ((CH-CW)<-Term && CW<0)
       {
       NeFlag=false;
       if ((CW-CM)<-Term && CM<0) ShortBuffer[i]=MA;
        else ShortBuffer[i]=MA;
       }
     if (NeFlag) NeutralBuffer[i]=MA;
   }   

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
