Experts_Two_Absorption

Author: Copyright 2016, MetaQuotes Software Corp.
Profit factor:
0.00
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
Experts_Two_Absorption
ÿþ//+------------------------------------------------------------------+

//|                                       Experts_Two_Absorption.mq4 |

//|                        Copyright 2016, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2016, MetaQuotes Software Corp."

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

#property version   "1.00"

#property strict



input int     Magic     = 1111;

input int     MaxRisk   = 5;                            // The maximum risk, percentage

input double  lots      = 0.01;                         // Fixed lot, 0-off 

input int     TP        = 120;                          // Take profit, points, 0-off

input int     SL        = 40;                           // Stop loss, points, 0-off 

input int     slippage  = 3;                            // Slippage

input int     total     = 100;                          // The number of candles to find pogloshenie

double Lots;

bool result;

int ticket;

datetime time;

double    old_point_b;

double    old_point_s;

string symbol="0";//45AL ?8H5< >48= 8=AB@C<5=B 4;O B>@3>2;8 5A;8 =C6=> B>@3>20BL B>;L:> ?> >4=><C 8;8 0 B>@3C5B =0 2A5E =0?@ "EURUSD" >1O70B5;L=> 2 :02KG:0E

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

//|                                                                  |

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

//8 ?>A;5 A<5=K :><?5;8@C5< 



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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(Symbol()!=symbol && symbol!="0")

      return;

   int IBARS=Bars;

   if(IBARS<total+2)

      return;

   if(time!=Time[1])

     {

      time=Time[1];

      //+------------------------------------------------------------------+buy

      if(Open[1]<Close[1] && Open[2]>Close[2] && Open[2]<Close[1])

        {

         int gg=0;

         double point1=0;

         double point2=0;

         int    point11=0;

         int    point22=0;

         point1=Open[1];

         point11=1;

         for(int i=3;i<=total;i++)

           {

            if(Open[i]<Close[i] && Open[i+1]>Close[i+1] && Open[i+1]<Close[i])

              {

               point2=Open[i];

               point22=i;

               if(point1>point2)

                 {

                  gg=1;

                 }

               break;

              }

           }

         if(gg==1)

           {

            double pips=(point1-point2)/(point22-point11);

            double pr_tr;

            for(int i=point11+2;i<point22-1;i++)

              {

               pr_tr=point1 -(i-point11)*pips;

               if(Low[i]<=pr_tr)

                 {

                  gg=0;

                  break;

                 }

              }

           }

         if(gg==1 && old_point_b!=point2)

           {

            old_point_b=point1;

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

              {

               if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL)

                 {

                  ClosePosBySelect();

                 }

              }

            Lots=Lots();

            result=CheckVolumeValue(Lots);

            if(result==false)

               return;

            double free_margin=AccountFreeMarginCheck(Symbol(),OP_BUY,Lots);

            if(free_margin<0)

              {

               Print("Not enough Deposit to Buy  ",Lots," ",Symbol()," Error code=",GetLastError());

               return;

              }

            if((TP<SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) && TP>0) || (SL<SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) && SL>0))

              {

               Print("Take profit or stop loss is less than acceptable!");

               return;

              }

            double Tp=0;

            double Sl=0;

            if(TP>0)

               Tp=NormalizeDouble(Bid+TP*Point,Digits);

            if(SL>0)

               Sl=NormalizeDouble(Bid-SL*Point,Digits);

            ticket=OpenPosition(NULL,OP_BUY,Lots,Sl,Tp,"",Magic);

           }

        }



      //+------------------------------------------------------------------+sell

      if(Open[1]>Close[1] && Open[2]<Close[2] && Open[2]>Close[1])

        {

         int gg=0;

         double point1=0;

         double point2=0;

         int    point11=0;

         int    point22=0;

         point1=Open[1];

         point11=1;

         for(int i=3;i<=total;i++)

           {

            if(Open[i]>Close[i] && Open[i+1]<Close[i+1] && Open[i+1]>Close[i])

              {

               point2=Open[i];

               point22=i;

               if(point1<point2)

                 {

                  gg=1;

                 }

               break;

              }

           }

         if(gg==1)

           {

            double pips=(point2-point1)/(point22-point11);

            double pr_tr;

            for(int i=point11+2;i<point22-1;i++)

              {

               pr_tr=point1+(i-point11)*pips;

               if(High[i]>=pr_tr)

                 {

                  gg=0;

                  break;

                 }

              }

           }

         if(gg==1 && old_point_b!=point2)

           {

            old_point_b=point1;

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

              {

               if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY)

                 {

                  ClosePosBySelect();

                 }

              }

            Lots=Lots();

            result=CheckVolumeValue(Lots);

            if(result==false)

               return;

            double free_margin=AccountFreeMarginCheck(Symbol(),OP_SELL,Lots);

            if(free_margin<0)

              {

               Print("Not enough Deposit to Sell  ",Lots," ",Symbol()," Error code=",GetLastError());

               return;

              }

            if((TP<SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) && TP>0) || (SL<SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL) && SL>0))

              {

               Print("Take profit or stop loss is less than acceptable!");

               return;

              }

            double Tp=0;

            double Sl=0;

            if(TP>0)

               Tp=NormalizeDouble(Ask-TP*Point,Digits);

            if(SL>0)

               Sl=NormalizeDouble(Ask+SL*Point,Digits);

            ticket=OpenPosition(NULL,OP_SELL,Lots,Sl,Tp,"",Magic);

           }

        }

     }

  }

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

int OpenPosition(string sy,int op,double ll,double sl,double tp,string komeent="",int mn=0)

  {

   datetime ot;

   double   pp,pa,pb;

   int      dg,it,Ticket=0;

   if(sy=="" || sy=="0")

      sy=Symbol();

   for(it=1; it<=10; it++)

     {

      if(!IsTesting() && (!IsExpertEnabled() || IsStopped()))

        {

         Print("Stop function!");

         break;

        }

      while(!IsTradeAllowed())

         Sleep(5000);

      RefreshRates();

      dg=(int)MarketInfo(sy, MODE_DIGITS);

      pa=MarketInfo(sy, MODE_ASK);

      pb=MarketInfo(sy, MODE_BID);

      if(op==OP_BUY) pp=pa; else pp=pb;

      pp=NormalizeDouble(pp, dg);

      ot=TimeCurrent();

      Ticket=OrderSend(sy,op,ll,pp,slippage,sl,tp,komeent,mn,0,CLR_NONE);

      if(Ticket>0)

        {

         break;

        }

      else

        {

         if(!Error(GetLastError(),it,"",5000))

            break;

        }

     }

   return(Ticket);

  }

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

bool Error(int err,int j,string informer="",int pause=5000)

  {

   bool is_open=true;



   switch(err)

     {

      case ERR_NO_ERROR:

      case ERR_NO_RESULT:

      case ERR_COMMON_ERROR:{is_open=false; break;}

      case ERR_BROKER_BUSY: {Print("BROKER_BUSY, try !"+DoubleToStr(j,0)); Sleep(pause);} break;

      case ERR_SERVER_BUSY: {Print("SERVER_BUSY, try !"+DoubleToStr(j,0)); Sleep(pause);} break;

      case ERR_NO_CONNECTION: {Print("NO_CONNECTION, try !"+DoubleToStr(j,0)); Sleep(pause);} break;

      case ERR_TRADE_TIMEOUT: {Print("TRADE_TIMEOUT, try !"+DoubleToStr(j,0)); Sleep(pause);} break;

      case ERR_PRICE_CHANGED: {Print("PRICE_CHANGED, try !"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break;

      case ERR_REQUOTE: {Print("REQUOTE, try !"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break;

      case ERR_TOO_FREQUENT_REQUESTS: {Print("TOO_FREQUENT_REQUESTS, try !"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break;

      case ERR_ORDER_LOCKED: {is_open=false; Print("ORDER_LOCKED");} break;

      case ERR_INVALID_STOPS: {Print("INVALID_STOPS, try !"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break;

      case ERR_INVALID_PRICE: {Print("INVALID_PRICE, try !"+DoubleToStr(j,0)+" "+informer);} break;

      case ERR_OFF_QUOTES: {Print("OFF_QUOTES, try !"+DoubleToStr(j,0)); Sleep(pause);} break;

      case ERR_INVALID_TRADE_VOLUME: {is_open=false; Print("INVALID_TRADE_VOLUME, "+informer);} break;

      case ERR_MARKET_CLOSED: {is_open=false; Print("MARKET_CLOSED");} break;

      case ERR_TRADE_DISABLED: {is_open=false; Print("TRADE_DISABLED");} break;

      case ERR_NOT_ENOUGH_MONEY: {is_open=false; Print("NOT_ENOUGH_MONEY, "+informer);} break;

      case ERR_LONG_POSITIONS_ONLY_ALLOWED: {is_open=false; Print("LONG_POSITIONS_ONLY_ALLOWED");} break;

      case ERR_TRADE_MODIFY_DENIED: {Print("TRADE_MODIFY_DENIED, try !"+DoubleToStr(j,0)+" "+informer); Sleep(pause);} break;

      case ERR_TRADE_EXPIRATION_DENIED: {is_open=false; Print("TRADE_EXPIRATION_DENIED");} break;

      case ERR_TRADE_TOO_MANY_ORDERS: {is_open=false; Print("TRADE_TOO_MANY_ORDERS.");} break;

      case ERR_TRADE_HEDGE_PROHIBITED: {is_open=false; Print("TRADE_HEDGE_PROHIBITED.");} break;

      case ERR_TRADE_PROHIBITED_BY_FIFO: {is_open=false; Print("TRADE_PROHIBITED_BY_FIFO.");} break;

      case ERR_ARRAY_INDEX_OUT_OF_RANGE: {is_open=false; break;}

      case ERR_LONGS_NOT_ALLOWED: {is_open=false; break;}

      case ERR_SHORTS_NOT_ALLOWED: {is_open=false; break;}

      case ERR_INVALID_TICKET: {is_open=false; break; }

      default: {is_open=false; Print("Unknown error: "+(string)err+" "+informer);} break;

     }



   return (is_open);

  }

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

double Lots()

  {

   if(lots>0)

      return(lots);

   double Free=AccountFreeMargin();

   double LotVal=MarketInfo(Symbol(),MODE_TICKVALUE);

   double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT);

   double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT);

   double Step=MarketInfo(Symbol(),MODE_LOTSTEP);

   double Lot=0;

   if(SL>0)

   Lot =MathFloor((AccountFreeMargin()* MaxRisk * 0.01)/(SL*LotVal)/Step)*Step;

   if(Lot<Min_Lot) Lot=Min_Lot;

   if(Lot>Max_Lot) Lot=Max_Lot;

   return(Lot);

  }

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

bool CheckVolumeValue(double volume)

  {

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume)

     {

      Print("The volume is less than the minimum :",min_volume);

      return(false);

     }

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume)

     {

      Print("The volume is greater than the maximum :",max_volume);

      return(false);

     }

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

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

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

     {

      Print("The volume is not a multiple of the minimum gradation closest to the correct amount :",volume_step," ",ratio*volume_step);

      return(false);

     }

   return(true);

  }

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

bool ClosePosBySelect()

  {

   bool   fc=false;

   double ll,pa,pb,pp;

   int    it;

   if(OrderType()==OP_BUY || OrderType()==OP_SELL)

     {

      for(it=1; it<=10; it++)

        {

         if(!IsTesting() && (!IsExpertEnabled() || IsStopped()))

            break;

         while(!IsTradeAllowed())

            Sleep(5000);

         RefreshRates();

         pa=MarketInfo(OrderSymbol(), MODE_ASK);

         pb=MarketInfo(OrderSymbol(), MODE_BID);

         if(OrderType()==OP_BUY)

           {

            pp=pb;

           }

         else

           {

            pp=pa;

           }

         ll=OrderLots();

         fc=OrderClose(OrderTicket(), ll, pp, slippage, CLR_NONE);

         if(fc)

           {

            break;

           }

         else

           {

            if(!Error(GetLastError(),it,"",5000))

               break;

           }

        }

      return(fc);

     }

   else {Print("Incorrect trading. Close");return(false);}

  }

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

Comments