
//+------------------------------------------------------------------+
//|                                                    MoonPhase.mq4 |
//|                                                            Abhay |
//|                                            itisabhay@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Abhay"
#property link      "itisabhay@hotmail.com"
#include <stdlib.mqh>

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LightBlue
#property indicator_color2 Red
#property indicator_color3 Lime

/*
#property indicator_color3 LightBlue
#property indicator_color4 Yellow
#property indicator_width3 1
#property indicator_width4 1
*/
 
extern datetime Start.Date=D'2008.07.01 00:00';
extern bool display.turningpoints=true;
extern int lookback.bars=1000;
extern color FullMoonLineColor1=Red;
extern color FullMoonLineColor2=Blue;
extern color FullMoonLineColor3=Yellow;
extern color FullMoonLineColor4=Green;

string mpstdt[];
string mpenddt[];
int    mpphase[];

double myPoint;

int    barseq=1,prevbar=0, eCount=0,eCount1=0;
int    limit,bar,error;

string str[],str1[],str.prev[],str1.prev[],mp1[],mp2[];

string shortName="MoonPhase";
string FileName ="MoonPhases.csv";
bool   look.next.turningpoint=true,look.next.trendchange=true,tp_drawn=true;
bool   comments.on=false,invalid.trend=false,signal.set=true;
double vect[], trend[],buffer[]; 
int    handle=0,handle1=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int deinit()
{
     ObjectsDeleteAll(0, OBJ_RECTANGLE);
     
     for (int bar=Bars-1;bar>=1;bar--)
     {
         ObjectDelete("MP"+bar);
     }
}

int init()
{
//---- additional buffers are used for counting
 
   IndicatorShortName(shortName);

   SetIndexBuffer(0,mpstdt); 
   SetIndexBuffer(1,mpenddt);
   SetIndexBuffer(2,mpphase);

//   SetIndexEmptyValue(0," ");
//   SetIndexEmptyValue(1," ");
//   SetIndexEmptyValue(2,-1);
   
   myPoint = SetPoint();
   
   if (handle==0) handle = FileOpen(FileName, FILE_CSV|FILE_READ,',');
   if(handle==0)
       Comment("File "+FileName+" not found.");
   else if (handle < 0)
   {
       FileSeek(handle, 0, SEEK_SET);
       Print("###  Handle: ",handle);
   } 

   
//  return(0);
  }
//+------------------------------------------------------------------+
//| MoonPhase Indicator                                              |
//+------------------------------------------------------------------+
int start()
  {
   int i=0;
   int counted_bars=IndicatorCounted();
   color lincol=FullMoonLineColor1;
   
// if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
//   int eCount=0,eCount1=0;

   string tp1[],tp2[];
   int    x = 0; 


   if (limit==1 && prevbar == Bars) return;
   
//---- 
   for(bar = 1 ;bar <= limit ;bar++)
   {
         if (limit > Bars/2 || (bar==limit && Bars > prevbar)) 
         {

            if (limit > Bars/2 || (bar==limit && Bars > prevbar)) 
            {
               prevbar = Bars;
            }
         }
           
         if ( Time[limit-bar] <= StrToTime(str1[eCount])) 
         {
            look.next.turningpoint=false;
         }
         
         if ( Time[limit-bar] > StrToTime(str1[eCount])) look.next.turningpoint=true;
            
            
            for(eCount = 0; look.next.turningpoint==true && !FileIsEnding(handle); eCount++)              // Read from NEWS.CSV; one line per event
            {
               if (TimeToStr(Time[limit-bar],TIME_DATE) == TimeToStr(StrToTime(str1[eCount]),TIME_DATE)) break;

               if(handle>0)
               {
                  str.prev[eCount] = str[eCount];
                  str1.prev[eCount]= str1[eCount];
                  
                  mp1[eCount]=FileReadString(handle);
                  mp2[eCount]=FileReadString(handle);
                  
                  str[eCount]  = StringSubstr(mp1[eCount],6,4)+"."+StringSubstr(mp1[eCount],0,2)+"."+StringSubstr(mp1[eCount],3,2)+" "+StringSubstr(mp1[eCount],13,5);
                  str1[eCount] = StringSubstr(mp2[eCount],6,4)+"."+StringSubstr(mp2[eCount],0,2)+"."+StringSubstr(mp2[eCount],3,2)+" "+StringSubstr(mp2[eCount],13,5);


                  barseq = barseq + 1;
                  
                  tp_drawn = false;
            
                  if ( StrToTime(str1[eCount]) - StrToTime(str[eCount]) > 30*24*3600)
                  {
                      return;
                  }


                  if ((Time[limit-bar] <= StrToTime(str[eCount]) && Time[limit-bar] >= StrToTime(str1[eCount]))
                       ||
                      (Time[limit-bar] >= StrToTime(str[eCount]) && Time[limit-bar] <= StrToTime(str1[eCount]))
                     )
                  {
                      break;
                  }

                  if (Time[limit-bar] <= StrToTime(str[eCount]))
                  {
                     break;
                  }
            
               }
            } // for (eCount=0.....)
      
            mpstdt[limit-bar] = str[eCount];
            mpenddt[limit-bar]= str1[eCount];

            if (MathMod(barseq,4)==0) mpphase[limit-bar]=0;
            if (MathMod(barseq,4)==1) mpphase[limit-bar]=1;
            if (MathMod(barseq,4)==2) mpphase[limit-bar]=2;
            if (MathMod(barseq,4)==3) mpphase[limit-bar]=3;

Print("!!mpstdt[",limit-bar,"]: ",mpstdt[limit-bar]," ","mpenddt[",limit-bar,"]: ",mpenddt[limit-bar]);


            
            if (
//                (Time[limit-bar] <= StrToTime(str[eCount]) && Time[limit-bar] >= StrToTime(str1[eCount]))
//                  ||
                  (Time[limit-bar] >= StrToTime(str[eCount]) && Time[limit-bar] <= StrToTime(str1[eCount]))
               )
            {
                     tp_drawn=true;
                     if (display.turningpoints==true)
                     {
                              if (MathMod(barseq,4)==0) lincol=FullMoonLineColor1;
                              if (MathMod(barseq,4)==1) lincol=FullMoonLineColor2;
                              if (MathMod(barseq,4)==2) lincol=FullMoonLineColor3;
                              if (MathMod(barseq,4)==3) lincol=FullMoonLineColor4;

                              ObjectDelete("MP"+(barseq));
         
                              ObjectCreate("MP"+barseq,OBJ_RECTANGLE,0,StrToTime(str[eCount]),0,Time[limit-bar],1000);
			                     ObjectSet("MP"+barseq,OBJPROP_COLOR,lincol);
			                     ObjectSet("MP"+barseq,OBJPROP_STYLE,STYLE_SOLID);
			                     ObjectSet("MP"+barseq,OBJPROP_WIDTH,1);
			                     ObjectSet("MP"+barseq,OBJPROP_BACK,true);
 
                     }
            }

   } //For (bar=1,bar <=limit,bar++)
   
//   FileClose(handle);
Print("!!##mpstdt[",0,"]: ",mpstdt[0]," ","mpenddt[",0,"]: ",mpenddt[0]," ","mpphase[0]: ",mpphase[0]);

//----
   return(0);
  }
//+------------------------------------------------------------------+
/******************************************************************************/
double SetPoint()
{
   double mPoint;
   
   if (Digits < 3)
      mPoint = 0.01;
   else if (Digits ==3)
      mPoint = 0.001;
   else if (Digits==4)
      mPoint = 0.0001;
   
   return(mPoint);
}
/******************************************************************************/

