//+------------------------------------------------------------------+
//|                                                Stoch_rainbow.mq4 |
//|                                                             Swan |
//|                                  http://ruforum.forexpeoples.com |
//|                                                           eugenk |
//|                                     http://forum.mql4.com/ru/9154|
//|                                                       Dm_35 2008 |
//|                                         http://dimdimych.ucoz.ru |
//+------------------------------------------------------------------+
#property copyright "Dm_35"
#property link      "dm34@mail.ru"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 20
#property indicator_level2 80
extern int KPeriod=8;
extern int Slowing=3;
extern int method=0;
extern int Step=1;//шаг изменения KPeriod
extern int Count=50;//количество линий
extern int Limit=400;//количество баров для отрисовки

int m;
int Window	= -1;
string Name1;
color Color=0;
int rainbow[10000];  //тут хранится радуга
int nRainbow;        //число цветов радуги
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   if(Count>70) Count=70;
   Name1="Stoch("+KPeriod+","+Slowing+")";
   IndicatorShortName(Name1);
   Window	= -1;
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   DeleteLine();
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
double ArrayMain[][50];
	Window = WindowFind(Name1);
	if ( Window < 0 )
	{
		Print( "WindowFind error!" );
		return(-1);
	}
DeleteLine(); m=0;


   ArrayResize(ArrayMain,Limit);
   for(int i=0;i<Limit;i++)
      {
      for(int n=0;n<Count;n++)
         {
         ArrayMain[i][n]=iStochastic(NULL,0,KPeriod+n*Step,1,Slowing,method,0,MODE_MAIN,i);
         }
      }
nRainbow=BuildRainbow();
int c;
   for(n=0;n<Count;n++)
      {
      for(i=0;i<Limit-1;i++)
         {
         c=GetRainbowColor(n);
         DrawLine(ArrayMain[i][n],ArrayMain[i+1][n],c,i);
         }
      }

WindowRedraw();
   return(0);
  }
//+------------------------------------------------------------------+
void DrawLine(double Var1,double Var2,int c,int i)
   {
   ObjectCreate("Stoch"+m,OBJ_TREND,Window,Time[i],Var1,Time[i+1],Var2);
   ObjectSet("Stoch"+m,OBJPROP_RAY,false);
   ObjectSet("Stoch"+m,OBJPROP_COLOR,c);
   m++;
   }
void DeleteLine()
   {
   for(int i=0;i<m;i++)
   ObjectDelete("Stoch"+i);
   }
int GetRGB(int r, int g, int b)
{//строит цвет по компонентам r, g, b
   return (256*(256*b + g) + r);
}

int BuildRainbow()
{//строит радугу, возвращая число цветов
//возможно Вы придумаете более выразительную палитру :)
   int r=255, g=0, b=0;
   int n=0;
   
   while(g !=255)
   {
      rainbow[n]=GetRGB(r, g, b);
      g++; n++;
   }   
   while(r !=0)
   {
      rainbow[n]=GetRGB(r, g, b);
      r--; n++;
   }
   while(b !=255)
   {
      rainbow[n]=GetRGB(r, g, b);
      b++; n++;
   }
   while(g !=0)
   {
      rainbow[n]=GetRGB(r, g, b);
      g--; n++;
   }
   while(r !=255)
   {
      rainbow[n]=GetRGB(r, g, b);
      r++; n++;
   }
   return (n);   
}

int GetRainbowColor(int c)
{//Выбирает из радуги отмасштабированный цвет
   double k=nRainbow/Count;
   int n=c*k;
   return (rainbow[n]);
}      