Close All at New Bar

Author: Copyright © 2022, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Close All at New Bar
ÿþ//+------------------------------------------------------------------+

//|                                         Close All at New Bar.mq5 |

//|                              Copyright © 2022, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2022, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.00"

//---

#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 ENUM_TIMEFRAMES   Inp_period     = PERIOD_D1;   // Timeframe new bar

input ulong             InpDeviation   = 10;          // Deviation, in Points (1.00045-1.00055=10 points)

input ulong             InpMagic       = 813;         // EA Magic number

//---

bool     m_need_close_all           = false;       // close all positions

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

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- forced initialization of variables

   m_need_close_all           = false;    // close all positions

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

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(Symbol());

   m_trade.SetDeviationInPoints(InpDeviation);

//---

   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;

        }

     }

  }

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

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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---