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
2 Views
0 Downloads
0 Favorites
Pin-Bar
ÿþ//+------------------------------------------------------------------+

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

  };

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

//|                                                                  |

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

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

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

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=offZeroDamage;// 568< @01>BK 57C1KB:0

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

input modePinBar          PinBar=offPinBar;        // 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 int                 distance = 50;           //  0AAB>O=85 >B High/Low 2 B5:CI59 A25G5

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



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

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

int ticket;

datetime relax;       //@5<O >B4KE0 

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

//|                                                                  |

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

int OnInit()

  {

//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);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   int b=Counter(OP_BUY); //!G8B05B 108

   int s=Counter(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((b+s)!=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(b,s);

      b=Counter(OP_BUY); //!G8B05B 108

      s=Counter(OP_SELL); // !G8B05B AB>?K

     }



   if(ZeroDamage==onZeroDamage)

     {

      useZeroDamage();

     }

  }

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

//|$#&                                                           |

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

int Counter(int type)// $#&/ !'"   

  {

   int n=0;

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

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

         if(OrderMagicNumber()==Magic)

            if(OrderType()==type)

               n++;

   return n;

  }

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

//|                                                                  |

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



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

//|                                                                  |

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

//$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(int b,int s)

  {

//A;8 >B:@KBKE >@45@>2 <=>3>, B> =5 2K?>;=OBL DC=:F8N

   if(b+s >= maxOpenOrders) return 0;

   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;



//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 SP = Bid+ stopLoss*_Point;

      double TP = Bid- takeProfit*_Point;



      ticket=OrderSend(_Symbol,OP_SELL,LotValue,Bid,Slip,0,0,"",Magic,0,clrRed);

      if(!ticket) Print("Error open sell order");

      if(!OrderModify(ticket,OrderOpenPrice(),SP,TP,0,clrRed))

         Print("error modify order");

     }

   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 SP = Ask- stopLoss*_Point;

         double TP = Ask + takeProfit*_Point;



         ticket=OrderSend(_Symbol,OP_BUY,LotValue,Ask,Slip,0,0,"",Magic,0,clrBlue);

         if(!ticket) Print("Error open buy order");

         if(!OrderModify(ticket,OrderOpenPrice(),SP,TP,0,clrRed))

            Print("error modify order");

        }



   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)

  {

   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=CORNER_LEFT_UPPER;// 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 ---