Navigation´╝ÜHome > Content >

#1_Trade_Levels_(White).mq4

Time: 2011-10-08 | Download file:#1_Trade_Levels_(White).mq4

//+-------------------------------------------------------------------------------------------+
//|                                                                                           |
//|                                    Trade Levels.mq4                                       |
//|                                                                                           |
//+-------------------------------------------------------------------------------------------+
#property copyright "Copyright @ 2015 traderathome and qFish"
#property link      "email: [email protected]"
#property strict
/*---------------------------------------------------------------------------------------------
User Notes:

This Trade Levels indicator is coded to run on MT4 Build 600+.  It shows labeled lines on the
chart for trade entries (EP), at the average level of the entries (AV), and at the levels
selected to take profit (TP) and stop loss (SL).  And it displays Profit/Loss (PL) labels for
the open parts of the trade and for the closed parts of the trade.  Everything is based on
MT4 orders executed.  Summaries of the features are as follows.  Details are further below.

//+-------------------------------------------------------------------------------------------+
//|                                                                                           |
//|    - All trade datum is automatically obtained from MT4 orders, no manual inputs of       |
//|      price or quantity are provided except to display a trial TP and SL.                  |
//|    - Exacting line and label placements for all time frame charts is fully automated      |
//|      using new MQL4 "Chart_Scale" tracking.                                               |
//|    - Permits single side variable size entries.  These entries can then be closed at      |
//|      different times and at different prices.                                             |
//|    - Open and closed trades can be displayed on the same chart concurrently.              |
//|    - For display, the user can base unit sizes traded on micro-lots, mini-lots, or        |
//|      full lots with a simple "1-2-3" input, making this applicable across the entire      |
//|      spectrum of forex traders.                                                           |
//|    - The labels for EP Levels lines can be turned on/off for a cleaner chart when not     |
//|      needed.  When displayed, they include the number of units (of the units size         |
//|      input selection) traded at each EP.                                                  |
//|    - EP/AV/TP/SL lines can individually be full lines, or just tabs for a cleaner chart.  |
//|    - Price Dots at trade entries and exits improve visibility and can be resized.         |
//|    - Connecting lines can be shown between EPs and the current bid/ask price of open      |
//|      trades, and between the EPs and close prices of closed trades.  These lines are      |
//|      color coded to show if each specific trade is in profit or loss.                     |
//|    - PL Labels for open & closed trades show number of EPs, number of traded units,       |
//|      average pips and total pips.  Monies and Account PL can be included in the labels.   |
//|    - The "yyyy.mm.dd hh:mm" start and end inputs can precisely define the time span       |
//|      for showing closed trades, either as a single group, or as separate groups.          |
//|    - Within the defined time span for showing closed trades, they can be shown as         |
//|      one single group of trades with one PL label, or as separate groups of trades with   |
//|      separate PL labels.                                                                  |
//|                                                                                           |
//+-------------------------------------------------------------------------------------------+

Displaying EP Levels -
   You can select to show EP Levels lines and labels.  The Level lines span the chart to the
   current candle, followed by the Level labels which include the EP number, price and
   quantity of units of the lot type input.  If you display the labels for the EP Level lines,
   the AV, TP and SL lines will shift to the right to maintain good visibility.  You can show
   the EP Level line a full line,or as a short mini-line or "tab".

Displaying Price Dots and Lines -
   For open trades you can show lines connecting the EPs to the current appropriate bid/ask
   price, color coded to represent if the trade is in profit or loss.  You can show at each
   entry price a Dot color coded to represent if the trade is long or short.  When the trade
   is closed the Dot is ringed with the color selected for closed trade rings.  This enables
   you to easily see, if a number of trade components remain active but some have been closed,
   which are the closed ones and which remain open.  The input "Dot_Size_12345" provides 5 Dot
   sizes to select from.

Displaying the AV Line -
   For open trades an AV Line is displayed.  It shows the rounded off average price for all
   open orders (even if varying in size), the number of open orders (separate EP events), and
   the number of open units of the type input.  You can show the AV Level line as a full line,
   or as a short mini-line or "tab".

Displaying TP and SL Lines -
   For open trades the TP and SL lines associated with the MT4 orders are displayed.  You can
   override these values by putting other values into the External Inputs.  You might do this
   to temporarily to help you decide the values to place with your MT4 orders.  An asterisk
   appears in the TP and SL labels when you are displaying your override values as a reminder
   they are not the values derived from the MT4 orders.  You can show the TP/SL Level line as
   full line, or as a short mini-line or "tab".  The pips displayed at TP/SL are the rounded
   off total pips of all the open units of your EPs of the type unit selected.  When the TP/SL
   is reached and the trade closes, the closed trade P/L might display differently from what
   the open trade PL label showed due to the rounding of the final average price which can
   involve some units closing at different, and sometimes better prices than intended.

Displaying Open Trade PL Label -
   For open trades the PL Label can be displayed in the lower left or right corner of the main
   chart window, or in the upper right corner of the first subwindow.  The label shows the
   number of discrete EPs made, the total number of open units of the size input, the average
   pips PL for each of these open units, and the total pips PL for these units.  You can also
   include monies PL and the percentage of account PL in the label.  The label text changes
   colors between green and red for profit/loss in pips, not in monies.  You can have a green
   text label with a few pips profit, but still be down in monies due to commissions/swaps.
   Be advised that the MT4 account "Profit" shown under the MT4 "Trade" tab might or might not
   already include adjustments for any commissions and/or swaps. Some brokers show "Profit"
   as gross profit (before any adjustments) and some show this as net, or partial net profit
   (after all, or after some adjustments).  You will have to delve into this on your own.
   This indicator will display the gross profit as "Monies".  You can then adjust by setting
   the External Inputs to adjust "Monies" for commissions and/or swaps.  Remember, swaps can
   be either a positive or a negative value.

Displaying a Closed Trade -
   You must specify the number of minutes backwards from current time within which you wish to
   display all closed trades.  There are two way to do this:
   1. The simple way is to input the number of minutes to "__Simple_Lookback_Time_Span", which
      by default is already set for one hour, an input of "60" minutes.
   2. The exacting method is to set the year/month/day/hour/minutes for the start or end, or
      both the start and end of the period for which you want closed trades to display.  This
      method takes precedence over any value entered for the "__Simple_Lookback_Time_Span".
      These start/end inputs must be in their default state (yyyy.mm.dd hh:mm) for the simple
      method to work.  You can copy & paste the "yyyy.mm.dd hh:mm" (provided in the External
      Inputs) to these start/end inputs to return them to their default state.

   All the trades closed within the set time span will be displayed.  You can display them
   either of two ways:
   1. Show all trades as one group of trades with one PL label:
      To do this, set "__Show_Separate_Trade_Groups" to "false" (it is "true" by default).
   2. Show all trades as separately grouped with each group having a PL label:
      To do this, set "__Show_Separate_Trade_Groups" to "true".  You must also define how to
      separate the groups.  This is done by specifying a minimum time span between the last
      closed trade of one group and the first closed trade of the next following group.  Of
      all the groups to be shown, the time span to use is the for the two groups with the
      shortest time between those close events.  Specify the time in minutes.  Use a number
      of minutes slightly less than that minimum time span.
      
   Using the same inputs selected for Open trades, the Closed trades can be shown with Price
   Dots at entries and closes with connecting lines colored to show if closed with profit or
   loss.  As with the open trade PL label, the PL Label for the closed trades displays the 
   number of EPs closed, the total number of closed units of the type you input, the average 
   pips PL for the unit type, and the total pips PL for the unit type.  You can also include 
   monies PL and account percent PL, both with or without including commissions and/or swaps.

   PL Labels will appear above or below the last close price of a trade unit in the trade
   group, based on whether the unit was closed below or above its' entry price.  You can
   raise or lower the PL label by increasing or decreasing the default "30" input provided.
   There are five label height inputs allowing adjustment of PL labels for up to five separate
   trade groups.  The sequence 1-2-3-5-5 starts with the oldest trade first.  If more than
   five separate trade groups are to be shown, the oldest five can be adjusted and the more
   recent ones will have the same height as the first/oldest trade shown.  So it is better to
   limit the display of closed trades to no more than five, and best to just show one closed
   trade group at a time.

Selecting the Unit Type Input -
   The External Inputs start with "Show_Micro_Mini_Full_123" to input the unit type number
   that will determine how the units traded and the pips involved are displayed:
      1. Select "1":  Displays are in terms of micro-lots and micro-lot pips ($0.10 MOL).
      2. Select "2":  Displays are in terms of mini-lots and mini-lot pips ($1.00 MOL).
      3. Select "3":  Displays are in terms of full lots and full lot pips ($10.00 MOL).

   For example, if you input "3" for standard lots, but make an entry of a half lot
   (5 mini-lots), and price moves in your favor 10 pips, then your gain is 5 standard lot
   pips, not 10.  As another example, if you input "1" for micro-lots but make a entry of
   one mini-lot (equivalent to 10 micro-lots), and price moves 10 pips in your favor, then
   your gain is 100 micro-lot pips, not 10.

   Protecting your privacy is another feature of the unit type input.  For example, suppose
   there are three traders:
      1. This trader makes a single micro-lot trade and selects unit type "1".
      2. This trader makes a single mini-lot trade and selects unit type "2".
      3. This trader makes a single full lot trade and selects unit type "3".
   Due to the unit types selected, the three charts will display identical data, all being
   based on "1" unit traded of the unit type selected.  Anyone viewing all three charts
   will see nothing that identifies the actual unit type being traded, only that "1" unit
   has been traded. Units displayed can only be viewed as generic units because they are not
   revealed as being based on micro, mini or full lots traded.  The only way a viewer of the
   chart can know the unit type of the units shown is for the user to select to display
   PL monies in the PL Labels for open and closed trades.  Without this information, a
   viewer cannot know if units are lots with $10 pips, or mini-lots with $1.00 pips, or
   micro-lots with $0.10 pips (MOL, depending on each forex pair).

Setting Time Frames for Display of Indicator -
   The indicator can be turned on/off without removing it from the chart.  Also, you can
   select a chart TF above which this indicator will automatically not display.

Chart Properties Settings -
   For this indicator to properly display Chart properties/Common tab/chart on foreground
   must be uncheck!  If checked, then MT4 candles are on top of, and can partially or
   totally obscure the Price Dots drawn by this indicator.

                                                                    - Traderathome, 05-30-2015
-----------------------------------------------------------------------------------------------
Acknowlegements:

St3ve -        For intiating the change from manually filled datum to an MT4 orders driven
               product.  This was a milestone influence and effort, which transitioned the
               indicator from a enhancement for educational charts into a tool for traders.

qFish -        For generously devoting his time and programming skills to overhaul the core
               code of this indicator that handles the retrieval of MT4 orders for open and
               closed trades, to expand the "lookback" feature that identifies the closed
               trades to display, and to provide other indispensable features to this very
               sophisitcated and complex indicator.

traderathome - For initating the Trade Levels indicator as an aid to presenting clear trade
               charts for educational purposes, and for tailoring the upgrades to maintain
               an easy to use, eye appealing product.

----------------------------------------------------------------------------------------------
Suggested Settings:           White Chart        Black Chart

EP_Level_Label                MediumBlue         C'094,150,253'
EP_Level_Line_                MediumBlue         C'064,133,208'
AV_Level_Label                DarkOrchid         C'205,072,238'
AV_Level_Line                 DarkOrchid         MediumOrchid
SL_Level_Label                C'213,000,000'     C'250,053,048'
SL_Level_Line                 C'052,160,101'     C'226,035,035'
TP_Level_Label                C'040,123,078'     C'046,186,046'
TP_Level_Line                 C'040,123,078'     C'015,168,084'
EP_Dot_Long                   C'008,084,223'     C'028,199,255'
EP_Dot_Short                  C'232,000,000'     C'255,128,128'
EP_Dot_Center                 White              Black
EP_Dot_Closed                 Black              White
CP_Dot_Center                 Yellow             Black
CP_Dot_Closed                 Black              Yellow
PL_Line_Trade_Positive        CornflowerBlue     C'028,199,255'
PL_Line_Trade_Negative        Salmon             C'255,128,128'
PL_Label_Background           C'255,255,185'     C'226,231,150'
PL_Label_Text_Positive        C'040,123,078'     C'040,123,078'
PL_Label_Text_Negative        C'213,000,000'     C'213,000,000'

---------------------------------------------------------------------------------------------*/


//+-------------------------------------------------------------------------------------------+
//| Indicator Global Inputs                                                                   |
//+-------------------------------------------------------------------------------------------+
#property indicator_chart_window

//User External Input Globals
extern bool    Indicator_On                  = true;
extern int     Show_Micro_Mini_Full_123      = 1;
extern bool    Show_Open_Trades              = true;
extern bool    Show_Closed_Trades            = true;

extern string  Show_in_PL_Labels             = "";
extern bool    __PL_as_Percent_of_Account    = false;
extern bool    __Monies                      = false;
extern bool    ___Comissions_Adjustment      = true;
extern bool    ___Swaps_Adjustment           = true;

extern string  Show_for_Open_Trades______    = "";
extern bool    __EP_Labels                   = true;
extern bool    __EP_Price_Dots               = true;
extern bool    __EP_PL_Lines                 = true;
extern bool    ___Subordinate_PL_Lines       = false;
extern bool    __Full_EP_Level_lines         = false;
extern bool    __Full_AV_Level_lines         = false;
extern bool    __Full_SL_Level_lines         = false;
extern bool    __Full_TP_Level_lines         = false;
extern double  __Trial_SL                    = 0;
extern double  __Trial_TP                    = 0;
extern bool    __PL_Label_At_Chart_Right     = true;
extern bool    __PL_Label_In_Subwindow_1     = true;

extern string  Show_for_Closed_Trades_____   = "";
extern bool    __Price_Dots                  = true;
extern bool    __Price_Lines                 = true;
extern bool    ___Subordinate_Lines          = false;
extern int     __Simple_Lookback_Time_Span   = 60;
extern string  ___Lookback_Exact_Start_Time  = "yyyy.mm.dd hh:mm";
extern string  ___Lookback_Exact_End_Time    = "yyyy.mm.dd hh:mm";
extern string  ____Reset_via_Copy_Paste      = "yyyy.mm.dd hh:mm";
extern bool    __Show_Separate_Trade_Groups  = false;
extern int     ___Min_Minutes_Btwn_Groups    = 60;
extern int     ___PL_Label_1_Adjust_Height   = 30;
extern int     ___PL_Label_2_Adjust_Height   = 30;
extern int     ___PL_Label_3_Adjust_Height   = 30;
extern int     ___PL_Label_4_Adjust_Height   = 30;
extern int     ___PL_Label_5_Adjust_Height   = 30;

extern string  Display_Colors______________  = "";
extern color   EP_Level_Label                = MediumBlue;
extern color   EP_Level_Line                 = MediumBlue;
extern color   AV_Level_Label                = DarkOrchid;
extern color   AV_Level_Line                 = DarkOrchid;
extern color   SL_Level_Label                = C'213,000,000';
extern color   SL_Level_Line                 = C'228,089,082';
extern color   TP_Level_Label                = C'040,123,078';
extern color   TP_Level_Line                 = C'052,160,101';
extern color   EP_Dot_Long                   = C'008,084,223';
extern color   EP_Dot_Short                  = C'232,000,000';
extern color   EP_Dot_Center                 = White;
extern color   EP_Dot_Closed                 = Black;
extern color   CP_Dot_Center                 = Yellow;
extern color   CP_Dot_Closed                 = Black;
extern int     Dot_Size_12345                = 2;
extern color   PL_Line_Trade_Positive        = CornflowerBlue;
extern color   PL_Line_Trade_Negative        = Salmon;
extern color   PL_Label_Background           = C'255,255,185';
extern color   PL_Label_Text_Positive        = C'040,123,078';
extern color   PL_Label_Text_Negative        = C'213,000,000';

extern string  Display_TF_Limitation________ = "";
extern int     Display_Max_TF                = 1440;
extern string  TF_Choices                    = "1-5-15-30-60-240-1440-10080-43200";

//Private Globals
bool     Deinitialized;
int      ordersTotal          = -1; //-1 = indy start and guarantees drawing at least once
double   Poin;                      //defined in init() to accommodate varying Point digits
string   Label_Style          = "Arial Narrow";
int      Label_Size           = 8;
string   Label_Style2         = "Courier New";
int      Label_Size2          = 9;
int      Chart_Scale;
datetime T0,T1,T2,T3,Ts,Ts1,Ts2,Tf,Tf1,Tf2;
int      cnr,W;
int      Price_Dot_Center_Size, Price_Dot_Normal_Size, Price_Dot_Ring_Size;
int      openEPs, orderType;
double   openMicroLots;
double   openTotalPips;
double   openProfit;
double   openComm,openSwap;
double   openSize;
double   openAvgPips;
double   openAvgPrice;
double   tpPrice,slPrice;
string   LOTstr,side;

//+-------------------------------------------------------------------------------------------+
//| Indicator De-initialization                                                               |
//+-------------------------------------------------------------------------------------------+
int deinit()
{
   int obj_total= ObjectsTotal();
   for(int k= obj_total; k>=0; k--)
   {
      string name= ObjectName(k);
      if(StringSubstr(name,0,7)=="[Trade]") {ObjectDelete(name);}
   }
   return(0);

}

//+-------------------------------------------------------------------------------------------+
//| Indicator Initialization                                                                  |
//+-------------------------------------------------------------------------------------------+
int init()
{
   // determine the current chart scale (chart scale number should be 0-5)
   Chart_Scale = ChartScaleGet();

   if(Show_Micro_Mini_Full_123 <1 || Show_Micro_Mini_Full_123 >3)
   {Show_Micro_Mini_Full_123 = 1;}

   // determine "Poin" used for proper decimal placement in open trade calculations
   Poin = Point*10;
   if((StringSubstr(Symbol(),0,6)=="XAUUSD")||(StringSubstr(Symbol(),0,4)=="GOLD")) {Poin=0.01;}
   else {if (StringSubstr(Symbol(),0,6) == "EURTRY") {Poin = 0.001;}
   else {if (StringSubstr(Symbol(),0,6) == "USDTRY") {Poin = 0.001;}
   else {if (StringSubstr(Symbol(),0,6) == "USDMXN") {Poin = 0.001;}
   else {if (StringSubstr(Symbol(),0,6) == "USDCZK") {Poin = 0.001;} }}}}

   Deinitialized = false;

   W= 0; cnr= 2; if(__PL_Label_At_Chart_Right) {cnr= 3;}
   if(__PL_Label_In_Subwindow_1 && WindowsTotal( ) > 1){W= 1; cnr= 1;}
   
   // initialze Dot size
   if(Dot_Size_12345<1 || Dot_Size_12345>5) {Dot_Size_12345= 1;}
   if(Dot_Size_12345== 1)
   {
      Price_Dot_Center_Size= 4;
      Price_Dot_Normal_Size= 8;
      Price_Dot_Ring_Size= 12;
   }
   else if(Dot_Size_12345== 2)
   {
      Price_Dot_Center_Size= 5;
      Price_Dot_Normal_Size= 10;
      Price_Dot_Ring_Size= 13;
   }
   else if(Dot_Size_12345== 3)
   {
      Price_Dot_Center_Size= 6;
      Price_Dot_Normal_Size= 11;
      Price_Dot_Ring_Size= 15;
   }
   else if(Dot_Size_12345== 4)
   {
      Price_Dot_Center_Size= 8;
      Price_Dot_Normal_Size= 15;
      Price_Dot_Ring_Size= 22;
   }
   else if(Dot_Size_12345== 5)
   {
      Price_Dot_Center_Size= 16;
      Price_Dot_Normal_Size= 31;
      Price_Dot_Ring_Size= 46;
   }
   
   return(0);
}

//+-------------------------------------------------------------------------------------------+
//| Indicator Start                                                                           |
//+-------------------------------------------------------------------------------------------+
int start()
{
   // If indicator "Off" or chart TF out of range, deinitialize only once, not every tick.
   if(!Indicator_On || Period() > Display_Max_TF)
   {
      if (!Deinitialized) {deinit(); Deinitialized = true;}
      return(0);
   }

   //If indicator is "ON" clear objects
   int obj_total= ObjectsTotal();
   for(int k= obj_total; k>=0; k--)
   {
      string name= ObjectName(k);
      if(StringSubstr(name,0,7)=="[Trade]") {ObjectDelete(name);}
   }

   // calculate every tick here
   if(Show_Open_Trades)
   {
      if(selectOpenFirstOrder())
      {
         applyChartScale();
         drawOpenTradeEPs();
         drawOpenTradeTPSL();
         drawOpenTradeAV();
         drawOpenTradePL();
      }
      else
      {
         deinit();
      }
   }
   if(Show_Closed_Trades)
   {
      applyChartScale();
      drawClosedTradeEPs();
   }

   return(0);
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Returns true/false that first trade order for symbol is found                 |
//+-------------------------------------------------------------------------------------------+
bool selectOpenFirstOrder()
{
   bool selected = false;

   // find the first order of this symbol
   for(int i=0; i 0)
   {
      tpPips= (NormalizeDouble((getAvgPriceOfOpenOrders()-tpPrice)/Poin,0))*openSize;
      tpPips= ternaryDbl(orderType==OP_BUY,-tpPips,tpPips);

      // draw TP line
      datetime Tx, Ty;
      int R = true;
      if(__EP_Labels)
      {
         if(__Full_TP_Level_lines)
         {
            Tx = T2; Ty = T0;
         }
         else
         {
            Tx = Ts2; Ty = Tf2;
            R = false;
         }
      }
      else
      {
         if(__Full_TP_Level_lines)
         {
            Tx = T3; Ty = T0;
         }
         else
         {
            Tx = Ts; Ty = Tf;
            R = false;
         }
      }
      ObjectCreate(tpLine,OBJ_TREND,0,Tx,tpPrice,Ty,tpPrice);
      ObjectSet(tpLine,OBJPROP_WIDTH,1);
      ObjectSet(tpLine,OBJPROP_COLOR,TP_Level_Line);
      ObjectSet(tpLine,OBJPROP_RAY,R);

      // draw TP label
      TPstr1 = DoubleToStr(tpPrice,Digits);
      if(StringLen(TPstr1)==6) {TPstr1 = "0"+ TPstr1;}
      TPstr2 = DoubleToStr(tpPips,0);
      if(__Trial_TP!=0) {TPstr2 = TPstr2 + "*";}
      if(tpPips > 0) {TPstr2 = "+" + TPstr2;}
      if(__EP_Labels)
      {
         ObjectCreate(tpLabel,OBJ_TEXT,0,T2,tpPrice);
         TPstr3 = StringConcatenate("TP: ",TPstr1,", p",TPstr2);
         TPstr3 = StringConcatenate(strRepeat(" ",28),TPstr3,strRepeat(" ",27-StringLen(TPstr3)));
         ObjectSetText(tpLabel,TPstr3,Label_Size2,Label_Style2,TP_Level_Label);
      }
      else
      {
         ObjectCreate(tpLabel,OBJ_TEXT,0,T3,tpPrice);
         TPstr3 = StringConcatenate("TP: ",TPstr1,", p",TPstr2);
         TPstr3 = StringConcatenate(strRepeat(" ",28),TPstr3,strRepeat(" ",27-StringLen(TPstr3)));
         ObjectSetText(tpLabel,TPstr3,Label_Size2,Label_Style2,TP_Level_Label);
      }
   }

   // draw SL
   double slPips;
   string SLstr1;
   string SLstr2;
   string SLstr3;
   string slLine = "[Trade] SL Level";
   string slLabel = "[Trade] SL Label";

   if(__Trial_SL!=0) {slPrice = __Trial_SL;}

   if(slPrice > 0)
   {
      slPips= (NormalizeDouble((getAvgPriceOfOpenOrders()-slPrice)/Poin,0))*openSize;
      slPips= ternaryDbl(orderType==OP_BUY,-slPips,slPips);

      // draw SL line
      datetime Tx, Ty;
      int R = true;
      if(__EP_Labels)
      {
         if(__Full_SL_Level_lines)
         {
            Tx = T2; Ty = T0;
         }
         else
         {
            Tx = Ts2; Ty = Tf2;
            R = false;
         }
      }
      else
      {
         if(__Full_SL_Level_lines)
         {
            Tx = T3; Ty = T0;
         }
         else
         {
            Tx = Ts; Ty = Tf;
            R = false;
         }
      }
      ObjectCreate(slLine,OBJ_TREND,0,Tx,slPrice,Ty,slPrice);
      ObjectSet(slLine,OBJPROP_WIDTH,1);
      ObjectSet(slLine,OBJPROP_COLOR,SL_Level_Line);
      ObjectSet(slLine,OBJPROP_RAY,R);

      // draw SL label
      SLstr1 = DoubleToStr(slPrice,Digits);
      if(StringLen(SLstr1)==6) {SLstr1 = "0"+ SLstr1;}
      SLstr2 = DoubleToStr(slPips,0);
      if(__Trial_SL!=0) {SLstr2 = SLstr2 + "*";}
      if(slPips > 0) {SLstr2 = "+" + SLstr2;}
      if(__EP_Labels)
      {
         ObjectCreate(slLabel,OBJ_TEXT,0,T2,slPrice);
         SLstr3 = StringConcatenate("SL: ",SLstr1,", p",SLstr2);
         SLstr3 = StringConcatenate(strRepeat(" ",28),SLstr3,strRepeat(" ",27-StringLen(SLstr3)));
         ObjectSetText(slLabel,SLstr3,Label_Size2,Label_Style2,SL_Level_Label);
      }
      else
      {
         ObjectCreate(slLabel,OBJ_TEXT,0,T3,slPrice);
         SLstr3 = StringConcatenate("SL: ",SLstr1,", p",SLstr2);
         SLstr3 = StringConcatenate(strRepeat(" ",28),SLstr3,strRepeat(" ",27-StringLen(SLstr3)));
         ObjectSetText(slLabel,SLstr3,Label_Size2,Label_Style2,SL_Level_Label);
      }
   }

   return;
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Draws the average line and label positioned to the right of the ep labels     |
//+-------------------------------------------------------------------------------------------+
void drawOpenTradeAV()
{
   string AVstr1;
   string AVstr2;
   string AVstr3;
   double Avg_Price = getAvgPriceOfOpenOrders();
   string avLine = "[Trade] Av Level";
   string avLabel = "[Trade] Av Label";
   string fix = ", ";

   // draw AV line
   datetime Tx, Ty;
   int R = true;
   if(__EP_Labels)
   {
      if(__Full_AV_Level_lines)
      {
         Tx = T2; Ty = T0;
      }
      else
      {
         Tx = Ts2; Ty = Tf2;
         R = false;
      }
   }
   else
   {
      if(__Full_AV_Level_lines)
      {
         Tx = T3; Ty = T0;
      }
      else
      {
         Tx = Ts; Ty = Tf;
         R = false;
      }
   }
   ObjectCreate(avLine,OBJ_TREND,0,Tx,Avg_Price,Ty,Avg_Price);
   ObjectSet(avLine,OBJPROP_WIDTH,1);
   ObjectSet(avLine,OBJPROP_COLOR,AV_Level_Line);
   ObjectSet(avLine,OBJPROP_RAY,R);

   // draw AV label
   AVstr1 = DoubleToStr(Avg_Price,Digits);
   AVstr1 = strRepeat("0",7-StringLen(AVstr1))+ AVstr1;
   AVstr2 = StringConcatenate(openEPs,"<",LOTstr);

   if(__EP_Labels)
   {
      ObjectCreate(avLabel,OBJ_TEXT,0,T2,Avg_Price);
      AVstr3 = StringConcatenate("AV: ",AVstr1,fix,openEPs,"<",LOTstr,side);
      AVstr3 = StringConcatenate(strRepeat(" ",28),AVstr3,strRepeat(" ",27-StringLen(AVstr3)));
      ObjectSetText(avLabel,AVstr3,Label_Size2,Label_Style2,AV_Level_Label);
   }
   else//Show Without EP_Labels
   {
      ObjectCreate(avLabel,OBJ_TEXT,0,T3,Avg_Price);
      AVstr3 = StringConcatenate("AV: ",AVstr1,fix,openEPs,"<",LOTstr,side);
      AVstr3 = StringConcatenate(strRepeat(" ",28),AVstr3,strRepeat(" ",27-StringLen(AVstr3)));
      ObjectSetText(avLabel,AVstr3,Label_Size2,Label_Style2,AV_Level_Label);
   }

   return;
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Draws open trade current P/L (with monies & percentage options)               |
//+-------------------------------------------------------------------------------------------+
void drawOpenTradePL ()
{
   // preclude "zero divide" error for closed trade
   if(AccountBalance() == 0) return;

   string PD=DoubleToStr(openAvgPips,1);
   if(openAvgPips >= 0) {PD = "+"+PD;}
   string PT=DoubleToStr(openTotalPips,1);
   if(openTotalPips >= 0) {PT = "+"+PT;}
   string PLstr;

   if(!__Monies && !__PL_as_Percent_of_Account)
   {
      PLstr = StringConcatenate(" PL: ",openEPs,"<",LOTstr," avg p",PD," = p",PT);
   }
   else //show pips together with $ or % or both
   {
      string A = StringConcatenate(" PL: ",openEPs,"<",LOTstr," avg p",PD," = p",PT);
      string PR = DoubleToStr(openProfit,2);
      if(openProfit >= 0) {PR = "+"+PR;}
      string B = StringConcatenate(", $",PR);
      string C;
      string D;
      double pct = (openProfit/AccountBalance())*100;
      if(pct<0) {D= ", ";}
      else {D= ", +";}
      if(pct < 10) {C = StringConcatenate(D+DoubleToStr(pct,2),"% ");}
      else {C = StringConcatenate(D+DoubleToStr(pct,1),"% ");}
      if(__Monies && !__PL_as_Percent_of_Account)
         {
            PLstr = A+B;
         }
      else if(!__Monies && __PL_as_Percent_of_Account)
         {
            PLstr = A+C;
         }
      else if(__Monies && __PL_as_Percent_of_Account)
         {
            PLstr = A+B+C;
         }
   }

   // show open trade PL label
   string open_BG = "[Trade] Open PL Label";
   ObjectCreate(open_BG,OBJ_LABEL,W,0,0);
   ObjectSet(open_BG,OBJPROP_CORNER,cnr);
   ObjectSet(open_BG,OBJPROP_XDISTANCE,0);
   ObjectSet(open_BG,OBJPROP_YDISTANCE,-2);
   ObjectSetText(open_BG,strRepeat("g", int(StringLen(PLstr)/2.8)),16,
     "Webdings",PL_Label_Background);

   // display P/L text
   int y= 3; if(W==1) y=2;
   string open_TX = "[Trade] Open PL Text";
   ObjectCreate(open_TX,OBJ_LABEL,W,0,0);
   ObjectSet(open_TX,OBJPROP_CORNER,cnr);
   ObjectSet(open_TX,OBJPROP_XDISTANCE,5);
   ObjectSet(open_TX,OBJPROP_YDISTANCE,y);
   ObjectSet(open_TX,OBJPROP_COLOR,ternaryColor(openTotalPips<0,
      PL_Label_Text_Negative,PL_Label_Text_Positive));
   ObjectSetText(open_TX,PLstr,Label_Size2,Label_Style2);

   return;
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Returns the average price of all buy/sell entries, factoring in variable sizes|
//+-------------------------------------------------------------------------------------------+
// Takes into account the micro-lot size of each trade of all executed open buy/sell orders.
// Trade orders for the pair must be either all buy or all sell.
double getAvgPriceOfOpenOrders()
{  
   openMicroLots = 0;
   openTotalPips = 0;
   openProfit = 0;
   openComm = 0;
   openSwap = 0;
   openSize = 0;
   openAvgPips = 0;
   openAvgPrice = 0;
   double openpriceLotProduct = 0;

   for(int i=0; i0) {return(openAvgPrice);}

   else {return(0);}
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Draws closed trade lines connecting EPs to closed bid/ask price               |
//+-------------------------------------------------------------------------------------------+
void drawClosedTradeEPs()
{
   int epNum = 1;
   int trNum = 0;
   int closedEPs = 0;
   double closedTotalPips = 0;
   double closedProfit = 0;
   double closedMicroLots = 0;
   datetime lastClosedTime = -1;
   double lastClosedPrice = -1;
   double openPrice =0;
   double closePrice =0;
   datetime openTime =0;
   datetime closeTime =0;

   for(int i=0; i=0;
         bool cet = StringFind(___Lookback_Exact_End_Time,"yyyy.mm.dd hh:mm")>=0;
         if(cst && cet)    // if they are both not used
            tradeHistoryTimeFlag = ((TimeCurrent()-OrderCloseTime())/60)<__Simple_Lookback_Time_Span;
         if(!cst && cet)   // if start only is used
            tradeHistoryTimeFlag = OrderCloseTime()>StrToTime(___Lookback_Exact_Start_Time);
         if(!cst && !cet)  // if start and end is used
            tradeHistoryTimeFlag = OrderCloseTime()>StrToTime(___Lookback_Exact_Start_Time)
            && OrderCloseTime()= 0) {PA = "+"+PA;}

   if(Show_Micro_Mini_Full_123 == 1)
   {
      closedSize = closedMicroLots;
      PT=DoubleToStr(closedTotalPips,1);
   }
   if(Show_Micro_Mini_Full_123 == 2)
   {
      closedSize = closedMicroLots/10;
      PT=DoubleToStr(closedTotalPips/10,1);
   }
   else if(Show_Micro_Mini_Full_123 == 3)
   {
      closedSize = closedMicroLots/100;
      PT=DoubleToStr(closedTotalPips/100,1);
   }
   if(StrToDouble(PT) >= 0) {PT = "+"+PT;}

   // get the "closedSize" digits to the right of the decimal
   int lot0 = int (100 * closedSize);
   int lot2 = lot0%10;
   int lot1 = lot0%100 - lot2;

   // use digits info to determine how many digits to display
   if((lot1==0) && (lot2==0)) {closedLOT=DoubleToStr(closedSize,0);}
   else if (lot2==0) {closedLOT=DoubleToStr(closedSize,1);}
   else {closedLOT=DoubleToStr(closedSize,2);}

   if(!__Monies && !__PL_as_Percent_of_Account)
   {
      //Show EPs, lot factored units, average pips, & resultant P/L total pips
      PLstr= StringConcatenate("PL: ",closedEPs,"<",closedLOT," avg p",PA," = p",PT);
   }
   else //show pips together with $ or % or both
   {
      string A = StringConcatenate("PL: ",closedEPs,"<",closedLOT," avg p",PA," = p",PT);
      string PR = DoubleToStr(closedProfit,2);
      if(closedProfit >= 0) {PR = "+"+PR;}
      string B = StringConcatenate(", $",PR);  
      string C;
      string D;
      double pct = closedProfit/AccountBalance()*100;
      if(pct<0) {D= ", ";}
      else {D= ", +";}
      if(pct < 10) {C = StringConcatenate(D+DoubleToStr(pct,2),"%");}
      else {C = StringConcatenate(D+DoubleToStr(pct,1),"%");}
      if(__Monies && !__PL_as_Percent_of_Account) {PLstr = A+B;}
      else if(!__Monies && __PL_as_Percent_of_Account) {PLstr = A+C;}
      else if(__Monies && __PL_as_Percent_of_Account) {PLstr = A+B+C;}
   }

   // shift label up/dn based on "Adjust_Label" input
   double labelprice;
   double adjustHeight;
   if(trNum<2 || trNum>3) {adjustHeight = ___PL_Label_1_Adjust_Height;}
   else if(trNum == 2) {adjustHeight = ___PL_Label_2_Adjust_Height;}
   else if(trNum == 3) {adjustHeight = ___PL_Label_3_Adjust_Height;}
   else if(trNum == 4) {adjustHeight = ___PL_Label_4_Adjust_Height;}
   else if(trNum == 5) {adjustHeight = ___PL_Label_5_Adjust_Height;}
   if(Digits == 2) {adjustHeight= ___PL_Label_1_Adjust_Height/10;}
   if(priceClosed > priceOpened)
      {labelprice = priceClosed + (adjustHeight*10*Point);}
   else {labelprice = priceClosed - (adjustHeight*10*Point);}

   // set P/L background to accomdate length of P/L text
   string closed_BG = StringConcatenate("[Trade] Closed PL Label ",priceClosed);
   ObjectCreate(closed_BG,OBJ_TEXT,0,datetime(t),labelprice);
   ObjectSetText(closed_BG,strRepeat("g",StringLen(PLstr)/2),11,
      "Webdings",PL_Label_Background);

   // display P/L text
   string closed_TX = StringConcatenate("[Trade] Closed PL Text ",priceClosed);
   ObjectCreate(closed_TX,OBJ_TEXT,0,datetime(t),labelprice);
   ObjectSet(closed_TX,OBJPROP_COLOR,ternaryColor(closedTotalPips<0,
      PL_Label_Text_Negative,PL_Label_Text_Positive));
   ObjectSetText(closed_TX,PLstr,Label_Size2,Label_Style2);

   return;
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Returns decided upon string expression (2 or 3) to use                        |
//+-------------------------------------------------------------------------------------------+
string ternaryStr(bool expression1, string expression2, string expression3)
{
   string result = expression2;
   if(!expression1) {result = expression3;}
   return(result);
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Returns decided upon integer expression (2 or 3) to use                       |
//+-------------------------------------------------------------------------------------------+
int ternaryInt(bool expression1, int expression2, int expression3)
{
   int result = expression2;
   if(!expression1) {result = expression3;}
   return(result);
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Returns decided upon double expression (2 or 3) to use                        |
//+-------------------------------------------------------------------------------------------+
double ternaryDbl(bool expression1, double expression2, double expression3)
{
   double result = expression2;
   if(!expression1) {result = expression3;}   
   return(result);
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Returns decided upon color expression (2 or 3) to use                         |
//+-------------------------------------------------------------------------------------------+
double ternaryColor(bool expression1, color expression2, color expression3)
{
   color result = expression2;
   if(!expression1) {result = expression3;}   
   return(result);
}

//+-------------------------------------------------------------------------------------------+
//| Subroutine: Returns string expression (s) repeated (n) times                              |
//+-------------------------------------------------------------------------------------------+
string strRepeat(string s, int n)
{
   string result = "";
   for(int i=0; i        

Recommend