//+------------------------------------------------------------------+
//|                                                   MOON_FAZES.mq4 |
//|                                                            Urain |
//+------------------------------------------------------------------+
#property copyright "Urain"
#property link      "http://www.mql4.com/ru/users/Urain"

#property indicator_separate_window
#property indicator_buffers 1

#property indicator_color1  clrDodgerBlue
#property indicator_width1  2

#property strict

#define pi 3.1415926535897932384626433832795
#define MOON_CYCLE 2551500
#define ETALON_MOON 1269926820
//
//
//
//
//

input bool               verticalLinesVisible  = true;            // Show vertical lines
input string             verticalLinesID       = "moon Lines";      // Lines ID
input color              verticalLinesUpColor  = clrDeepSkyBlue;   // Lines up color 
input color              verticalLinesDnColor  = clrPaleVioletRed; // Lines down color
input ENUM_LINE_STYLE    verticalLinesStyle    = STYLE_DOT;        // Lines style
input int                verticalLinesWidth    = 0;                // lines width

double moon[],trend[];


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int OnInit()
{
   IndicatorBuffers(2);
   SetIndexBuffer(0,moon,   INDICATOR_DATA); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,trend); 
   
   
   IndicatorSetString(INDICATOR_SHORTNAME," moon fazes ");
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{

string tlookFor       = verticalLinesID+":";
   int    tlookForLength = StringLen(tlookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,tlookForLength) == tlookFor) ObjectDelete(objectName);
   }     
   
}

//
//
//

int OnCalculate (const int       rates_total,
                 const int       prev_calculated,
                 const datetime& time[],
                 const double&   open[],
                 const double&   high[],
                 const double&   low[],
                 const double&   close[],
                 const long&     tick_volume[],
                 const long&     volume[],
                 const int&      spread[])
{
   int i,limit=fmin(rates_total-prev_calculated+1,rates_total-1); 
     
      
   //
   //
   //
          
  
   for(i=limit;i>=0; i--)
   {  
      moon[i]=MathCos(2*pi*(Time[i]-ETALON_MOON)/MOON_CYCLE);
     trend[i] = (i<Bars-1) ? (moon[i]>0) ? 1 : (moon[i]<0) ? -1 : trend[i+1] : 0; 
        
  if (verticalLinesVisible)
        {
           string tlookFor = verticalLinesID+":"+(string)Time[i]; ObjectDelete(tlookFor);  
           if (i<Bars-1 && trend[i]!=trend[i+1])
           {
              if (trend[i] == 1) drawLine(i,verticalLinesUpColor);
              if (trend[i] ==-1) drawLine(i,verticalLinesDnColor);
           }
         } 
   }
return(rates_total);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//

void drawLine(int i,color theColor)
{
      string name = verticalLinesID+":"+(string)Time[i];
   
      //
      //
      //
      //
      //
         
      datetime time = Time[i]; 
      ObjectCreate(name,OBJ_VLINE,0,time,0);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         ObjectSet(name,OBJPROP_STYLE,verticalLinesStyle);
         ObjectSet(name,OBJPROP_WIDTH,verticalLinesWidth);
         ObjectSet(name,OBJPROP_BACK,true);
}


//
//


string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
 

