//+------------------------------------------------------------------+ 
//|   _TRO_COMPASS_IND_MP_MAIC                                       | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpldone@gmail.com                                         |  
//+------------------------------------------------------------------+ 


#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpldone.com/" 

//---- indicator settings 
#property indicator_separate_window
#property indicator_buffers 0
//#property indicator_minimum 0
//#property indicator_maximum 1



//---- indicator parameters  
 

//extern int win = 1;
extern int price.x.offset= 0 ;//250
extern int price.y.offset= -160 ;//-160

extern bool my.alert.sound     =false ;
extern double my.alert.diff    = 7 ;

//extern int  myDecimals = 2 ;

extern string myFont       = "Arial Bold" ;
extern int    myFontSize   = 20 ;
extern int    myArrowSize  = 20 ;
extern int    myDigitSize  = 20 ;
extern int    xStart       = -800 ;
extern int    xOffSet      = 100 ;



extern int       MAPeriod =5;
extern string note1 = "Type: SMA=0, EMA=1, SMMA=2, LWMA=3";
extern int       MAType=1;
extern int       myLineWidth=3;


extern color myIndColor = DarkGray ;
extern color N_color  = Lime;
extern color NE_color = LimeGreen;
extern color E_color  = Gold;
extern color SE_color = Orange;
extern color S_color  = Red;


//---- indicator buffers 

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];


// int   Chart_Period    = PERIOD_H1;


//---- buffers

string Arrow12 = "ñ"; // 12 oclock
string Arrow2  = "ö";  // 2 oclock
string Arrow3  = "ð";  // 3 oclock
string Arrow5  = "ø";  // 5 oclock
string Arrow6  = "ò";  // 6 oclock


string tObjName01   ;
string tObjName02   ;
string tObjName03   ;
string tObjName04 , tObjName05, tObjName06, IndHead ;

double open    ;  
double close    ; 
double close1   ;  
double high    ;    
double high1    ;   
double low     ;    
double low1     ; 

double mid      ; 
double mid1    ; 


double pCount, qCount   ;


color  MaxColor, MinColor ;

string PriceDir   ;
string cDir, pDir ;
string tPeriod, cTrade, pTrade   ; 

double cHiCl, cClLo, cHi,  cLo,  cX, cVal , cVal1 ;
double pHiCl, pClLo, pHi,  pLo,  pX, pVal , pVal1 ;
double MaxVal  ;
double MinVal  ;
double pMaxVal  ;
double pMinVal  ;



//+------------------------------------------------------------------+
int      totalLabels;
string   labelNames;
int      corner;
int      window; 
string   shortName ;
int      win ;


//---- variables

int    MAMode;
string strMAType;

string tShortName ;

color ColorMA , pColorMA ;

   double MA_Cur, MA_Prev, MA_Diff;
   int limit = 2;
   
  
//+------------------------------------------------------------------+

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
   }
   return(tfs);
}

//+------------------------------------------------------------------+

color fColor( double i)
{
color rColor ;

    if ( i ==  2 ) rColor = N_color  ; else {
    if ( i ==  1 ) rColor = NE_color  ; else {
    if ( i == -1 ) rColor = SE_color  ; else {
    if ( i == -2 ) rColor = S_color  ; else {     
                   rColor = E_color ; } } } }

return ( rColor );
}
//+------------------------------------------------------------------+

int fDirection( double i, double j )
{
int rDir ;

    if( i > j  )   rDir = 2;  else 
    if( i < j  )   rDir = -2; else     
                   rDir = 0; 
    
return ( rDir );
}


//+------------------------------------------------------------------+


string fCompass( double i )
{
string rCompass ;

    if ( i == 2 ) rCompass = Arrow12  ; else {
    if ( i == -2 ) rCompass = Arrow6  ; else {      
    rCompass = Arrow3 ; } } 
    
return ( rCompass );
}

//+------------------------------------------------------------------+

double LSMA(int Rperiod, int shift)
{
   int i;
   double sum;
   int length;
   double lengthvar;
   double tmp;
   double wt;

   length = Rperiod;
 
   sum = 0;
   for(i = length; i >= 1  ; i--)
   {
     lengthvar = length + 1;
     lengthvar /= 3;
     tmp = 0;
     tmp = ( i - lengthvar)*Close[length-i+shift];
     sum+=tmp;
    }
    wt = sum*6/(length*(length+1));
    
    return(wt);
}
//+------------------------------------------------------------------+

int init() 
  { 
   shortName  = MakeUniqueName("MAIC ","");
   labelNames = shortName;
   IndicatorShortName(shortName);
   totalLabels     = 0;

switch (MAType)
   {
      case 1: strMAType="EMA"; MAMode=MODE_EMA; break;
      case 2: strMAType="SMMA"; MAMode=MODE_SMMA; break;
      case 3: strMAType="LWMA"; MAMode=MODE_LWMA; break;
      case 4: strMAType="LSMA"; break;
      default: strMAType="SMA"; MAMode=MODE_SMA; break;
   }

   return(0); 
  } 

int deinit() 
{ 
   while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
  
DoDelObj( PERIOD_M1 ) ;
DoDelObj( PERIOD_M5 ) ;
DoDelObj( PERIOD_M15 ) ;  
DoDelObj( PERIOD_H1 ) ;
DoDelObj( PERIOD_H4 ) ;
DoDelObj( PERIOD_D1 ) ;
DoDelObj( PERIOD_W1 ) ;
DoDelObj( PERIOD_MN1 ) ;

   
}  

//+------------------------------------------------------------------+ 
//| Range                                                            | 
//+------------------------------------------------------------------+ 
int start() 
  { 


int xStart2 = xStart + xOffSet ;
int xStart3 = xStart + xOffSet*2 ;
int xStart4 = xStart + xOffSet*3 ;
int xStart5 = xStart + xOffSet*4 ;
int xStart6 = xStart + xOffSet*5 ;
int xStart7 = xStart + xOffSet*6 ;
int xStart8 = xStart + xOffSet*7 ;

win =    WindowFind(shortName);

bool sFirst = true ;

if ( sFirst ) {
sFirst = false ;
ObjectDelete(tObjName04);

ObjectsDeleteAll( win, 21);
ObjectsDeleteAll( win, 22);
ObjectsDeleteAll( win, 23);
}

IndHead =  "MAIC " + "("+DoubleToStr(MAPeriod,0)+")"  ;

DoHead(IndHead );

DoWork( PERIOD_M1 ) ;
DoShow( PERIOD_M1, xStart ) ;

DoWork( PERIOD_M5 ) ;
DoShow( PERIOD_M5, xStart2 ) ;

DoWork( PERIOD_M15 ) ;
DoShow( PERIOD_M15, xStart3 ) ;                      

DoWork( PERIOD_H1 ) ;
DoShow( PERIOD_H1, xStart4 ) ;

DoWork( PERIOD_H4 ) ;
DoShow( PERIOD_H4, xStart5 ) ;

DoWork( PERIOD_D1 ) ;
DoShow( PERIOD_D1, xStart6 ) ;

DoWork( PERIOD_W1 ) ;
DoShow( PERIOD_W1, xStart7 ) ;

DoWork( PERIOD_MN1 ) ;
DoShow( PERIOD_MN1, xStart8 ) ;

WindowRedraw();    
 


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

void DoWork( int myPeriod  )  
{

   limit = 2;
   
   for(int i=limit; i>=0; i--)
   {
      if (MAType == 4)
      {
        MA_Cur = LSMA(MAPeriod,i);
        MA_Prev = LSMA(MAPeriod,i+1);
      }
      else
      {
        MA_Cur  = iMA(NULL,myPeriod,MAPeriod,0,MAMode,PRICE_CLOSE,i);
        MA_Prev = iMA(NULL,myPeriod,MAPeriod,0,MAMode,PRICE_CLOSE,i+1);
      }
 
         
//========== COLOR CODING ===========================================               
        
       ExtMapBuffer3[i] = MA_Cur; //red 
       ExtMapBuffer2[i] = MA_Cur; //green
       ExtMapBuffer1[i] = MA_Cur; //yellow
       
        pColorMA =  ColorMA ;
        pDir     =  cDir ;
        pTrade   =  cTrade ;
        
        if (MA_Prev > MA_Cur)
        {
        ExtMapBuffer2[i] = EMPTY_VALUE;
        ColorMA = Red ;
        cDir    = Arrow6 ;
        cTrade  = "Short" ;
        }
       else if (MA_Prev < MA_Cur) 
        {
        ExtMapBuffer1[i] = EMPTY_VALUE; //-1 red/greem tight
        ColorMA = Green ;
        cDir    = Arrow12 ;
        cTrade  = "Long" ;
        }
         else 
         {
         ColorMA = Yellow ;
         cDir    = Arrow3 ;
         cTrade  = "Wait" ;
         ExtMapBuffer1[i]=EMPTY_VALUE;//EMPTY_VALUE;
         ExtMapBuffer2[i]=EMPTY_VALUE;//EMPTY_VALUE;
         }
        
      } // for


  
}

//+------------------------------------------------------------------+ 
void DoDelObj( int lPeriod  )  
{

tObjName01 = labelNames +lPeriod +  "MAIC030" ;
tObjName02 = labelNames +lPeriod +  "MAIC031" ;
tObjName03 = labelNames +lPeriod +  "MAIC032" ;
//tObjName04 = labelNames +  "MAIC000" ;
tObjName05 = labelNames +lPeriod +  "MAIC035" ;
tObjName06 = labelNames +lPeriod +  "MAIC036" ; 

ObjectDelete(tObjName01);
ObjectDelete(tObjName02);
ObjectDelete(tObjName03);
ObjectDelete(tObjName04);
ObjectDelete(tObjName05); 
ObjectDelete(tObjName06); 
}

//+------------------------------------------------------------------+ 
void DoShow( int lPeriod , int xInc)  
{

tObjName01 = labelNames +lPeriod +  "MAIC030" ;
tObjName02 = labelNames +lPeriod +  "MAIC031" ;
tObjName03 = labelNames +lPeriod +  "MAIC032" ;
tObjName05 = labelNames +lPeriod +  "MAIC035" ;
tObjName06 = labelNames +lPeriod +  "MAIC036" ;

tPeriod  = TimeFrameToString( lPeriod ) ;

if(cDir != pDir) 
  {
  MA_Diff = MathAbs(Close[0] - MA_Cur)/Point ;
  
  
  if ( my.alert.sound 
  && MA_Diff <= my.alert.diff ) {Alert(Symbol(),", MAIC change ", tPeriod, " "+cTrade+" ", DoubleToStr(MA_Cur,Digits) ); }
  }


   ObjectCreate(tObjName01, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName01, tPeriod, myFontSize, myFont,  ColorMA );
   ObjectSet(tObjName01, OBJPROP_CORNER, 0);
   ObjectSet(tObjName01, OBJPROP_XDISTANCE,  xInc+980+price.x.offset); // 760
   ObjectSet(tObjName01, OBJPROP_YDISTANCE, 175+price.y.offset); // 160

int sInc = 0 ;

 

sInc = 50 ;
   
   ObjectCreate(tObjName03, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName03, cDir , myArrowSize , "Wingdings" ,  ColorMA );
   ObjectSet(tObjName03, OBJPROP_CORNER, 0);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE, xInc+1000+price.x.offset); // 1000
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 210+price.y.offset); 

  
   ObjectCreate(tObjName05, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName05, pDir , myArrowSize , "Wingdings" ,  pColorMA );
   ObjectSet(tObjName05, OBJPROP_CORNER, 0);
   ObjectSet(tObjName05, OBJPROP_XDISTANCE, xInc+980+price.x.offset); // 1000
   ObjectSet(tObjName05, OBJPROP_YDISTANCE, 210+price.y.offset); // 240
      

   
    
   ObjectCreate(tObjName06, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName06, DoubleToStr(MA_Cur,Digits) ,myDigitSize, myFont , ColorMA );
   ObjectSet(tObjName06, OBJPROP_CORNER, 0);
   ObjectSet(tObjName06, OBJPROP_XDISTANCE, xInc+980+price.x.offset); // 765
   ObjectSet(tObjName06, OBJPROP_YDISTANCE, sInc+210+price.y.offset); 
 
 

}
//+------------------------------------------------------------------+ 

void DoHead( string dHead  )  
{

tObjName04 = labelNames +  "MAIC000" ;

   ObjectCreate(tObjName04, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName04, dHead, myFontSize, myFont, myIndColor );
   ObjectSet(tObjName04, OBJPROP_CORNER, 0);
   ObjectSet(tObjName04, OBJPROP_XDISTANCE,  00+price.x.offset); // 760
   ObjectSet(tObjName04, OBJPROP_YDISTANCE, 210+price.y.offset); // 160
}

//+------------------------------------------------------------------+ 


// string next() { totalLabels++; return(totalLabels); }  

//+------------------------------------------------------------------+

 
string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

   while (WindowFind(result)> 0)
          result = first+(MathRand()%1001)+rest;
   return(result);
}

/*

val=High[iHighest(NULL,0,MODE_HIGH,20,4)];

Constant	Value	Description
MODE_OPEN	0	Open price.
MODE_LOW	   1	Low price.
MODE_HIGH	2	High price.
MODE_CLOSE	3	Close price.
MODE_VOLUME	4	Volume, used in iLowest() and iHighest() functions.
MODE_TIME	5	Bar open time, used in ArrayCopySeries() function.

*/