Balance drawdown for one position Close all

Author: Copyright © 2021, Vladimir Karputov
1 Views
0 Downloads
0 Favorites
Balance drawdown for one position Close all
ÿþ//+------------------------------------------------------------------+

//|                  Balance drawdown for one position Close all.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.000"

#property description "If, at least for one position, the balance drawdown ('%') reaches the specified value, we close all positions"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

//--- input parameters

input double   InpDrawdown = 5;        // Balance drawdown for one position ('%')

input ulong    InpMagic    = 20254786; // Magic number (of this expert)

//---

bool     m_need_close_all  = false;    // close all positions

bool     m_init_error      = false;    // error on InInit

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- forced initialization of variables

   m_init_error            = false;    // error on InInit

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(Symbol());

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   if(m_need_close_all)

     {

      if(IsPositionExists())

        {

         CloseAllPositions();

         return;

        }

      else

        {

         m_need_close_all=false;

        }

     }

//---

   double max_drawdown=0.0;

   double balance=AccountInfoDouble(ACCOUNT_BALANCE);

   if(balance>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

           {

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

            if(profit<0.0)

              {

               /*

               balance  -> 100%

               profit   -> x%

               drawdown = 100-x;

               */

               if((-profit)*100.0/balance>=InpDrawdown)

                 {

                  m_need_close_all=true;

                  return;

                 }

              }

           }

  }

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

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

         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

        {

         if(m_position.PositionType()==POSITION_TYPE_BUY)

           {

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

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

           }

         if(m_position.PositionType()==POSITION_TYPE_SELL)

           {

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

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

           }

        }

  }

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

Comments