//+------------------------------------------------------------------+
//|                                 Nice Value Chart (Light Version) |
//+------------------------------------------------------------------+
#property copyright "Nedd and iJSmile`"
#property link      "www.binaryoptionsedge.com/user/958-neddihrehat/ & http://www.binaryoptionsedge.com/user/20350-ijsmile/"
#property version   "1.0"
#property description "A Nice and Light Oscillator that lets you rule the world. Of course we are joking... :)"

#property indicator_separate_window
#property indicator_levelcolor clrSilver
#property indicator_levelstyle 1
#property indicator_buffers 4
#property indicator_color1 CLR_NONE
#property indicator_color2 CLR_NONE
#property indicator_color3 CLR_NONE
#property indicator_color4 CLR_NONE
#property indicator_level1 8
#property indicator_level2 -8
#property indicator_minimum -15
#property indicator_maximum 15

extern string Note1 = "--------------------------- Main -------------------------";
extern int NumBars                     = 5;
extern int BarsHistory                 = 1000;
extern int OBLevel                     = 8;
extern int OSLevel                     = -8;

extern string Note2 = "--------------------------- Design ------------------------";
extern color BaseColor                 = clrSilver;
extern color OB                        = clrGreen;
extern color OS                        = clrRed;
extern int CandleSize                  = 7;

extern string Note3 = "--------------------------- Alerts ------------------------";
extern bool EnableAlerts               = True;
extern bool SoundOnly                  = False;
extern string SoundFile                = "alert.wav";

string objPrefix = "NVC Light";

double OO[],HH[],LL[],CC[];
datetime timeAlert;
int indiWindowId; 

int init() 
{
   SetIndexStyle(0, DRAW_NONE);
   SetIndexBuffer(0, OO);
   SetIndexLabel(0, NULL);
   
   SetIndexStyle(1, DRAW_NONE);
   SetIndexBuffer(1, HH);
   SetIndexLabel(1, NULL);
   
   SetIndexStyle(2, DRAW_NONE);
   SetIndexBuffer(2, LL);
   SetIndexLabel(2, NULL);
   
   SetIndexStyle(3, DRAW_NONE);
   SetIndexBuffer(3, CC);
   SetIndexLabel(3, NULL);
 
   IndicatorShortName(objPrefix);
   indiWindowId = WindowFind(objPrefix);
   return (0);
}

int deinit() 
{
   ObjectsDeleteAll(indiWindowId);
   return (0);
}

int start() 
{
   double SUM, MVA, ATR;
   int firstUncountedBarIndex, countedBars;
    
   countedBars = IndicatorCounted();
   firstUncountedBarIndex = Bars - countedBars - 1;  
   if (firstUncountedBarIndex > BarsHistory - 1)
   {  
      firstUncountedBarIndex = BarsHistory - 1;
   } 

   for (int i = 0; i <= firstUncountedBarIndex; i++) 
   {  
      SUM = 0;
      for (int k = i; k < NumBars + i; k++)                                                                                                                                                                                                                                                                                                                                                                                                                  
      {
         SUM += (High[k] + Low[k]) / 2.0;
      }
      MVA = SUM / NumBars;
      
      SUM = 0;
      for (k = i; k < NumBars + i; k++) 
      {
         SUM += High[k] - Low[k];
      }
      ATR = 0.2 * (SUM / NumBars);

      HH[i] = (High[i] - MVA) / ATR;
      LL[i] = (Low[i] - MVA) / ATR;
      OO[i] = (Open[i] - MVA) / ATR;
      CC[i] = (Close[i] - MVA) / ATR;
     
      if(EnableAlerts && i == 0 && timeAlert != Time[0])
      {
         if(CC[i] >= OBLevel)
         {
            ShowAlert("OB. " + NormalizeDouble(CC[i], 2));
            timeAlert = Time[0];
         }
         if(CC[i] <= OSLevel)
         {
            ShowAlert("OS. " + NormalizeDouble(CC[i], 2));
            timeAlert = Time[0]; 
         }
      } 
   }
       
   for (k = 0; k <= firstUncountedBarIndex; k++) 
   {
      string baseCandleName = objPrefix + "_HL_" + Time[k];
      ObjectDelete(baseCandleName);
      DrawTrendLine(baseCandleName, BaseColor, CandleSize,indiWindowId, Time[k], HH[k], Time[k], LL[k]);
           
      if(HH[k] >= OBLevel && HH[k] != EMPTY_VALUE)
      {
         string obOverlayName = objPrefix + "_OB_" + Time[k];
         ObjectDelete(obOverlayName);
         DrawTrendLine(obOverlayName, OB, CandleSize, indiWindowId, Time[k], HH[k] , Time[k], OBLevel);
      }
       
      if(LL[k] <= OSLevel && LL[k] != EMPTY_VALUE)
      {
         string osOverlayName = objPrefix + "_OS_" + Time[k];
         ObjectDelete(osOverlayName);
         DrawTrendLine(osOverlayName, OS, CandleSize, indiWindowId, Time[k], LL[k] , Time[k], OSLevel);
      } 
   }
   return (0);
}

void ShowAlert(string message)
{
   if(SoundOnly)
   {
      PlaySound(SoundFile);
   }
   else
   {
      string finalMessage = StringFormat("%s %s, %d | %s", objPrefix, _Symbol, _Period, message);
      Alert(finalMessage);
   }
}

void DrawTrendLine(string objName, color clr, int width, int windowId, 
                   datetime time1, double price1, datetime time2, double price2)
{
   ObjectCreate(objName, OBJ_TREND, windowId, time1, price1 , time2, price2);
   ObjectSet(objName, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(objName, OBJPROP_RAY, FALSE);
   ObjectSet(objName, OBJPROP_WIDTH, width);
   ObjectSet(objName, OBJPROP_SELECTABLE, FALSE); 
   ObjectSet(objName, OBJPROP_COLOR, clr); 
}