//+------------------------------------------------------------------+
//--------------------------------------------------------------------

#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh> 

extern double  RiskPercent             = 2.0;
extern double  AdditionalEquity        = 0;
extern bool    PendingLine             = false;
extern string  TP_SL_Settings          = "覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧";
extern bool    UseHiddenStopLoss       = true;
extern int     HiddenSL                = 150;
extern bool    UseEmergencyStopLoss    = true;
extern int     EmergencySL             = 200;
extern bool    UseHiddenTakeProfit     = true;
extern int     HiddenTP1               = 100;
double  TakeProfitMultiplier    = 0;
//Use one or the other
extern string  Trailingstop_Settings   = "覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧";
extern bool    ProfitTrailing          = true;
extern bool    UseHiddenTrailingStop   = true;
 bool    ATRTrail                = false;
 int     ATRTF                   = 5;
 int     TSPeriod                = 9;
 int     BarsBack                = 10;
 string  TSComment               = "If not using ATRTrail";
extern int     TrailingStop            = 50;
extern int     TrailingStep            = 1;
extern bool    UseSound                = false;
extern string  NameFileSound           = "cork_pop.wav";
extern string  Breakeven_Settings      = "覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧";
extern string  BE                      = "Break even settings";
extern bool    BreakEven               = true;
extern bool    UseHiddenBreakEven      = false;
extern int     BreakEvenPips           = 35;
extern string  Line_Settings           = "覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧";
extern string  LS                      = "Line settings";
extern bool    DrawLines               = true;
extern color   TPColor                 = Green;
extern color   SLColor                 = Red;
extern color   BEColor                 = Yellow;
extern color   TSColor                 = Magenta;
extern color   TextColor               = DimGray;
extern int     ShiftLabel              = 6;
extern bool    FullScreenLines         = true;
extern string  FSL                     = "If False, lines start at current bar";
extern string  Other_Settings          = "覧覧覧覧覧覧覧覧覧覧覧覧覧覧覧";
extern string  OS                      = "Other settings";
extern double  Commission              = 7;
extern double  MaxSpread               = 2.5;
extern bool    DeleteOCO               = true;
extern string  OCO_Comment             = "Will delete remaining Pending orders";
extern int     MagicNumber             = 32769;
extern int     ScreenWidth             = 1280;
extern int     ScreenHeight            = 640;
extern int     DeletePendingAfterXBars = 2;
extern bool    ShowComments            = false;
extern bool    CloseChart              = false;
extern bool    Trade50Levels           = true;
extern bool    FirstLevelOnly          = true;
//extern int  one=1;
bool  FinalScreenshot = false;

double HSL,HTP,HBE,HTS,BEPrice;
string ObjPref="WUKAR_";
int    LineType;
color  LineColor;
bool   IsTrade;
double BuyEntryPrice,SellEntryPrice,EntryPrice,Entry;
int    Mode;

double Lots;
double equity;
double StopLoss,StopLevel,TakeProfit;
double ReqMargin;
double TradeReq;
double EquityRisk;
double expiration;
double BETarget;

string TicketNo;
bool   DeleteGV=false;
bool  PlaceTrade;
string ScreenMsg;
bool  ScreenShot;
string OK;
bool LinePlaced=false;
double pBid,pAsk,pp;
static string OrderTime;
//+------------------------------------------------------------------+
void init()
  {
   double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
   Commission=MathRound(Commission/TickValue);

   ScreenShot=false;
   StopLoss=0;
   BuyEntryPrice=10000;
   SellEntryPrice=-1;
   expiration=Time[0];

   if(FullScreenLines)
      LineType=OBJ_HLINE;
   else
      LineType=OBJ_TREND;
  }
//+------------------------------------------------------------------+
void deinit()
  {
//     GlobalVariableDel(Symbol());
//   DeleteAllLines();
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void start()
  {
   if(!ScreenShot)
     {
      Sleep(2000);
      if(!checkVariable("Open",1))
         setVariable("Open",1,Hour());
      else
         OrderTime = StringFormat("%02s",DoubleToString(getVariable("Open",1),0));
//Print("OrderTime=",OrderTime);              
                  if(ScreenHeight==0)
                     ScreenHeight=ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
                  if(ScreenWidth==0)
                     ScreenWidth=ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0);
                  if(WindowScreenShot("shots\\"+TimeDayOfYear(TimeCurrent())+"-"+OrderTime+Symbol()+TimeCurrent()+".gif",ScreenWidth,ScreenHeight))
         OK="ok";
      ScreenShot=true;
     }

   if(PendingLine && !LinePlaced)
     {
      PlacePendingLine();
     }

   ScreenMsg="";
   if(DeleteGV)
      if(getOpenOrders()==0) // if no open orders in current TF
        {
         GlobalVariableDel(Symbol()+"Open1");
         DeleteGV=false;
        }

   IsTrade=false;
   PlaceTrade=false;
   bool result=false;
   int PendingOrder= 0;
   int totalorders = OrdersTotal();
   for(int i=totalorders-1; i>=0; i--)
     {
      result=OrderSelect(i,SELECT_BY_POS);
      if(OrderType()>OP_SELL && OrderSymbol()==Symbol())
         PendingOrder=OrderTicket();

      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         IsTrade=True;
         DeleteGV=true;
         TicketNo= OrderTicket();
         pp=MarketInfo(OrderSymbol(),MODE_POINT);

         if(OrderType()==OP_BUY)
           {
            pBid=MarketInfo(OrderSymbol(),MODE_BID);
            if(ObjectFind("HBE line")==0 && HBE==0)
               HBE=ObjectGetDouble(0,"HBE line",OBJPROP_PRICE,0);
            if(HBE!=0.0 && pBid<=HBE)
              {
               OrderClose(OrderTicket(),OrderLots(),pBid,5,Red);
               return;
              }

            if(BreakEven==true)
              {
               BETarget=OrderOpenPrice()+BreakEvenPips*pp;
               if(DrawLines)
                 {
                  LineColor=BEColor;
                  DrawLine("BE label","BE","BE line",LineType,LineColor,BEPrice);
                 }
               if(HBE==0.0
                  && BEPrice==0.0
                  && pBid>BETarget)
                 {
                  BEPrice=OrderOpenPrice()+Commission*pp;
                 }
              }
           }

         if(OrderType()==OP_SELL)
           {
            pAsk=MarketInfo(OrderSymbol(),MODE_ASK);
            if(HBE!=0.0 && pAsk>=HBE)
              {
               OrderClose(OrderTicket(),OrderLots(),pAsk,5,Red);
               return;
              }

            if(BreakEven==true)
              {
               BETarget=OrderOpenPrice()-BreakEvenPips*pp;
               if(DrawLines)
                 {
                  LineColor=BEColor;
                  DrawLine("BE label","BE","BE line",LineType,LineColor,BEPrice);
                 }
               if(HBE==0.0
                  && BEPrice==0.0
                  && pAsk<BETarget)
                 {
                  BEPrice=OrderOpenPrice()-Commission*pp;
                 }
              }
           }

         if(BEPrice!=0)
           {
            if(UseHiddenBreakEven)
              {
               if(HBE == 0)
                  HBE = BEPrice;
               if(DrawLines)
                 {
                  LineColor=BEColor;
                  DrawLine("HBE label","HBE","HBE line",LineType,LineColor,HBE);
                 }
              }
            else
            if(ModifyStopLoss(BEPrice,"BE"))
              {
               Print("WUKAR_WB Modified BE Stop Loss");
               BEPrice=0;
              }

           }

         if(ProfitTrailing)
            TrailingPositions();

         if(UseHiddenStopLoss)
            HiddenStopLoss();
         if(UseHiddenTakeProfit)
            HiddenTakeProfit();
         if(UseEmergencyStopLoss)
            EmergencyStopLoss();
        }

      if(IsTrade && DeleteOCO && PendingOrder>0)
         OrderDelete(PendingOrder);

      if(ObjectFind("EJS_PL")==0 && IsTrade)
        {
         ObjectDelete("EJS_PL");
//         ScreenShot=false;
        }
     }
   if(!IsTrade)
     {
      DeleteAllLines();
      HSL = 0;
      HTP = 0;
      HBE = 0;
      HTS = 0;
      BEPrice=0;
      EntryPrice=0;

      if(ObjectFind("EJS_PL")==0)
        {
         if(!ScreenShot)
           {
            Sleep(2000);
                  if(ScreenHeight==0)
                     ScreenHeight=ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
                  if(ScreenWidth==0)
                     ScreenWidth=ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0);
                  if(WindowScreenShot("shots\\"+TimeDayOfYear(TimeCurrent())+"-"+OrderTime+Symbol()+TimeCurrent()+".gif",ScreenWidth,ScreenHeight))
               OK="ok";
            ScreenShot=true;
           }

         if(Time[0]>expiration || (checkVariable("News",1) && getVariable("News",1)>-30 && getVariable("News",1)<=60))
           {
            ObjectDelete("EJS_PL");
            BuyEntryPrice=10000;
            SellEntryPrice=-1;
           }
         else
           {
            expiration=iTime(Symbol(),PERIOD_CURRENT,0)+(DeletePendingAfterXBars*Period()*60-1);
           }
         ScreenMsg=StringConcatenate(ScreenMsg,"\nexpiration=",TimeToString(expiration,TIME_DATE|TIME_MINUTES));

         EntryPrice=ObjectGet("EJS_PL",OBJPROP_PRICE1);

         if(StopLoss==0)
           {
            if(EntryPrice>Open[0])
              {
               BuyEntryPrice=EntryPrice;
               if(ATRTrail)
                  StopLoss=NormalizeDouble(EntryPrice-(Low[1]-iATR(Symbol(),ATRTF,TSPeriod,1)),Digits)/Point;
               else
                  StopLoss=HiddenSL;
              }
            else
              {
               SellEntryPrice=EntryPrice;
               if(ATRTrail)
                  StopLoss=NormalizeDouble((High[1]+iATR(Symbol(),ATRTF,TSPeriod,1)-EntryPrice),Digits)/Point;
               else
                  StopLoss=HiddenSL;
              }
           }
         ScreenMsg=StringConcatenate(ScreenMsg,"\nEntryPrice=",EntryPrice);

         //Print("StopLoss=",StopLoss);

         if(MarketInfo(NULL,MODE_SPREAD)<=MaxSpread*10)
           {
            if(Ask>=BuyEntryPrice)
              {
               Entry= Ask;
               Mode = OP_BUY;
               PlaceTrade=true;
              }

            if(Bid<=SellEntryPrice)
              {
               Entry= Bid;
               Mode = OP_SELL;
               PlaceTrade=true;
              }
           }

         if(PlaceTrade)
           {
            double Risk=RiskPercent/100;

            equity=AccountBalance()+AdditionalEquity;

            int min_size_digits;

            //check account type (micro / mini / normal)
            if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.001)
               min_size_digits=3;
            if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01)
               min_size_digits=2;
            if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.05)
               min_size_digits=2;
            if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1)
               min_size_digits=1;
            if(MarketInfo(Symbol(),MODE_LOTSTEP)==1)
               min_size_digits=0;

            Lots=NormalizeDouble(equity*Risk/StopLoss/(MarketInfo(Symbol(),MODE_TICKVALUE)),2);
            if(Lots<MarketInfo(Symbol(),MODE_MINLOT))
               Lots=MarketInfo(Symbol(),MODE_MINLOT);
            if(Lots>MarketInfo(Symbol(),MODE_MAXLOT))
               Lots=MarketInfo(Symbol(),MODE_MAXLOT);
            Lots=NormalizeDouble(Lots,min_size_digits);
            if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.05)
              {
               int templots=Lots*100;
               double remainder=templots%5;
               templots=templots-remainder;
               Lots = templots;
               Lots/=100;
              }

            //Check free margin
            ReqMargin=MarketInfo(Symbol(),MODE_MARGINREQUIRED)*Lots;
            EquityRisk=StopLoss*MarketInfo(Symbol(),MODE_TICKVALUE)*Lots;
            TradeReq=ReqMargin+EquityRisk;
            while(TradeReq>AccountFreeMargin())
              {
               Lots=Lots-MarketInfo(Symbol(),MODE_LOTSTEP);
               if(Lots<MarketInfo(Symbol(),MODE_MINLOT))
                 {
                  Print("Insufficient Funds to trade. Free Margin = ",AccountFreeMargin());
                  return(-1);
                 }// end if(Lots < MarketInfo(Symbol(), MODE_MINLOT))
               ReqMargin=MarketInfo(Symbol(),MODE_MARGINREQUIRED)*Lots;
               EquityRisk=StopLoss*MarketInfo(Symbol(),MODE_TICKVALUE)*Lots;
               TradeReq=ReqMargin+EquityRisk;
              }// end while(TradeReq > AccountFreeMargin() )

            //Print("Lots= ",Lots);

            if(Lots>0)
              {
               int ticket=OrderSend(Symbol(),Mode,Lots,Entry,3,0,0,"",MagicNumber,NULL,Red);
               if(ticket>0)
                 {
                  ObjectDelete("EJS_PL");
                  if(ScreenHeight==0)
                     ScreenHeight=ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
                  if(ScreenWidth==0)
                     ScreenWidth=ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0);
                  if(WindowScreenShot("shots\\"+TimeDayOfYear(TimeCurrent())+"-"+OrderTime+Symbol()+TimeCurrent()+".gif",ScreenWidth,ScreenHeight))
                     OK="ok";
                  ScreenShot=false;
                  StopLoss=0;
                  BuyEntryPrice=10000;
                  SellEntryPrice=-1;
                 }
              }// end if(Lots > 0)          
           }
        }
      else
        {
        if(!FinalScreenshot)
         {
         expiration=Time[0];
         Sleep(2000);
                  if(ScreenHeight==0)
                     ScreenHeight=ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
                  if(ScreenWidth==0)
                     ScreenWidth=ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0);
                  if(WindowScreenShot("shots\\"+TimeDayOfYear(TimeCurrent())+"-"+OrderTime+Symbol()+TimeCurrent()+".gif",ScreenWidth,ScreenHeight))
            OK="ok";
         GlobalVariablesDeleteAll(Symbol()+"Open1");
         GlobalVariablesDeleteAll(Symbol()+"Take");
         FinalScreenshot=true;
         }
//         if(CloseChart)
//            ChartClose(0);
        }

     }

   if(ShowComments)
      Comment(ScreenMsg);

   return;

/* Do not uncomment the below. It does not do anything useful except to
  open orders, so as to test, trail stop, tp aand stop loss of the two EAs 
  merged. Its a work around to test the EA is working in the tester. thats all */

// OpenPosition();    
  }
//+------------------------------------------------------------------+
void TrailingPositions()
  {

   RefreshRates();
   pp=MarketInfo(OrderSymbol(),MODE_POINT);
   double NewStop=0;

   if(ObjectFind("HTS line")==0 && HTS==0)
      HTS=ObjectGetDouble(0,"HTS line",OBJPROP_PRICE,0);

   if(OrderType()==OP_BUY)
     {
      pBid=MarketInfo(OrderSymbol(),MODE_BID);
      if(HTS!=0.0 && pBid<=HTS)
        {
         OrderClose(OrderTicket(),OrderLots(),pBid,5,Red);
         return;
        }

      if(ATRTrail)
        {
         NewStop=NormalizeDouble(Low[1]-iATR(Symbol(),ATRTF,TSPeriod,1),Digits);
        }
      else
      if(pBid-OrderOpenPrice()>=(TrailingStop+TrailingStep) *pp)
                                NewStop=pBid-TrailingStop*pp;
     }

   if(OrderType()==OP_SELL)
     {
      pAsk=MarketInfo(OrderSymbol(),MODE_ASK);
      if(HTS!=0.0 && pAsk>=HTS)
        {
         OrderClose(OrderTicket(),OrderLots(),pAsk,5,Red);
         return;
        }

      if(ATRTrail)
        {
         NewStop=NormalizeDouble(High[1]+iATR(Symbol(),ATRTF,TSPeriod,1),Digits);
        }
      else
      if(OrderOpenPrice()-pAsk>=(TrailingStop+TrailingStep)*pp)
                                NewStop=pAsk+TrailingStop*pp;
     }

   if(NewStop!=0)
     {
      if(UseHiddenTrailingStop)
        {
         if((OrderType()==OP_BUY && HTS<NewStop) || (OrderType()==OP_SELL && HTS>NewStop) || HTS==0)
            HTS=NewStop;
         if(DrawLines)
           {
            LineColor=TSColor;
            DrawLine("HiddenTS label","HTS","HTS line",LineType,LineColor,HTS);
           }
        }
      else
      if((OrderType()==OP_BUY && OrderStopLoss()<NewStop) || (OrderType()==OP_SELL && OrderStopLoss()>NewStop))
      if(ModifyStopLoss(NewStop,"SL"))
         Print("WUKAR_WB Modified TS Stop Loss");
     }

   if(ObjectFind(0,"HTS line")==0 && ObjectFind(0,"HBE line")==0)
     {
      ObjectDelete(0,"HBE line");
      ObjectDelete(0,"HiddenBE label");
     }

   return;
  }
//+------------------------------------------------------------------+
bool ModifyStopLoss(double ldStopLoss,string Type)
  {
   bool fm;
   fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
   if(fm && UseSound)
      PlaySound(NameFileSound);
   if(!fm)
     {
      int err;
      err=GetLastError();
      Print("Wheelbarrel ",Type," trailing error (",err,")");
      return false;
     }
   return true;
  }
//+------------------------------------------------------------------+
void HiddenTakeProfit()
  {
   bool result=false;
   if(OrderSymbol()==Symbol())
     {
      if(OrderType()==OP_BUY)
        {
         if(UseHiddenTakeProfit)
           {
            if(!checkVariable("TakeProfit",TicketNo))
              {
               StopLevel=NormalizeDouble(Low[1]-iATR(Symbol(),ATRTF,TSPeriod,1),Digits);
               if(TakeProfitMultiplier>0)
                  HTP=OrderOpenPrice()+MathAbs(OrderOpenPrice()-StopLevel)*TakeProfitMultiplier;
               else
                  HTP=OrderOpenPrice()+(HiddenTP1+Commission)*Point;
               setVariable("TakeProfit",TicketNo,HTP);
              }// end if 
            else
               HTP=getVariable("TakeProfit",TicketNo);
            if(Bid>=HTP)
               result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),5,Red);
           }
        }

      if(OrderType()==OP_SELL)
        {
         if(UseHiddenTakeProfit)
           {
            if(!checkVariable("TakeProfit",TicketNo))
              {
               StopLevel=NormalizeDouble(High[1]+iATR(Symbol(),ATRTF,TSPeriod,1),Digits);
               if(TakeProfitMultiplier>0)
                  HTP=OrderOpenPrice()-MathAbs(OrderOpenPrice()-StopLevel)*TakeProfitMultiplier;
               else
                  HTP=OrderOpenPrice()-(HiddenTP1+Commission)*Point;
               setVariable("TakeProfit",TicketNo,HTP);
              }// end if 
            else
               HTP=getVariable("TakeProfit",TicketNo);
            if(Ask<=HTP)
               result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),5,Red);
           }
        }

      if(DrawLines)
        {
         LineColor=TPColor;
         if(UseHiddenTakeProfit)
           {
            DrawLine("HiddenTP label","HTP","HTP line",LineType,LineColor,HTP);
           }
        }
     }
   return;
  }
//+-------------------------------------------------------------------------------------------+
void HiddenStopLoss()
  {
   bool result=false;
   if(OrderSymbol()==Symbol())
     {
      if(OrderType()==OP_BUY)
        {
         HSL=OrderOpenPrice()-(HiddenSL-Commission)*Point;
         if(Bid<=HSL)
            result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),5,Red);
        }

      if(OrderType()==OP_SELL)
        {
         HSL=OrderOpenPrice()+(HiddenSL-Commission)*Point;
         if(Ask>=HSL)
            result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),5,Red);
        }

      if(DrawLines)
        {
         LineColor=SLColor;
         DrawLine("HiddenSL label","HSL","HSL line",LineType,LineColor,HSL);
        }
     }
   return;
  }
//+-------------------------------------------------------------------------------------------+
void EmergencyStopLoss()
  {
   bool result=false;

   if(OrderSymbol()==Symbol())
     {
      if(OrderType()==OP_BUY && OrderStopLoss()==0.0)
         result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-EmergencySL*Point,OrderTakeProfit(),0,clrNONE);
      if(OrderType()==OP_SELL && OrderStopLoss()==0.0)
         result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+EmergencySL*Point,OrderTakeProfit(),0,clrNONE);
     }
   return;
  }
//+-------------------------------------------------------------------------------------------+
void OpenPosition()
  {
   int ans=0;
   if(Open[0]>Close[0])
     {
      ans=OrderSend(Symbol(),OP_BUY,0.01,Ask,25,0,0,NULL,10,Blue); //Opening Buy        
     }
   else
     {
      ans=OrderSend(Symbol(),OP_SELL,0.01,Bid,25,0,0,NULL,10,Blue); //Opening Buy  
     }
   if(ans<0)
     {
      Print("Error when opening a order #",GetLastError());
      Sleep(10000);
      return;
     }
  }
//+-------------------------------------------------------------------------------------------+
//| Subroutine to draw pivot lines and labels                                |                                                                                  |
//+-------------------------------------------------------------------------------------------+
void DrawLine(string LineLabel,string LineText,string TMLine,int LnType,color LnColor,double LinePrice)
  {
   if(ObjectFind(LineLabel)!=0)
     {
      ObjectCreate(LineLabel,OBJ_TEXT,0,Time[0]+Period()*60*ShiftLabel,LinePrice);
      ObjectSetText(LineLabel,LineText,8,"Arial",EMPTY);
     }
   else
     {
      ObjectMove(LineLabel,0,Time[0]+Period()*60*ShiftLabel,LinePrice);
      ObjectSetText(LineLabel,LineText,8,"Arial",EMPTY);
     }

   if(ObjectFind(TMLine)!=0)
     {
      ObjectCreate(TMLine,LnType,0,Time[1],LinePrice,Time[0],LinePrice);
      ObjectSet(TMLine,OBJPROP_STYLE,STYLE_DASHDOTDOT);
      ObjectSet(TMLine,OBJPROP_COLOR,LnColor);
      //         ObjectSet(TMLine, OBJPROP_RAY, true);
     }
   else
     {
      ObjectMove(TMLine,0,Time[1],LinePrice);
      ObjectMove(TMLine,1,Time[0],LinePrice);
     }
  }
//+------------------------------------------------------------------+
void DeleteAllLines()
  {
   ObjectDelete(0,"HiddenSL label");
   ObjectDelete(0,"HSL line");
   ObjectDelete(0,"HiddenTP label");
   ObjectDelete(0,"HTP line");
   ObjectDelete(0,"HiddenBE label");
   ObjectDelete(0,"HBE line");
   ObjectDelete(0,"BE label");
   ObjectDelete(0,"BE line");
   ObjectDelete(0,"HiddenTS label");
   ObjectDelete(0,"HTS line");
   return;
  }
//+------------------------------------------------------------------+
//-----------------------------------------------------
int getOpenOrders()
  {
   int CurrentPairOrders=0;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
         continue;

      if(OrderSymbol()!=Symbol())
         continue;

      if(OrderSymbol()==Symbol())// && OrderMagicNumber() == MagicNumber)
         CurrentPairOrders++;
     }// end for (int i = 0; i < OrdersTotal(); i++)
   return(CurrentPairOrders);
  }// end int getOpenOrders()
//---------------------------------------------------------------
void setVariable(string name,string TicketNo,double value)
  {
   GlobalVariableSet(getFullName(name,TicketNo),value);
  }
//---------------------------------------------------------------
double getVariable(string name,string TicketNo)
  {
   return(GlobalVariableGet(getFullName(name, TicketNo)));
  }
//---------------------------------------------------------------
bool checkVariable(string name,string TicketNo)
  {
   return(GlobalVariableCheck(getFullName(name, TicketNo)));
  }
//---------------------------------------------------------------
string getFullName(string name,string TicketNo)
  {
//   return(WindowExpertName() + Symbol() + Period() + name);
   return(Symbol() + name + TicketNo);
  }
//+------------------------------------------------------------------+
void PlacePendingLine()
  {
   bool DrawLine=false;
   double multiplier=1;
   int NrOfDigits=MarketInfo(Symbol(),MODE_DIGITS);          // Nr. of decimals used by Symbol
   if(NrOfDigits == 5)
      multiplier = 100;
   double D1Open = getVariable("D1Open",1);
   Print("D1Open=",D1Open);

   if(Close[0]>D1Open) // ********** Should be D1 open *********** //
     {
      if(!Trade50Levels)
      {
      double entry_price=NormalizeDouble(MathCeil(Open[0]*(multiplier*10))/10,1);
      entry_price/=multiplier;
      }
      else
      {
      entry_price=NormalizeDouble(MathFloor(Open[0]*(multiplier*100)),0);
      int TestPrice=entry_price;
//      Print("Buy TestPrice=",TestPrice);
      if(TestPrice%5!=0) 
         TestPrice=TestPrice+(5-(TestPrice%5));
//      Print("TestPrice=",TestPrice);
      entry_price=TestPrice;
//      TestPrice=entry_price-MathFloor(entry_price);
//      if(TestPrice>0.95 || TestPrice<0.15)
//         Alert("100/000 is invalid sell price.");
//      else
//        {
         NormalizeDouble(entry_price/=(multiplier*100),Digits);
       }
         if(FirstLevelOnly)
            if(Close[0]<entry_price)
              {
   //      Alert("Buy entry_price=",entry_price);
               DrawLine=true;
              }
         else
               DrawLine=true;
     }
   else
     {
      if(!Trade50Levels)
      {
      entry_price=NormalizeDouble(MathFloor(Open[0]*(multiplier*10))/10,Digits);
      entry_price/=multiplier;
      }
      else
      {
      entry_price=NormalizeDouble(MathFloor(Open[0]*(multiplier*100)),0);
      TestPrice=entry_price;
//      Print("Sell TestPrice=",TestPrice);
      TestPrice=TestPrice-(TestPrice%5);
//      Print("TestPrice=",TestPrice);
      entry_price=TestPrice;
//      if(TestPrice>0.85 || TestPrice<0.05)
//         Alert("900/000 is invalid buy price.");
//      else
//        {
         NormalizeDouble(entry_price/=(multiplier*100),Digits);
      }
         if(FirstLevelOnly)
            if(Close[0]>entry_price)
              {
   //      Alert("Sell entry_price=",entry_price);
               DrawLine=true;
              }
         else
               DrawLine=true;
     }

   if(DrawLine)
     {
      if(ObjectFind("EJS_PL")!=0)
      {
      ObjectCreate("EJS_PL",OBJ_HLINE,0,Close[0],entry_price);
      ObjectSet("EJS_PL",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("EJS_PL",OBJPROP_WIDTH,2);
      ObjectSet("EJS_PL",OBJPROP_BACK,1);
      ObjectSet("EJS_PL",OBJPROP_COLOR,Magenta);
      }
      else
      ObjectMove(0,"EJS_PL",0,Close[0],entry_price);

      Sleep(2000);
      if(WindowScreenShot("shots\\"+TimeDayOfYear(TimeCurrent())+"-"+OrderTime+Symbol()+TimeCurrent()+".gif",1280,640))
         OK="ok";

      LinePlaced=true;
     }
   return(0);
  }
//+------------------------------------------------------------------+
