//+------------------------------------------------------------------+
//|                                                    TestVedic.mq4 |
//|     Copyright © 2011, Christian R. Donahue and Peter Greenhough. |
//|                                                                  |
//+------------------------------------------------------------------+

#property copyright "Christian R. Donahue and Peter Greenhough"
#property indicator_chart_window
//--- input parameters

//---- indicator buffers

double currentY, prevY;
datetime currentX, prevX;
extern int BarsBack = 100;
extern double GannScale = 3.750776;
extern color highColor = Red;
extern color lowColor = Red;
extern int showLines = 0;
extern int showNumbers = 1;
extern int KeyNumber = 0;
string picker = "";



int flag;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init(){ 
  // ObjectsDeleteAll();
    flag = 0;
      delObjs("High", BarsBack);
   delObjs("Low", BarsBack);
 
   
}
 

   

  
int deinit(){
   delObjs("Open", BarsBack);
  // delObjs("Low", BarsBack);
}
//+------------------------------------------------------------------+
//| Insert Vedic  3 6 9 Price points                          |
//+------------------------------------------------------------------+
int start(){
      if (flag == 1) {
         return(0);
         }
      flag = 1;   
      int limit = BarsBack;
      if (limit == 0){ 
         limit = Bars; 
      }
      int i = 0; 
      int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
      limit = limit-counted_bars;  
      limit = BarsBack; 
      for(i=0; i<limit; i++) {
         int tv = calcNines(sumDigits(TimeYear(Time[i]))+sumDigits(TimeMonth(Time[i]))+sumDigits(TimeDay(Time[i]))+sumDigits(TimeHour(Time[i])));
      // Print(sumDigits(TimeYear(Time[i]))+sumDigits(TimeMonth(Time[i]))+sumDigits(TimeDay(Time[i]))+sumDigits(TimeHour(Time[i])));
        
         int v= calcNines(Open[i]);
         int vedic = calcNines(v + tv);
         int vp = 0;
         if(KeyNumber != 0) {
             vp = MathMod(vedic, KeyNumber);
         }
         string vs = v;
         if ((v!=0) && 0 == vp  ){           
            addNumber("Open" + i, v, highColor, Time[i], Open[i], tv);
         }
        
         
      } 
       WindowRedraw();
} 

int calcNines( double price) {
      double bucket = MathRound(100000 * price);
    //  Print(bucket + " - " + price);
      while (bucket > 9) { 
         bucket = sumDigits(bucket);
        
      }   
    //  Print(bucket);
      return(bucket);
}  

double sumDigits( double bucket1){
      double sum = 0;
      while (bucket1 > 0) {
          // Print("Bucket1 -" + bucket1);
          sum = sum + (MathMod(bucket1,10) );
         // Print ("Sum - " + sum);
          bucket1 = MathFloor(bucket1/10);
        } 
        return(sum);
}

void addNumber(string name, int value, color lineColor, datetime x, double y, int vedicTime) {
    
     if(showNumbers == 1) {    
       int vedicx = calcNines(vedicTime + value); 
       name = name + "T";
       if(!ObjectCreate(name , OBJ_TEXT, 0, x, y)) {
            Print("error: can''t create Text! code #",GetLastError());
            return(0);
       } 
       string sv = value +"/"+vedicTime + "=" + vedicx ;
       ObjectSetText(name, sv, 8, "Arial", lineColor);
       }
       
     //  Print(vedic+"-"+sv+"-"+vedicTime);
       string vt = vedicx;
      if ( showLines == 1  ){
      
     
         int style = STYLE_SOLID;
         if (vedicTime == 3) { style = STYLE_DOT;}
         if (vedicTime == 6) { style = STYLE_DASH;}
        
         
          name = name + "G";
       if(!ObjectCreate(name  , OBJ_GANNLINE, 0, x, y,  x+(60*Period()), 0)){
            Print("error: can''t create Gann! code #",GetLastError());
            return(0);
       } 
      
       ObjectSet(name, OBJPROP_COLOR, lineColor); 
       ObjectSet(name, OBJPROP_STYLE, style);     
    // ObjectSet(name, OBJPROP_TIMEFRAMES, OBJ_PERIOD_H1);
       ObjectSet(name, OBJPROP_SCALE, GannScale);
       name = name + "M";
       if(!ObjectCreate(name  , OBJ_GANNLINE, 0, x, y,  x+(60*Period()), 0)){
            Print("error: can''t create Gann! code #",GetLastError());
            return(0);
       } 
      
       ObjectSet(name, OBJPROP_COLOR, lineColor);  
       ObjectSet(name, OBJPROP_STYLE, style);  
     // ObjectSet(name, OBJPROP_TIMEFRAMES, OBJ_PERIOD_H1);
       ObjectSet(name, OBJPROP_SCALE, -1*GannScale);
       
    }
      //  WindowRedraw();
     //  Print("gann scale "  + name + ObjectGet(name, OBJPROP_SCALE)); 
}
/*

void addLineB(string name, color lineColor, datetime x, double y ) {
     
      if(!ObjectCreate(name, OBJ_RECTANGLE, 0, x, y, Time[0], y+0.00008)) {
            Print("error: can''t create Hline! code #",GetLastError());
            return(0);
       } 
       ObjectSet(name, OBJPROP_COLOR, lineColor); 
       ObjectSet(name, OBJPROP_STYLE, STYLE_DOT); 
       name = name + "T";
       if(!ObjectCreate(name , OBJ_TEXT, 0, x, y)) {
            Print("error: can''t create Hline! code #",GetLastError());
            return(0);
       } 
      
       ObjectSetText(name, DoubleToStr(y,5), 8, "Arial", lineColor);
}

void addLineC(string name, color lineColor, datetime x, double y ) {
     
      if(!ObjectCreate(name, OBJ_RECTANGLE, 0, x, y-0.00008, Time[0], y)) {
            Print("error: can''t create Hline! code #",GetLastError());
            return(0);
       } 
       ObjectSet(name, OBJPROP_COLOR, lineColor); 
       ObjectSet(name, OBJPROP_STYLE, STYLE_DOT); 
       name = name + "T";
       if(!ObjectCreate(name , OBJ_TEXT, 0, x, y)) {
            Print("error: can''t create Hline! code #",GetLastError());
            return(0);
       } 
      
       ObjectSetText(name, DoubleToStr(y,5), 8, "Arial", lineColor);
}
*/

//+------------------------------------------------------------------+



void delObjs(string objpref, int count){  
   
   for (int ii=0; ii<count+1; ii=ii+1) {
      ObjectDelete(objpref + ii); 
      ObjectDelete(objpref + ii + "T");
       ObjectDelete(objpref + ii + "TG");
        ObjectDelete(objpref + ii + "TGM");
     
   }
 }
 

