//+------------------------------------------------------------------+
//|                                               mn Zone colour.mq4 |
//+------------------------------------------------------------------+
#property copyright "mn"
#property indicator_chart_window

extern int mBarsHist    = 300,
           mMA          = 50,
           mMethod      = 1,
           mChannelWidth = 50;
extern color mUpCol     = Lime,
             mDownCol   = Red;  
extern bool  mBackGrnd  = true;

int mCounted = 0, mWind = 0, mLastBar;

double mPipFact = 1, mMaValue, mMaValue1, mMaValue2;
double mUpper, mLower, mMid;

//------------------------------------------------------------------|
int init()
  {
   for(int i = ObjectsTotal(); i >= 0; i--)
     if(StringSubstr(ObjectName(i), 0, 2) == "m.")
       ObjectDelete(ObjectName(i));
    Comment("");
   
   if(Digits == 3 || Digits == 5)
     mPipFact = 10;
     
   return(0);
  }

//+------------------------------------------------------------------+
int deinit()
  {
   for(int i = ObjectsTotal(); i >= 0; i--)
     if(StringSubstr(ObjectName(i), 0, 2) == "m.")
       ObjectDelete(ObjectName(i));
    Comment("");

   return(0);
  }

//+------------------------------------------------------------------+
int start()
  {
   if(mBarsHist <= 0) mBarsHist += 1;
   

   while(mBarsHist >= 0)
     {
        mMaValue = iMA(NULL, 0, mMA, 0, mMethod, PRICE_CLOSE, mBarsHist);
        mMaValue1 = mMaValue + mChannelWidth * Point * mPipFact;
        mMaValue2 = mMaValue - mChannelWidth * Point * mPipFact;
        mUpper = mMaValue1;
        mMid = mMaValue;
        mLower =  mMaValue2;
    
        NewObj(mUpper, mMid, mLower, mBarsHist);

 	     mBarsHist--;
     }
     
   return(0);
  }
  
//+------------------------------------------------------------------+
void NewObj(double mUp, double mM, double mL, int mBar)
 {
   static int mCnt;
   
   if(ObjectFind("m.Upper"+mBar) == -1)
         ObjectCreate("m.Upper"+mBar, OBJ_TREND, mWind, Time[mBar], mUp, Time[mBar], mM);
   ObjectSet("m.Upper"+mBar, OBJPROP_RAY, false);
   ObjectSet("m.Upper"+mBar, OBJPROP_WIDTH, 5);
   ObjectSet("m.Upper"+mBar, OBJPROP_PRICE1, mUp);
   ObjectSet("m.Upper"+mBar, OBJPROP_PRICE2, mM);
   ObjectSet("m.Upper"+mBar, OBJPROP_COLOR, mUpCol);
   ObjectSet("m.Upper"+mBar, OBJPROP_BACK, mBackGrnd);
   
   if(ObjectFind("m.Lower"+mBar) == -1)
       ObjectCreate("m.Lower"+mBar, OBJ_TREND, mWind, Time[mBar], mM, Time[mBar], mL);
   ObjectSet("m.Lower"+mBar, OBJPROP_RAY, false);
   ObjectSet("m.Lower"+mBar, OBJPROP_WIDTH, 5);
   ObjectSet("m.Lower"+mBar, OBJPROP_PRICE1, mM);
   ObjectSet("m.Lower"+mBar, OBJPROP_PRICE2, mL);
   ObjectSet("m.Lower"+mBar, OBJPROP_COLOR, mDownCol);
   ObjectSet("m.Lower"+mBar, OBJPROP_BACK, mBackGrnd);
   
   
   return(0);
  }
  
//+------------------------------------------------------------------+

