//+------------------------------------------------------------------+
//|                            _TRO_IND2_SW                          |
//|   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/" 

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 2
#property indicator_buffers 2
#property indicator_color2 Magenta
#property indicator_color1 Turquoise

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

  

extern string myFont       = "Arial Bold" ;
extern int    myFontSize   = 20 ;
extern int    xStart       = 0 ;
extern int    xOffSet      = 0 ;

//---- parameters
extern int       MAPeriod         = 5;
extern int       MAType           = 1;

extern int BarWidth = 0;

extern color BarColorUp   = Turquoise;
extern color BarColorDown = Magenta;
extern color TextColor    = White;

extern int MaxBars=500;
extern int myWingding = 110;// 112

double Gap = 1; // Gap between the lines of bars
//---- buffers

double buf1_up[];
double buf1_down[];



//---- variables

int    MAMode;
string strMAType;
double MA_Cur, MA_Prev;

string tTrade = "" ;   
color tColor    = White;
string tObjName01   ;
int      corner;
int      window; 
string   shortName ;
int      win ;  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   shortName  = MakeUniqueName("Ind2sw ("+MAPeriod+")","");
   tObjName01 = shortName +  "0" ;

   IndicatorShortName(shortName);  

//---- indicators
   SetIndexStyle(0,DRAW_ARROW,0,BarWidth,BarColorUp);
   SetIndexArrow(0,myWingding);
   SetIndexBuffer(0,buf1_up);
   SetIndexEmptyValue(0,0.0);
   
   SetIndexStyle(1,DRAW_ARROW,0,BarWidth,BarColorDown);
   SetIndexArrow(1,myWingding);
   SetIndexBuffer(1,buf1_down);
   SetIndexEmptyValue(1,0.0);

   
   SetIndexLabel(0,NULL);
   SetIndexLabel(1,NULL);

   
   IndicatorDigits(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);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+


int start()
  {
  
  win =    WindowFind(shortName);
bool sFirst = true ;

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

ObjectsDeleteAll( win, 21);
ObjectsDeleteAll( win, 22);
ObjectsDeleteAll( win, 23);
}  
   int counted_bars=IndicatorCounted();
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   int i,tf;
 
for(i=0;i<limit;i++)
{ 
      
MA_Cur  = iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,i);
MA_Prev = iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,i+1);

buf1_down[i]=EMPTY_VALUE;
buf1_up[i]  =EMPTY_VALUE; 

if (MA_Cur < MA_Prev){buf1_down[i] = 1;    }
else { buf1_up[i] = 1;    }

} // for 



MA_Cur  = iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,0);
MA_Prev = iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,1);


if (MA_Cur > MA_Prev){ tTrade = "Long " ; tColor = Turquoise ; }
else { tTrade = "Short " ; tColor = Magenta ;}



   ObjectCreate(tObjName01, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName01, tTrade+ DoubleToStr(MA_Cur,Digits), myFontSize, myFont, tColor );
   ObjectSet(tObjName01, OBJPROP_CORNER, 0);
   ObjectSet(tObjName01, OBJPROP_XDISTANCE, 550+price.x.offset); // 
   ObjectSet(tObjName01, OBJPROP_YDISTANCE, 0+price.y.offset); //  


   return(0);
  }
  

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

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

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




