//+------------------------------------------------------------------+
//|                                                  Moon Phases.mq4 |
//|                                        Copyright © 2008, LEGRUPO |
//|                                           http://www.legrupo.com |
//|                                                     Version: 1.0 |
//| History:                                                         |
//| 1.0 => Release version to the public                             |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, LEGRUPO"
#property link      "http://www.legrupo.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 RoyalBlue

extern color FullMoon = Yellow;
extern color NewMoon = Lime;

double new_moon[];
double full_moon[];
int ExtCountedBars=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW, DRAW_ARROW, 1, NewMoon);
   SetIndexBuffer(0, new_moon);
   SetIndexStyle(1,DRAW_ARROW, DRAW_ARROW, 1, FullMoon);
   SetIndexBuffer(1, full_moon);
   SetIndexArrow(0,242);
   SetIndexArrow(1,241);
   SetIndexDrawBegin(0,5);
   
   SetIndexBuffer(0,new_moon);
   SetIndexBuffer(1,full_moon);
   
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   int limit=Bars-counted_bars;
   for(int m = 0 ;m <= limit ;m++) {
//----
         int moon_actual = moon_phase(TimeYear(Time[m]),TimeMonth(Time[m]),TimeDay(Time[m]));
         
         if (moon_actual == 0) {
            new_moon[m] = High[m];
         } else {
            //new_moon[m] = 0.0;
         }
         if (moon_actual == 4) {
            full_moon[m] = High[m];
         } else {
            //full_moon[m] = 0.0;
         }
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
int moon_phase(int y, int m, int d)
{
    /*
      calculates the moon phase (0-7), accurate to 1 segment.
      0 = > new moon.
      4 => full moon.
      */

    int c,e;
    double jd;
    int b;

    if (m < 3) {
        y--;
        m += 12;
    }
    m = 1+m;
    c = 365.25*y;
    e = 30.6*m;
    jd = c+e+d-694039.09;  /* jd is total days elapsed */
    jd /= 29.53;           /* divide by the moon cycle (29.53 days) */
    b = jd;		   /* int(jd) -> b, take integer part of jd */
    jd -= b;		   /* subtract integer part to leave fractional part of original jd */
    b = jd*8 + 0.5;	   /* scale fraction from 0-8 and round by adding 0.5 */
    b = b & 7;		   /* 0 and 8 are the same so turn 8 into 0 */
    return(b);
}