Close All Main position

Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Close All Main position
ÿþ//+------------------------------------------------------------------+

//|                                      Close All Main position.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.000"

#property description "We have a 'Main position' - it has its own Magic number. "

#property description "As soon as the 'Main position' reaches the target profit, "

#property description "we close the positions indicated in the list"

/*

   barabashkakvn Trading engine 3.151

*/

#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 group             "Main position"

input double   InpProfit            = 15;          // Main position: Profit target (in money deposit)

input ulong    InpMagic             = 1;           // Main position: Magic number

input group             "List positions"

input string   InpList              = "2;3;4";     // List positions (ATTENTION: separator character ';')

//---

bool     m_need_close_all           = false; // close all positions

ulong    arr_list[];

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   string to_split=InpList;      // a string to split into substrings

   string sep=";";               // a separator as a character

   ushort u_sep;                 // the code of the separator character

   string result[];              // an array to get strings

//--- Get the separator code

   u_sep=StringGetCharacter(sep,0);

//--- Split the string to substrings

   int k=StringSplit(to_split,u_sep,result);

//--- Show a comment

   PrintFormat("Strings obtained: %d. Used separator '%s' with the code %d",k,sep,u_sep);

//--- Now output all obtained strings

   if(k>0)

     {

      for(int i=0; i<k; i++)

        {

         PrintFormat("arr_list[%d]=\"%s\"",i,result[i]);

         //--- we leave only the numbers (0-9) and the "."

         string text=result[i];

         StringTrimLeft(text);

         StringTrimRight(text);

         string temp="";

         int len=StringLen(text);

         string numbers="0123456789";

         for(int n=0; n<len; n++)

           {

            bool find=false;

            for(int j=0; j<11; j++)

              {

               if(text[n]==numbers[j])

                 {

                  find=true;

                  break;

                 }

              }

            if(find)

               temp+=StringSubstr(text,n,1);

           }

         long d=StringToInteger(temp);

         int size=ArraySize(arr_list);

         ArrayResize(arr_list,size+1);

         arr_list[size]=d;

        }

     }

   int size=ArraySize(arr_list);

   ArrayResize(arr_list,size+1);

   arr_list[size]=InpMagic;

//---

   /*m_trade.SetExpertMagicNumber(arr_list[0]);

   bool trade_result=false;

   while(!trade_result)

     {

      trade_result=m_trade.Buy(0.01,"EURUSD");

      Sleep(1000);

     }

   m_trade.SetExpertMagicNumber(arr_list[1]);

   trade_result=false;

   while(!trade_result)

     {

      trade_result=m_trade.Buy(0.02,"USDJPY");

      Sleep(1000);

     }

   m_trade.SetExpertMagicNumber(arr_list[2]);

   trade_result=false;

   while(!trade_result)

     {

      trade_result=m_trade.Buy(0.01,"EURPLN");

      Sleep(1000);

     }

   m_trade.SetExpertMagicNumber(arr_list[3]);

   trade_result=false;

   while(!trade_result)

     {

      trade_result=m_trade.Sell(0.02,"EURUSD");

      Sleep(1000);

     }*/

//---

   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>=InpProfit)

     {

      m_need_close_all=true;

     }

  }

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

//| 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(m_position.Magic()==InpMagic)

            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

        {

         int size=ArraySize(arr_list);

         for(int j=0; j<size; j++)

           {

            if(m_position.Magic()==arr_list[j])

               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

        {

         int size=ArraySize(arr_list);

         for(int j=0; j<size; j++)

           {

            if(m_position.Magic()==arr_list[j])

              {

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

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

               break;

              }

           }

        }

  }

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

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