//+------------------------------------------------------------------+
//|                                                 Last chance .mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "vorese"
#property link      "https://www.mql5.com/ru/users/vorese"
#property version   "1.00"
#property strict
#property indicator_chart_window
input int scroll_speed=20;
input color clr_buy=clrMediumBlue;
input color clr_sell=clrRed;
input color clr_frame=clrSaddleBrown;
input color clr_button=clrSaddleBrown;
input int xdist=0;
input int ydist=20;

bool
tr_1=true,
tr_2=true,
tr_3=true,
tr_4=true,
button_on=false;

int
offset1=0,
offset2=0,
offset3=0,
offset4=0,
count_0=1,
count_1=0,
count_2=0,
count_3=0,
count_4=0;

string text_butt="START";
#define attempt 15
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetMillisecondTimer(scroll_speed);
   ButtonCreate("butt11",125+xdist,3+ydist,120,104,"",11,clrNONE,clr_frame);
   ButtonCreate("butt8",120+xdist,30+ydist,110,50,"",11,clrNONE,clr_button);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   f_Delete_object();
//--- destroy timer
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   if(button_on)
     {
      count_1++; count_2++; count_3++; count_4++;
      
      tr_1=f_Offset(offset1,count_1,5,tr_1);
      tr_2=f_Offset(offset2,count_2,3,tr_2);
      tr_3=f_Offset(offset3,count_3,11,tr_3);
      tr_4=f_Offset(offset4,count_4,7,tr_4); 
      
       if(count_0<attempt)
        {
         if(tr_1  &&  tr_2  &&  tr_3 && tr_4)tr_3=false;
         if(!tr_1 && !tr_2 && !tr_3 && !tr_4)tr_3=true;
        }               
     }
   else
     {
      if(offset1<30)offset1++;
      if(offset2<30)offset2++;
      if(offset3<30)offset3++;
      if(offset4<30)offset4++;
          
      if((tr_1 && tr_2 && tr_3 && tr_4) || (!tr_1 && !tr_2 && !tr_3 && !tr_4))count_0=0;
     }
   f_Scroll(0,1,115,offset1,"B","S",tr_1);
   f_Scroll(2,3,90,offset2,"U","E",tr_2);
   f_Scroll(4,5,65,offset3,"Y","L",tr_3);
   f_Scroll(6,7,40,offset4,"","L",tr_4);

   ButtonCreate("butt9",120+xdist,71+ydist,110,30,text_butt,10,clrWhite,clr_button);
   ButtonCreate("butt10",120+xdist,9+ydist,110,30,"LAST CHANCE",8,clrWhite,clr_button);
   ChartRedraw();
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(sparam=="butt9" && !button_on)
     {
      button_on=true; text_butt="STOP"; count_0++;
      return;
     }
   if(sparam=="butt9" && button_on)
     {
      button_on=false; text_butt="START";
      return;
     }
  }
//====================================================================
void f_Delete_object()
  {
   for(int v=0;v<=11;v++)
     {
      ObjectDelete("butt"+(string)v);
     }
  }
//====================================================================  
bool f_Offset(int &offset,int &count,int speed, bool tr)
  {
   offset=count*speed;
      if(offset>=30)
        {
         offset=0; count=0;
         if(tr)tr=false;
         else
            tr=true;
        }
     return(tr);   
  }

//+------------------------------------------------------------------+
void f_Scroll(int num_1,int num_2,int xd,int speed,string letter_a,string letter_b,bool tr)
  {
   int offset_a=0;
   int offset_b=0;
   if(tr)
     {
      offset_a=30; offset_b=0;
     }
   else
     {
      offset_a=0; offset_b=30;
     }
   ButtonCreate("butt"+(string)num_1,xd+xdist,11+ydist+offset_a+speed,25,30,letter_a,20,clrYellow,clr_buy,clrLimeGreen);
   ButtonCreate("butt"+(string)num_2,xd+xdist,11+ydist+offset_b+speed,25,30,letter_b,20,clrYellow,clr_sell,clrLimeGreen);
  }
//===============кнопки===============================================
bool ButtonCreate(const string  name="button",      // имя кнопки
                  const int x=0,                    // координата по оси X
                  const int y=0,                    // координата по оси Y
                  const int width=0,                // ширина кнопки
                  const int height=0,               // высота кнопки
                  const string text="error",        // текст
                  const int font_size=6,            // размер шрифта
                  const color clr=clrNONE,          // цвет текста
                  const color  back_clr=clrNONE,    // цвет фона                                                 
                  const color border_clr=clrNONE,   // цвет границы  
                  const string  font="Verdana",     // шрифт            
                  const bool back=false,            // на заднем плане
                  const bool selection=false,       // выделить для перемещений
                  const bool hidden=false,          // скрыт в списке объектов
                  const bool state=false,           // нажата/отжата  
                  const long z_order=0)             // приоритет на нажатие мышью
  {
   ResetLastError();
   if(ObjectFind(name)==-1)
     {
      ObjectCreate(0,name,OBJ_BUTTON,0,0,0);
      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,name,OBJPROP_XSIZE,width);
      ObjectSetInteger(0,name,OBJPROP_YSIZE,height);
      ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
      ObjectSetString(0,name,OBJPROP_FONT,font);
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,font_size);
      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,border_clr);
      ObjectSetInteger(0,name,OBJPROP_BACK,back);
      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,selection);
      ObjectSetInteger(0,name,OBJPROP_SELECTED,selection);
      ObjectSetInteger(0,name,OBJPROP_HIDDEN,hidden);
      ObjectSetInteger(0,name,OBJPROP_ZORDER,z_order);
     }
   ObjectSetInteger(0,name,OBJPROP_STATE,state);
   ObjectSetString(0,name,OBJPROP_TEXT,text);
   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
   return(true);
  }
//+------------------------------------------------------------------+
