eClose_All_if_Profit_or_Loss V20191223

Author: Copyright 2019,SergeyGulyaev
0 Views
0 Downloads
0 Favorites
eClose_All_if_Profit_or_Loss V20191223
ÿþ//+------------------------------------------------------------------+

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

#define NAME_EXPERT "eClose_All_if_Profit_or_Loss_Equity.mq4"  

#define RELIZ      "V20191222"

#property link      "https://www.mql5.com"

#property copyright "Copyright 2019,SergeyGulyaev"

#property strict

#property description "040G0 A>25B=8:0 >3@0=8G820BL C1KB>: 0::0C=B0 =5 >3@0=8G820O ?@81K;L" 

#property description "1.!>25B=8: D8:A8@C5B ?@81K;L 8;8 C1KB>: ?@8 4>AB865=88 7040==>3> 87<5=5=8O M:28B8." 

#property description "2.A;8 CAB0=>2;5=> A:>;L65=85, 8 4>AB83=CB0 7040==0O ?@81K;L, B>: AB0@B>2>5 M:28B8 ?5@5>?@545;O5BAO, ?>78F88 =5 70:@K20NBAO, C1KB>: >BAG8BK205BAO >B =>2>3> 7=0G5=8O."

#property description "3.@8 D8:A0F88 70:@K20NBAO B>;L:> @K=>G=K5 ?>78F88." 

#property script_show_inputs

//---

#include <stdlib.mqh>

//#include <mylib.mqh>

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

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

//--- input parameters

//--- parameters

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

//| Expert initialization function                                   |

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

int OnInit()

{  

   RefreshRates();  

   SetBeginningDayEquity();

   Alert("-------");

   Alert(" 01>B0 =0 AGQB5 - ",IntegerToString(AccountNumber()));

   Alert("<O 3;>10;L=>9 ?5@5<5==>9 - ",Name_GV_start_equity());

   Alert("5=L <5AOF0 - ",DoubleToStr(GlobalVariableGet(Name_GV_day()),0));

   Alert("(;O ?@>A<>B@0 A?8A:0 3;>10;L=KE ?5@5<5==KE =06<8B5 F3)");

   Alert("-------");

//---

   string _txt = "";

   if(GlobalVariableCheck(Name_GV_start_equity()))

   {

      // 5A;8 ?5@5<5==0O AB0@B>2>3> M:28B8 ACI5AB2C5B

      Alert("!B0@B>2K9 M:28B8 >?@545;Q= 2 ?@54K4CI8E A50=A0E: "+DoubleToStr(GlobalVariableGet(Name_GV_start_equity()),2));

      if(AccountEquity() - GlobalVariableGet(Name_GV_start_equity()) >= fix_profit && fix_profit != 0 && AUTO_CHENGE_START_EQUITY == false)

      {

         _txt = "2=8<0=85 ?@8 ?@>4>;65=88 @01>BK M:A?5@B0 eCombo @K=>G=K5 >@45@0 1C4CB 02B><0B8G5A:8 70:@KBK !  +,., GB>1K 871560BL 8E 70:@KB8O =5>1E>48<> ?5@5>?@545;8BL AB0@B>2K9 M:28B8 " + DoubleToStr((AccountEquity()-GlobalVariableGet(Name_GV_start_equity())),2)+" \n?5@5>?@545;8BL AB0@B>2K9 M:28B8?";

         switch(MessageBox(_txt, "!", MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON1))

         { 

            case IDYES: 

               GlobalVariableDel(Name_GV_start_equity());

               break;

            case IDNO: 

               break; 

            case IDCANCEL: 

               ExpertRemove(); 

               break;

            default: 

               GlobalVariableDel(Name_GV_start_equity());

               break;

         }

      }

      if(GlobalVariableGet(Name_GV_start_equity()) - AccountEquity() >= fix_loss && fix_loss != 0)

      {

         _txt = "2=8<0=85 ?@8 ?@>4>;65=88 @01>BK M:A?5@B0 eCombo 2A5 @K=>G=K5 >@45@0 1C4CB 02B><0B8G5A:8 70:@KBK ! #+", GB>1K 871560BL 8E 70:@KB8O =5>1E>48<> ?5@5>?@545;8BL AB0@B>2K9 M:28B8 " + DoubleToStr((GlobalVariableGet(Name_GV_start_equity()) - AccountEquity()),2)+" ?5@5>?@545;8BL AB0@B>2K9 M:28B8?";

         switch(MessageBox(_txt, "!", MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON1))

         { 

            case IDYES: 

               GlobalVariableDel(Name_GV_start_equity());

               break;

            case IDNO: 

               break; 

            case IDCANCEL: 

               ExpertRemove(); 

               break;

            default: 

               GlobalVariableDel(Name_GV_start_equity());

               break;

         }

      }

   }

   

   if(SetAccountEquity()) return(INIT_SUCCEEDED);

   else return(REASON_INITFAILED);

//---

}

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

{

//---

   Comment("");

//---      

}

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

//| Expert tick function                                             |

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

void OnTick(){

   Comment(NAME_EXPERT,RELIZ);

// 4;O @CG=>3> ?5@5>?@545;5=8O ?@8 C40;5=88 3;>10;L=>9 ?5@5<5==>9 G5@57 F3

   SetAccountEquity();

//---      

   SetBeginningDayEquity();

//---      

   FixProfitOrChengeStartEquity();

//---      

   FixLoss();

//---  

   ShowInfo();

//---       

}

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

//+--

//---

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

string TF_To_String(int TF)

{

//---

   switch(TF)

   {

      case PERIOD_M1:   return ("PERIOD_M1");

      case PERIOD_M5:   return ("PERIOD_M5");

      case PERIOD_M15:  return ("PERIOD_M15");

      case PERIOD_M30:  return ("PERIOD_M30");

      case PERIOD_H1:   return ("PERIOD_H1");

      case PERIOD_H4:   return ("PERIOD_H4");

      case PERIOD_D1:   return ("PERIOD_D1");

      case PERIOD_W1:   return ("PERIOD_W1");

      case PERIOD_MN1:  return ("PERIOD_MN1");

   }

return ("");

}

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

//+--

//+--

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

//+--

bool Close_All_Open(int _reson=0)  

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

// 70:@K205B 2A5 @K=>G=K5 >@45@0 

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

{ 

string   _txt = "";

   switch(_reson)

   {

      case 1: 

         _txt = " ?@81K;L";

         break;

      case 2: 

         _txt = " C1KB>:";

         break;

      default: 

         _txt = " ?@8G8=0 =5 8725AB=0";

   }

   bool FLAG=true;

//---      

   for(int i=OrdersTotal()-1; i>=0 && FLAG == true; i--) 

   {  if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

      {  RefreshRates(); 

         if(OrderType() == OP_BUY )

         {  for(int x=1;x<=10 && FLAG == true;x++)

            {  if(OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_BID), 20, CLR_NONE))

               {  FLAG=true;

                  break;

               }

               else FLAG=false;

            }

         }

         if(OrderType() == OP_SELL)

         {  for(int x=1;x<=10 && FLAG == true;x++) 

            {  if(OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_ASK), 20, CLR_NONE))

               {  FLAG=true;

                  break;

               }               

               else FLAG=false;

            }

         }

      } 

   }

   if(FLAG)

   {

      Alert("2A5 @K=>G=K5 ?>78F88 70:@KBK: " + TimeToString(TimeCurrent(), TIME_SECONDS)+_txt);

   }

   else

   {

      Alert(">H81:0 ?@8 70:@KB88 "+ ErrorDescription(GetLastError()) + TimeToString(TimeCurrent(), TIME_SECONDS)+_txt);

   }

//---         

   return(FLAG);

}

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

//+--

//+--

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

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

void SetBeginningDayEquity()

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

// 87<5=O5B M:28B8 0::0C=B0 =0 <><5=B ?5@2>3> 2E>40 70 45=L

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

{

   if(!GlobalVariableCheck(Name_GV_day()))

   {

      GlobalVariableSet(Name_GV_day(),Day());

   }

   if(!GlobalVariableCheck(Name_GV_equity_beginning_day()))

   {

      GlobalVariableSet(Name_GV_equity_beginning_day(),AccountEquity());

   }

//--- 5A;8 45=L A<5=8;AO 

   if(GlobalVariableGet(Name_GV_day())!= Day())

   {

      GlobalVariableSet(Name_GV_day(),Day());

      GlobalVariableSet(Name_GV_equity_beginning_day(),AccountEquity());

   }

   return;

}

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

//+--

//+--

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

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

bool SetAccountEquity()

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

// 70405B AB0@B>2K9 M:28B8

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

{

// >?@545;O5< 8<O ?5@5<5==>9 4;O E@0=5=8O AB0@B>2>3> M:28B8

// 5A;8 ?5@5<5==0O AB0@B>2>3> M:28B8 >BACBAB2C5B,

   if(!GlobalVariableCheck(Name_GV_start_equity()))

   {

// A>740Q< 5Q 8 A>E@0=O5< 2 =59 B5:CI55 7=0G5=85 M:28B8 

// 2 40;L=59H5< DC=:F88 ?@8 >B@01>B:5 ?> CA;>28O< 4>;6=K C40;OBL 3;>10;L=CN ?5@5<5==CN   

// A ?@54K4CI8< M:28B8 8 >=0 A>740QBAO A =>2K< 7=0G5=85<

      if(GlobalVariableSet(Name_GV_start_equity(),AccountEquity())==0)

      {

         Alert("H81:0 8=8F80;870F88 3;>10;L=>9 ?5@5<5==>9");

         return(false);

      }

      else 

         Alert("!B0@B>2K9 M:28B8 >?@545;Q= 2 B5:CI5< A50=A5: "+DoubleToStr(GlobalVariableGet(Name_GV_start_equity()),2));

         return(true);

   }

   return(true);

}

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

//+--

//+--

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

extern int      fix_profit  =  20;// 25;8G8=0 D8:A8@C5<>3> ?@>D8B0 2 20;NB5 45?> 

extern bool     AUTO_CHENGE_START_EQUITY = false;//TRUE - A:>;L65=85 70 ?@81K;LN

//|---

void FixProfitOrChengeStartEquity(){

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

//    D8:A8@C5B ?@>D8B 8;8 ?5@5>?@545;O5B AB0@B>2K9 M:28B8 5A;8 2:;NG5= 02B><0B

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

//---      

   if(AccountEquity()-GlobalVariableGet(Name_GV_start_equity()) >= fix_profit && fix_profit>0) 

   {

      if(AUTO_CHENGE_START_EQUITY)

      {  

         GlobalVariableSet(Name_GV_start_equity(),AccountEquity());

         // C402:0 ?> C1KB:C

         if(fix_loss > fix_profit+10) fix_loss = fix_loss-fix_profit;

         //---

         Alert("AB0@B>2K9 M:28B8 C25;8G5= 157 D8:A0F88: "+DoubleToStr(GlobalVariableGet(Name_GV_start_equity()),2));

         SetAccountEquity();

      }

      else 

      {

         if(Close_All_Open(1)) 

         {

            GlobalVariableDel(Name_GV_start_equity());

            SetAccountEquity();

         }

      }

   }

//---         

}

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

//+--

//+--

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

extern int     fix_loss    =  100;// 25;8G8=0 D8:A8@C5<>3> C1KB:0 2 20;NB5 45?> 

//|---

void FixLoss(){

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

//    D8:A8@C5B C1KB>:

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

//---         

   if(GlobalVariableGet(Name_GV_start_equity()) - AccountEquity() >= fix_loss && fix_loss!=0) 

   {

      if(Close_All_Open(2)) 

      {

            GlobalVariableDel(Name_GV_start_equity());

            SetAccountEquity();

      }

   }

//---            

}

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

//+--

//+--

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

//|---

void ShowInfo(){

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

//    2K2>48B 8=D>@<0F8N

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

//---         

string   str1 = "", 

         str2 = "", 

         str3 = "",

         str4 = "",

         str5 = "";

   str1 = "start_equity: "+DoubleToStr(GlobalVariableGet(Name_GV_start_equity()),2);

   if(fix_profit!=0) str2 = "  fix_profit: "+DoubleToStr(GlobalVariableGet(Name_GV_start_equity())+fix_profit,2);

   else str2 = "   fix_profit =5 CAB0=>2;5=";

   if(fix_loss  !=0) str3 = "  fix_loss: "+DoubleToStr(GlobalVariableGet(Name_GV_start_equity())-fix_loss,2);

   else str3 = "   fix_loss =5 CAB0=>2;5=";

   str4 = "\nequity =0G0;0 4=O: "+DoubleToStr(GlobalVariableGet(Name_GV_equity_beginning_day()),2);

   if(AUTO_CHENGE_START_EQUITY) str5 = "   @568< A:>;L65=8O - 70:@KB85 ?@>87>94QB ?> C1KB:C =0 >B:0B5 8;8 @072>@>B5";

   else str5 = "   @568< D8:A0F88";

   Comment(str1 + str2 + str3 + str4 + str5);

}

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

//+--

//+--

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

string Name_GV_start_equity()

{

   string Name="";

   Name = "GV_start_equity" + IntegerToString(AccountNumber());

   return(Name);

}

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

//+--

//+--

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

string Name_GV_equity_beginning_day()

{

   string Name="";

   Name = "GV_equity_beginning_day" + IntegerToString(AccountNumber());

   return(Name);

}

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

//+--

//+--

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

string Name_GV_day()

{

   string Name="";

   Name = "GV_day" + IntegerToString(AccountNumber());

   return(Name);

}

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

//+--

Comments