Close All Main Trailing Money Breakeven

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
Series array that contains tick volumes of each bar
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Close All Main Trailing Money Breakeven
ÿþ//+------------------------------------------------------------------+

//|                      Close All Main Trailing Money Breakeven.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.001"

#property description "We have a 'Main position' - it has its own Magic number. "

#property description "As soon as the 'Main position' reaches the target profit, "

#property description "we close the positions indicated in the list"

#property description "--- --- ---"

#property description "Breakeven in the money of the deposit - we monitor the position online and close by the market"

#property description "Breakeven Stop == '0' -> Breakeven OFF"

/*

   barabashkakvn Trading engine 3.153

*/

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

#include <Trade\AccountInfo.mqh>

#include <Trade\DealInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

CSymbolInfo    m_symbol;                     // object of CSymbolInfo class

CAccountInfo   m_account;                    // object of CAccountInfo class

CDealInfo      m_deal;                       // object of CDealInfo class

//+------------------------------------------------------------------+

//| Enum Pips Or Points                                              |

//+------------------------------------------------------------------+

enum ENUM_PIPS_OR_POINTS

  {

   pips=0,     // Pips (1.00045-1.00055=1 pips)

   points=1,   // Points (1.00045-1.00055=10 points)

  };

//--- input parameters

input group             "Trailing Main position"

input ENUM_TIMEFRAMES      InpWorkingPeriod=PERIOD_CURRENT; // Working timeframe

input ENUM_PIPS_OR_POINTS  InpPipsOrPoints=pips;      // Pips Or Points:

input ushort   InpTrailingFrequency    = 10;          // Trailing, in seconds (< "10" -> only on a new bar)

input uint     InpTrailingStop         = 25;          // Trailing Stop (from price to SL) (=='0.0' -> OFF Trailing)

input uint     InpTrailingStep         = 5;           // Trailing Step

input group             "Main position"

input double   InpProfit               = 15;          // Profit target (in money deposit) (<='0.0' -> OFF Profit target)

input ulong    InpMagic                = 1;           // Magic number

input bool     InpTrackPositionClosing = true;        // Track position closing

input group             "List positions"

input string   InpMagicList            = "2;3;4";     // List of magic number positions (ATTENTION: separator character ';')

input group             "Main position Breakeven, in money deposit"

input double   InpBreakevenStop        = 20;          // Breakeven Stop (<='0.0' -> OFF Breakeven)

input double   InpBreakevenStep        = 5;           // Breakeven Step

input group             "Additional features"

input bool     InpPrintLog             = true;        // Print log

input uchar    InpFreezeCoefficient    = 1;           // Coefficient (if Freeze==0 Or StopsLevels==0)

input ulong    InpDeviation            = 10;          // Deviation

//---

double   m_trailing_stop            = 0.0;      // Trailing Stop              -> double

double   m_trailing_step            = 0.0;      // Trailing Step              -> double



double   m_adjusted_point;                      // point value adjusted for 3 or 5 points

bool     m_need_close_all           = false;    // close all positions

datetime m_last_trailing            = 0;        // "0" -> D'1970.01.01 00:00';

datetime m_prev_bars                = 0;        // "0" -> D'1970.01.01 00:00';

int      m_bar_current              = 0;

bool     m_init_error               = false;    // error on InInit



ulong    arr_magic_list[];                      // array magic numbers

long     m_pos_id                   = -1;

double   m_breakeven_step           = -1.0;     // breakeven step

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- forced initialization of variables

   m_trailing_stop            = 0.0;      // Trailing Stop              -> double

   m_trailing_step            = 0.0;      // Trailing Step              -> double

   m_need_close_all           = false;    // close all positions

   m_last_trailing            = 0;        // "0" -> D'1970.01.01 00:00';

   m_prev_bars                = 0;        // "0" -> D'1970.01.01 00:00';

   m_bar_current              = 0;

   m_init_error               = false;    // error on InInit

   ArrayFree(arr_magic_list);

   m_pos_id                   = -1;

   m_breakeven_step           = -1.0;

//---

   if(InpTrailingStop!=0 && InpTrailingStep==0)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      ""@59;8=3 =52>7<>65=: ?0@0<5B@ \"Breakeven Stop\" 8;8 \"Breakeven Step\" <5=LH5 =C;O!":

                      "Trailing is not possible: parameter \"Breakeven Stop\" or \"Breakeven Step\" is less than zero!";

      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters

         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      else // if the Expert Advisor is run on the chart, tell the user about the error

         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

   if(InpBreakevenStop<0.0 || InpBreakevenStep<0.0)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "57C1KB>: =52>7<>65=: ?0@0<5B@ \"Trailing Step\" @025= =C;N!":

                      "Trailing is not possible: parameter \"Trailing Step\" is zero!";

      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters

         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      else // if the Expert Advisor is run on the chart, tell the user about the error

         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   string to_split=InpMagicList;      // a string to split into substrings

   string sep=";";               // a separator as a character

   ushort u_sep;                 // the code of the separator character

   string result[];              // an array to get strings

//--- Get the separator code

   u_sep=StringGetCharacter(sep,0);

//--- Split the string to substrings

   int k=StringSplit(to_split,u_sep,result);

//--- Show a comment

   PrintFormat("Strings obtained: %d. Used separator '%s' with the code %d",k,sep,u_sep);

//--- Now output all obtained strings

   if(k>0)

     {

      for(int i=0; i<k; i++)

        {

         PrintFormat("arr_magic_list[%d]=\"%s\"",i,result[i]);

         //--- we leave only the numbers (0-9) and the "."

         string text=result[i];

         StringTrimLeft(text);

         StringTrimRight(text);

         string temp="";

         int len=StringLen(text);

         string numbers="0123456789";

         for(int n=0; n<len; n++)

           {

            bool find=false;

            for(int j=0; j<11; j++)

              {

               if(text[n]==numbers[j])

                 {

                  find=true;

                  break;

                 }

              }

            if(find)

               temp+=StringSubstr(text,n,1);

           }

         long d=StringToInteger(temp);

         int size=ArraySize(arr_magic_list);

         ArrayResize(arr_magic_list,size+1);

         arr_magic_list[size]=d;

        }

     }

   int size=ArraySize(arr_magic_list);

   ArrayResize(arr_magic_list,size+1);

   arr_magic_list[size]=InpMagic;

//---

   Print(__FUNCTION__,

         ", 'Main position: Magic number' ",IntegerToString(InpMagic),

         ", 'Main position: Track position closing' ",InpTrackPositionClosing,

         ", m_pos_id ",IntegerToString(m_pos_id));

   InitParameters();

//---

   /*m_trade.SetExpertMagicNumber(arr_magic_list[0]);

   bool trade_result=false;

   while(!trade_result)

     {

      trade_result=m_trade.Buy(0.01,"EURUSD");

      Sleep(1000);

     }

   m_trade.SetExpertMagicNumber(arr_magic_list[1]);

   trade_result=false;

   while(!trade_result)

     {

      trade_result=m_trade.Buy(0.02,"USDJPY");

      Sleep(1000);

     }

   m_trade.SetExpertMagicNumber(arr_magic_list[2]);

   trade_result=false;

   while(!trade_result)

     {

      trade_result=m_trade.Buy(0.01,"EURPLN");

      Sleep(1000);

     }

   m_trade.SetExpertMagicNumber(arr_magic_list[3]);

   trade_result=false;

   while(!trade_result)

     {

      trade_result=m_trade.Sell(0.05,"EURUSD");

      Sleep(1000);

     }*/

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

   if(m_init_error)

      return;

//---

   if(m_pos_id==-1)

      InitParameters();

//---

   if(m_need_close_all)

     {

      if(IsPositionExists())

        {

         CloseAllPositions();

         return;

        }

      else

        {

         m_need_close_all=false;

         m_pos_id=-1;

         m_breakeven_step=-1.0;

        }

     }

//---

   double profit=ProfitAllPositions();

   if(m_need_close_all)

      return;

   if(InpProfit>0.0)

      if(profit>=InpProfit)

        {

         m_need_close_all=true;

         return;

        }

//---

   if(m_pos_id==-1)

      return;

   if(InpTrailingFrequency>=10) // trailing no more than once every 10 seconds

     {

      datetime time_current=TimeCurrent();

      if(time_current-m_last_trailing>InpTrailingFrequency)

        {

         Trailing();

         m_last_trailing=time_current;

        }

     }

//--- we work only at the time of the birth of new bar

   if(InpTrailingFrequency<10)

     {

      datetime time_0=iTime(m_symbol.Name(),InpWorkingPeriod,0);

      if(time_0==m_prev_bars)

         return;

      m_prev_bars=time_0;

      if(InpTrailingFrequency<10) // trailing only at the time of the birth of new bar

        {

         Trailing();

        }

     }

  }

//+------------------------------------------------------------------+

//| TradeTransaction function                                        |

//+------------------------------------------------------------------+

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

   if(!InpTrackPositionClosing)

      return;

//--- get transaction type as enumeration value

   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;

//--- if transaction is result of addition of the transaction in history

   if(type==TRADE_TRANSACTION_DEAL_ADD)

     {

      ResetLastError();

      if(HistoryDealSelect(trans.deal))

         m_deal.Ticket(trans.deal);

      else

        {

         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,") error: ",GetLastError());

         return;

        }

      if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)

         if(m_deal.Entry()==DEAL_ENTRY_OUT)

            if(!m_need_close_all)

              {

               if(InpPrintLog)

                  Print("DEAL_ENTRY_OUT, m_deal.PositionId ",IntegerToString(m_deal.PositionId()),", main position Identifier ",IntegerToString(m_pos_id));

               if(m_deal.PositionId()==m_pos_id)

                  m_need_close_all=true;

              }

     }

  }

//+------------------------------------------------------------------+

//| Refreshes the symbol quotes data                                 |

//+------------------------------------------------------------------+

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");

      return(false);

     }

//--- protection against the return value of "zero"

   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");

      return(false);

     }

//---

   return(true);

  }

//+------------------------------------------------------------------+

//| Trailing                                                         |

//|   InpTrailingStop: min distance from price to Stop Loss          |

//+------------------------------------------------------------------+

void Trailing()

  {

   if(InpTrailingStop==0)

      return;

   double freeze=0.0,stops=0.0;

   FreezeStopsLevels(freeze,stops);

   double max_levels=(freeze>stops)?freeze:stops;

   /*

   SYMBOL_TRADE_FREEZE_LEVEL shows the distance of freezing the trade operations

      for pending orders and open positions in points

   ------------------------|--------------------|--------------------------------------------

   Type of order/position  |  Activation price  |  Check

   ------------------------|--------------------|--------------------------------------------

   Buy Limit order         |  Ask               |  Ask-OpenPrice  >= SYMBOL_TRADE_FREEZE_LEVEL

   Buy Stop order          |  Ask               |  OpenPrice-Ask  >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell Limit order        |  Bid               |  OpenPrice-Bid  >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell Stop order         |  Bid               |  Bid-OpenPrice  >= SYMBOL_TRADE_FREEZE_LEVEL

   Buy position            |  Bid               |  TakeProfit-Bid >= SYMBOL_TRADE_FREEZE_LEVEL

                           |                    |  Bid-StopLoss   >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell position           |  Ask               |  Ask-TakeProfit >= SYMBOL_TRADE_FREEZE_LEVEL

                           |                    |  StopLoss-Ask   >= SYMBOL_TRADE_FREEZE_LEVEL

   ------------------------------------------------------------------------------------------

   */

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions

      if(m_position.SelectByIndex(i))

         if(m_position.Magic()==InpMagic)

           {

            double price_current = m_position.PriceCurrent();

            double price_open    = m_position.PriceOpen();

            double stop_loss     = m_position.StopLoss();

            double take_profit   = m_position.TakeProfit();

            double ask           = m_symbol.Ask();

            double bid           = m_symbol.Bid();

            //---

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               if(price_current-price_open>m_trailing_stop+m_trailing_step)

                  if(stop_loss<price_current-(m_trailing_stop+m_trailing_step))

                     if(m_trailing_stop>=max_levels && (take_profit-bid>=max_levels || take_profit==0.0))

                       {

                        if(!m_trade.PositionModify(m_position.Ticket(),

                                                   m_symbol.NormalizePrice(price_current-m_trailing_stop),

                                                   take_profit))

                           if(InpPrintLog)

                              Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify BUY ",m_position.Ticket(),

                                    " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),

                                    ", description of result: ",m_trade.ResultRetcodeDescription());

                        if(InpPrintLog)

                          {

                           RefreshRates();

                           m_position.SelectByIndex(i);

                           PrintResultModify(m_trade,m_symbol,m_position);

                          }

                        continue;

                       }

              }

            else

              {

               if(price_open-price_current>m_trailing_stop+m_trailing_step)

                  if((stop_loss>(price_current+(m_trailing_stop+m_trailing_step))) || (stop_loss==0))

                     if(m_trailing_stop>=max_levels && ask-take_profit>=max_levels)

                       {

                        if(!m_trade.PositionModify(m_position.Ticket(),

                                                   m_symbol.NormalizePrice(price_current+m_trailing_stop),

                                                   take_profit))

                           if(InpPrintLog)

                              Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify SELL ",m_position.Ticket(),

                                    " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),

                                    ", description of result: ",m_trade.ResultRetcodeDescription());

                        if(InpPrintLog)

                          {

                           RefreshRates();

                           m_position.SelectByIndex(i);

                           PrintResultModify(m_trade,m_symbol,m_position);

                          }

                       }

              }

           }

  }

//+------------------------------------------------------------------+

//| Print CTrade result                                              |

//+------------------------------------------------------------------+

void PrintResultModify(CTrade &trade,CSymbolInfo &symbol,CPositionInfo &position)

  {

   Print("File: ",__FILE__,", symbol: ",symbol.Name());

   Print("Code of request result: "+IntegerToString(trade.ResultRetcode()));

   Print("code of request result as a string: "+trade.ResultRetcodeDescription());

   Print("Deal ticket: "+IntegerToString(trade.ResultDeal()));

   Print("Order ticket: "+IntegerToString(trade.ResultOrder()));

   Print("Volume of deal or order: "+DoubleToString(trade.ResultVolume(),2));

   Print("Price, confirmed by broker: "+DoubleToString(trade.ResultPrice(),symbol.Digits()));

   Print("Current bid price: "+DoubleToString(symbol.Bid(),symbol.Digits())+" (the requote): "+DoubleToString(trade.ResultBid(),symbol.Digits()));

   Print("Current ask price: "+DoubleToString(symbol.Ask(),symbol.Digits())+" (the requote): "+DoubleToString(trade.ResultAsk(),symbol.Digits()));

   Print("Broker comment: "+trade.ResultComment());

   Print("Freeze Level: "+DoubleToString(symbol.FreezeLevel(),0),", Stops Level: "+DoubleToString(symbol.StopsLevel(),0));

   Print("Price of position opening: "+DoubleToString(position.PriceOpen(),symbol.Digits()));

   Print("Price of position's Stop Loss: "+DoubleToString(position.StopLoss(),symbol.Digits()));

   Print("Price of position's Take Profit: "+DoubleToString(position.TakeProfit(),symbol.Digits()));

   Print("Current price by position: "+DoubleToString(position.PriceCurrent(),symbol.Digits()));

  }

//+------------------------------------------------------------------+

//| Check Freeze and Stops levels                                    |

//+------------------------------------------------------------------+

void FreezeStopsLevels(double &freeze,double &stops)

  {

//--- check Freeze and Stops levels

   /*

   SYMBOL_TRADE_FREEZE_LEVEL shows the distance of freezing the trade operations

      for pending orders and open positions in points

   ------------------------|--------------------|--------------------------------------------

   Type of order/position  |  Activation price  |  Check

   ------------------------|--------------------|--------------------------------------------

   Buy Limit order         |  Ask               |  Ask-OpenPrice  >= SYMBOL_TRADE_FREEZE_LEVEL

   Buy Stop order          |  Ask               |  OpenPrice-Ask  >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell Limit order        |  Bid               |  OpenPrice-Bid  >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell Stop order         |  Bid               |  Bid-OpenPrice  >= SYMBOL_TRADE_FREEZE_LEVEL

   Buy position            |  Bid               |  TakeProfit-Bid >= SYMBOL_TRADE_FREEZE_LEVEL

                           |                    |  Bid-StopLoss   >= SYMBOL_TRADE_FREEZE_LEVEL

   Sell position           |  Ask               |  Ask-TakeProfit >= SYMBOL_TRADE_FREEZE_LEVEL

                           |                    |  StopLoss-Ask   >= SYMBOL_TRADE_FREEZE_LEVEL

   ------------------------------------------------------------------------------------------



   SYMBOL_TRADE_STOPS_LEVEL determines the number of points for minimum indentation of the

      StopLoss and TakeProfit levels from the current closing price of the open position

   ------------------------------------------------|------------------------------------------

   Buying is done at the Ask price                 |  Selling is done at the Bid price

   ------------------------------------------------|------------------------------------------

   TakeProfit        >= Bid                        |  TakeProfit        <= Ask

   StopLoss          <= Bid                        |  StopLoss          >= Ask

   TakeProfit - Bid  >= SYMBOL_TRADE_STOPS_LEVEL   |  Ask - TakeProfit  >= SYMBOL_TRADE_STOPS_LEVEL

   Bid - StopLoss    >= SYMBOL_TRADE_STOPS_LEVEL   |  StopLoss - Ask    >= SYMBOL_TRADE_STOPS_LEVEL

   ------------------------------------------------------------------------------------------

   */

   double coeff=(double)InpFreezeCoefficient;

   if(!RefreshRates() || !m_symbol.Refresh())

      return;

//--- FreezeLevel -> for pending order and modification

   double freeze_level=m_symbol.FreezeLevel()*m_symbol.Point();

   if(freeze_level==0.0)

      if(InpFreezeCoefficient>0)

         freeze_level=(m_symbol.Ask()-m_symbol.Bid())*coeff;

//--- StopsLevel -> for TakeProfit and StopLoss

   double stop_level=m_symbol.StopsLevel()*m_symbol.Point();

   if(stop_level==0.0)

      if(InpFreezeCoefficient>0)

         stop_level=(m_symbol.Ask()-m_symbol.Bid())*coeff;

//---

   freeze=freeze_level;

   stops=stop_level;

//---

   return;

  }

//+------------------------------------------------------------------+

//| Profit all positions                                             |

//+------------------------------------------------------------------+

double ProfitAllPositions(void)

  {

   double profit=0.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.Magic()==InpMagic)

           {

            profit=m_position.Commission()+m_position.Swap()+m_position.Profit();

            if(InpBreakevenStop>0.0)

              {

               if(m_breakeven_step==-1.0)

                 {

                  if(profit>=InpBreakevenStop)

                    {

                     Print("Profit ",DoubleToString(profit,2)," >= 'Breakeven Stop' (",DoubleToString(InpBreakevenStop,2),")");

                     m_breakeven_step=InpBreakevenStep;

                    }

                 }

               else

                 {

                  if(profit<=m_breakeven_step)

                    {

                     Print("Need close all: Profit ",DoubleToString(profit,2)," <= 'Breakeven Step' (",DoubleToString(m_breakeven_step,2),")");

                     m_need_close_all=true;

                    }

                 }

              }

            break;

           }

//---

   return(profit);

  }

//+------------------------------------------------------------------+

//| 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

        {

         int size=ArraySize(arr_magic_list);

         for(int j=0; j<size; j++)

           {

            if(m_position.Magic()==arr_magic_list[j])

               return(true);

           }

        }

//---

   return(false);

  }

//+------------------------------------------------------------------+

//| Close all positions                                              |

//+------------------------------------------------------------------+

void CloseAllPositions(void)

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

        {

         int size=ArraySize(arr_magic_list);

         for(int j=0; j<size; j++)

           {

            if(m_position.Magic()==arr_magic_list[j])

              {

               if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol

                 {

                  if(InpPrintLog)

                     Print("ERROR: PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());

                 }

               else

                 {

                  if(InpPrintLog)

                     Print("OK: Position Ticket '",IntegerToString(arr_magic_list[j]),

                           "', Position Magic '",IntegerToString(arr_magic_list[j]),"' was closed successfully");

                 }

               break;

              }

           }

        }

  }

//+------------------------------------------------------------------+

//| Init Parameters                                                  |

//+------------------------------------------------------------------+

void InitParameters()

  {

   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.Magic()==InpMagic)

           {

            //---

            ResetLastError();

            if(!m_symbol.Name(m_position.Symbol())) // sets symbol name

              {

               Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");

               return;

              }

            RefreshRates();

            m_trade.SetMarginMode();

            m_trade.SetTypeFillingBySymbol(m_symbol.Name());

            //--- 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;

            if(InpPipsOrPoints==pips) // Pips (1.00045-1.00055=1 pips)

              {

               m_trailing_stop            = InpTrailingStop             * m_adjusted_point;

               m_trailing_step            = InpTrailingStep             * m_adjusted_point;

              }

            else // Points (1.00045-1.00055=10 points)

              {

               m_trailing_stop            = InpTrailingStop             * m_symbol.Point();

               m_trailing_step            = InpTrailingStep             * m_symbol.Point();

              }

            //---

            m_pos_id=m_position.Identifier();

            if(InpPrintLog)

               Print("Main position: Magic number ",IntegerToString(InpMagic),

                     ", main position Identifier ",IntegerToString(m_pos_id),

                     ", main position Ticket ",IntegerToString(m_position.Ticket()));

            break;

           }

  }

//+------------------------------------------------------------------+

Comments