#property copyright "KecoaKecil"
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Blue
#property indicator_color2 Blue
#property indicator_color3 Blue
#property indicator_color4 Blue
#property indicator_color5 Blue
#property indicator_color6 Blue
#property indicator_color7 Blue
#property indicator_color8 Blue
extern string newyork = "40.5N 74W";
extern string tokyo = "35.40N 139.45E";
extern string london = "51.32N 0.5W";
extern double latitude = 40.5;
extern double longitude = -74;
extern int prec = 10;
extern bool cmb1 = true;
extern int  pl1a = 1;
extern int  pl1b = 2;
extern double dg1 = 0.5;
extern bool cmb2 = true;
extern int  pl2a = 1;
extern int  pl2b = 3;
extern bool cmb3 = true;
extern int  pl3a = 1;
extern int  pl3b = 4;
extern bool cmb4 = true;
extern int  pl4a = 1;
extern int  pl4b = 5;
extern bool cmb5 = true;
extern int  pl5a = 1;
extern int  pl5b = 6;
extern bool cmb6 = true;
extern int  pl6a = 1;
extern int  pl6b = 7;
extern bool cmb7 = true;
extern int pl7a = 1;
extern int pl7b = 8;
extern bool cmb8 = true;
extern int pl8a = 1;
extern int pl8b = 9;
extern int    iflag = 0;
extern int    FutureBars=200;
extern int    BarsLimit=10000;

extern int Period_ = 1;


int period_ = 0;
double past_longtitude = -1;


#include <astro_swiss.mq4>
#include <astro_func.mq4>
#include <time.mq4>

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   string short_name = "MoonConjunct";
   if(Period_ > 1)
   {
     short_name = short_name + " " + DoubleToStr(Period_,0) + " periods";
   }
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Mercury");   
   SetIndexStyle(0,DRAW_ARROW,0,1);
   SetIndexArrow(0,140);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexShift(0,FutureBars);
   SetIndexLabel(1,"Venus");   
   SetIndexStyle(1,DRAW_ARROW,0,1);
   SetIndexArrow(1,141);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexShift(1,FutureBars);
   SetIndexLabel(2,"Mars");   
   SetIndexStyle(2,DRAW_ARROW,0,1);
   SetIndexArrow(2,142);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexShift(2,FutureBars);
   SetIndexLabel(3,"Jupiter");   
   SetIndexStyle(3,DRAW_ARROW,0,1);
   SetIndexArrow(3,143);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexShift(3,FutureBars);
   SetIndexLabel(4,"Saturnus");   
   SetIndexStyle(4,DRAW_ARROW,0,1);
   SetIndexArrow(4,144);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexShift(4,FutureBars);
   SetIndexLabel(5,"Uranus");   
   SetIndexStyle(5,DRAW_ARROW,0,1);
   SetIndexArrow(5,145);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexShift(5,FutureBars);
   SetIndexLabel(6,"Neptunus");   
   SetIndexStyle(6,DRAW_ARROW,0,1);
   SetIndexArrow(6,146);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexShift(6,FutureBars);
   SetIndexLabel(7,"Pluto");   
   SetIndexStyle(7,DRAW_ARROW,0,1);
   SetIndexArrow(7,147);
   SetIndexBuffer(7,ExtMapBuffer8);
   SetIndexShift(7,FutureBars);
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   int limit;
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   int shift = limit - FutureBars;
   if (shift < 0)
   {
     return(0);
   }
//---- TODO: add your code here
   
   
   // topo
   //spb astro_swe_set_topo(53,50,0);
   // new york
   // 40.5 N 74W
   astro_swe_set_topo(latitude,longitude,0);
   
   
   
   // Draw Normal
   //
   if (shift > BarsLimit)
   {
     shift = BarsLimit;
   }
   int ctr=0;
   double v1;
   while ( shift > 0 )
   {
   ctr = 0;
   if (cmb1)
   {
    v1 = get_astro_value(Time[shift],pl1a,pl1b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {if (Open[shift]<Close[shift]) {ExtMapBuffer1[shift+FutureBars]=Close[shift]+ctr*10*Point;ctr++;} else {ExtMapBuffer1[shift+FutureBars]=Close[shift]-ctr*10*Point;ctr++;}}
   }
   if (cmb2)
   {
    v1 = get_astro_value(Time[shift],pl2a,pl2b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {if (Open[shift]<Close[shift]) {ExtMapBuffer2[shift+FutureBars]=Close[shift]+ctr*10*Point;ctr++;} else {ExtMapBuffer2[shift+FutureBars]=Close[shift]-ctr*10*Point;ctr++;}}
   }
   if (cmb3)
   {
    v1 = get_astro_value(Time[shift],pl3a,pl3b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {if (Open[shift]<Close[shift]) {ExtMapBuffer3[shift+FutureBars]=Close[shift]+ctr*10*Point;ctr++;} else {ExtMapBuffer3[shift+FutureBars]=Close[shift]-ctr*10*Point;ctr++;}}
   }
   if (cmb4)
   {
    v1 = get_astro_value(Time[shift],pl4a,pl4b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {if (Open[shift]<Close[shift]) {ExtMapBuffer4[shift+FutureBars]=Close[shift]+ctr*10*Point;ctr++;} else {ExtMapBuffer4[shift+FutureBars]=Close[shift]-ctr*10*Point;ctr++;}}
   }
   if (cmb5)
   {
    v1 = get_astro_value(Time[shift],pl5a,pl5b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {if (Open[shift]<Close[shift]) {ExtMapBuffer5[shift+FutureBars]=Close[shift]+ctr*10*Point;ctr++;} else {ExtMapBuffer5[shift+FutureBars]=Close[shift]-ctr*10*Point;ctr++;}}
   }
   if (cmb6)
   {
    v1 = get_astro_value(Time[shift],pl6a,pl6b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {if (Open[shift]<Close[shift]) {ExtMapBuffer6[shift+FutureBars]=Close[shift]+ctr*10*Point;ctr++;} else {ExtMapBuffer6[shift+FutureBars]=Close[shift]-ctr*10*Point;ctr++;}}
   }
   if (cmb7)
   {
    v1 = get_astro_value(Time[shift],pl7a,pl7b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {if (Open[shift]<Close[shift]) {ExtMapBuffer7[shift+FutureBars]=Close[shift]+ctr*10*Point;ctr++;} else {ExtMapBuffer7[shift+FutureBars]=Close[shift]-ctr*10*Point;ctr++;}}
   }
   if (cmb8)
   {
    v1 = get_astro_value(Time[shift],pl8a,pl8b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {if (Open[shift]<Close[shift]) {ExtMapBuffer8[shift+FutureBars]=Close[shift]+ctr*10*Point;ctr++;} else {ExtMapBuffer8[shift+FutureBars]=Close[shift]-ctr*10*Point;ctr++;}}
   }
   shift = shift - 1;
   }
 
   // Draw Future
   // now shift = 0
   datetime curr_datetime = Time[shift];
   int period_ = get_period(shift);
   while ( shift > -FutureBars )
   {
   ctr =0;
   if (cmb1)
   {
    v1 = get_astro_value(curr_datetime,pl1a,pl1b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {ExtMapBuffer1[shift+FutureBars]=Bid+ctr*10*Point;ctr++;}
   }
   if (cmb2)
   {
    v1 = get_astro_value(curr_datetime,pl2a,pl2b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {ExtMapBuffer2[shift+FutureBars]=Bid+ctr*10*Point;ctr++;}
   }
   if (cmb3)
   {
    v1 = get_astro_value(curr_datetime,pl3a,pl3b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {ExtMapBuffer3[shift+FutureBars]=Bid+ctr*10*Point;ctr++;}
   }
   if (cmb4)
   {
    v1 = get_astro_value(curr_datetime,pl4a,pl4b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {ExtMapBuffer4[shift+FutureBars]=Bid+ctr*10*Point;ctr++;}
   }
   if (cmb5)
   {
    v1 = get_astro_value(curr_datetime,pl5a,pl5b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {ExtMapBuffer5[shift+FutureBars]=Bid+ctr*10*Point;ctr++;}
   }
   if (cmb6)
   {
    v1 = get_astro_value(curr_datetime,pl6a,pl6b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {ExtMapBuffer6[shift+FutureBars]=Bid+ctr*10*Point;ctr++;}
   }
   if (cmb7)
   {
    v1 = get_astro_value(curr_datetime,pl7a,pl7b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {ExtMapBuffer7[shift+FutureBars]=Bid+ctr*10*Point;ctr++;}
   }
   if (cmb8)
   {
    v1 = get_astro_value(curr_datetime,pl8a,pl8b);
    if (MathAbs(MathRound(v1*10)-MathRound(dg1*10))<=prec) {ExtMapBuffer8[shift+FutureBars]=Bid+ctr*10*Point;ctr++;}
   }
     shift = shift - 1;
     curr_datetime = curr_datetime + period_;
   }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

double get_astro_value(datetime d,int Body, int Body2)
{
   double longtitude, latitude;
   double longtitude2, latitude2;
   double distance, distance2;
   double speed;

   astro_swe_calc(d, Body, iflag, longtitude, latitude, distance, speed);
   astro_swe_calc(d, Body2, iflag, longtitude2, latitude2, distance2, speed);
   return(func_P(longtitude,longtitude2));
}


