MultiCurrency Close All

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
MultiCurrency Close All
ÿþ//+------------------------------------------------------------------+

//|                                      MultiCurrency Close All.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.147

*/

#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 group             "Trading settings"

input string   InputSymbol_0     = "EURUSD";    // Symbol 0 (invalid symbol -> OFF)

input string   InputSymbol_1     = "USDJPY";    // Symbol 1 (invalid symbol -> OFF)

input string   InputSymbol_2     = "NZDJPY";    // Symbol 2 (invalid symbol -> OFF)

input double   InpMaxLoss        = -10.0;       // Max Loss

input double   InpTargetProfit   = 30.0;        // Target Profit

input group             "Additional features"

input ulong    InpMagic          = 67468240;    // Magic number

//---

bool     m_use_symbol_0       = false;

bool     m_use_symbol_1       = false;

bool     m_use_symbol_2       = false;

bool     m_need_close_all     = false;       // close all positions

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   m_use_symbol_0=m_symbol.Name(InputSymbol_0);

   if(m_use_symbol_0)

      Print(__FILE__," ",__FUNCTION__," Symbol 0 (",InputSymbol_0,") -> true");

   else

      Print(__FILE__," ",__FUNCTION__," Symbol 0 (",InputSymbol_0,") -> false");

//---

   m_use_symbol_1=m_symbol.Name(InputSymbol_1);

   if(m_use_symbol_1)

      Print(__FILE__," ",__FUNCTION__," Symbol 1 (",InputSymbol_1,") -> true");

   else

      Print(__FILE__," ",__FUNCTION__," Symbol 1 (",InputSymbol_1,") -> false");

//---

   m_use_symbol_2=m_symbol.Name(InputSymbol_2);

   if(m_use_symbol_2)

      Print(__FILE__," ",__FUNCTION__," Symbol 2 (",InputSymbol_2,") -> true");

   else

      Print(__FILE__," ",__FUNCTION__," Symbol 2 (",InputSymbol_2,") -> false");

//---

   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 profit=ProfitAllPositions();

   if(profit<=InpMaxLoss)

     {

      Print(__FILE__," ",__FUNCTION__,", Profit All Positions: ",DoubleToString(profit,2)," <= Max Loss (",DoubleToString(InpMaxLoss,2),")");

      m_need_close_all=true;

      return;

     }

   else

     {

      if(profit>=InpTargetProfit)

        {

         Print(__FILE__," ",__FUNCTION__,", Profit All Positions: ",DoubleToString(profit,2)," >= Target Profit (",DoubleToString(InpTargetProfit,2),")");

         m_need_close_all=true;

         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

        {

         string symbol=m_position.Symbol();

         if((m_use_symbol_0 && symbol==InputSymbol_0) || (m_use_symbol_1 && symbol==InputSymbol_1) ||(m_use_symbol_2 && symbol==InputSymbol_2))

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

        }

//---

   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

        {

         string symbol=m_position.Symbol();

         if((m_use_symbol_0 && symbol==InputSymbol_0) || (m_use_symbol_1 && symbol==InputSymbol_1) || (m_use_symbol_2 && symbol==InputSymbol_2))

            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

        {

         string symbol=m_position.Symbol();

         if((m_use_symbol_0 && symbol==InputSymbol_0) || (m_use_symbol_1 && symbol==InputSymbol_1) || (m_use_symbol_2 && symbol==InputSymbol_2))

            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());

        }

  }

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

Comments