//+------------------------------------------------------------------+
//|                                         colorstoplevels_v1.0.mq4 |
//|                                     Copyright 2016, Anton Zverev |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Anton Zverev."
#property version   "1.0"
#property strict
#property indicator_chart_window

extern string p1="================================="; // * 
extern color  Buy_O= clrLimeGreen; //  Buy Order
extern color  SL_b   = clrCrimson; //    SL Buy
extern color  TP_b=clrRoyalBlue; //  TP Buy
extern string p2="================================="; // * 
extern color  Sell_O=clrRed; //        Sell Order
extern color  SL_s=clrOrangeRed; //  SL Sell
extern color  TP_s=clrAqua; //       TP Sell
extern string p3="================================="; // *

extern ENUM_LINE_STYLE style_or=STYLE_SOLID; // Style Line Orders
extern ENUM_LINE_STYLE style_st=STYLE_DOT; // Style Line SL/TP
extern int             w_width=1; // Width

double oop_b,osl_b,otp_b;
string ob_name="",osl_name="",otp_name="";

double oop_s,osl_s,otp_s;
string ob_name2="",osl_name2="",otp_name2="";
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectDel();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   INControl();

   for(int i=0; i<OrdersTotal(); i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderSymbol()==_Symbol) 
           {
            //-----------------------------------------------------------------
            if(OrderType()==OP_BUY)
              {
               oop_b=OrderOpenPrice();
               osl_b=OrderStopLoss();
               otp_b=OrderTakeProfit();

               ob_name    = "Buy "+(string)OrderTicket();
               otp_name   = "otp_b "+(string)OrderTicket();
               osl_name   = "osl_b "+(string)OrderTicket();

               if(ObjectGet(ob_name,OBJPROP_PRICE1)!=oop_b) 
                 {
                  ObjectOrderLine(ob_name,oop_b,Buy_O,style_or,w_width);
                 }

               if(ObjectGet(otp_name,OBJPROP_PRICE1)!=otp_b) 
                 {
                  ObjectDelete(otp_name);
                  ObjectOrderLine(otp_name,otp_b,TP_b,style_st,w_width);
                 }

               if(ObjectGet(osl_name,OBJPROP_PRICE1)!=osl_b) 
                 {
                  ObjectDelete(osl_name);
                  ObjectOrderLine(osl_name,osl_b,SL_b,style_st,w_width);
                 }
              }
            //-----------------------------------------------------------------
            if(OrderType()==OP_SELL)
              {
               oop_s=OrderOpenPrice();
               osl_s=OrderStopLoss();
               otp_s=OrderTakeProfit();

               ob_name2    = "Sell "+(string)OrderTicket();
               otp_name2   = "otp_s "+(string)OrderTicket();
               osl_name2   = "osl_s "+(string)OrderTicket();

               if(ObjectGet(ob_name2,OBJPROP_PRICE1)!=oop_s) 
                 {
                  ObjectOrderLine(ob_name2,oop_s,Sell_O,style_or,w_width);
                 }

               if(ObjectGet(otp_name2,OBJPROP_PRICE1)!=otp_s) 
                 {
                  ObjectDelete(otp_name2);
                  ObjectOrderLine(otp_name2,otp_s,TP_s,style_st,w_width);
                 }

               if(ObjectGet(osl_name2,OBJPROP_PRICE1)!=osl_s) 
                 {
                  ObjectDelete(osl_name2);
                  ObjectOrderLine(osl_name2,osl_s,SL_s,style_st,w_width);
                 }
              }
            //-----------------------------------------------------------------
           }
        }
     }

   for(int n=0; n<OrdersHistoryTotal(); n++) 
     {
      if(OrderSelect(n,SELECT_BY_POS,MODE_HISTORY)) 
        {
         //---------------------------------
         if(OrderType()==OP_BUY) 
           {
            ob_name    = "Buy "+(string)OrderTicket();
            otp_name   = "otp_b "+(string)OrderTicket();
            osl_name   = "osl_b "+(string)OrderTicket();

            if(OrderOpenPrice()==ObjectGet(ob_name,OBJPROP_PRICE1)) ObjectDelete(ob_name);
            if(OrderTakeProfit()==ObjectGet(otp_name,OBJPROP_PRICE1)) ObjectDelete(otp_name);
            if(OrderStopLoss()==ObjectGet(osl_name,OBJPROP_PRICE1)) ObjectDelete(osl_name);
           }
         //---------------------------------
         if(OrderType()==OP_SELL) 
           {
            ob_name2    = "Sell "+(string)OrderTicket();
            otp_name2   = "otp_s "+(string)OrderTicket();
            osl_name2   = "osl_s "+(string)OrderTicket();

            if(OrderOpenPrice()==ObjectGet(ob_name2,OBJPROP_PRICE1)) ObjectDelete(ob_name2);
            if(OrderTakeProfit()==ObjectGet(otp_name2,OBJPROP_PRICE1)) ObjectDelete(otp_name2);
            if(OrderStopLoss()==ObjectGet(osl_name2,OBJPROP_PRICE1)) ObjectDelete(osl_name2);
           }
         //---------------------------------
        }
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
void INControl()
  {
   long BACKGROUND=ChartGetInteger(ChartID(),CHART_COLOR_BACKGROUND);

   if(ChartGetInteger(ChartID(),CHART_COLOR_STOP_LEVEL)!=BACKGROUND) ChartSetInteger(ChartID(),CHART_COLOR_STOP_LEVEL,BACKGROUND);
   if(ChartGetInteger(ChartID(),CHART_COLOR_VOLUME)!=BACKGROUND) ChartSetInteger(ChartID(),CHART_COLOR_VOLUME,BACKGROUND);
  }
//+------------------------------------------------------------------+
void ObjectOrderLine(string name="",double price=0,color clr=clrNONE,ENUM_LINE_STYLE style=STYLE_SOLID,int width=1)
  {
   long chart_ID=ChartID();
   int  sub_window=0;

   if(style!=STYLE_SOLID) width=1;

   if(ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price)) 
     {
      ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
      ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
      ObjectSetInteger(chart_ID,name,OBJPROP_BACK,false);
      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,false);
      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,false);
      ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,true);
     }
  }
//+------------------------------------------------------------------+
void ObjectDel()
  {
   for(int i=0; i<OrdersTotal(); i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderSymbol()==_Symbol) 
           {
            //---------------------------------
            if(OrderType()==OP_BUY)
              {
               oop_b=OrderOpenPrice();
               osl_b=OrderStopLoss();
               otp_b=OrderTakeProfit();

               ob_name    = "Buy "+(string)OrderTicket();
               otp_name   = "otp_b "+(string)OrderTicket();
               osl_name   = "osl_b "+(string)OrderTicket();

               ObjectDelete(ob_name);
               ObjectDelete(otp_name);
               ObjectDelete(osl_name);
              }
            //---------------------------------
            if(OrderType()==OP_SELL)
              {
               oop_s=OrderOpenPrice();
               osl_s=OrderStopLoss();
               otp_s=OrderTakeProfit();

               ob_name2    = "Sell "+(string)OrderTicket();
               otp_name2   = "otp_s "+(string)OrderTicket();
               osl_name2   = "osl_s "+(string)OrderTicket();

               ObjectDelete(ob_name2);
               ObjectDelete(otp_name2);
               ObjectDelete(osl_name2);
              }
            //---------------------------------
           }
        }
     }
  }
//+------------------------------------------------------------------+
