//+------------------------------------------------------------------+
//|                                                    Toothman1.mq4 |
//|                                         Copyright © 2011, zznbrm |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, zznbrm"

//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <WinUser32.mqh>
#include <LibOrderReliable4.mqh>

//+------------------------------------------------------------------+
//| Constants                                                        |
//+------------------------------------------------------------------+
#define cVERSION              "1.0"
#define cPROGRAM_ID           201

#define cSTATUS_INIT          0
#define cSTATUS_IDLE_SHORT    1
#define cSTATUS_IDLE_LONG     2
#define cSTATUS_PEND_SHORT    3
#define cSTATUS_PEND_LONG     4
#define cSTATUS_TRADE_SHORT   5
#define cSTATUS_TRADE_LONG    6

#define cTREND_NONE           0  
#define cTREND_LONG           1
#define cTREND_SHORT          2

//+------------------------------------------------------------------+
//| User Inputs                                                      |
//+------------------------------------------------------------------+
extern double edblMaxHighLowPips = 40.0;
extern double edblMaxBodyPct = 50.0;
extern bool eblnEcn = true;
extern double edblLots = 0.1;
extern int eintSlippage = 0;

//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
datetime gdtHoldBar, gdtPendBar, gdtOpen;
double   gdblTruePoint, gdblPendOpen, gdblPendSl, gdblOpenPrice, gdblSl, gdblSlRange, gdblTp;
double   gdblTrigBarPips, gdblTrigBodyPips, gdblTrigBodyPct, gdblProfit;
int      gintStrategyStatus, gintErrCount, gintTrend, gintTicket;
int      gintTickCount, gintCurrSpeed, gintMaxSpeed; 
string   gstrTrigHL;
int gintStatusWidth = 39;
int gintStatusFont = 8;
string gstrStatusFont = "Lucida Console"; //"Courier New";

//*********************************************************************************************
//* STANDARD FUNCTIONS BELOW                                                                  *
//*********************************************************************************************
//+------------------------------------------------------------------+
//| init()                                                           |
//+------------------------------------------------------------------+
int init()
{     
   gintStrategyStatus = cSTATUS_INIT;
   return( 0 );
}

//+------------------------------------------------------------------+
//| deinit()                                                         |
//+------------------------------------------------------------------+
int deinit()
{   
   persistData();
   deleteAll();
   return( 0 );
}

//+------------------------------------------------------------------+
//| start()                                                          |
//+------------------------------------------------------------------+
int start()
{  
   int intStart = GetTickCount();
   
   // Check if we have too many errors
   if ( gintErrCount > 3 )
   {
      Print( "Too many errors!!!!" );
      //Send Email
      genShutdownEA();
   }
   
   // Check if initialization is required
   if ( gintStrategyStatus == cSTATUS_INIT )
   {
      if ( !doInit() )     
      {
         Print( "doInit failed!!!!" );
         genShutdownEA();
      }
   }
      
   // Process new tick
   doTick();
   
   // Process new bar
   if ( gdtHoldBar < Time[0] ) 
   {
      gdtHoldBar = Time[0];    
      doNewBar();
   }
   
   gintTickCount++;
   gintCurrSpeed = GetTickCount() - intStart;
   gintMaxSpeed = MathMax( gintMaxSpeed, gintCurrSpeed );
   updDisplay();   
   return( 0 );
}

//*********************************************************************************************
//* MAIN PROCESSING FUNCTIONS BELOW                                                           *
//*********************************************************************************************
//+------------------------------------------------------------------+
//| doInit()                                                         |
//+------------------------------------------------------------------+
bool doInit()
{
   deleteAll();
   gintErrCount = 0;
   gintMaxSpeed = 0;
   gintCurrSpeed = 0;
   gintTickCount = 0;
   gdtHoldBar = Time[0];
   gdblTruePoint = genTruePoint();
   gdblPendOpen = 0.0;
   gdblPendSl = 0.0;
   O_R_Config_use2step( eblnEcn );
   setTrend();
   
   initDisplay();
      
   gintTicket = GlobalVariableGet( cPROGRAM_ID + "-" + Symbol() + "-Tkt" );
   gdblSlRange = GlobalVariableGet( cPROGRAM_ID + "-" + Symbol() + "-SLRange" );
      
   if ( checkOpenTrade() )
   {
      drawHL( cPROGRAM_ID + "-Open", gdblOpenPrice, Khaki, STYLE_DOT );
      drawHL( cPROGRAM_ID + "-SL", gdblSl, Red, STYLE_DOT );
      drawHL( cPROGRAM_ID + "-TP", gdblTp, Lime, STYLE_DOT );
      drawVL( cPROGRAM_ID + "-OpenBar", gdtOpen, DimGray, STYLE_DOT );
   }
   else
   {      
      if ( gintTrend == cTREND_LONG )        setStatus( cSTATUS_IDLE_LONG );
      else if ( gintTrend == cTREND_SHORT )  setStatus( cSTATUS_IDLE_SHORT );
   }
   
   return( true );
}

//+------------------------------------------------------------------+
//| doNewBar()                                                       |
//+------------------------------------------------------------------+
bool doNewBar()
{
   setTrend();
   
   if ( gintStrategyStatus == cSTATUS_IDLE_SHORT )
   {
      if ( gintTrend == cTREND_LONG )     setStatus( cSTATUS_IDLE_LONG );
      else
      {
         setBarStats( 1 );
         
         if ( isTrigger() )
         {
            setStatus( cSTATUS_PEND_SHORT );
            gdblPendOpen = NormalizeDouble( Low[1] - gdblTruePoint, Digits );
            gdblPendSl = NormalizeDouble( High[1], Digits );
            gdtPendBar = Time[1];
            drawHL( cPROGRAM_ID + "-PendPrice", gdblPendOpen, Khaki, STYLE_DOT );
            drawVL( cPROGRAM_ID + "-TriggerBar", Time[1], DimGray, STYLE_DOT );
         }  
         else
         {
            setBarStats( 0 );
         }    
      }
   }
   else if ( gintStrategyStatus == cSTATUS_IDLE_LONG )
   {
      if ( gintTrend == cTREND_SHORT )     setStatus( cSTATUS_IDLE_SHORT );
      else
      {
         setBarStats( 1 );
         
         if ( isTrigger() )
         {
            setStatus( cSTATUS_PEND_LONG );
            gdblPendOpen = NormalizeDouble( High[1] + gdblTruePoint, Digits );
            gdblPendSl = NormalizeDouble( Low[1], Digits );
            gdtPendBar = Time[1];
            drawHL( cPROGRAM_ID + "-PendPrice", gdblPendOpen, Khaki, STYLE_DOT );
            drawVL( cPROGRAM_ID + "-TriggerBar", Time[1], DimGray, STYLE_DOT );
         }
         else
         {
            setBarStats( 0 );
         }
      }        
   }
   else if ( gintStrategyStatus == cSTATUS_PEND_SHORT )
   {
      if ( iBarShift( Symbol(), Period(), gdtPendBar, true ) >= 4 )
      {
         if ( gintTrend == cTREND_SHORT )       setStatus( cSTATUS_IDLE_SHORT );
         else if ( gintTrend == cTREND_LONG )   setStatus( cSTATUS_IDLE_LONG );
      }
      else
      {
         if ( gintTrend == cTREND_LONG )   setStatus( cSTATUS_IDLE_LONG );
      }     
   }
   else if ( gintStrategyStatus == cSTATUS_PEND_LONG )
   {
      if ( iBarShift( Symbol(), Period(), gdtPendBar, true ) >= 4 )
      {
         if ( gintTrend == cTREND_SHORT )       setStatus( cSTATUS_IDLE_SHORT );
         else if ( gintTrend == cTREND_LONG )   setStatus( cSTATUS_IDLE_LONG );
      }
      else
      {
         if ( gintTrend == cTREND_SHORT )   setStatus( cSTATUS_IDLE_SHORT );
      }     
   }
   
   return( true );
}

//+------------------------------------------------------------------+
//| doTick()                                                         |
//+------------------------------------------------------------------+
bool doTick()
{
   double dblSl;
   
   if ( isIdleStatus() )
   {
      setBarStats( 0 );
   }
   else if ( gintStrategyStatus == cSTATUS_PEND_SHORT )
   {
      if ( Bid <= gdblPendOpen )
      {
         dblSl = gdblPendSl + Ask - Bid;
         gintTicket = OrderSendReliable( Symbol(), OP_SELL, edblLots, Bid, eintSlippage, dblSl, 0.0,
                                         cPROGRAM_ID, cPROGRAM_ID );         
         
         if ( gintTicket >= 0 )
         {
            Sleep( 1000 );
            setStatus( cSTATUS_TRADE_SHORT );            
            OrderSelect( gintTicket, SELECT_BY_TICKET );
            gdblOpenPrice = OrderOpenPrice();
            gdblSl = OrderStopLoss();
            gdtOpen = OrderOpenTime();
            gdblSlRange = NormalizeDouble( gdblSl - gdblOpenPrice, Digits );
            gdblTp = gdblSl - (2.0*gdblSlRange);
            drawHL( cPROGRAM_ID + "-Open", gdblOpenPrice, Khaki, STYLE_DOT );
            drawHL( cPROGRAM_ID + "-SL", gdblSl, Red, STYLE_DOT );
            drawHL( cPROGRAM_ID + "-TP", gdblTp, Lime, STYLE_DOT );
            drawVL( cPROGRAM_ID + "-OpenBar", gdtOpen, DimGray, STYLE_DOT );
            persistData();
         }
         else   gintErrCount++;
      }
   }
   else if ( gintStrategyStatus == cSTATUS_PEND_LONG )
   {
      if ( Bid >= gdblPendOpen )
      {
         dblSl = gdblPendSl;
         gintTicket = OrderSendReliable( Symbol(), OP_BUY, edblLots, Ask, eintSlippage, dblSl, 0.0,
                                         cPROGRAM_ID, cPROGRAM_ID );
         
         if ( gintTicket >= 0 )
         {
            Sleep( 1000 );
            setStatus( cSTATUS_TRADE_LONG );            
            OrderSelect( gintTicket, SELECT_BY_TICKET );
            gdblOpenPrice = OrderOpenPrice();
            gdblSl = OrderStopLoss();
            gdtOpen = OrderOpenTime();
            gdblSlRange = NormalizeDouble( gdblOpenPrice - gdblSl, Digits );
            gdblTp = gdblSl + (2.0*gdblSlRange); 
            drawHL( cPROGRAM_ID + "-Open", gdblOpenPrice, Khaki, STYLE_DOT );
            drawHL( cPROGRAM_ID + "-SL", gdblSl, Red, STYLE_DOT );
            drawHL( cPROGRAM_ID + "-TP", gdblTp, Lime, STYLE_DOT );
            drawVL( cPROGRAM_ID + "-OpenBar", gdtOpen, DimGray, STYLE_DOT );           
            persistData();
         }
         else   gintErrCount++;
      }
   }
   else if ( gintStrategyStatus == cSTATUS_TRADE_SHORT )
   {
      if ( checkOpenTrade() )
      {
         if ( Ask <= gdblTp )
         {
            if ( OrderModifyReliable( gintTicket, Ask, gdblSl - gdblSlRange, 0.0, 0 ) )
            {
               gdblSl -= gdblSlRange;
               gdblTp -= gdblSlRange;
               drawHL( cPROGRAM_ID + "-SL", gdblSl, Red, STYLE_DOT );
               drawHL( cPROGRAM_ID + "-TP", gdblTp, Lime, STYLE_DOT );               
            }
            else
            {
               gintErrCount++;
            }       
         }
      }
      else
      {
         if ( gintTrend == cTREND_SHORT )       setStatus( cSTATUS_IDLE_SHORT );
         else if ( gintTrend == cTREND_LONG )   setStatus( cSTATUS_IDLE_LONG );
      }
   }
   else if ( gintStrategyStatus == cSTATUS_TRADE_LONG )
   {
      if ( checkOpenTrade() )
      {
         if ( Bid >= gdblTp )
         {
            if ( OrderModifyReliable( gintTicket, Bid, gdblSl + gdblSlRange, 0.0, 0 ) )
            {
               gdblSl += gdblSlRange;
               gdblTp += gdblSlRange;
               drawHL( cPROGRAM_ID + "-SL", gdblSl, Red, STYLE_DOT );
               drawHL( cPROGRAM_ID + "-TP", gdblTp, Lime, STYLE_DOT );               
            }
            else
            {
               gintErrCount++;
            }      
         }
      }
      else
      {
         if ( gintTrend == cTREND_SHORT )       setStatus( cSTATUS_IDLE_SHORT );
         else if ( gintTrend == cTREND_LONG )   setStatus( cSTATUS_IDLE_LONG );
      }
   }
   
   return( true );
}

//*********************************************************************************************
//* UTILITY FUNCTIONS BELOW                                                                   *
//*********************************************************************************************
//+------------------------------------------------------------------+
//| setTrend()                                                       |
//+------------------------------------------------------------------+
bool setTrend()
{
   double dblMa = NormalizeDouble( iMA( Symbol(), Period(), 50, 0, MODE_SMA, PRICE_CLOSE, 1 ), Digits );
      
   if ( Close[1] > dblMa )        gintTrend = cTREND_LONG;
   else if ( Close[1] < dblMa )   gintTrend = cTREND_SHORT;
   else
   {
      if ( gintTrend == cTREND_NONE )   gintTrend = cTREND_LONG;
   }        

   return( true );
}  

//+------------------------------------------------------------------+
//| setStatus()                                                      |
//+------------------------------------------------------------------+
void setStatus( int intStatus )
{
   bool blnChg = ( intStatus != gintStrategyStatus );
   gintStrategyStatus = intStatus;
   
   if ( blnChg )
   {
      deleteLines();
   }
}

//+------------------------------------------------------------------+
//| setBarStats()                                                    |
//+------------------------------------------------------------------+
bool setBarStats( int inx )
{
   gdblTrigBarPips = NormalizeDouble( ( High[inx] - Low[inx] ) / gdblTruePoint, 1 );
   gdblTrigBodyPips = NormalizeDouble( MathAbs( Open[inx] - Close[inx] ) / gdblTruePoint, 1 );
   
   if ( gdblTrigBarPips > 0.0 )   gdblTrigBodyPct = NormalizeDouble( 100.0 * gdblTrigBodyPips / gdblTrigBarPips, 1 );
   else                           gdblTrigBodyPct = 100.0;
   
   gstrTrigHL = "No";
   
   if ( gintStrategyStatus == cSTATUS_IDLE_SHORT )
   {
      // Check for LH and LL
      if ( ( High[inx] < High[inx+1] ) && ( Low[inx] < Low[inx+1] ) )     gstrTrigHL = "Yes";
   }
   else if ( gintStrategyStatus == cSTATUS_IDLE_LONG )
   {
      // Check for HH and HL
      if ( ( High[inx] > High[inx+1] ) && ( Low[inx] > Low[inx+1] ) )     gstrTrigHL = "Yes";
   }
   
   return( true );
} 

//+------------------------------------------------------------------+
//| isTrigger()                                                      |
//+------------------------------------------------------------------+
bool isTrigger()
{
   // Check the High and Low
   if ( gstrTrigHL != "Yes" )    return( false );
   
   // Check candle size
   if ( gdblTrigBarPips > edblMaxHighLowPips )     return( false );
   
   // Check candle body size
   if ( gdblTrigBodyPct > edblMaxBodyPct )     return( false );

   return( true );
} 

//+------------------------------------------------------------------+
//| genTruePoint()                                                   |
//+------------------------------------------------------------------+
double genTruePoint()
{
   double dblPoint;
      
   // Reset point value for 5 digit brokers
   switch( Digits )
   {
      case 5:
      case 4:     dblPoint = 0.0001;   break;
      case 3:
      case 2:     dblPoint = 0.01;     break;
      case 1:     dblPoint = 0.1;      break;
      case 0:     dblPoint = 0.0;      break;
      default:    dblPoint = 0.0001;
   }
   
   return( dblPoint );
}

//+------------------------------------------------------------------+
//| checkOpenTrade()                                                 |
//+------------------------------------------------------------------+
bool checkOpenTrade()
{
   if ( gintTicket >= 0 )
   {
      for ( int inx = 0; inx < 3; inx++ )
      {
         if ( OrderSelect( gintTicket, SELECT_BY_TICKET ) )
         {
            if ( ( Symbol() == OrderSymbol() ) && ( OrderMagicNumber() == cPROGRAM_ID ) && ( OrderCloseTime() == 0 ) )    
            {
               if ( OrderType() == OP_BUY )          
               {
                  setStatus( cSTATUS_TRADE_LONG );
                  gdblOpenPrice = OrderOpenPrice();
                  gdblSl = OrderStopLoss();
                  gdblProfit = OrderProfit() + OrderSwap() + OrderCommission();
                  gdtOpen = OrderOpenTime();
                  gdblTp = gdblSl + (2.0*gdblSlRange);
                  return( true );
               }
               else if ( OrderType() == OP_SELL )    
               {
                  setStatus( cSTATUS_TRADE_SHORT );
                  gdblOpenPrice = OrderOpenPrice();
                  gdblSl = OrderStopLoss();
                  gdblProfit = OrderProfit() + OrderSwap() + OrderCommission();
                  gdtOpen = OrderOpenTime();
                  gdblTp = gdblSl - (2.0*gdblSlRange);
                  return( true );
               }
            }
         }
         
         Sleep( 500 );
      }
   }
   
   gintTicket = -1;
   gdblOpenPrice = 0.0;
   gdblSl = 0.0; 
   gdblSlRange = 0.0;
   gdblProfit = 0.0;
   persistData();     
   return( false );
}

//+------------------------------------------------------------------+
//| persistData()                                                    |
//+------------------------------------------------------------------+
bool persistData()
{ 
   GlobalVariableSet( cPROGRAM_ID + "-" + Symbol() + "-Tkt", gintTicket );
   GlobalVariableSet( cPROGRAM_ID + "-" + Symbol() + "-SLRange", gdblSlRange );
   return( true );
}

//+------------------------------------------------------------------+
//| isPendStatus()                                                   |
//+------------------------------------------------------------------+
bool isPendStatus()
{ 
   return( gintStrategyStatus == cSTATUS_PEND_SHORT || gintStrategyStatus == cSTATUS_PEND_LONG );
}

//+------------------------------------------------------------------+
//| isTradeStatus()                                                  |
//+------------------------------------------------------------------+
bool isTradeStatus()
{ 
   return( gintStrategyStatus == cSTATUS_TRADE_SHORT || gintStrategyStatus == cSTATUS_TRADE_LONG );
}

//+------------------------------------------------------------------+
//| isIdleStatus()                                                   |
//+------------------------------------------------------------------+
bool isIdleStatus()
{ 
   return( gintStrategyStatus == cSTATUS_IDLE_SHORT || gintStrategyStatus == cSTATUS_IDLE_LONG );
}

//+------------------------------------------------------------------+
//| genShutdownEA()                                                  |
//+------------------------------------------------------------------+
void genShutdownEA()
{
   int intWindow = WindowHandle( Symbol(), Period() );
      
   if ( intWindow <= 0 )   Print( "shutdownEA() - Unable to get window handle !!!!" );
   else                    PostMessageA( intWindow, WM_COMMAND, 33050, 0 );
}

//*********************************************************************************************
//* DRAWING FUNCTIONS BELOW                                                                   *
//*********************************************************************************************
//+------------------------------------------------------------------+
//| drawHL()                                                         |
//+------------------------------------------------------------------+
void drawHL( string strName, double dblPrice, color clr, int intStyle )
{      
   if ( ObjectFind( strName ) == -1 )
   {
      ObjectCreate( strName, OBJ_HLINE, 0, TimeCurrent(), dblPrice );
   } 
   else
   {
      ObjectMove( strName, 0, TimeCurrent(), dblPrice );
   } 
   
   ObjectSet( strName, OBJPROP_BACK, true );
   ObjectSet( strName, OBJPROP_STYLE, intStyle );
   ObjectSet( strName, OBJPROP_COLOR, clr );
}

//+------------------------------------------------------------------+
//| drawVL()                                                         |
//+------------------------------------------------------------------+
void drawVL( string strName, datetime dt, color clr, int intStyle )
{      
   if ( ObjectFind( strName ) == -1 )
   {
      ObjectCreate( strName, OBJ_VLINE, 0, dt, Bid );
   } 
   else
   {
      ObjectMove( strName, 0, dt, Bid );
   } 
   
   ObjectSet( strName, OBJPROP_BACK, true );
   ObjectSet( strName, OBJPROP_STYLE, intStyle );
   ObjectSet( strName, OBJPROP_COLOR, clr );
}

//+------------------------------------------------------------------+
//| deleteAll()                                                      |
//+------------------------------------------------------------------+
void deleteAll()
{
   int intTotal = ObjectsTotal();   
   string strName;
   string strId = cPROGRAM_ID;
   
   for ( int inx = intTotal - 1; inx >= 0; inx-- )
   {
      strName = ObjectName( inx );
      
      if ( StringFind( strName, strId ) == 0 )    ObjectDelete( strName );
   }
}

//+------------------------------------------------------------------+
//| deleteLines()                                                    |
//+------------------------------------------------------------------+
void deleteLines()
{
   int intTotal = ObjectsTotal();  
   int intType; 
   string strName;
   string strId = cPROGRAM_ID;
   
   for ( int inx = intTotal - 1; inx >= 0; inx-- )
   {      
      strName = ObjectName( inx );
      intType = ObjectType( strName );
      
      if ( ( intType == OBJ_HLINE ) || ( intType == OBJ_VLINE ) )
      {     
         if ( StringFind( strName, strId ) == 0 )    ObjectDelete( strName );
      }
   }
}

//+------------------------------------------------------------------+
//| initDisplay()                                                    |
//+------------------------------------------------------------------+
void initDisplay()
{
   // Draw status background
   drawLabel( cPROGRAM_ID + "-0", "g", "", 250, 26, 270.0, Navy, 200, "Webdings", 0, false );
   
   // Draw static program data
   drawLabel( cPROGRAM_ID + "-PgmName", "Toothman1 v" + cVERSION, "", 5, 28, 0.0, White, 9, gstrStatusFont, 0, false );  
   drawLabel( cPROGRAM_ID + "-MagicNbr", "Magic Nbr", cPROGRAM_ID, 5, 43, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );  
   drawLabel( cPROGRAM_ID + "-Lots", "Lots", DoubleToStr( edblLots, 2 ), 5, 56, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
   drawLabel( cPROGRAM_ID + "-LotSize", "Lot Size", DoubleToStr( MarketInfo( Symbol(), MODE_LOTSIZE ), 0 ), 5, 69, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
   drawLabel( cPROGRAM_ID + "-MaxTrigger", "Max Trigger Pips", DoubleToStr( edblMaxHighLowPips, 1 ), 5, 82, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
   drawLabel( cPROGRAM_ID + "-MaxBody", "Max Trigger Body %", DoubleToStr( edblMaxBodyPct, 1 ) + "%", 5, 95, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
   // 108
   drawLabel( cPROGRAM_ID + "-StartTime", "Start Time", TimeToStr( TimeCurrent(), TIME_DATE|TIME_SECONDS ), 5, 121, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
}

//+------------------------------------------------------------------+
//| updDisplay()                                                     |
//+------------------------------------------------------------------+
void updDisplay()
{
   string strTemp = gintCurrSpeed + " - " + gintMaxSpeed;
   
   // Draw dynamic program data
   drawLabel( cPROGRAM_ID + "-ProcTime", "Process Time", TimeToStr( TimeCurrent(), TIME_DATE|TIME_SECONDS ), 5, 134, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
   drawLabel( cPROGRAM_ID + "-Tick", "Ticks", gintTickCount, 5, 147, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
   drawLabel( cPROGRAM_ID + "-Speed", "Process Speed", strTemp, 5, 160, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
   drawLabel( cPROGRAM_ID + "-Errors", "Errors", gintErrCount, 5, 173, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
   // 186
   drawLabel( cPROGRAM_ID + "-Status", "Status", getStatusStr(), 5, 199, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false ); 
   
   if ( isPendStatus() )
   {
      drawLabel( cPROGRAM_ID + "-Detail1", "Trigger Pips", DoubleToStr( gdblTrigBarPips, 1 ), 5, 212, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      drawLabel( cPROGRAM_ID + "-Detail2", "Trigger Body Pips", DoubleToStr( gdblTrigBodyPips, 1 ), 5, 225, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      drawLabel( cPROGRAM_ID + "-Detail3", "Trigger Body %", DoubleToStr( gdblTrigBodyPct, 1 ) + "%", 5, 238, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      drawLabel( cPROGRAM_ID + "-Detail4", "Pend Open Price", DoubleToStr( gdblPendOpen, Digits ), 5, 251, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
   }
   else if ( isTradeStatus() )
   {
      drawLabel( cPROGRAM_ID + "-Detail1", "Ticket", gintTicket, 5, 212, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      drawLabel( cPROGRAM_ID + "-Detail2", "Open Price", DoubleToStr( gdblOpenPrice, Digits ), 5, 225, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      drawLabel( cPROGRAM_ID + "-Detail3", "SL Price", DoubleToStr( gdblSl, Digits ), 5, 238, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      drawLabel( cPROGRAM_ID + "-Detail4", "TP Price", DoubleToStr( gdblTp, Digits ), 5, 251, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
   }
   else if ( isIdleStatus() )
   {
      drawLabel( cPROGRAM_ID + "-Detail1", "Curr Bar Pips", DoubleToStr( gdblTrigBarPips, 1 ), 5, 212, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      drawLabel( cPROGRAM_ID + "-Detail2", "Curr Body Pips", DoubleToStr( gdblTrigBodyPips, 1 ), 5, 225, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      drawLabel( cPROGRAM_ID + "-Detail3", "Curr Body %", DoubleToStr( gdblTrigBodyPct, 1 ) + "%", 5, 238, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      
      if ( gintStrategyStatus == cSTATUS_IDLE_LONG )
         drawLabel( cPROGRAM_ID + "-Detail4", "Curr HL HH", gstrTrigHL, 5, 251, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
      else if ( gintStrategyStatus == cSTATUS_IDLE_SHORT )
         drawLabel( cPROGRAM_ID + "-Detail4", "Curr LH LL", gstrTrigHL, 5, 251, 0.0, DarkGray, gintStatusFont, gstrStatusFont, 0, false );
   } 
}

//+------------------------------------------------------------------+
//| drawLabel()                                                      |
//+------------------------------------------------------------------+
void drawLabel( string lblName, string str1, string str2, int xc, int yc, double dblAngle, color textColor, 
                int textSize, string fontName, int corner=0, bool bg=false )
{            
   if ( ObjectFind( lblName ) == -1 )
   {
      ObjectCreate( lblName, OBJ_LABEL, 0, 0, 0 );      
      ObjectSet( lblName, OBJPROP_XDISTANCE, xc );
      ObjectSet( lblName, OBJPROP_YDISTANCE, yc );  
      ObjectSet( lblName, OBJPROP_CORNER, corner );    
      ObjectSet( lblName, OBJPROP_BACK, bg ); 
      ObjectSet( lblName, OBJPROP_ANGLE, dblAngle );  
   }
   
   string strText = str1;
   
   if ( str2 != "" ) strText = fmtLabel( str1, str2 );
         
   ObjectSetText( lblName, strText, textSize, fontName, textColor );
}

//+------------------------------------------------------------------+
//| fmtLabel()                                                       |
//+------------------------------------------------------------------+
string fmtLabel( string str1, string str2 )
{
   int intLen1 = StringLen( str1 );
   int intLen2 = StringLen( str2 );   
   string strSpaces = "                                                          ";
   return( str1 + StringSubstr( strSpaces, 0, gintStatusWidth-intLen1-intLen2 ) + str2 );
}

//+------------------------------------------------------------------+
//| getStatusStr()                                                   |
//+------------------------------------------------------------------+
string getStatusStr()
{
   string strMsg;
   
   switch( gintStrategyStatus )
   {
      case  cSTATUS_IDLE_SHORT:   strMsg = "Idle Short";            break;
      case  cSTATUS_IDLE_LONG:    strMsg = "Idle Long";             break;
      case  cSTATUS_PEND_SHORT:   strMsg = "Pending Short Trade";   break;
      case  cSTATUS_PEND_LONG:    strMsg = "Pending Long Trade";    break;
      case  cSTATUS_TRADE_SHORT:  strMsg = "Short Trade";           break;
      case  cSTATUS_TRADE_LONG:   strMsg = "Long Trade";            break;
      default:                    strMsg = "Init";                  break;
   }
   
   return( strMsg );
}

   




      
      














