Market Capture

Author: Copyright © 2010, Trishkin Artyom A.
Price Data Components
Series array that contains the lowest prices of each barSeries array that contains the highest prices of each barSeries array that contains tick volumes of each bar
0 Views
0 Downloads
0 Favorites
Market Capture
ÿþ//+------------------------------------------------------------------+

//|                      Market Capture(barabashkakvn's edition).mq5 |

//|                             Copyright © 2010, Trishkin Artyom A. |

//|                                           support@goldsuccess.ru |

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

#property copyright "Copyright © 2010, Trishkin Artyom A."

#property link      "support@goldsuccess.ru"

#property version   "1.000"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>  

#include <Trade\AccountInfo.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

CAccountInfo   m_account;                    // account info wrapper

//--- input parameters

input string P_Expert= "-- Experts Variables --";

input bool   TradeBuy=true;                                   // ">@3>2;O 2 Buy, true = @07@5H5=0, false = =5 @07@5H5=0

input bool   TradeSell=true;                                  // ">@3>2;O 2 Sell, true = @07@5H5=0, false = =5 @07@5H5=0

input bool   UseEquClose=true;                                // A?>;L7>20BL ;8 D-F8N 70:@KB8O ?> M:28B8 true = 40, false = =5B

input bool   TrackLossEquity=true;                            // A?>;L7>20BL ;8 70:@KB85 ?> M:28B8 2 ?@>A04:5 true = 40, false = =5B

input double   InpLots                    = 0.1;               // Lots

input ushort   InpTakeProfit              = 10;                // Take Profit (in pips)

input ushort   InpDistanceOpenPositions   = 10;                // Distance between positions (in pips)

input double PercentEquityForClose=5.0;                       // @>F5=B ?@8@>AB0 M:28B8 4;O 70:@KB8O C1KB>G=KE ?>78F89

input double PercentLossEquityForClose=5.0;                   // @>F5=B ?045=8O M:28B8 4;O 70:@KB8O C1KB>G=KE ?>78F89

input int    NumberLossPoseForCloseEquUP=5;                   // >;8G5AB2> 70:@K205<KE C1KB>G=KE ?>78F89 ?@8 C25;8G5=88 M:28B8

input int    NumberLossPoseForCloseEquDN=5;                   // >;8G5AB2> 70:@K205<KE C1KB>G=KE ?>78F89 ?@8 ?@>A04:5 M:28B8

//--- 0@0<5B@K 8A?>;=5=8O B>@3>2KE ?@8:07>2

input string P_Performanc="-- Parameters for Trades --";

input ulong    m_magic=9938165;           // magic number

//---

ulong          m_slippage=10;                // slippage

//---

int      Level_old,Level_new,TradeDirection;

int      NumberUpLevels=1;                                     // >;8G5AB2> C@>2=59 2KH5 F5=K

int      NumberDnLevels=1;                                     // >;8G5AB2> C@>2=59 =865 F5=K

int      MassUpLen,MassDnLen;

string   Prefix,Exp_Name,NameGL_Equ,NameGL_Add,NameGL_Loss,PriceFirstSell;

color    clUP,clDN;

double   EquStart,EquAdd,EquClose,EquLoss,EquPercLoss;

double   CenterLevel,GL_CenterLevel;

double   m_adjusted_point;             // point value adjusted for 3 or 5 points

double   ExtTakeProfit=0;

double   ExtDistanceOpenPositions=0;



double   MassUpLev[];

double   MassDnLev[];

double   MassCtLev[1];

double   HighestLev;

double   LowestLev;

bool     CloseFact=true;

bool     FirstStart=true;

//--- 5@5<5==K5 4;O DC=:F89

bool   AllMessages,PrintEnable;

bool   gbDisabled=false;           // $;03 1;>:8@>2:8 A>25B=8:0

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   if(!m_symbol.Name(Symbol())) // sets symbol name

      return(INIT_FAILED);

   RefreshRates();



   string err_text="";

   if(!CheckVolumeValue(InpLots,err_text))

     {

      Print(__FUNCTION__,", ERROR: ",err_text);

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

   m_trade.SetExpertMagicNumber(m_magic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

   m_trade.SetDeviationInPoints(m_slippage);

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)

      digits_adjust=10;

   m_adjusted_point=m_symbol.Point()*digits_adjust;



   ExtTakeProfit           = InpTakeProfit            * m_adjusted_point;

   ExtDistanceOpenPositions= InpDistanceOpenPositions * m_adjusted_point;

//---

   Level_old=m_symbol.StopsLevel();             // 8=8<. 48AB0=8F8O CAB0=>2:8 AB>?>2

   if(Level_old==0)

      Level_old=(int)((m_symbol.Ask()-m_symbol.Bid())/Point()*3.0);

//---

   Exp_Name=MQLInfoString(MQL_PROGRAM_NAME);    // <O M:A?5@B0

   Prefix=Exp_Name+"_M"+EnumToString(Period()); // @5D8:A 4;O 8<Q= >1J5:B>2

   NameGL_Equ=Prefix+"_GL_Equ";

   NameGL_Add=Prefix+"_GL_Add";

   NameGL_Loss=Prefix+"_GL_Loss";

   PriceFirstSell=Prefix+"_GL_PFSell";

   if(!GlobalVariableCheck(NameGL_Equ)) // A;8 =5BC 3;>10;L=>9 ?5@5<5==>9 B5@<8=0;0 A 8<5=5< NameGL_Equ

     {

      EquStart=m_account.Equity();                                // !B0@B>2K9 M:28B8 = M:28B8

      EquAdd=NormalizeDouble(EquStart/100.0*PercentEquityForClose,m_symbol.Digits()); // @>F5=B >B A@54AB2, =0 :>B>@K9 >=8 4>;6=K C25;8G8BLAO 4;O 70:@KB8O

      EquPercLoss=NormalizeDouble(EquStart/100.0*PercentLossEquityForClose,m_symbol.Digits());// @>F5=B A@54AB2, ?@8 ?045=88 =0 :>B>@K9 70:@KBL C1. ?>7K

      GlobalVariableSet(NameGL_Equ,EquStart);                  // !>74048< 3;>10;L=CN ?5@5<5==CN B5@<8=0;0 8 ?@8A2>8< 59 7=0G5=85 M:28B8

      GlobalVariableSet(NameGL_Add,EquAdd);

      GlobalVariableSet(NameGL_Loss,EquLoss);

     }

   else

     {

      EquStart=GlobalVariableGet(NameGL_Equ);      // A;8 ?5@5<5==0O B5@<8=0;0 C65 5ABL, B> AB0@B>2K9 M:28B8 = 7=0G5=8N MB>9 ?5@5<5==>9

      EquAdd=GlobalVariableGet(NameGL_Add);

      EquPercLoss=GlobalVariableGet(NameGL_Loss);

     }

   double p1=GlobalVariableGet(NameGL_Equ);

   double p2=GlobalVariableGet(NameGL_Add);

   double p3=GlobalVariableGet(NameGL_Loss);

   EquClose=NormalizeDouble(p1+p2,m_symbol.Digits());

   EquLoss=NormalizeDouble(p1-p3,m_symbol.Digits());

   if(PercentEquityForClose<=0)

     {

      Print(__FUNCTION__," ERROR @>F5=B ?@8@>AB0 M:28B8 4;O 70:@KB8O C1KB>G=KE ?>78F89: ",PercentEquityForClose);

      return(INIT_PARAMETERS_INCORRECT);

     }

   if(NumberLossPoseForCloseEquUP<=0)

     {

      Print(__FUNCTION__," ERROR >;8G5AB2> 70:@K205<KE C1KB>G=KE ?>78F89 ?@8 C25;8G5=88 M:28B8: ",NumberLossPoseForCloseEquUP);

      return(INIT_PARAMETERS_INCORRECT);

     }

   if(NumberLossPoseForCloseEquDN<=0)

     {

      Print(__FUNCTION__," ERROR >;8G5AB2> 70:@K205<KE C1KB>G=KE ?>78F89 ?@8 ?@>A04:5 M:28B8: ",NumberLossPoseForCloseEquUP);

      return(INIT_PARAMETERS_INCORRECT);

     }

   if(InpDistanceOpenPositions==0 || InpDistanceOpenPositions*m_adjusted_point<=Level_old*m_symbol.Point())

      ExtDistanceOpenPositions=(Level_old+1)*m_symbol.Point();

   if(InpTakeProfit*m_adjusted_point<=Level_old*m_symbol.Point())

      ExtTakeProfit=(Level_old+1)*m_symbol.Point();

//-- 0?>;=5=85 <0AA82>2 40==K<8 > F5=>2KE C@>2=OE

   ArrayResize(MassUpLev, NumberUpLevels);                     // #25;8G8< @07<5@K <0AA82>2 ...

   ArrayResize(MassDnLev, NumberDnLevels);                     // ... ?>4 :>;8G5AB2> C@>2=59

   ArrayInitialize(MassCtLev, 0);                              // =8F80;878@C5< <0AA82 =C;O<8

   ArrayInitialize(MassUpLev, 0);                              // =8F80;878@C5< <0AA82 =C;O<8

   ArrayInitialize(MassDnLev, 0);                              // =8F80;878@C5< <0AA82 =C;O<8

   CenterLevel=m_symbol.Ask();

   MassCtLev[0]=CenterLevel;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//--- C40;5=8O 2A5E >1J5:B>2, ?>AB@>5==KE A>25B=8:>< =0 3@0D8:5

   ObjectsDeleteAll(0,Prefix,0);

//--- >=5F 1;>:0 C40;5=8O 2A5E >1J5:B>2, ?>AB@>5==KE A>25B=8:>< =0 3@0D8:5

//if(!IsTesting())

//{

   Comment("");// #40;5=85 :><<5=B0@852 (=5 ?@8 >B;04:5)

               //}                               

//else if(IsTesting()) // A;8 2 B5AB5@5, B> ...

//if(GlobalVariableCheck(NameGL_Equ))

//  {                       // ... 5A;8 5ABL 3;>10;L=0O ?5@5<5==0O B5@<8=0;0 A 8<5=5< NameGL_Equ ...

//   GlobalVariableDel(NameGL_Equ);                           // ... C40;8< 5Q

//   GlobalVariableDel(NameGL_Add);

//   GlobalVariableDel(NameGL_Loss);

//   GlobalVariableDel(PriceFirstSell);

//  }

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(!RefreshRates())

      return;

//---

   Level_new=m_symbol.StopsLevel();                      // >A;54=55 <8=8<0;L=>5 7=0G5=85 C@>2=O CAB0=>2:8 AB>?>2

   if(Level_new==0)

      Level_new=(int)((m_symbol.Ask()-m_symbol.Bid())/Point()*3.0);

   if(Level_old!=Level_new) // >2>5 =5 @02=> AB0@><C, 7=0G8B 87<5=8;AO C@>25=L                         

      Level_old=Level_new; // >2>5 "AB0@>5 7=0G5=85" 

   if(InpDistanceOpenPositions*m_adjusted_point<=Level_new*m_symbol.Point())

      ExtDistanceOpenPositions=(Level_new+1)*m_symbol.Point();

   if(InpTakeProfit*m_adjusted_point<=Level_new*m_symbol.Point())

      ExtTakeProfit=(Level_new+1)*m_symbol.Point();

//--- K2>4 48AB0=F89 StopLevel =0 3@0D8:

   string RectUP=Prefix+"_RectUp_StLevel";

   double pUP1=m_symbol.Ask();

   double pUP2=m_symbol.Ask()+Level_new*m_symbol.Point();

   RectangleCreate(0,RectUP,0,

                   iTime(m_symbol.Name(),Period(),3),pUP1,

                   iTime(m_symbol.Name(),Period(),0)+3*PeriodSeconds(),pUP2,clrFireBrick);

   string RectDN=Prefix+"_RectDn_StLevel";

   double pDN1=m_symbol.Bid();

   double pDN2=m_symbol.Bid()-Level_new*m_symbol.Point();

   RectangleCreate(0,RectDN,0,

                   iTime(m_symbol.Name(),Period(),3),pDN1,

                   iTime(m_symbol.Name(),Period(),0)+3*PeriodSeconds(),pDN2,clrFireBrick);

   Informations();

//--- B:@KB85 ?5@2>9 ?>78F88 Sell

   if(FirstStart)

     {                                              // A;8 MB> ?5@2K9 AB0@B A>25B=8:0

      if(!GlobalVariableCheck(PriceFirstSell))

        {

         //--- A;8 =5B 3;>10;L=>9 ?5@5<. B5@<8=0;0 PriceFirstSell (=5 ?5@5703@C7:0)

         GlobalVariableSet(PriceFirstSell,0);                     // !B028< 5Q 2 0

         double tp=m_symbol.Bid()-ExtTakeProfit;

         string New_Comm="5@20O_?>78F8O_Sell";

         Print("5@2K9 AB0@B: >B:@K205< Sell");

         if(OpenSell(0.0,tp,New_Comm))

            GlobalVariableSet(PriceFirstSell,m_trade.ResultPrice());

        }

     }

//--- BA;56820=85 C@>2=59 >B=>A8B5;L=> F5=K

   SetLevels();

   if(m_symbol.Ask()>CenterLevel+ExtDistanceOpenPositions)

     {

      CenterLevel=CenterLevel+ExtDistanceOpenPositions;

      MassCtLev[0]=CenterLevel;

      SetLevels();

     }

   if(m_symbol.Bid()<CenterLevel-ExtDistanceOpenPositions)

     {

      CenterLevel=CenterLevel-ExtDistanceOpenPositions;

      MassCtLev[0]=CenterLevel;

      SetLevels();

     }

   if(m_symbol.Ask()-CenterLevel>0.5*m_adjusted_point && 

      CenterLevel-iLow(m_symbol.Name(),Period(),1)>=0.5*m_adjusted_point)

      OpenPosOnTradeMode(POSITION_TYPE_BUY,CenterLevel);

   if(CenterLevel-m_symbol.Bid()>0.5*m_adjusted_point && 

      iHigh(m_symbol.Name(),Period(),1)-CenterLevel>=0.5*m_adjusted_point)

      OpenPosOnTradeMode(POSITION_TYPE_SELL,CenterLevel);

//--- BA;56820=85 M:28B8 8 70:@KB85 C1KB>G=KE ?>78F89

   int    TotalLoss=0;

   int    NumPoseForClose=0;

   double loss=0;

   double p1=GlobalVariableGet(NameGL_Equ);

   double p2=GlobalVariableGet(NameGL_Add);

   double p3=GlobalVariableGet(NameGL_Loss);

   EquClose=p1+p2;

   EquLoss=p1-p3;

   if(UseEquClose)

     {

      if(m_account.Equity()>=EquClose)

        {

         EquStart=m_account.Equity();

         GlobalVariableSet(NameGL_Equ,EquStart);

         int count_buys=0;

         int count_sells=0;

         CalculateLossPositions(count_buys,count_sells);

         if(count_buys+count_sells==0)

           {

            Print("!@54AB20 4>AB83;8 7040==>3> 7=0G5=8O ",DoubleToString(EquClose,2),", C1KB>G=KE ?>78F89 =5B");

            CloseFact=false;

            return;

           }

         if(NumberLossPoseForCloseEquUP==1)

           {

            string type="";

            if(ClosePosWithMaxLossInCurrency(loss,type))

               Print("!@54AB20 4>AB83;8 7040==>3> 7=0G5=8O ",DoubleToString(EquClose,2),

                     ", 70:@K;8 ?>78F8N ",type," A =081>;LH8< C1KB:>< (",DoubleToString(loss,2),")");

           }

         else if(NumberLossPoseForCloseEquUP>1)

           {

            TotalLoss=count_buys+count_sells;

            if(TotalLoss<NumberLossPoseForCloseEquUP)

               NumPoseForClose=TotalLoss;

            else

               NumPoseForClose=NumberLossPoseForCloseEquUP;

            Print("!@54AB20 4>AB83;8 7040==>3> 7=0G5=8O ",DoubleToString(EquClose,2),

                  ", 70:@K205< ",NumPoseForClose," C1KB>G=KE ?>78F89");

            double loss_temp=0;

            string type_temp="";

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

               ClosePosWithMaxLossInCurrency(loss_temp,type_temp);

           }

         CloseFact=false;

        }

      else if(m_account.Equity()<=EquLoss)

        {

         if(TrackLossEquity)

           {

            int count_buys=0;

            int count_sells=0;

            CalculateLossPositions(count_buys,count_sells);

            if(count_buys+count_sells==0)

              {

               Print("@>A04:0 1>;LH5 7040==>3> 7=0G5=8O ",DoubleToString(EquLoss,2),", C1KB>G=KE ?>78F89 =5B");

               CloseFact=false;

               return;

              }

            if(NumberLossPoseForCloseEquDN==1)

              {

               string type="";

               if(ClosePosWithMaxLossInCurrency(loss,type))

                  Print("@>A04:0 1>;LH5 7040==>3> 7=0G5=8O ",DoubleToString(EquLoss,2),

                        ", 70:@K;8 ?>78F8N ",type," A =081>;LH8< C1KB:>< (",DoubleToString(loss,2),")");

              }

            else if(NumberLossPoseForCloseEquDN>1)

              {

               TotalLoss=count_buys+count_sells;

               if(TotalLoss<NumberLossPoseForCloseEquDN)

                  NumPoseForClose=TotalLoss;

               else

                  NumPoseForClose=NumberLossPoseForCloseEquDN;

               Print("@>A04:0 1>;LH5 7040==>3> 7=0G5=8O ",DoubleToString(EquLoss,2),

                     ", 70:@K205< ",NumPoseForClose," C1KB>G=KE ?>78F89");

               double loss_temp=0;

               string type_temp="";

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

                  ClosePosWithMaxLossInCurrency(loss_temp,type_temp);

              }

            CloseFact=true;

            EquStart=m_account.Equity();              // !B0@B>2K9 M:28B8 = M:28B8

            EquPercLoss=EquStart/100.0*PercentLossEquityForClose;// @>F5=B A@54AB2, ?@8 ?045=88 =0 :>B>@K9 70:@KBL C1. ?>7K

            GlobalVariableSet(NameGL_Equ,EquStart);   // !>74048< 3;>10;L=CN ?5@5<5==CN B5@<8=0;0 8 ?@8A2>8< 59 7=0G5=85 M:28B8

            GlobalVariableSet(NameGL_Loss,EquPercLoss);

            p1=GlobalVariableGet(NameGL_Equ);

            p2=GlobalVariableGet(NameGL_Add);

            p3=GlobalVariableGet(NameGL_Loss);

            EquClose=p1+p2;

            EquLoss=p1-p3;

            Print(">2K9 AB0@B>2K9 M:28B8 = ",EquStart,

                  ", =>2K9 ?@>F5=B ?@>A04:8 = ",EquPercLoss,", C@>25=L A;54. 70:@KB8O = ",EquLoss);

           }

        }

     }

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//---



  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates(void)

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

     {

      Print("RefreshRates error");

      return(false);

     }

//--- protection against the return value of "zero"

   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)

      return(false);

//---

   return(true);

  }

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

//| Check the correctness of the position volume                     |

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

bool CheckVolumeValue(double volume,string &error_description)

  {

//--- minimal allowed volume for trade operations

   double min_volume=m_symbol.LotsMin();

   if(volume<min_volume)

     {

      if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         error_description=StringFormat("1J5< <5=LH5 <8=8<0;L=> 4>?CAB8<>3> SYMBOL_VOLUME_MIN=%.2f",min_volume);

      else

         error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }

//--- maximal allowed volume of trade operations

   double max_volume=m_symbol.LotsMax();

   if(volume>max_volume)

     {

      if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         error_description=StringFormat("1J5< 1>;LH5 <0:A8<0;L=> 4>?CAB8<>3> SYMBOL_VOLUME_MAX=%.2f",max_volume);

      else

         error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }

//--- get minimal step of volume changing

   double volume_step=m_symbol.LotsStep();

   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

     {

      if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         error_description=StringFormat("1J5< =5 :@0B5= <8=8<0;L=><C H03C SYMBOL_VOLUME_STEP=%.2f, 1;8609H89 ?@028;L=K9 >1J5< %.2f",

                                        volume_step,ratio*volume_step);

      else

         error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",

                                        volume_step,ratio*volume_step);

      return(false);

     }

   error_description="Correct volume value";

   return(true);

  }

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

//| Create rectangle by the given coordinates                        | 

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

bool RectangleCreate(const long            chart_ID=0,        // chart's ID 

                     const string          name="Rectangle",  // rectangle name 

                     const int             sub_window=0,      // subwindow index  

                     datetime              time1=0,           // first point time 

                     double                price1=0,          // first point price 

                     datetime              time2=0,           // second point time 

                     double                price2=0,          // second point price 

                     const color           clr=clrRed,        // rectangle color 

                     const ENUM_LINE_STYLE style=STYLE_SOLID, // style of rectangle lines 

                     const int             width=1,           // width of rectangle lines 

                     const bool            fill=false,        // filling rectangle with color 

                     const bool            back=false,        // in the background 

                     const bool            selection=false,   // highlight to move 

                     const bool            hidden=true,       // hidden in the object list 

                     const long            z_order=0)         // priority for mouse click 

  {

//--- set anchor points' coordinates if they are not set 

   ChangeRectangleEmptyPoints(time1,price1,time2,price2);

   if(ObjectFind(0,name)<0)

     {

      //--- reset the error value 

      ResetLastError();

      //--- create a rectangle by the given coordinates 

      if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2))

        {

         Print(__FUNCTION__,

               ": failed to create a rectangle! Error code = ",GetLastError());

         return(false);

        }

      //--- set rectangle color 

      ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

      //--- set the style of rectangle lines 

      ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

      //--- set width of the rectangle lines 

      ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

      //--- enable (true) or disable (false) the mode of filling the rectangle 

      ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);

      //--- display in the foreground (false) or background (true) 

      ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

      //--- enable (true) or disable (false) the mode of highlighting the rectangle for moving 

      //--- when creating a graphical object using ObjectCreate function, the object cannot be 

      //--- highlighted and moved by default. Inside this method, selection parameter 

      //--- is true by default making it possible to highlight and move the object 

      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

      //--- hide (true) or display (false) graphical object name in the object list 

      ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

      //--- set the priority for receiving the event of a mouse click in the chart 

      ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

     }

   else

     {

      RectanglePointChange(chart_ID,name,0,time1,price1);

      RectanglePointChange(chart_ID,name,1,time2,price2);

     }

//--- successful execution 

   return(true);

  }

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

//| Move the rectangle anchor point                                  | 

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

bool RectanglePointChange(const long   chart_ID=0,       // chart's ID 

                          const string name="Rectangle", // rectangle name 

                          const int    point_index=0,    // anchor point index 

                          datetime     time=0,           // anchor point time coordinate 

                          double       price=0)          // anchor point price coordinate 

  {

//--- if point position is not set, move it to the current bar having Bid price 

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value 

   ResetLastError();

//--- move the anchor point 

   if(!ObjectMove(chart_ID,name,point_index,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

//| Check the values of rectangle's anchor points and set default    | 

//| values for empty ones                                            | 

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

void ChangeRectangleEmptyPoints(datetime &time1,double &price1,

                                datetime &time2,double &price2)

  {

//--- if the first point's time is not set, it will be on the current bar 

   if(!time1)

      time1=TimeCurrent();

//--- if the first point's price is not set, it will have Bid value 

   if(!price1)

      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- if the second point's time is not set, it is located 9 bars left from the second one 

   if(!time2)

     {

      //--- array for receiving the open time of the last 10 bars 

      datetime temp[10];

      CopyTime(Symbol(),Period(),time1,10,temp);

      //--- set the second point 9 bars left from the first one 

      time2=temp[0];

     }

//--- if the second point's price is not set, move it 300 points lower than the first one 

   if(!price2)

      price2=price1-300*SymbolInfoDouble(Symbol(),SYMBOL_POINT);

  }

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

//|                                                                  |

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

void Informations()

  {

   string name="",text="",font="Lucida Console";

   int    xDist=0,yDist=0;

   int yDistStart=(ChartGetInteger(0,CHART_SHOW_ONE_CLICK)==true)?10+65:10;

//---

   text="0;0=A AGQB0: ";

   name=Prefix+"_txt_Balance";

   xDist=10; yDist=yDistStart+70;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrLightBlue);

//---

   text=DoubleToString(m_account.Balance(),2); // '8A;>2>5 7=0G5=85 10;0=A0

   name=Prefix+"_num_AccBalance";

   xDist=170; yDist=yDistStart+70;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrNavajoWhite);

//---

   text="!2>1>4=K5 A@54AB20: ";

   name=Prefix+"_txt_Equity";

   xDist=10; yDist=yDistStart+50;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrLightBlue);

//---

   text=DoubleToString(m_account.Equity(),2); // '8A;>2>5 7=0G5=85 M:28B8

   name=Prefix+"_num_AccEquity";

   xDist=170; yDist=yDistStart+50;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrNavajoWhite);

//---

   text="0:@KB85 =0 C@>2=5: ";

   name=Prefix+"_txt_CloseEquity";

   xDist=10; yDist=yDistStart+30;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrLightBlue);

//---

   text=DoubleToString(EquClose,2); // '8A;>2>5 7=0G5=85 M:28B8

   name=Prefix+"_num_ClsEquity";

   xDist=170; yDist=yDistStart+30;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrNavajoWhite);

//---

   text="@8B8G5A:89 C@>25=L: ";

   name=Prefix+"_txt_CriticEquity";

   xDist=10; yDist=yDistStart+10;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrLightBlue);

//---

   text=DoubleToString(EquLoss,2); // '8A;>2>5 7=0G5=85 M:28B8

   name=Prefix+"_num_CriticEquity";

   xDist=170; yDist=yDistStart+10;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrNavajoWhite);

//---

   text="#1KB>G=KE Buy: ";

   name=Prefix+"_txt_LossBuy";

   xDist=270; yDist=yDistStart+50;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrLightBlue);

//---

   int count_buys=0;

   int count_sells=0;

   CalculateLossPositions(count_buys,count_sells);



   text=DoubleToString(count_buys,0);

   name=Prefix+"_num_LossBuy";

   xDist=400; yDist=yDistStart+50;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrNavajoWhite);

//---

   text="#1KB>G=KE Sell: ";

   name=Prefix+"_txt_LossSell";

   xDist=270; yDist=yDistStart+30;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrLightBlue);

//---

   text=DoubleToString(count_sells,0);

   name=Prefix+"_num_LossSell";

   xDist=400; yDist=yDistStart+30;

   LabelCreate(0,name,0,xDist,yDist,CORNER_LEFT_UPPER,text,font,10,clrNavajoWhite);

  }

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

//| Create a text label                                              | 

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

bool LabelCreate(const long              chart_ID=0,               // chart's ID 

                 const string            name="Label",             // label name 

                 const int               sub_window=0,             // subwindow index 

                 const int               x=0,                      // X coordinate 

                 const int               y=0,                      // Y coordinate 

                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring 

                 const string            text="Label",             // text 

                 const string            font="Arial",             // font 

                 const int               font_size=10,             // font size 

                 const color             clr=clrRed,               // color 

                 const double            angle=0.0,                // text slope 

                 const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type 

                 const bool              back=false,               // in the background 

                 const bool              selection=false,          // highlight to move 

                 const bool              hidden=true,              // hidden in the object list 

                 const long              z_order=0)                // priority for mouse click 

  {

//--- reset the error value 

   ResetLastError();

   if(ObjectFind(0,name)<0)

     {

      //--- create a text label 

      if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))

        {

         Print(__FUNCTION__,

               ": failed to create text label! Error code = ",GetLastError());

         return(false);

        }

      //--- set label coordinates 

      ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

      ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

      //--- set the chart's corner, relative to which point coordinates are defined 

      ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

      //--- set the text 

      ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

      //--- set text font 

      ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

      //--- set font size 

      ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

      //--- set the slope angle of the text 

      ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

      //--- set anchor type 

      ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

      //--- set color 

      ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

      //--- display in the foreground (false) or background (true) 

      ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

      //--- enable (true) or disable (false) the mode of moving the label by mouse 

      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

      //--- hide (true) or display (false) graphical object name in the object list 

      ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

      //--- set the priority for receiving the event of a mouse click in the chart 

      ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

     }

   else

     {

      LabelMove(chart_ID,name,x,y);

      LabelTextChange(chart_ID,name,text);

     }

//--- successful execution 

   return(true);

  }

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

//| Move the text label                                              | 

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

bool LabelMove(const long   chart_ID=0,   // chart's ID 

               const string name="Label", // label name 

               const int    x=0,          // X coordinate 

               const int    y=0)          // Y coordinate 

  {

//--- reset the error value 

   ResetLastError();

//--- move the text label 

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))

     {

      Print(__FUNCTION__,

            ": failed to move X coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))

     {

      Print(__FUNCTION__,

            ": failed to move Y coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

//| Change the label text                                            | 

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

bool LabelTextChange(const long   chart_ID=0,   // chart's ID 

                     const string name="Label", // object name 

                     const string text="Text")  // text 

  {

//--- reset the error value 

   ResetLastError();

//--- change object text 

   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))

     {

      Print(__FUNCTION__,

            ": failed to change the text! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

//| Calculate lossing positions Buy and Sell                         |

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

void CalculateLossPositions(int &count_buys,int &count_sells)

  {

   count_buys=0;

   count_sells=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.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)

           {

            bool loss=(m_position.Commission()+m_position.Swap()+m_position.Profit()<0);

            if(m_position.PositionType()==POSITION_TYPE_BUY && loss)

               count_buys++;



            if(m_position.PositionType()==POSITION_TYPE_SELL && loss)

               count_sells++;

           }

//---

   return;

  }

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

//| Open Buy position                                                |

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

bool OpenBuy(double sl,double tp,const string comment)

  {

   if(!TradeBuy)

     {

      Print("">@3>2;O 2 Buy >B:;NG5=0 2 =0AB@>9:0E A>25B=8:0");

      return(false);

     }

//---

   sl=m_symbol.NormalizePrice(sl);

   tp=m_symbol.NormalizePrice(tp);

//--- check volume before OrderSend to avoid "not enough money" error (CTrade)

   double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),InpLots,m_symbol.Ask(),ORDER_TYPE_BUY);



   if(check_volume_lot!=0.0)

     {

      if(check_volume_lot>=InpLots)

        {

         if(m_trade.Buy(InpLots,m_symbol.Name(),m_symbol.Ask(),sl,tp,comment))

           {

            if(m_trade.ResultDeal()==0)

              {

               Print(__FUNCTION__,", #1 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

               PrintResultTrade(m_trade,m_symbol);

               return(false);

              }

            else

              {

               Print(__FUNCTION__,", #2 Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

               PrintResultTrade(m_trade,m_symbol);

               return(true);

              }

           }

         else

           {

            Print(__FUNCTION__,", #3 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),

                  ", description of result: ",m_trade.ResultRetcodeDescription());

            PrintResultTrade(m_trade,m_symbol);

            return(false);

           }

        }

      else

        {

         Print(__FUNCTION__,", ERROR: method CheckVolume (",DoubleToString(check_volume_lot,2),") ",

               "< Lots (",DoubleToString(InpLots,2),")");

         return(false);

        }

     }

   else

     {

      Print(__FUNCTION__,", ERROR: method CheckVolume returned the value of \"0.0\"");

      return(false);

     }

//---

   return(false);

  }

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

//| Open Sell position                                               |

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

bool OpenSell(double sl,double tp,const string comment)

  {

   if(!TradeSell)

     {

      Print("">@3>2;O 2 Sell >B:;NG5=0 2 =0AB@>9:0E A>25B=8:0");

      return(false);

     }

//---

   sl=m_symbol.NormalizePrice(sl);

   tp=m_symbol.NormalizePrice(tp);

//--- check volume before OrderSend to avoid "not enough money" error (CTrade)

   double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),InpLots,m_symbol.Bid(),ORDER_TYPE_SELL);



   if(check_volume_lot!=0.0)

     {

      if(check_volume_lot>=InpLots)

        {

         if(m_trade.Sell(InpLots,m_symbol.Name(),m_symbol.Bid(),sl,tp,comment))

           {

            if(m_trade.ResultDeal()==0)

              {

               Print(__FUNCTION__,", #1 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

               PrintResultTrade(m_trade,m_symbol);

               return(false);

              }

            else

              {

               Print(__FUNCTION__,", #2 Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),

                     ", description of result: ",m_trade.ResultRetcodeDescription());

               PrintResultTrade(m_trade,m_symbol);

               return(true);

              }

           }

         else

           {

            Print(__FUNCTION__,", #3 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),

                  ", description of result: ",m_trade.ResultRetcodeDescription());

            PrintResultTrade(m_trade,m_symbol);

            return(false);

           }

        }

      else

        {

         Print(__FUNCTION__,", ERROR: method CheckVolume (",DoubleToString(check_volume_lot,2),") ",

               "< Lots (",DoubleToString(InpLots,2),")");

         return(false);

        }

     }

   else

     {

      Print(__FUNCTION__,", ERROR: method CheckVolume returned the value of \"0.0\"");

      return(false);

     }

//---

   return(false);

  }

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

//| Print CTrade result                                              |

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

void PrintResultTrade(CTrade &trade,CSymbolInfo &symbol)

  {

   Print("File: ",__FILE__,", symbol: ",m_symbol.Name());

   Print("Code of request result: "+IntegerToString(trade.ResultRetcode()));

   Print("code of request result as a string: "+trade.ResultRetcodeDescription());

   Print("Deal ticket: "+IntegerToString(trade.ResultDeal()));

   Print("Order ticket: "+IntegerToString(trade.ResultOrder()));

   Print("Volume of deal or order: "+DoubleToString(trade.ResultVolume(),2));

   Print("Price, confirmed by broker: "+DoubleToString(trade.ResultPrice(),symbol.Digits()));

   Print("Current bid price: "+DoubleToString(symbol.Bid(),symbol.Digits())+" (the requote): "+DoubleToString(trade.ResultBid(),symbol.Digits()));

   Print("Current ask price: "+DoubleToString(symbol.Ask(),symbol.Digits())+" (the requote): "+DoubleToString(trade.ResultAsk(),symbol.Digits()));

   Print("Broker comment: "+trade.ResultComment());

  }

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

//|                                                                  |

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

void SetLevels()

  {

   int i;

   double step,lev;

   MassCtLev[0]=CenterLevel;

   string NameLevelCT=Prefix+"_CenterLevel";                   // <O F5=B@0;L=>9 ;8=88

   ArrowRightPriceCreate(0,NameLevelCT,0,iTime(m_symbol.Name(),Period(),0),CenterLevel,clrMediumSpringGreen);

//--- K2>4 25@E=8E C@>2=59 =0 3@0D8:

   for(i=0; i<NumberUpLevels; i++)

     {

      step=((i+1)*ExtDistanceOpenPositions);                   // (03 ?@8@0I5=8O F5=K

      lev=NormalizeDouble(MassCtLev[0]+step,m_symbol.Digits());               // &5=>2>5 7=0G5=85 i-3> C@>2=O

      MassUpLev[i]=lev;                                        // 0?>;=8< <0AA82

      string NameLevelUP=Prefix+"_UpLevel_"+DoubleToString(i,0);  // <O ;8=88

      //--- 0@8AC5< i-C@>25=L

      ArrowRightPriceCreate(0,NameLevelUP,0,iTime(m_symbol.Name(),Period(),0),lev,clrDeepSkyBlue);

     }

//--- K2>4 =86=8E C@>2=59 =0 3@0D8:

   for(i=0; i<NumberDnLevels; i++)

     {

      step=((i+1)*ExtDistanceOpenPositions);                  // (03 ?@8@0I5=8O F5=K

      lev=NormalizeDouble(MassCtLev[0]-step,m_symbol.Digits());               // &5=>2>5 7=0G5=85 i-3> C@>2=O

      MassDnLev[i]=lev;                                        // 0?>;=8< <0AA82

      string NameLevelDN=Prefix+"_DnLevel_"+DoubleToString(i,0);  // <O ;8=88

      //--- 0@8AC5< i-C@>25=L

      ArrowRightPriceCreate(0,NameLevelDN,0,iTime(m_symbol.Name(),Period(),0),lev,clrGold);

     }

   MassUpLen=ArrayRange(MassUpLev,0);

   MassDnLen=ArrayRange(MassDnLev,0);

   HighestLev=MassUpLev[MassUpLen-1];

   LowestLev =MassDnLev[MassDnLen-1];

   return;

  }

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

//| Create the right price label                                     | 

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

bool ArrowRightPriceCreate(const long            chart_ID=0,        // chart's ID 

                           const string          name="RightPrice", // price label name 

                           const int             sub_window=0,      // subwindow index 

                           datetime              time=0,            // anchor point time 

                           double                price=0,           // anchor point price 

                           const color           clr=clrRed,        // price label color 

                           const ENUM_LINE_STYLE style=STYLE_SOLID, // border line style 

                           const int             width=1,           // price label size 

                           const bool            back=false,        // in the background 

                           const bool            selection=true,    // highlight to move 

                           const bool            hidden=true,       // hidden in the object list 

                           const long            z_order=0)         // priority for mouse click 

  {

//--- set anchor point coordinates if they are not set 

   ChangeArrowEmptyPoint(time,price);

   if(ObjectFind(0,name)<0)

     {

      //--- reset the error value 

      ResetLastError();

      //--- create a price label 

      if(!ObjectCreate(chart_ID,name,OBJ_ARROW_RIGHT_PRICE,sub_window,time,price))

        {

         Print(__FUNCTION__,

               ": failed to create the right price label! Error code = ",GetLastError());

         return(false);

        }

      //--- set the label color 

      ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

      //--- set the border line style 

      ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

      //--- set the label size 

      ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

      //--- display in the foreground (false) or background (true) 

      ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

      //--- enable (true) or disable (false) the mode of moving the label by mouse 

      //--- when creating a graphical object using ObjectCreate function, the object cannot be 

      //--- highlighted and moved by default. Inside this method, selection parameter 

      //--- is true by default making it possible to highlight and move the object 

      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

      //--- hide (true) or display (false) graphical object name in the object list 

      ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

      //--- set the priority for receiving the event of a mouse click in the chart 

      ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

     }

   else

     {

      ArrowRightPriceMove(chart_ID,name,time,price);

     }

//--- successful execution 

   return(true);

  }

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

//| Move the anchor point                                            | 

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

bool ArrowRightPriceMove(const long   chart_ID=0,        // chart's ID 

                         const string name="RightPrice", // label name 

                         datetime     time=0,            // anchor point time coordinate 

                         double       price=0)           // anchor point price coordinate 

  {

//--- if point position is not set, move it to the current bar having Bid price 

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value 

   ResetLastError();

//--- move the anchor point 

   if(!ObjectMove(chart_ID,name,0,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

//| Check anchor point values and set default values                 | 

//| for empty ones                                                   | 

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

void ChangeArrowEmptyPoint(datetime &time,double &price)

  {

//--- if the point's time is not set, it will be on the current bar 

   if(!time)

      time=TimeCurrent();

//--- if the point's price is not set, it will have Bid value 

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

  }

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

//| B:@KB85 ?>78F89 ?@8 @01>B5 ?> @K=:C                             |

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

void OpenPosOnTradeMode(ENUM_POSITION_TYPE type,double price)

  {

   double tp=0,pp=0;

   string New_Comm="";

   Print("&5=0 ?5@5A5:;0 C@>25=L ",price,", ?@>25@8< =5>1E>48<>ABL >B:@KB8O @K=>G=>9 ?>78F88");

   if(type==POSITION_TYPE_BUY)

     {

      tp=m_symbol.Ask()+ExtTakeProfit;

      New_Comm=" 01>B0 ?> @K=:C_Buy";

      if(!PresentPosNearestLev(type,price))

        {

         Print("1;878 C@>2=O ",DoubleToString(price,m_symbol.Digits())," =5B ?>78F89, >B:@K205< Buy");

         OpenBuy(0.0,tp,New_Comm);

         return;

        }

     }

   else if(type==POSITION_TYPE_SELL)

     {

      tp=NormalizeDouble(m_symbol.Bid()-ExtTakeProfit,m_symbol.Digits());

      New_Comm=" 01>B0 ?> @K=:C_Sell";

      if(!PresentPosNearestLev(type,price))

        {

         Print("1;878 C@>2=O ",DoubleToString(price,m_symbol.Digits())," =5B ?>78F89, >B:@K205< Sell");

         OpenSell(0.0,tp,New_Comm);

        }

     }

   return;

  }

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

//|                                                                  |

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

bool PresentPosNearestLev(ENUM_POSITION_TYPE type,double price)

  {

   double delta=ExtDistanceOpenPositions/2.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.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)

           {

            if(m_position.PositionType()==type)

               if(MathAbs(m_position.PriceOpen()-price)<=delta)

                 {

                  string pos=(m_position.PositionType()==POSITION_TYPE_BUY)?"Buy":"Sell";

                  Print("1;878 C@>2=O ",DoubleToString(price,m_symbol.Digits()),

                        " C65 5ABL ?>78F8O ",pos,

                        ", >B:@KB0O ?> F5=5 ",DoubleToString(m_position.PriceOpen(),m_symbol.Digits()));

                  return(true);

                 }

           }

//---

   return(false);

  }

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

//|                                                                  |

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

bool ClosePosWithMaxLossInCurrency(double lossing,string &type)

  {

   lossing=DBL_MAX;

   ulong ticket=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.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)

           {

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

            if(profit<lossing)

              {

               lossing=profit;

               type=(m_position.PositionType()==POSITION_TYPE_BUY)?"Buy":"Sell";

               ticket=m_position.Ticket();

              }

           }

   if(lossing<0)

     {

      m_trade.PositionClose(ticket);

      return(true);

     }

//---

   return(false);

  }

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

Comments