Close all positions when profit is reached

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Close all positions when profit is reached
ÿþ//+------------------------------------------------------------------+

//|                   Close all positions when profit is reached.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   "1.000"

/*

   barabashkakvn Trading engine 3.143

*/

#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 bool     InpOnlyCurrentSymbol = false;       // Close: 'true' -> current Symbol, 'false' -> all sybmols

input double   InpProfit            = 46;          // Target Profit, in Money

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

input ulong    InpMagic             = 350512800;   // Magic number

//---

bool     m_need_close_all           = false;       // close all positions

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

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

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

//---

   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;

        }

     }

//---

   if(InpSignalsFrequency>=10) // search for trading signals no more than once every 10 seconds

     {

      datetime time_current=TimeCurrent();

      if(time_current-m_last_signal>InpSignalsFrequency)

        {

         m_last_signal=time_current;

         //--- search for trading signals

         if(ProfitAllPositions()>=InpProfit)

           {

            m_need_close_all=true;

            return;

           }

        }

     }

   else

     {

      //--- search for trading signals only at the time of the birth of new bar

      datetime time_0=iTime(Symbol(),Period(),0);

      if(time_0==m_prev_bars)

         return;

      m_prev_bars=time_0;

      //--- search for trading signals

      if(ProfitAllPositions()>=InpProfit)

        {

         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

         if((InpOnlyCurrentSymbol && m_position.Symbol()==Symbol()) || !InpOnlyCurrentSymbol)

            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((InpOnlyCurrentSymbol && m_position.Symbol()==Symbol()) || !InpOnlyCurrentSymbol)

            if(!m_trade.PositionClose(m_position.Ticket())) // close a position

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

  }

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

//| 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((InpOnlyCurrentSymbol && m_position.Symbol()==Symbol()) || !InpOnlyCurrentSymbol)

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

//---

   return(profit);

  }

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

Comments