//+-------------------------------------------------------------------------------------------+
//|                                                                                           |
//|                                     Snake Candles B1.mq4                               |
//|                                                      				                         |
//+-------------------------------------------------------------------------------------------+
#property copyright "Copyright @ 2017 wiserltz"
#property link      "email: wiserltz@sina.com"

//+-------------------------------------------------------------------------------------------+
//| Indicator Global Inputs                                                                   |
//+-------------------------------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 204

//Global External Inputs
extern bool   Indicator_On=true;

//Global Buffers and Variables
bool          Deinitialized;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
struct BarSE
  {
   double            b[];
   double            e[];

  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
struct BarColor
  {
   BarSE             c[10];
  };

BarColor          Bar[10];
BarSE             Candle[2];
int               Chart_Scale=-1,Bar_Width,va,i,j,counted_bars,limit,ExtCountedBars;
extern color      BullColor    = DarkSeaGreen;
extern color      BearColor    = Maroon;
//Default PVA Colors
extern int        ColorGroup=5;
extern color      Bar1Color1   = C'240,240,240';//light
extern color      Bar2Color1   = C'220,220,220';//   |
extern color      Bar3Color1   = C'200,200,200';//   |
extern color      Bar4Color1   = C'180,180,180';//   |
extern color      Bar5Color1   = C'160,160,160';//   |
extern color      Bar6Color1   = C'140,140,140';//   |
extern color      Bar7Color1   = C'120,120,120';//   |
extern color      Bar8Color1   = C'100,100,100';//   |
extern color      Bar9Color1   = C'080,080,080';//   |
extern color      Bar10Color1  = C'060,060,060';//heavy

extern color      Bar1Color2   = C'0xff,0xf3,0xe0';
extern color      Bar2Color2   = C'0xff,0xe0,0xb2';
extern color      Bar3Color2   = C'0xff,0xcc,0x80';
extern color      Bar4Color2   = C'0xff,0xb7,0x4d';
extern color      Bar5Color2   = C'0xff,0xa7,0x26';
extern color      Bar6Color2   = C'0xff,0x98,0x00';
extern color      Bar7Color2   = C'0xfb,0x8c,0x00';
extern color      Bar8Color2   = C'0xf5,0x7c,0x00';
extern color      Bar9Color2   = C'0xef,0x6c,0x00';
extern color      Bar10Color2  = C'0xe6,0x51,0x00';

extern color      Bar1Color3   = C'0xef,0xeb,0xe9';
extern color      Bar2Color3   = C'0xd7,0xcc,0xc8';
extern color      Bar3Color3   = C'0xbc,0xaa,0xa4';
extern color      Bar4Color3   = C'0xa1,0x88,0x7f';
extern color      Bar5Color3   = C'0x8d,0x6e,0x63';
extern color      Bar6Color3   = C'0x79,0x55,0x48';
extern color      Bar7Color3   = C'0x6d,0x4c,0x41';
extern color      Bar8Color3   = C'0x5d,0x40,0x37';
extern color      Bar9Color3   = C'0x4e,0x34,0x3e';
extern color      Bar10Color3  = C'0x3c,0x27,0x23';

extern color      Bar1Color4   = C'0xe0,0xf7,0xfa';
extern color      Bar2Color4   = C'0xb2,0xeb,0xf2';
extern color      Bar3Color4   = C'0x80,0xde,0xea';
extern color      Bar4Color4   = C'0x4d,0xd0,0xe1';
extern color      Bar5Color4   = C'0x26,0xc6,0xda';
extern color      Bar6Color4   = C'0x00,0xbc,0xd4';
extern color      Bar7Color4   = C'0x00,0xac,0xc1';
extern color      Bar8Color4   = C'0x00,0x97,0xa7';
extern color      Bar9Color4   = C'0x00,0x83,0x8f';
extern color      Bar10Color4  = C'0x00,0x60,0x64';

extern color      Bar1Color5   = C'0xed,0xe7,0xf6';
extern color      Bar2Color5   = C'0xd1,0xc4,0xe9';
extern color      Bar3Color5   = C'0xb3,0x9d,0xdb';
extern color      Bar4Color5   = C'0x95,0x75,0xcd';
extern color      Bar5Color5   = C'0x7e,0x57,0xc2';
extern color      Bar6Color5   = C'0x67,0x3a,0xb7';
extern color      Bar7Color5   = C'0x5e,0x35,0xb1';
extern color      Bar8Color5   = C'0x51,0x2d,0xa8';
extern color      Bar9Color5   = C'0x45,0x27,0xa0';
extern color      Bar10Color5=C'0x31,0x1b,0x92';

extern color      Bar1Color6   = C'0xe8,0xea,0xf6';
extern color      Bar2Color6   = C'0xc5,0xca,0xe9';
extern color      Bar3Color6   = C'0x9f,0xa8,0xda';
extern color      Bar4Color6   = C'0x79,0x86,0xcb';
extern color      Bar5Color6   = C'0x5c,0x6b,0xc0';
extern color      Bar6Color6   = C'0x3f,0x51,0xb5';
extern color      Bar7Color6   = C'0x39,0x49,0xab';
extern color      Bar8Color6   = C'0x30,0x3f,0x9f';
extern color      Bar9Color6   = C'0x28,0x35,0x93';
extern color      Bar10Color6=C'0x1a,0x23,0x7e';

extern string     _WeightBars__________="Same TF X Prevous bars";
color             BarColors[10],CandleColors[2];
int               peroid;
int               WeightBars=1;
//+-------------------------------------------------------------------------------------------+
//| Custom indicator deinitialization function                                                |
//+-------------------------------------------------------------------------------------------+
int deinit()
  {
   int obj_total=ObjectsTotal();
   for(int k=obj_total; k>=0; k--)
     {
      string name=ObjectName(k);
      if(StringSubstr(name,0,12)=="[Snake Candles B1") {ObjectDelete(name);}
     }
   return(0);
  }
//+-------------------------------------------------------------------------------------------+
//| Custom indicator initialization function                                                  |
//+-------------------------------------------------------------------------------------------+
int init()
  {
   peroid=Period();

   Deinitialized=false;

   Chart_Scale=ChartScaleGet();

   if(Chart_Scale==0) {Bar_Width=1;}
   else
     {
      if(Chart_Scale==1) Bar_Width=2;
      else if(Chart_Scale==2) Bar_Width=2;
      else if(Chart_Scale==3) Bar_Width=3;
      else if(Chart_Scale==4) Bar_Width=6;
      else Bar_Width=13;
     }

   CandleColors[0]=BullColor;
   CandleColors[1]=BearColor;

   if(ColorGroup==0)
     {
      BarColors[0]=Bar1Color1;//light
      BarColors[1]=Bar2Color1;//   |
      BarColors[2]=Bar3Color1;//   |
      BarColors[3]=Bar4Color1;//   |
      BarColors[4]=Bar5Color1;//   |
      BarColors[5]=Bar6Color1;//   |
      BarColors[6]=Bar7Color1;//   |
      BarColors[7]=Bar8Color1;//   |
      BarColors[8]=Bar9Color1;//   |
      BarColors[9]=Bar10Color1;//heavy
     }
   else if(ColorGroup==1)
     {
      BarColors[0]=Bar1Color2;//light
      BarColors[1]=Bar2Color2;//   |
      BarColors[2]=Bar3Color2;//   |
      BarColors[3]=Bar4Color2;//   |
      BarColors[4]=Bar5Color2;//   |
      BarColors[5]=Bar6Color2;//   |
      BarColors[6]=Bar7Color2;//   |
      BarColors[7]=Bar8Color2;//   |
      BarColors[8]=Bar9Color2;//   |
      BarColors[9]=Bar10Color2;//heavy
     }
   else if(ColorGroup==2)
     {
      BarColors[0]=Bar1Color3;//light
      BarColors[1]=Bar2Color3;//   |
      BarColors[2]=Bar3Color3;//   |
      BarColors[3]=Bar4Color3;//   |
      BarColors[4]=Bar5Color3;//   |
      BarColors[5]=Bar6Color3;//   |
      BarColors[6]=Bar7Color3;//   |
      BarColors[7]=Bar8Color3;//   |
      BarColors[8]=Bar9Color3;//   |
      BarColors[9]=Bar10Color3;//heavy
     }
   else if(ColorGroup==3)
     {
      BarColors[0]=Bar1Color4;//light
      BarColors[1]=Bar2Color4;//   |
      BarColors[2]=Bar3Color4;//   |
      BarColors[3]=Bar4Color4;//   |
      BarColors[4]=Bar5Color4;//   |
      BarColors[5]=Bar6Color4;//   |
      BarColors[6]=Bar7Color4;//   |
      BarColors[7]=Bar8Color4;//   |
      BarColors[8]=Bar9Color4;//   |
      BarColors[9]=Bar10Color4;//heavy
     }
   else if(ColorGroup==4)
     {
      BarColors[0]=Bar1Color5;//light
      BarColors[1]=Bar2Color5;//   |
      BarColors[2]=Bar3Color5;//   |
      BarColors[3]=Bar4Color5;//   |
      BarColors[4]=Bar5Color5;//   |
      BarColors[5]=Bar6Color5;//   |
      BarColors[6]=Bar7Color5;//   |
      BarColors[7]=Bar8Color5;//   |
      BarColors[8]=Bar9Color5;//   |
      BarColors[9]=Bar10Color5;//heavy
     }
   else if(ColorGroup==5)
     {
      BarColors[0]=Bar1Color6;//light
      BarColors[1]=Bar2Color6;//   |
      BarColors[2]=Bar3Color6;//   |
      BarColors[3]=Bar4Color6;//   |
      BarColors[4]=Bar5Color6;//   |
      BarColors[5]=Bar6Color6;//   |
      BarColors[6]=Bar7Color6;//   |
      BarColors[7]=Bar8Color6;//   |
      BarColors[8]=Bar9Color6;//   |
      BarColors[9]=Bar10Color6;//heavy
     }

   for(int b=0;b<10;b++)
      for(int c=0;c<10;c++)
        {
         SetIndexBuffer(b*20+c*2,Bar[b].c[c].b);
         SetIndexStyle(b*20+c*2,DRAW_HISTOGRAM,0,Bar_Width,BarColors[c]);
         SetIndexBuffer(b*20+c*2+1,Bar[b].c[c].e);
         SetIndexStyle(b*20+c*2+1,DRAW_HISTOGRAM,0,Bar_Width,BarColors[c]);
        }

   int candleWidth=1;

   SetIndexBuffer(200,Candle[0].b);
   SetIndexStyle(200,DRAW_HISTOGRAM,0,candleWidth,CandleColors[0]);
   SetIndexBuffer(201,Candle[0].e);
   SetIndexStyle(201,DRAW_HISTOGRAM,0,candleWidth,CandleColors[0]);

   SetIndexBuffer(202,Candle[1].b);
   SetIndexStyle(202,DRAW_HISTOGRAM,0,candleWidth,CandleColors[1]);
   SetIndexBuffer(203,Candle[1].e);
   SetIndexStyle(203,DRAW_HISTOGRAM,0,candleWidth,CandleColors[1]);

   IndicatorShortName("Snake Candles H1 H4 D1");
   return(0);
  }
//+-------------------------------------------------------------------------------------------+
//| 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 tmp;
   if(!Indicator_On)
     {
      if(!Deinitialized)
        {
         deinit();
         Deinitialized=true;
        }
      return(0);
     }

   limit=rates_total-prev_calculated;

   for(i=limit; i>=0; i--)
     {
      if(i>(ArraySize(Volume)-WeightBars-1))
         continue;

      if(open[i]<close[i])
        {
         Candle[0].b[i]=open[i];
         Candle[0].e[i]=close[i];

        }
      else
        {
         Candle[1].b[i]=open[i];
         Candle[1].e[i]=close[i];

        }

      datetime wantTime=Time[i];

      //计算前面WeightBars个柱的总volume,总pip
      double barVolume=0,barPoint=0;
      for(int tvi=0;tvi<WeightBars;tvi++)
        {
         tmp=i+tvi+1;
         barVolume+=Volume[tmp];
         barPoint+=(High[tmp]-Low[tmp])/Point;
        }
      double SectorVolumeAva=tick_volume[i]/10;

      double step=(high[i]-low[i])/10;

      int m1Index=iBarShift(NULL,PERIOD_M1,wantTime);
      datetime m1Time=iTime(0,PERIOD_M1,m1Index);

      double vInColorSec[10];//每个color-sector的volume
      double ColorSectorIndex[10];
      for(tmp=0;tmp<10;tmp++)
        {
         vInColorSec[tmp]=0;
         ColorSectorIndex[tmp]=0;
        }

      if(wantTime==m1Time)//循环包含的m1柱
        {
         for(int m1Count=0;m1Count<peroid;m1Count++)
           {
            int m1IndexLoop=m1Index-m1Count;
            if(m1IndexLoop>=0)
              {
               m1Time=iTime(0,PERIOD_M1,m1IndexLoop);
               if((m1Time-wantTime)<(60*peroid))//保证在m1上没有丢柱子
                 {
                  double h=iHigh(0,PERIOD_M1,m1IndexLoop);
                  double l=iLow(0,PERIOD_M1,m1IndexLoop);
                  if(h>high[i])
                     h=high[i];//保证在m1柱子在当前柱子范围内
                  if(l<low[i])
                     l=low[i];//保证在m1柱子在当前柱子范围内
                  h-=low[i];
                  l-=low[i];

                  if(step!=0)
                    {
                     double lMark=l/step;
                     if(lMark>9)
                        lMark=9;
                     else if(lMark<0)
                        lMark=0;
                     double hMark=h/step;
                     if(hMark>9)
                        hMark=9;
                     else if(hMark<0)
                        hMark=0;

                     double w=iVolume(0,PERIOD_M1,m1IndexLoop);
                     if(hMark!=lMark)
                        w=w/(hMark-lMark);

                     for(int iw=lMark;iw<=hMark;iw++)
                        vInColorSec[iw]+=w;
                    }
                 }
              }
            else
               break;
           }

         for(tmp=0;tmp<10;tmp++)
           {
            if(step!=0 && SectorVolumeAva!=0)
               ColorSectorIndex[tmp]=vInColorSec[tmp]/SectorVolumeAva*5;
            else
               ColorSectorIndex[tmp]=5;
            if(ColorSectorIndex[tmp]>9)
               ColorSectorIndex[tmp]=9;
            else if(ColorSectorIndex[tmp]<0)
               ColorSectorIndex[tmp]=0;
           }

         for(tmp=0;tmp<10;tmp++)
            if(ColorSectorIndex[tmp]<0)
               int abd1dd=0;
        }
      ClearlColorForBar(i);
      for(int secBottom2Top=0;secBottom2Top<10;secBottom2Top++)
        {
         Bar[secBottom2Top].c[(int)ColorSectorIndex[secBottom2Top]].b[i]=low[i]+step*secBottom2Top;
         Bar[secBottom2Top].c[(int)ColorSectorIndex[secBottom2Top]].e[i]=low[i]+step*(secBottom2Top+1);
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ClearlColorForBar(int i3)
  {
   for(int tmp=0;tmp<10;tmp++)
      for(int tmp1=0;tmp1<10;tmp1++)
        {
         Bar[tmp].c[tmp1].b[i3]=0;
         Bar[tmp].c[tmp1].e[i3]=0;
        }
  }
//+-------------------------------------------------------------------------------------------+
//| Subroutine:  Set up to get the chart scale number                                         |
//+-------------------------------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   int tmp=ChartScaleGet();
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   if(Chart_Scale!=tmp)
     {
      Chart_Scale=tmp;
      init();
     }
  }
//+-------------------------------------------------------------------------------------------+
//| Subroutine:  Get the chart scale number                                                   |
//+-------------------------------------------------------------------------------------------+
int ChartScaleGet()
  {
   long result=-1;
   ChartGetInteger(0,CHART_SCALE,0,result);
   return((int)result);
  }
//+------------------------------------------------------------------+
