//+------------------------------------------------------------------+
//|                                                                  |
//|                                            TradeOnThisChart.mq4  |
//|                                                   2nd May 2013   |
//|                                                  First Version   |
//|                                                                  |
//|         Re:  http://www.forexfactory.com/showthread.php?t=424015 |
//|                                                                  |
//| ---------------------------------------------------------------- |
//|                                                                  |
//|                                                   Version 1.1    |
//|                                                   3rd May 2013   |
//|                                                                  |
//|         Re: http://www.forexfactory.com/showthread.php?p=6654675 |
//|                                                                  |
//|    Addeed input for label corner and window,  added arrow to     |
//|      show when trades were opened.                               |
//|                                                                  |
//+------------------------------------------------------------------+

#property copyright  "Copyright © 2013, RaptorUK"
#property indicator_chart_window

#define VERSION      "1.1"
#define INDICATOR_NAME     "TradeOnThisChart"


// -------  External variables  --------
extern string TradesOpenText = "Trades are open";
extern string TextFont = "none";

extern color TextColor = Yellow;    //  default colour for the symbol
extern int TextSize = 13;            //  default size of the symbol

extern color TradeMarkerColor = Yellow;

extern int Text_Window = 0;
extern int Text_Corner = 1;
extern int Text_X_Position = 20;
extern int Text_Y_Position = 20;

extern int MagicNumber = 0;               //  set other than zero to check by Magic Number

string LabelObjectName = "OpenTradesLabel";


// -------  Debugging and error reporting  -------
extern int Debug_Level=0 ;             // Set to a value from 0 to 5, 0 = no debugging (default), 5 = max debugging messages

bool D1, D2, D3, D4, D5;               // debug values used like this  if(D2) do . . . 

extern color Status_Color = White;


//+---------------------------------------------------------+
//| Custom  initialization function                         |
//+---------------------------------------------------------+
int init()
   {
   IndicatorShortName(StringConcatenate(INDICATOR_NAME, " ", VERSION));   
   
   Print(StringConcatenate(INDICATOR_NAME, " ", VERSION), " launched."); 
   
   D1=false; D2=false;D3=false;D4=false;D5=false;  // set debug level 
   switch(Debug_Level) {
      case 5: D5=true;
      case 4: D4=true;
      case 3: D3=true;
      case 2: D2=true;
      case 1: D1=true;
      }
   if (D2) Print("Debug status: Debug_Level= ", Debug_Level," (D1= ", D1, " D2= ", D2, " D3= ", D3, " D4=", D4, " D5= ", D5," )");

   CommentLab("");
      
   return(0);
   }
  
  
//+--------------------------------------------------------+
//| Custom deinitialization function                       |
//+--------------------------------------------------------+
int deinit()
   {

   Print("Thank you for traveling with us today . . your spread has been . . .", DoubleToStr(Ask - Bid, Digits));
   Print("You have been running: ", INDICATOR_NAME, " Version: ", VERSION );
   
   ObjectDelete(LabelObjectName);
   ClearTradeMarkers();
   
   CommentLab("");
   
   return(0);
   }


//+------------------------------------------------------------------+
//| Indicator iteration function                                     |
//+------------------------------------------------------------------+
int start()
   {
   int TextXPosition = 0, TextYPosition = 0;
   string LabelText, lTextFont;
   int Visibility = 0;
   
   CommentLab("");
   
   if(TextFont == "none") lTextFont = "Tahoma";
   
   if(CountTrades(MagicNumber) > 0)
      {
      Visibility = NULL;
      LabelText = TradesOpenText;
      PlaceTradeMarkers(MagicNumber);
      }
      
   else
      {
      Visibility = EMPTY;
      ClearTradeMarkers();
      }
      
   if(ObjectFind(LabelObjectName) == -1)
      {
      ObjectCreate(LabelObjectName, OBJ_LABEL, Text_Window, 0, 0);
      ObjectSet(LabelObjectName, OBJPROP_CORNER, Text_Corner);
      ObjectSet(LabelObjectName, OBJPROP_XDISTANCE, Text_X_Position);
      ObjectSet(LabelObjectName, OBJPROP_YDISTANCE, Text_Y_Position );
      ObjectSetText(LabelObjectName, LabelText, TextSize, lTextFont, TextColor ); 
      ObjectSet(LabelObjectName, OBJPROP_TIMEFRAMES, Visibility );
      }

   else
      {
      ObjectSetText(LabelObjectName, LabelText, TextSize, lTextFont, TextColor ); 
      ObjectSet(LabelObjectName, OBJPROP_TIMEFRAMES, Visibility );
      if(ObjectGet(LabelObjectName, OBJPROP_XDISTANCE) != Text_X_Position  || ObjectGet(LabelObjectName, OBJPROP_YDISTANCE) != Text_Y_Position )
         {
         CommentLab(StringConcatenate("Please set new X & Y position in Inputs: X ", 
            ObjectGet(LabelObjectName, OBJPROP_XDISTANCE), " Y ", ObjectGet(LabelObjectName, OBJPROP_YDISTANCE) ) );
         }
      }
       

   return(0);
   } // end of start
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| CountTrades function - counts open trades for MN and Symbol      |
//+------------------------------------------------------------------+
int CountTrades(int lMagicNumber)
   {
   int OrderPoolIndex = 0;
   int lOrderType = 0, TradeCount = 0;
   bool FunctionResult = true;
   
   for(OrderPoolIndex = OrdersTotal() - 1; OrderPoolIndex >= 0; OrderPoolIndex--)
      {
      if(!OrderSelect(OrderPoolIndex, SELECT_BY_POS, MODE_TRADES))
         {
         Print("CountTrades: OrderSelect failed ! ! OrderPoolIndex: ", OrderPoolIndex, " OrdersTotal: ", OrdersTotal() );
         OrderPoolIndex = OrdersTotal() - 1;
         continue;
         }

      if( ( (lMagicNumber > 0 && OrderMagicNumber() == lMagicNumber) || lMagicNumber == 0 ) &&
         OrderSymbol() == Symbol() )    
         {
         lOrderType = OrderType();
         
         if(lOrderType == OP_BUY || lOrderType == OP_SELL )
            {
            TradeCount++;
            }
         
         }
      }
      
   return(TradeCount);
   }
//+------------------------------------------------------------------+


//+---------------------------------------------------------------------------------+
//| PlaceTradeMarkers function - placees a marker under bar where trade was opened  |
//+---------------------------------------------------------------------------------+
void PlaceTradeMarkers(int lMagicNumber)
   {
   int OrderPoolIndex = 0;
   int lOrderType = 0;
   string OpenArrowName;

   
   for(OrderPoolIndex = OrdersTotal() - 1; OrderPoolIndex >= 0; OrderPoolIndex--)
      {
      if(!OrderSelect(OrderPoolIndex, SELECT_BY_POS, MODE_TRADES))
         {
         Print("CountTrades: OrderSelect failed ! ! OrderPoolIndex: ", OrderPoolIndex, " OrdersTotal: ", OrdersTotal() );
         OrderPoolIndex = OrdersTotal() - 1;
         continue;
         }

      if( ( (lMagicNumber > 0 && OrderMagicNumber() == lMagicNumber) || lMagicNumber == 0 ) &&
         OrderSymbol() == Symbol() )    
         {
         lOrderType = OrderType();
         
         if(lOrderType == OP_BUY || lOrderType == OP_SELL )
            {
            OpenArrowName = StringConcatenate("TOTC-", OrderTicket()); 
            if(ObjectFind(OpenArrowName) == -1 )
               {
               ObjectCreate(OpenArrowName, OBJ_ARROW, 0, OrderOpenTime(), Low[iBarShift(NULL, 0, OrderOpenTime())] - ( 10 * Point) );
               }
            ObjectSet(OpenArrowName, OBJPROP_TIME1, OrderOpenTime());  
            ObjectSet(OpenArrowName, OBJPROP_PRICE1, Low[iBarShift(NULL, 0, OrderOpenTime())] - ( 10 * Point));
            ObjectSet(OpenArrowName, OBJPROP_ARROWCODE, 4);  //  dash
            ObjectSet(OpenArrowName, OBJPROP_COLOR, TradeMarkerColor);  
            ObjectSet(OpenArrowName, OBJPROP_WIDTH, 1); 

            
            }
         
         }
      }
      
   return;
   }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| ClearTradeMarkers function - deletes all trade markers           |
//+------------------------------------------------------------------+
void ClearTradeMarkers()
   {
   int ObjectIndex = 0;
   string ObjectName.;
   
   for(ObjectIndex = ObjectsTotal() - 1; ObjectIndex >= 0; ObjectIndex--)
      {
      ObjectName. = ObjectName(ObjectIndex);
      if(StringFind(ObjectName., "TOTC-") > -1)
         {
         ObjectDelete(ObjectName.);
         }
      }
   return;
   }
//+------------------------------------------------------------------+


void CommentLab(string CommentText)
   {
   string CommentLabel;
   int CommentIndex = 0;
   
   if (CommentText == "")
      {
      //  delete all Comment texts
      while(ObjectFind(StringConcatenate("CommentLabel", CommentIndex )) >= 0)
         {
         ObjectDelete(StringConcatenate("CommentLabel", CommentIndex ));
         CommentIndex++;
         }
      return;
      }
   
   while(ObjectFind(StringConcatenate("CommentLabel", CommentIndex )) >= 0)
      {
      CommentIndex++;
      }
      
   CommentLabel = StringConcatenate("CommentLabel", CommentIndex);  
   ObjectCreate(CommentLabel, OBJ_LABEL, 0, 0, 0 );
   ObjectSet(CommentLabel, OBJPROP_CORNER, 0);
   ObjectSet(CommentLabel, OBJPROP_XDISTANCE, 5);
   ObjectSet(CommentLabel, OBJPROP_YDISTANCE, 45 + (CommentIndex * 15) );
   ObjectSetText(CommentLabel, CommentText, 10, "Tahoma", Status_Color );   
   
   }

//+----------------------------------------------------------------------------+


