Pin-Bar_full

Author: MagistrSanich
Price Data Components
Series array that contains open prices of each barSeries array that contains close prices for each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
0 Views
0 Downloads
0 Favorites
Pin-Bar_full
ÿþ//+------------------------------------------------------------------+

//|                                                    MagistrSanich |

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

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

#property copyright "MagistrSanich"

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

#property version   "1.00"

#property strict

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

//|                                                                  |

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

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

//|                                                                  |

//|                                                                  |

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

enum modeStopLoss //57C1KB>:

  {

   offZeroDamage,//B:;NG8BL

   onZeroDamage //:;NG8BL

  };

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

//|                                                                  |

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

enum modePinBar //:;NG5=85 ?8=10@0

  {

   offPinBar,//B:;NG8BL

   onPinBar  //:;NG8BL

  };

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

//|                                                                  |

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

enum modeOpenOrder  // 568< >B:@KB8O >B;>65==>3> >@45@0

  {

   byLow,//> Low/High

   byPathCandle //> G0AB8 A25G8

  };



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

//|                                                                  |

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

input int                 period=5;                //"09<D@59< (2 <8=CB0E)

input int                 maxOpenOrders=1;         //3@0=8G5=85 ?> :>;8G5AB2C B5:CI8E >@45@>2

input int                 relaxTime=30;            //@5<O >B4KE0 2 <8=CB0E ?>A;5 >B:@KB8O >@45@0

input bool                dinamicLot=true;         // 568< @0AG5B0 ;>B0

input double              Lot=0.1;                 //>B ;81> ?@>F5=B >B 45?>78B0

input modeStopLoss        ZeroDamage=onZeroDamage; // 568< @01>BK 57C1KB:0

input int                 profit=50;               //@>D8B ?@8 :>B>@>< =0G8=0BL 459AB2>20BL

input modePinBar          PinBar=onPinBar;         // 01>B0 A 8= 0@><

input int                 takeProfit=400;          //"59:?@>D8B 2 ?8?A0E

input int                 stopLoss=200;            //!B>?;>AA 2 ?8?A0E

input int                 difference=100;          // 07=8F0 <564C High/Low

input modeOpenOrder       whenOpen=byLow;          // 568< >B:@KB8O >B;>65==>3> >@45@0

input int                 distance=50;             // 0AAB>O=85 >B High/Low 2 B5:CI59 A25G5

input double              pathCandle=50;           // :0:>9 G0AB8 A25G8 >B:@K20BL >@45@ (2 %)

input int                 sameBars=3;              //>;8G5AB2> 10@>2 ?>4@O4 >4=>3> F25B0

input int                 Slip= 30;                //@>A:0;L7K20=85

input int                 expiration=120;          //@5<O 687=8 >B;>65==>3> >@45@0 (<8=)

input int                 Magic= 0;                //038: ( >@45@>2 B5:CI53> >B0)

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

//|                                                                  |

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

class ClassOrders

  {

private:

   int               orders_magic;                   // Magic Number for the filtering orders. If 0, then doesn't filter.

   string            orders_symbol;               // Currency symbol for the filtering orders. If "", then doesn't filter.

   int               trycount;

   int               slippage;



   int               m_lastorder_type;

   double            m_lastorder_price;

   double            m_lastorder_lots;



public:

                     ClassOrders();

   double            GetAllProfit(); //@81K;L >@45@>2

   int               GetCountOrders(int fOrderType); // >;8G5AB2> >@45@>2 7040==>3> B8?0

   int               GetAllCountOrders(); //>72@0I05B :>;8G5AB2> 2A5E >B:@KBKE >@45@>2, 2 B>< G8A;5 >B;>65==KE

   int               SetOrder(int type,double openprice,double lot,double sl,double tp,string comm=NULL,datetime exptime=0); //>B:@KB85 >@45@0

   int GetDigits() { return((int)MarketInfo(orders_symbol,MODE_DIGITS)); };

   double GetPoint() { return(MarketInfo(orders_symbol,MODE_POINT)); };

   double GetAsk() { RefreshRates(); return (MarketInfo(orders_symbol,MODE_ASK)); };

   double GetBid() { RefreshRates(); return (MarketInfo(orders_symbol,MODE_BID)); };

   string GetSymbol() { return(orders_symbol); }

   double GetStopLevel() { return((MarketInfo(orders_symbol,MODE_STOPLEVEL)+1)*MarketInfo(orders_symbol,MODE_POINT)); }

   int GetLastOrderType() { return(m_lastorder_type);};

   double GetLastOrderPrice() { return(m_lastorder_price);};

   double GetLastOrderLots() { return m_lastorder_lots; };

   void SetMagic(int fMagicNum=0) { orders_magic = fMagicNum; };

   void SetSymbol(string fSymbol) { orders_symbol = fSymbol; };



  };

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

//|>=AB@C:B>@                                                       |

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

ClassOrders::ClassOrders(void)

  {

   this.orders_magic=0;

   this.orders_symbol=Symbol();

   this.trycount = 3;

   this.slippage = 10;

  }

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

//|>;8G5AB2> >@45@>2 40==>3> B8?0                                   |

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

int ClassOrders::GetCountOrders(int fOrderType=-1)

  {

   int orderscnt=0;

//----

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

      if(OrderSymbol()==this.orders_symbol && (this.orders_magic<0 || OrderMagicNumber()==this.orders_magic) && (OrderType()==fOrderType || fOrderType==-1))

        {

         orderscnt++;

        }

     }

//---- return orders volume

   return(orderscnt);

  }

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

//|                                                                  |

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

int ClassOrders::GetAllCountOrders()

  {

   int orderscnt=0;

//----

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

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

      if(OrderSymbol()==this.orders_symbol && (this.orders_magic<0 || OrderMagicNumber()==this.orders_magic))

        {

         orderscnt++;

        }

     }

//---- return orders volume

   return(orderscnt);

  }

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

//| 0AG5B ?@81K;8 2A5E >@45@>2                                       |

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

double ClassOrders::GetAllProfit()

  {

   double Profit=0;

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

     {

      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false) break;

      if(OrderSymbol()==this.orders_symbol && OrderMagicNumber()==this.orders_magic && OrderType()<=OP_SELL)

        {

         Profit+=OrderProfit()+OrderSwap()+OrderCommission();

        }

     }

   return(Profit);

  }

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

//| B:@KB85 >@45@0                                                  |

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

int ClassOrders::SetOrder(int type,double openprice,double lot,double sl,double tp,string comm=NULL,datetime exptime=0)

  {

   int ticket=0;

   int i=0;

//----

   if(lot<MarketInfo(this.orders_symbol,MODE_MINLOT))

      return(0);

   if(lot>MarketInfo(this.orders_symbol,MODE_MAXLOT))

      lot=MarketInfo(this.orders_symbol,MODE_MAXLOT);



   if(exptime>0 && (exptime-TimeCurrent())<11*60)

     {

      exptime=0;

      Print("54>?CAB8<K5 ?0@0<5B@K M:A?8@0F88. >@@5:B8@>2:0 >B;>65==>3> >@45@0.");

     }

   if(openprice==0 && type==OP_BUY) openprice = MarketInfo(this.orders_symbol,MODE_ASK);

   if(openprice==0 && type==OP_SELL) openprice = MarketInfo(this.orders_symbol,MODE_BID);

   while(i<this.trycount)

     {

      ticket=OrderSend(this.orders_symbol,type,lot,openprice,5,sl,tp,comm,this.orders_magic,exptime);

      if(ticket>0)

        {

         return(ticket);

        }

      else

        {

         Print("Eror open order");



         return(0);

        }

     }

   return(0);

  }

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

//|                    ;>10;L=K5 ?5@5<5==K5                         |

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

double countOrders=0; //70?><8=05B :>;8G5AB2> >B:@KBKE >@45@>2

double LotValue;      //>1J5< ;>B0

datetime relax;       //@5<O >B4KE0 

ClassOrders Orders;

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

//|                                                                  |

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

int OnInit()

  {

   Orders.SetMagic(Magic);

   Orders.SetSymbol(Symbol());



//K2>4 =5:>B>@KE =0AB@>5: 2 =0G0;5, 4;O C4>1AB20

//---

   int j=0,x=15,y=15;

   string name = "txt" + IntegerToString(j);

   string text = ""09<D@59< (2 <8=CB0E)= " +IntegerToString(period);

   labelCreat(name,text,x,y);

//#2

   y+=15;j++;

   name = "txt" + IntegerToString(j);

   text = "3@0=8G5=85 ?> :>;8G5AB2C B5:CI8E >@45@>2 = "+IntegerToString(maxOpenOrders);

   labelCreat(name,text,x,y);

//#3

   y+=15;j++;

   name = "txt" + IntegerToString(j);

   text = "@5<O >B4KE0 2 <8=CB0E ?>A;5 >B:@KB8O >@45@0 =  "+IntegerToString(relaxTime);

   labelCreat(name,text,x,y);

   if(ZeroDamage!=0)

     {

      //#4

      y+=15;j++;

      name = "txt" + IntegerToString(j);

      text = "57C1KB>: 2:;NG5=";

      labelCreat(name,text,x,y,clrRed);

      //#5

      y+=15;j++;

      name = "txt" + IntegerToString(j);

      text = "@>D8B = "+IntegerToString(profit);

      labelCreat(name,text,x,y);

     }

   if(PinBar!=0)

     {

      //#6

      y+=15;j++;

      name = "txt" + IntegerToString(j);

      text = "8=0@ 2:;NG5= ";

      labelCreat(name,text,x,y,clrRed);

      //#7

      y+=15;j++;

      name = "txt" + IntegerToString(j);

      text = ""59:@>D8B  = "+IntegerToString(takeProfit);

      labelCreat(name,text,x,y);

      //#8

      y+=15;j++;

      name = "txt" + IntegerToString(j);

      text = "!B>?>AA  = "+IntegerToString(stopLoss);

      labelCreat(name,text,x,y);

      //#9

      y+=15;j++;

      name = "txt" + IntegerToString(j);

      text = " 07=8F0 <564C High/Low = "+IntegerToString(difference);

      labelCreat(name,text,x,y);

      //#10

      y+=15;j++;

      name = "txt" + IntegerToString(j);

      text = " 0AAB>O=85 >B High/Low 2 B5:CI59 A25G5 = "+IntegerToString(distance);

      labelCreat(name,text,x,y);

      //#11

      y+=15;j++;

      name = "txt" + IntegerToString(j);

      text = ">;8G5AB2> 10@>2 ?>4@O4 >4=>3> F25B0 = "+IntegerToString(sameBars);

      labelCreat(name,text,x,y);

     }

//K2>4 8=D>@<0F88 >1 >@54@0E

//#1

   y=15;j=0;x=250;

   name = "info" + IntegerToString(j);

   text = "@81K;L: "+"0";

   labelCreat(name,text,x,y,16777215,CORNER_RIGHT_UPPER);

//#2

   y+=15;j++;

   name = "info" + IntegerToString(j);

   text = ">;8G5AB2> buy: "+"0" + "  sell: " + "0";

   labelCreat(name,text,x,y,16777215,CORNER_RIGHT_UPPER);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   int b=Orders.GetCountOrders(OP_BUY); //!G8B05B 108

   int s=Orders.GetCountOrders(OP_SELL); // !G8B05B AB>?K



                                         //B4KE ?>A;5 A45;>:

//A;8 :>;8G5AB2> >B:@KBKE >@45@>2 A 0H8< 038:>< C25;8G8;>AL

//B> =0 7040==>5 2@5<O ?5@5<5==0O relax=false

   bool relaxNow=true;

   if(Orders.GetAllCountOrders()!=countOrders)

     {

      if((b+s)>countOrders)

         relax=TimeCurrent()+relaxTime*60;



      countOrders=b+s;

     }

   if(TimeCurrent()<relax)

     {

      relaxNow=false;

     }

   if(PinBar==onPinBar && relaxNow)

     {

      int tick;

      tick=orderOpenPin();

     }



   if(ZeroDamage==onZeroDamage)

     {

      useZeroDamage();

     }



//#1

   int j=0;

   string name = "info" + IntegerToString(j);

   string text = "?@81K;L >@45@>2 = "+DoubleToString(NormalizeDouble(Orders.GetAllProfit(),2));

   ObjectSetString(0,name,OBJPROP_TEXT,text);



//#2

   j++;

   name = "info" + IntegerToString(j);

   text = ">;8G5AB2> >@45@>2 buy: "+IntegerToString(Orders.GetCountOrders(OP_BUY)) + "  sell: " + IntegerToString(Orders.GetCountOrders(OP_SELL));

   ObjectSetString(0,name,OBJPROP_TEXT,text);



  }

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

//|                   $C=:F88                                        |

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

//$C=:F8O ?@>25@:8 =0?@02;5=8O 7040==>3> :>;8G5AB20 A25G59

//>72@0I05B

//1 -A;8 1KGLO A25G0    2 - A;8 <54256LO   -1 - A;8 =8 B0 =8 4@C30O

int trendCandles(int n)

  {

   int bear=0,bull=0;

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

      if((iClose(_Symbol,period,i)-iOpen(_Symbol,period,i))>0)

        {

         bull++;

        }

   else bear++;



   if(bear == 0 && bull!=0) return 1;

   else if(bear !=0 && bull ==0) return 2;

   return -1;

  }

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

//|                                                                  |

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

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

//|                                                                  |

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



//" +"       #

int orderOpenPin()

  {

//A;8 >B:@KBKE >@45@>2 <=>3>, B> 2KE>48BL

   if(Orders.GetAllCountOrders() >= maxOpenOrders) return 0;

//@>25@:0 B@5=40 ?>A;54=8E A25G59

   int lastCandles=trendCandles(sameBars);

   if(dinamicLot==true)

     {

      LotValue=AccountBalance() *Lot/100;

      if(LotValue<SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN)) LotValue=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);

     }

   else LotValue=Lot;

   if(whenOpen==byLow)

     {

      //B:@KB85 Sell->@45@0

      if(lastCandles==1 && iHigh(_Symbol,period,0)-iHigh(_Symbol,period,1)>=difference*_Point && 

         (iHigh(_Symbol,period,0)-Bid)>=distance*_Point)

        {

         double price=iLow(_Symbol,period,0);

         double sl=price+stopLoss*_Point;

         double tp=price-takeProfit*_Point;

         Orders.SetOrder(OP_SELLSTOP,price,LotValue,sl,tp,"",expiration);

        }

      else

      //B:@KB85 Buy->@45@0

         if(lastCandles==2 && iLow(_Symbol,period,1)-iLow(_Symbol,period,0)>=difference*_Point && 

            (Ask-iLow(_Symbol,period,0))>=distance*_Point)

           {

            double price=iHigh(_Symbol,period,0);

            double sl = price- stopLoss*_Point;

            double tp = price + takeProfit*_Point;



            Orders.SetOrder(OP_BUYSTOP,price,LotValue,sl,tp,"",expiration);

           }

     }

   else if(whenOpen==byPathCandle)

     {

      //B:@KB85 Sell->@45@0

      if(lastCandles==1 && iHigh(_Symbol,period,0)-iHigh(_Symbol,period,1)>=difference*_Point && 

         (iHigh(_Symbol,period,0)-Bid)>=distance*_Point)

        {

         double price=NormalizeDouble((iHigh(_Symbol,period,0)-iOpen(_Symbol,period,0))*pathCandle/100+iOpen(_Symbol,period,0),Digits);

         double sl=price+stopLoss*_Point;

         double tp=price-takeProfit*_Point;



         datetime expirationTime=TimeCurrent()+expiration*60;



         Orders.SetOrder(OP_SELLLIMIT,price,LotValue,sl,tp,"",expirationTime);

        }

      else

      //B:@KB85 Buy->@45@0

         if(lastCandles==2 && iLow(_Symbol,period,1)-iLow(_Symbol,period,0)>=difference*_Point && 

            (Ask-iLow(_Symbol,period,0))>=distance*_Point)

           {

            double price=NormalizeDouble(iOpen(_Symbol,period,0)-(iOpen(_Symbol,period,0)-iLow(_Symbol,period,0))*pathCandle/100,Digits);

            double sl = price- stopLoss*_Point;

            double tp = price + takeProfit*_Point;

            datetime expirationTime=TimeCurrent()+expiration*60;



            Orders.SetOrder(OP_BUYLIMIT,iHigh(_Symbol,period,0),LotValue,sl,tp,"",expirationTime);

           }



     }



   return 0;

  }

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

//|                                                                  |

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

void useZeroDamage()

  {

   int n=0;

   for(int i=0;i<=(OrdersTotal()+1);i++)

      if(OrderSelect(i,SELECT_BY_POS)==true)

         if(OrderMagicNumber()==Magic && (OrderType()==OP_BUY || OrderType()==OP_SELL))

           {

            if(OrderType()==OP_BUY)

              {

               //"5:CI89 AB>?;>AA 4>;65= 1KBL <5=LH5 AB>?0 ?> C<>;G0=8N

               if(OrderOpenPrice() -(OrderStopLoss()-5*_Point)>=stopLoss*_Point)

                  if((Ask-OrderOpenPrice())>=profit*_Point)

                    {

                     double SP=NormalizeDouble(OrderOpenPrice()+SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)*_Point,Digits);

                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),SP,OrderTakeProfit(),0,clrYellow))

                        Print("H81:0 >48D8:0F88 BUY ?> 57C1KB:C SP=",SP," OrOp =",OrderOpenPrice());

                    }

              }

            else if(OrderType()==OP_SELL)

              {

               //"5:CI89 AB>?;>AA 4>;65= 1KBL <5=LH5 AB>?0 ?> C<>;G0=8N

               if((OrderStopLoss()+5*_Point)-OrderOpenPrice()>=stopLoss*_Point)

                  if((OrderOpenPrice()-Bid)>=profit*_Point)

                    {

                     double SP=NormalizeDouble(OrderOpenPrice()-SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)*_Point,Digits);

                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),SP,OrderTakeProfit(),0,clrYellow))

                        Print("H81:0 >48D8:0F88 SELL ?> 57C1KB:C Sp = ",SP," OrOp =",OrderOpenPrice());

                    }

              }

           }

  }

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

//|                                                                  |

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

bool labelCreat(string name,string text,int XDistance,int YDistance,color clr=clrWhite,ENUM_BASE_CORNER place=CORNER_LEFT_UPPER)

  {

   string            font="Arial";             // H@8DB 

   int               font_size=8;             // @07<5@ H@8DB0 

   ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER; // A?>A>1 ?@82O7:8 

   long              chart_ID=0;

   const ENUM_BASE_CORNER  corner=place;// C3>; 3@0D8:0 4;O ?@82O7:8 

   const int        sub_window=0;            // =><5@ ?>4>:=0 



   ObjectDelete(chart_ID,name);

   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0)) return(false);

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,XDistance);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,YDistance);

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- CAB0=>28< H@8DB B5:AB0 

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//--- CAB0=>28< @07<5@ H@8DB0 

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//--- CAB0=>28< C3>; =0:;>=0 B5:AB0 

//--- CAB0=>28< A?>A>1 ?@82O7:8 

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- CAB0=>28< F25B 

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

   return(true);

  }

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

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