#property copyright "KK"
#property strict

#property description "CCI indicator in separate window with 5 switchable periods"
#property description "Periods: 14, 28, 70, 140, 280"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1

#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_width1 1
#property indicator_label1 "CCI"

#property indicator_level1 100
#property indicator_level2 0
#property indicator_level3 -100
#property indicator_levelcolor clrBlack
#property indicator_levelstyle STYLE_DOT

input int CCI_Period_1 = 14;    // CCI Period 1
input int CCI_Period_2 = 28;    // CCI Period 2
input int CCI_Period_3 = 70;    // CCI Period 3
input int CCI_Period_4 = 140;   // CCI Period 4
input int CCI_Period_5 = 280;   // CCI Period 5
input int ButtonX = 1450;       // Button X Position
input int ButtonY = 20;         // Button Y Position

double CCIBuffer[];

int CCI_Periods[5];
int CurrentPeriodIndex = 0;
int CurrentCCI_Period;

string ButtonNames[5];

void OnInit()
{
    CCI_Periods[0] = CCI_Period_1;
    CCI_Periods[1] = CCI_Period_2;
    CCI_Periods[2] = CCI_Period_3;
    CCI_Periods[3] = CCI_Period_4;
    CCI_Periods[4] = CCI_Period_5;

    string gvName = "CCI_Window_Period_Index_" + Symbol() + "_" + IntegerToString(Period());
    if(GlobalVariableCheck(gvName))
    {
        CurrentPeriodIndex = (int)GlobalVariableGet(gvName);
        if(CurrentPeriodIndex < 0 || CurrentPeriodIndex > 4)
            CurrentPeriodIndex = 0;
    }

    CurrentCCI_Period = CCI_Periods[CurrentPeriodIndex];

    SetIndexBuffer(0, CCIBuffer);
    SetIndexLabel(0, "CCI(" + IntegerToString(CurrentCCI_Period) + ")");

    IndicatorShortName("CCI v3 (" + IntegerToString(CurrentCCI_Period) + ")");

    CreateButtons();
}

void OnDeinit(const int reason)
{
    DeleteButtons();
}

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
    if(id == CHARTEVENT_OBJECT_CLICK)
    {
        for(int i = 0; i < 5; i++)
        {
            if(sparam == ButtonNames[i])
            {
                if(CurrentPeriodIndex == i) return;

                CurrentPeriodIndex = i;
                CurrentCCI_Period = CCI_Periods[i];

                string gvName = "CCI_Window_Period_Index_" + Symbol() + "_" + IntegerToString(Period());
                GlobalVariableSet(gvName, CurrentPeriodIndex);

                UpdateButtons();
                UpdateIndicatorName();

                long chartID = ChartID();
                ChartSetSymbolPeriod(chartID, Symbol(), Period());

                return;
            }
        }
    }
}

void CreateButtons()
{
    int buttonWidth = 70;
    int buttonHeight = 25;
    int buttonSpacing = 5;

    for(int i = 0; i < 5; i++)
    {
        ButtonNames[i] = "CCI_Win_Button_" + IntegerToString(CCI_Periods[i]);

        if(ObjectCreate(0, ButtonNames[i], OBJ_BUTTON, ChartWindowFind(), 0, 0))
        {
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_XDISTANCE, ButtonX);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_YDISTANCE, ButtonY + (buttonHeight + buttonSpacing) * i);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_XSIZE, buttonWidth);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_YSIZE, buttonHeight);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_CORNER, CORNER_LEFT_UPPER);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_FONTSIZE, 9);
            ObjectSetString(0, ButtonNames[i], OBJPROP_TEXT, "CCI " + IntegerToString(CCI_Periods[i]));
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_COLOR, clrWhite);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_BGCOLOR, (i == CurrentPeriodIndex) ? clrGreen : clrDarkSlateGray);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_BORDER_COLOR, clrBlack);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_SELECTABLE, false);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_SELECTED, false);
        }
    }
}

void UpdateButtons()
{
    for(int i = 0; i < 5; i++)
    {
        if(ObjectFind(0, ButtonNames[i]) >= 0)
        {
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_BGCOLOR, (i == CurrentPeriodIndex) ? clrGreen : clrDarkSlateGray);
            ObjectSetInteger(0, ButtonNames[i], OBJPROP_STATE, false);
        }
    }
}

void DeleteButtons()
{
    for(int i = 0; i < 5; i++)
    {
        if(ObjectFind(0, ButtonNames[i]) >= 0)
            ObjectDelete(0, ButtonNames[i]);
    }
}

void UpdateIndicatorName()
{
    IndicatorShortName("CCI v3 (" + IntegerToString(CurrentCCI_Period) + ")");
    SetIndexLabel(0, "CCI(" + IntegerToString(CurrentCCI_Period) + ")");
}

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;

    if(prev_calculated == 0)
        limit = rates_total - 1;
    else
        limit = MathMin(rates_total - prev_calculated + 1, 10);

    for(int i = 0; i < limit; i++)
    {
        CCIBuffer[i] = iCCI(NULL, 0, CurrentCCI_Period, PRICE_CLOSE, i);
    }

    return rates_total;
}
//+------------------------------------------------------------------+