//+------------------------------------------------------------------+ 
//|   _TRO_COMPASS_TREND                                             | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpledone@gmail.com                                        |  
//+------------------------------------------------------------------+ 
//+------------------------------------------------------------------+
//| Trend Signal  //| Ramdass - Conversion only
//+------------------------------------------------------------------+


#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.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 show.arrow = true ;
extern bool show.value = false ;
//extern int  myDecimals = 2 ;

extern string myFont       = "Arial Bold" ;
extern int    myFontSize   = 30 ;
extern int    myArrowSize  = 40 ;
extern int    myDigitSize  = 20 ;


extern color  myIndColor = DarkGray ;

extern int RISK=3;
extern int CountBars=50;

/*

PARM6 FOR MA = 

PRICE_CLOSE	0	Close price.
PRICE_OPEN	1	Open price.
PRICE_HIGH	2	High price.
PRICE_LOW	3	Low price.
PRICE_MEDIAN	4	Median price, (high+low)/2.
PRICE_TYPICAL	5	Typical price, (high+low+close)/3.
PRICE_WEIGHTED	6	Weighted close price, (high+low+close+close)/4.


PARM3 FOR MA = 
MODE_SMA	0	Simple moving average,
MODE_EMA	1	Exponential moving average,
MODE_SMMA	2	Smoothed moving average,
MODE_LWMA	3	Linear weighted moving average.



extern double iParm3 = 5;
extern double iParm4 = 0;
extern double iParm5 = 1;  
extern double iParm6 = 0;
extern double iParm7 = 0;
extern double iParm8 = 0;
*/

extern color N_color  = Lime;
extern color NE_color = Lime;
extern color E_color  = Yellow;
extern color SE_color = Red;
extern color S_color  = Red;



//---- 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, IndHead ;

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

double mid      ; 
double mid1    ; 


double pCount   ;

int   HighDir, LowDir, MidDir, Mid1Dir, CloseDir, ClOpDir ;
color HighColor, LowColor, MidColor, Mid1Color, CloseColor ;

string PriceDir   ;

string tPeriod   ; 

double cVal, pVal ;

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

//---- buffers
double val1[];
double val2[];



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 );
}

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

string fCompass( double i )
{
string rCompass ;

    if ( i == 2 ) rCompass = Arrow2  ; else {
    if ( i == 1 ) rCompass = Arrow2  ; else {
    if ( i == -1 ) rCompass = Arrow5  ; else {
    if ( i == -2 ) rCompass = Arrow5  ; else {      
    rCompass = Arrow3 ; } } } }
    
return ( rCompass );
}



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

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

        

   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 ) ;

   
}  


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

int start() 
  { 

int xStart  = -600 ;
int xOffSet = 100 ;
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 =  "TREND" + "("+RISK+")"  ;

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  )  
{
   
DoWork2(myPeriod) ;
                             
 

                                                                                                                                                                         
// pCount  = fDirection( cVal , pVal ) ;

CloseColor = fColor( cVal ) ;

PriceDir = fCompass( cVal ) ;

}

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

tObjName01 = labelNames +lPeriod +  "TND230" ;
tObjName02 = labelNames +lPeriod +  "TND231" ;
tObjName03 = labelNames +lPeriod +  "TND232" ;
//tObjName04 = labelNames +  "TND200" ;
tObjName05 = labelNames +lPeriod +  "TND235" ;

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

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

tObjName01 = labelNames +lPeriod +  "TND230" ;
tObjName02 = labelNames +lPeriod +  "TND231" ;
tObjName03 = labelNames +lPeriod +  "TND232" ;
tObjName05 = labelNames +lPeriod +  "TND235" ;


tPeriod  = TimeFrameToString( lPeriod ) ;

   ObjectCreate(tObjName01, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName01, tPeriod, myFontSize, myFont, CloseColor );
   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 ;

if(show.arrow){

sInc = 50 ;
   
   ObjectCreate(tObjName03, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName03, PriceDir , myArrowSize , "Wingdings" , CloseColor );
   ObjectSet(tObjName03, OBJPROP_CORNER, 0);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE, xInc+980+price.x.offset); // 765
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 210+price.y.offset); 
   
} //if(show.arrow)


if(show.value){
    
   ObjectCreate(tObjName05, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName05, DoubleToStr(cVal,Digits) ,myDigitSize, myFont , CloseColor );
   ObjectSet(tObjName05, OBJPROP_CORNER, 0);
   ObjectSet(tObjName05, OBJPROP_XDISTANCE, xInc+980+price.x.offset); // 765
   ObjectSet(tObjName05, OBJPROP_YDISTANCE, sInc+210+price.y.offset); 
} //if(show.value)



}

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

void DoHead( string dHead  )  
{

tObjName04 = labelNames +  "TND200" ;

   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);
}


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

void DoWork2( int TF    )
{

   if (CountBars>=1000) CountBars=950;

   int i,shift,counted_bars=IndicatorCounted();
   int Counter,i1,value10,value11;
   double value1,x1,x2;
   double value2,value3;
   double TrueCount,Range,AvgRange,MRO1,MRO2;
   double Table_value2[1000];
   
   value10=3+RISK*2;
   x1=67+RISK;
   x2=33-RISK;
   value11=value10;
//----
   if(Bars<=11+1) return(0);
//---- initial zero
   if(counted_bars<11+1)
   {
      for(i=1;i<=0;i++) val1[CountBars-i]=0.0;
      for(i=1;i<=0;i++) val2[CountBars-i]=0.0;
   }
//----
   shift=CountBars-11-1;
   while(shift>=0)
     {
     
   Counter=shift;
	Range=0.0;
	AvgRange=0.0;
	high  = iHigh(NULL,TF,Counter) ;
	low   = iLow(NULL,TF,Counter) ;	
	for (Counter=shift; Counter<=shift+9; Counter++) AvgRange=AvgRange+MathAbs(high-low);
		
	Range=AvgRange/10;
	Counter=shift;
	TrueCount=0;
	while (Counter<shift+9 && TrueCount<1)
	open  = iOpen(NULL,TF,Counter) ;
	close = iClose(NULL,TF,Counter+1) ;	
		{if (MathAbs(open -close )>=Range*2.0) TrueCount=TrueCount+1;
		Counter=Counter+1;
		}
	if (TrueCount>=1) {MRO1=Counter;} else {MRO1=-1;}
	Counter=shift;
	TrueCount=0;
	while (Counter<shift+6 && TrueCount<1)
	close  = iClose(NULL,TF,Counter+1) ;	
	close1 = iClose(NULL,TF,Counter+3) ;			
		{if (MathAbs(close1 -close )>=Range*4.6) TrueCount=TrueCount+1;
		Counter=Counter+1;
		}
	if (TrueCount>=1) {MRO2=Counter;} else {MRO2=-1;}
	if (MRO1>-1) {value11=3;} else {value11=value10;}
	if (MRO2>-1) {value11=4;} else {value11=value10;}
	value2=100-MathAbs(iWPR(NULL,0,value11,shift)); // PercentR(value11=9)
	Table_value2[shift]=value2;
	val1[shift]=0;
	val2[shift]=0;
	value3=0;
	if (value2<x2)
		{i1=1;
		while (Table_value2[shift+i1]>=x2 && Table_value2[shift+i1]<=x1){i1++;}
		if (Table_value2[shift+i1]>x1) 
			{
       	high  = iHigh(NULL,TF,shift) ;			
			value3=high+Range*0.5;
			val1[shift]=value3;
			} 
		}
	if (value2>x1)
		{i1=1;
		while (Table_value2[shift+i1]>=x2 && Table_value2[shift+i1]<=x1){i1++;}
		if (Table_value2[shift+i1]<x2) 
			{
       	low  = iLow(NULL,TF,shift) ;			
			value3=low-Range*0.5;
			val2[shift]=value3;
			}
		}
      
      shift--;
     }
} // DoWork2