//+------------------------------------------------------------------+ 
//|   TRO_Squiggly                                                   | 
//|                                                                  | 
//|   Copyright � 2009, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - THERUMPLEDONE@GMAIL.COM                               |  
//+------------------------------------------------------------------+ 
//| Use http://therumpledone.mbtrading.com/fx/ as your forex broker  |  
//| ...tell them therumpledone sent you!                             |  
//+------------------------------------------------------------------+ 
#property  copyright "Copyright � 2009, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 
 

#property indicator_chart_window
 

extern int myChartX   = 10 ;
extern int myChartY   = 400 ;  
  
extern int myCorner   = 1 ;
extern string myFont  = "Courier New" ;
extern int myFontSize = 12 ;
 

extern int myPeriod = 0 ;


extern string    notetype         = "0=SMA,1=EMA,2=SMMA,3=LWMA" ;
extern string    noteprice        = "0=CLOSE,1=OPEN,2=HIGH,3=LOW,4=MEDIAN,5=PP,6=WEIGHT" ; 

extern color colorHead   = Orange ;
extern color colorUP = Lime ;
extern color colorEQ = Yellow ;
extern color colorDN = Red ;


extern int myRows     = 11 ;

extern int       MAType   = 1;
extern int       MAPrice  = PRICE_CLOSE ;


extern int iMovAvg0= 5;
 
extern int iMovAvg1= 10;
 
extern int iMovAvg2= 20;
 
extern int iMovAvg3= 30;
 
extern int iMovAvg4= 40;
 
extern int iMovAvg5= 50;
 
extern int iMovAvg6= 60;
 
extern int iMovAvg7= 80;
 
extern int iMovAvg8= 100;
 
extern int iMovAvg9= 200;
 
extern int iMovAvg10 = 400;
 


double Hi, Li, C;

double prev_high, prev_low, prev_close, D, p, value ;
int h  ;
string lbl[15], lbl2[15];
int n, j, i, shift, stop, nHH, nLL  ;

color pColor = Blue ,  maColor = Blue ;

string symbol, tChartPeriod, shortName, ShortName ;  

int    digits2, digits, period, win ; 
double    H1L0, L1H0 ;

double point,  open, high, low, close, mid, range, pClose, pOpen, pHigh, pLow, pMid, pRange, cRange, xValue, diff  ;  
double xMax = - 1,  xMin = 9999999999999, xDiff, xClose, xHigh, xLow, xRunUp, xRunDn, xPain ;
double High0, Low0, Open0,  Close0, Open1,High1, Low1, Close1   ;  
 

int FillAmt = 11 , x001, x002, x003, x004, x005, x006, x007 ;   
 
//+------------------------------------------------------------------+  
int aMovAvg[];   

double aValue[], pValue[]; 
 
 
  

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

int init()
  {
   if(myPeriod == 0) { period = Period() ; } else { period = myPeriod ; }  
      
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
   ShortName    =  "Squiggly"+symbol+period ;
   win          =  0 ;

 
   if(digits == 5 || digits == 3) { digits = digits - 1 ; point = point * 10 ; }   

   ArrayResize(aMovAvg,myRows) ;      
   ArrayResize(aValue,myRows) ; 
   ArrayResize(pValue,myRows) ;  
 
   shortName = "Squiggly" ; 
 
   ObDeleteObjectsByPrefix(shortName);
   

aMovAvg[0] = iMovAvg0 ;
aMovAvg[1] = iMovAvg1 ;
aMovAvg[2] = iMovAvg2 ;
aMovAvg[3] = iMovAvg3 ;
aMovAvg[4] = iMovAvg4 ;
aMovAvg[5] = iMovAvg5 ;
aMovAvg[6] = iMovAvg6 ;
aMovAvg[7] = iMovAvg7 ;
aMovAvg[8] = iMovAvg8 ;
aMovAvg[9] = iMovAvg9 ;
aMovAvg[10] = iMovAvg10 ;
 

if( myCorner == 0 || myCorner == 2 )
{ 
   x001 = 0 ;
   x002 = 0 ;
   x003 = 90 ; 
   x004 = 150;
   x005 = 100 ;
   x006 = 90 ;  
   x007 = 90 ;
   FillAmt = 20 ;     
}
else
{
   x001 = 0 ;
   x002 = 110 ; //  
   x003 = 40 ;  // price 
   x004 = 0 ; // diff
   x005 = 10 ;
   x006 = 10 ;  
   x007 = 10 ;  
   FillAmt = 20 ; 
}
           
   return(0);
  }


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

void ObDeleteObjectsByPrefix(string Prefix)
  {
   int L = StringLen(Prefix);
   int i = 0; 
   while(i < ObjectsTotal())
     {
       string ObjName = ObjectName(i);
       if(StringSubstr(ObjName, 0, L) != Prefix) 
         { 
           i++; 
           continue;
         }
       ObjectDelete(ObjName);
     }
  }  
//+------------------------------------------------------------------+
int deinit()
  {
       
   ObDeleteObjectsByPrefix(shortName);    
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
{
n       = 10;
j       = 0 ;

lbl[j]  = ShortName+j;
lbl2[j] = fFill(tChartPeriod + " Squiggly" , 19) ;
DoShowHead(j, colorHead);
 

close = iClose( symbol,period,0);

for( j = 0 ; j < myRows ; j++ )
{
   lbl[j]  = ShortName+j;
   DoCount( j , aMovAvg[j]) ;
   DoColor( aValue[j] , pValue[j] ) ;     
   maColor = pColor ;
   DoColor( close , aValue[j] ) ;  
   lbl2[j] = fFill("MA(" +  aMovAvg[j] + ") " , 8) ;   
   DoShow(j, pColor); 
}
 
return(0);
}

//+------------------------------------------------------------------+
  
void DoShowHead( int u, color dsColor )
{
string rlabelH = lbl[u] + "rcHead" ;
ObjectCreate(rlabelH,23,0,Time[0],PRICE_CLOSE);
ObjectSet(rlabelH, OBJPROP_CORNER, myCorner );
ObjectSet(rlabelH,OBJPROP_XDISTANCE, myChartX);
ObjectSet(rlabelH,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(rlabelH,lbl2[u],myFontSize,myFont,dsColor);

n = n+20;

return(0);
}

//+------------------------------------------------------------------+
  
void DoShow( int u, color dsColor )
{

ObjectCreate(lbl[u],23,0,Time[0],PRICE_CLOSE);
ObjectSet(lbl[u], OBJPROP_CORNER, myCorner );
ObjectSet(lbl[u],OBJPROP_XDISTANCE, x002+myChartX);
ObjectSet(lbl[u],OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(lbl[u],lbl2[u],myFontSize,myFont,maColor);

string Obj001 = lbl[u] + "val" ;
string sVal = fFill(DoubleToStr(aValue[u],digits), 7)    ;
ObjectCreate(Obj001,23,0,Time[0],PRICE_CLOSE);
ObjectSet(Obj001, OBJPROP_CORNER, myCorner );
ObjectSet(Obj001,OBJPROP_XDISTANCE, x003+myChartX);
ObjectSet(Obj001,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(Obj001,sVal,myFontSize,myFont,dsColor);


string Obj002 = lbl[u] + "dif" ;
string sDif =  rtadjust( DoubleToStr(diff,0) ) ;
ObjectCreate(Obj002,23,0,Time[0],PRICE_CLOSE);
ObjectSet(Obj002, OBJPROP_CORNER, myCorner );
ObjectSet(Obj002,OBJPROP_XDISTANCE, x004+myChartX);
ObjectSet(Obj002,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(Obj002,sDif,myFontSize,myFont,dsColor);

n = n+20;

return(0);

}

//+------------------------------------------------------------------+
  
void DoColor( double c1, double c2 )
{

if(c1 > c2 ) { pColor = colorUP ; } else {
if(c1 < c2 ) { pColor = colorDN ; } else { pColor = colorEQ ; } } 

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

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="MN";
   }
   return(tfs);
}

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

void DoCount( int c, int maPer) 
{

aValue[c] =  iMA(symbol,period,maPer,0,MAType,MAPrice,0);    
pValue[c] =  iMA(symbol,period,maPer,0,MAType,MAPrice,1);  

diff      =  (close - aValue[c]) / point ; 
} 

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

string fFill(string filled, int f ) 
{
   string FILLED ;
   
   FILLED = StringSubstr(filled + "                                         ",0,f) ;
   
return(FILLED);
}



//+------------------------------------------------------------------+
string rtadjust( string rString )
{
 
 int sl = StringLen(rString) ;
 
 while(true)
 {
 
   if( sl == 5) { break ; } 
   if( sl == 4) { rString = " " + rString ; break ; }    
   if( sl == 3) { rString = "  " + rString ; break ; } 
   if( sl == 2) { rString = "   " + rString ; break ; } 
   if( sl == 1) { rString = "    " + rString ; break ; } 
    
   break ;
 }

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

/*

Comment(

"total = " , total , "\n" ,   
"xValue " , DoubleToStr(xValue,Digits) , "\n" ,     
"") ; 

 
*/