Close all if a loss

Author: Copyright © 2020, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
1 Views
0 Downloads
0 Favorites
Close all if a loss
ÿþ//+------------------------------------------------------------------+

//|                                          Close all if a loss.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.112

*/

#include <Trade\Trade.mqh>

#include <Trade\AccountInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

CAccountInfo   m_account;                    // object of CAccountInfo class

//--- input parameters

input double   InpStart             = 1500;        // Start Equity, in money

input double   InpLoss              = 150;         // Loss Equity, in money

input bool     InpPrintLog          = false;       // Print log

input ulong    InpMagic             = 42967428;    // Magic number

//---

bool     m_need_close_all           = false;       // close all positions

bool     m_stop                     = false;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   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;

         Alert("It is necessary to restart the adviser");

         m_stop=true;

        }

     }

   if(m_stop)

      return;

//---

   if(m_account.Equity()<=InpStart-InpLoss)

      m_need_close_all=true;

//---

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//---



  }

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

//| 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_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol

            if(InpPrintLog)

               Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());

  }

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

Comments