Author: Ben L.
MoonPhases

This script is designed to automatically identify and display significant lunar cycles—specifically Full Moons and New Moons—directly on your trading chart. By visualizing these astronomical events, traders who look for correlations between celestial patterns and market behavior can easily see historical and future occurrences at a glance.

How the Logic Works

1. Automated Calculations Instead of manually searching for dates in an external calendar, the script uses complex mathematical formulas to calculate the exact timing of moon phases. It processes these calculations based on a base starting point, ensuring that each subsequent Full Moon or New Moon is accurately projected forward in time.

2. User-Defined Customization The tool offers several ways to tailor the information shown on your screen:

  • Visibility Toggles: You can choose whether you want to see Full Moons, New Moons, or both.
  • Visual Styling: You can customize the colors and thickness of the lines so they remain clear and do not clutter your workspace.
  • Timeframes: You can define a specific start and end date to narrow down the period of interest on your chart.

3. Timezone Synchronization The script includes a specific adjustment setting to account for the difference between Greenwich Mean Time (GMT) and your local chart time. This ensures that the lines appear at the correct moment relative to your specific viewing settings.

4. Dynamic Chart Drawing Once the calculations are complete, the script automatically draws vertical lines on your chart at the precise moments of each moon phase. It identifies every occurrence within your chosen date range and places a permanent marker, allowing for seamless backtesting or analysis of how the market reacted during those specific lunar events.

5. Error Management The underlying logic includes a safeguard system that monitors the drawing process. If the platform encounters any difficulty placing a line (such as a conflict with other objects), the script is programmed to alert you, ensuring you are aware of the integrity of the data being displayed.

Miscellaneous
It issuies visual alerts to the screen
12 Views
0 Downloads
0 Favorites
MoonPhases
//+------------------------------------------------------------------+
//|                                                   MoonPhases.mq4 |
//+------------------------------------------------------------------+
//
// Calculation taken from:
//  http://individual.utoronto.ca/kalendis/lunar/index.htm
//
//  Coded by:  Ben Letto, sbgtrading@yahoo.com
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

#property copyright "Ben L."
#include <stdlib.mqh>

#property indicator_chart_window

extern bool DrawFullMoon=true;
extern color FullMoonLineColor=White;
extern bool DrawNewMoon=false;
extern color NewMoonLineColor=DimGray;
extern datetime StartDate= D'2006.01.01 00:00';
extern datetime EndDate= D'2008.01.01 00:00';
//extern string OutFileName="FullMoonDates_out.csv";
extern int ChartTimeMinusGMTInMinutes = 120;

int      OutputHandle,i;
datetime FullMoonTime,NewMoonTime;

string LineName;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   if(StartDate<Time[Bars-1]) StartDate=Time[Bars-1];
//----
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
//----

   int total=300;
   for(i=total;i>0;i--)
   {  LineName=StringConcatenate("Full_",DoubleToStr(i,0));
      ObjectDelete(LineName);
      LineName=StringConcatenate("New_",DoubleToStr(i,0));
      ObjectDelete(LineName);
   }
 
   i=1;
   int errCount=0;
   FullMoonTime=0;
   NewMoonTime=0;

   int BarsForOpenLine=10*24*60/Period();
   double SecondsPerDay=60*60*24;

   while(FullMoonTime < EndDate)
   {  if(DrawFullMoon) FullMoonTime=StrToTime("2000.01.01 00:00")+MathRound(SecondsPerDay*(20.362954+29.5305888531*i+0.00000000010219*i*i)+ChartTimeMinusGMTInMinutes*60);
      else FullMoonTime=-1;
      if(FullMoonTime>StartDate)
      {  LineName=StringConcatenate("Full_",DoubleToStr(i,0));
         if(!ObjectCreate(LineName,OBJ_VLINE,0,FullMoonTime,0,0,0)) {HandleError(errCount, "Problem creating "+LineName+" "+TimeToStr(FullMoonTime,TIME_DATE));}
         else {ObjectSet(LineName,OBJPROP_COLOR,FullMoonLineColor);ObjectSet(LineName,OBJPROP_WIDTH,2);}
      }

      if(DrawNewMoon) NewMoonTime=StrToTime("2000.01.01 00:00")+MathRound(SecondsPerDay*(5.597661+29.5305888610*i+0.00000000010219*i*i)+ChartTimeMinusGMTInMinutes*60);
      else NewMoonTime=-1;
      if(NewMoonTime > StartDate)
      {  LineName=StringConcatenate("New_",DoubleToStr(i,0));
         if(!ObjectCreate(LineName,OBJ_VLINE,0,NewMoonTime,0,0,0)) {HandleError(errCount, "Problem creating "+LineName+" "+TimeToStr(NewMoonTime,TIME_DATE));}
         else {ObjectSet(LineName,OBJPROP_COLOR,NewMoonLineColor);ObjectSet(LineName,OBJPROP_WIDTH,2);}
      }
//      HandleError(errCount,LineName);
//      FileWrite(OutputHandle,TimeToStr(TheDates[i],TIME_DATE||TIME_MINUTES),DoubleToStr(Open[iBarShift(Symbol(),0,TheDates[i],false)],Digits));
      i++;
   }

//   FileClose(InputHandle);
//   FileClose(OutputHandle);

//----
   return(0);
}

void HandleError(int& errCount, string estr)
{  int err=GetLastError();
   if(err>1) 
   {  errCount++;
      if(errCount==1) Alert("New errors start here!");
   }
   if (err>0) Alert("Error: ",err," ",ErrorDescription(err)," e: ",estr);

}




Comments