//+------------------------------------------------------------------+
//|                                      Virtual Trailing Stop 2.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//|                     https://www.mql5.com/ru/market/product/43516 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property link      "https://www.mql5.com/ru/market/product/43516"
#property version   "2.001"
/*
   barabashkakvn Trading engine 3.118
*/
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input uint              InpStopLoss          = 30;                // Stop Loss, in pips (1.00045-1.00055=1 pips)
input uint              InpTakeProfit        = 50;                // Take Profit, in pips (1.00045-1.00055=1 pips)
input uint              InpTrailingStop      = 5;                 // Trailing Stop (min distance from price to Stop Loss, in pips
input uint              InpTrailingStart     = 5;                 // Minimum profit to start trailing, in pips (1.00045-1.00055=1 pips)
input uint              InpTrailingStep      = 1;                 // Trailing Step, in pips (1.00045-1.00055=1 pips)
//--- HLine
input string            InpNameBuy           = "SL Buy";          // BUY Line name
input string            InpNameSell          = "SL Sell";         // SELL Line name
input ENUM_LINE_STYLE   InpStyle             = STYLE_DASHDOTDOT;  // Line style
//---
input ulong             InpDeviation         = 10;                // Deviation, in points (1.00045-1.00055=10 points)
//---
double         TrallB = 0;
double         TrallS = 0;

double   m_stop_loss                = 0.0;      // Stop Loss                        -> double
double   m_take_profit              = 0.0;      // Take Profit                      -> double
double   m_trailing_stop            = 0.0;      // Trailing Stop                    -> double
double   m_trailing_start           = 0.0;      // Minimum profit to start trailing -> double
double   m_trailing_step            = 0.0;      // Trailing Step                    -> double

double   m_adjusted_point;                      // point value adjusted for 3 or 5 points
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
   RefreshRates();
//---
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(InpDeviation);
//--- tuning for 3 or 5 digits
   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
   m_adjusted_point=m_symbol.Point()*digits_adjust;

   m_stop_loss             = InpStopLoss              * m_adjusted_point;
   m_take_profit           = InpTakeProfit            * m_adjusted_point;
   m_trailing_stop         = InpTrailingStop          * m_adjusted_point;
   m_trailing_start        = InpTrailingStart         * m_adjusted_point;
   m_trailing_step         = InpTrailingStep          * m_adjusted_point;
//---
   if(!HLineCreate(0,InpNameBuy,0,0,clrBlue,InpStyle) || !HLineCreate(0,InpNameSell,0,0,clrRed,InpStyle))
      return(INIT_FAILED);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   HLineDelete(0,InpNameBuy);
   HLineDelete(0,InpNameSell);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int b=0,s=0;
   ulong TicketB=0,TicketS=0;
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name())
           {
            if(!RefreshRates())
               return;
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               b++;
               if(InpStopLoss!=0 && m_symbol.Bid()<=m_position.PriceOpen()-m_stop_loss)
                 {
                  if(m_trade.PositionClose(m_position.Ticket()))
                     continue;
                 }
               if(InpTakeProfit!=0 && m_symbol.Bid()>=m_position.PriceOpen()+m_take_profit)
                 {
                  if(m_trade.PositionClose(m_position.Ticket()))
                     continue;
                 }
               TicketB=m_position.Ticket();
               if(InpTrailingStop>0)
                 {
                  double SL=m_symbol.Bid()-m_trailing_stop;
                  if(SL>=m_position.PriceOpen()+m_trailing_start && (TrallB==0 || TrallB+m_trailing_step<SL))
                     TrallB=SL;
                 }
              }
            if(m_position.PositionType()==POSITION_TYPE_SELL)
              {
               s++;
               if(InpStopLoss!=0 && m_symbol.Ask()>=m_position.PriceOpen()+m_stop_loss)
                 {
                  if(m_trade.PositionClose(m_position.Ticket()))
                     continue;
                 }
               if(InpTakeProfit!=0 && m_symbol.Ask()<=m_position.PriceOpen()-m_take_profit)
                 {
                  if(m_trade.PositionClose(m_position.Ticket()))
                     continue;
                 }
               TicketS=m_position.Ticket();
               if(InpTrailingStop>0)
                 {
                  double SL=m_symbol.Ask()+m_trailing_stop;
                  if(SL<=m_position.PriceOpen()-m_trailing_start && (TrallS==0 || TrallS-m_trailing_step>SL))
                     TrallS=SL;
                 }
              }
           }
//---
   if(b!=0)
     {
      if(b>1)
        {
         string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?
                         "Трейлинг корректно работает только с одной позицией!":
                         "Trailing correctly works with only one position!";
         Comment(err_text);
        }
      else
         if(TrallB!=0)
           {
            string text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?
                        "Трейлинг позиции ":
                        "Trailing position ";
            Comment(text,TicketB);
            HLineMove(0,InpNameBuy,TrallB);
            if(m_symbol.Bid()<=TrallB)
              {
               if(m_position.SelectByTicket(TicketB))
                  if(m_position.Profit()>0.0)
                     if(!m_trade.PositionClose(TicketB))
                       {
                        string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?
                                        "Ошибка закрытия позиции. ":
                                        "Error closing position. ";
                        Comment(err_text," Result Retcode: ",m_trade.ResultRetcode(),
                                ", description of result: ",m_trade.ResultRetcodeDescription());
                       }
              }
           }
     }
   else
     {
      TrallB=0;
      HLineMove(0,InpNameBuy,0.1);
     }
//---
   if(s!=0)
     {
      if(s>1)
        {
         string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?
                         "Трейлинг корректно работает только с одной позицией!":
                         "Trailing correctly works with only one position!";
         Comment(err_text);
        }
      else
         if(TrallS!=0)
           {
            string text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?
                        "Трейлинг позиции ":
                        "Trailing position ";
            Comment(text,TrallS);
            HLineMove(0,InpNameSell,TrallS);
            if(m_symbol.Ask()>=TrallS)
              {
               if(m_position.SelectByTicket(TicketS))
                  if(m_position.Profit()>0.0)
                     if(!m_trade.PositionClose(TicketS))
                       {
                        string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?
                                        "Ошибка закрытия позиции. ":
                                        "Error closing position. ";
                        Comment(err_text," Result Retcode: ",m_trade.ResultRetcode(),
                                ", description of result: ",m_trade.ResultRetcodeDescription());
                       }
              }
           }
     }
   else
     {
      TrallS=0;
      HLineMove(0,InpNameSell,0.1);
     }
//---
   /*
      MQL_PROFILER
      The flag, that indicates the program operating in the code profiling mode

      MQL_TESTER
      The flag, that indicates the tester process

      MQL_OPTIMIZATION
      The flag, that indicates the optimization process

      MQL_VISUAL_MODE
      The flag, that indicates the visual tester process
   */
//if((MQLInfoInteger(MQL_PROFILER) || MQLInfoInteger(MQL_TESTER) ||
//    MQLInfoInteger(MQL_OPTIMIZATION) || MQLInfoInteger(MQL_VISUAL_MODE)) && !IsPositionExists())
//  {
//   m_trade.Buy(m_symbol.LotsMin(),m_symbol.Name());
//   m_trade.Sell(m_symbol.LotsMin(),m_symbol.Name());
//  }
//---
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//---

  }
//+------------------------------------------------------------------+
//| Create the horizontal line                                       |
//+------------------------------------------------------------------+
bool HLineCreate(const long            chart_ID=0,        // chart's ID
                 const string          name="HLine",      // line name
                 const int             sub_window=0,      // subwindow index
                 double                price=0,           // line price
                 const color           clr=clrRed,        // line color
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
                 const int             width=1,           // line width
                 const bool            back=false,        // in the background
                 const bool            selection=false,   // highlight to move
                 const bool            hidden=true,       // hidden in the object list
                 const long            z_order=0)         // priority for mouse click
  {
//--- if the price is not set, set it at the current Bid price level
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value
   ResetLastError();
//--- create a horizontal line
   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))
     {
      Print(__FUNCTION__,
            ": failed to create a horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- set line color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Move horizontal line                                             |
//+------------------------------------------------------------------+
bool HLineMove(const long   chart_ID=0,   // chart's ID
               const string name="HLine", // line name
               double       price=0)      // line price
  {
//--- if the line price is not set, move it to the current Bid price level
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value
   ResetLastError();
//--- move a horizontal line
   if(!ObjectMove(chart_ID,name,0,0,price))
     {
      Print(__FUNCTION__,
            ": failed to move the horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Delete a horizontal line                                         |
//+------------------------------------------------------------------+
bool HLineDelete(const long   chart_ID=0,   // chart's ID
                 const string name="HLine") // line name
  {
//--- reset the error value
   ResetLastError();
//--- delete a horizontal line
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": failed to delete a horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates(void)
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      Print("RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| Is position exists                                               |
//+------------------------------------------------------------------+
bool IsPositionExists(void)
  {
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name())
            return(true);
//---
   return(false);
  }
//+------------------------------------------------------------------+
