Trend_Line_Pending_Orders

Author: Copyright © 2011, Хлыстов Владимир
Price Data Components
Series array that contains the lowest prices of each barSeries array that contains the highest 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
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Trend_Line_Pending_Orders
ÿþ//+------------------------------------------------------------------+

//|  CG=0O CAB0=>2:0 8 ?5@5<5I5=85 >@45@>2                           |

//|                     Manual Open and Modify Stop Limit orders.mq4 |

//|                               Copyright © 2011, %;KAB>2 ;048<8@ |

//|                                                cmillion@narod.ru |

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

#property copyright "Copyright © 2011, %;KAB>2 ;048<8@"

#property link      "cmillion@narod.ru"

#property strict



//--------------------------------------------------------------------

extern bool    StopOrders        = false;  //2KAB02;OBL AB>? >@45@0

extern bool    LimitOrders       = true;  //2KAB02;OBL ;8<8B >@45@0



extern double  PriceSellStop     = 0;     //5A;8 0, B> =0G0;L=0O ;8=8O AB028BAO =0 <8=8<C< ?@>H;>3> 4=O

extern double  PriceBuyStop      = 0;     //5A;8 0, B> =0G0;L=0O ;8=8O AB028BAO =0 <0:A8<C< ?@>H;>3> 4=O

extern double  PriceSellLimit    = 0;     //5A;8 0, B> =0G0;L=0O ;8=8O AB028BAO =0 <0:A8<C< ?@>H;>3> 4=O

extern double  PriceBuyLimit     = 0;     //5A;8 0, B> =0G0;L=0O ;8=8O AB028BAO =0 <8=8<C< ?@>H;>3> 4=O



extern int     Stoploss          = 0,     //AB>?;>AA >@45@>2

               Takeprofit        = 0;     //B59:?@>D8B >@45@>2

extern double  Lot               = 0.1;   //

extern int     Magic             = 100;   //C=8:0;L=K9 =><5@ >@45@>2 MB>3> A>25B=8:0

extern int     FontSize          = 8;     //@07<5@ H@8DB0

extern color   ColorText         = clrLime;



string txt= "",ss,bs,ssn,bsn,sl,bl,sln,bln;

double SL,TP,STOPLEVEL;

//--------------------------------------------------------------------

int OnInit()

{

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

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

   if (Lot>MAXLOT) Lot = MAXLOT;

   if (Lot<MINLOT) Lot = MINLOT;

   ss = StringConcatenate("LINES SELLSTOP",Magic);

   bs = StringConcatenate("LINES BUYSTOP",Magic);

   ssn = StringConcatenate("LINES SELLSTOPn",Magic);

   bsn = StringConcatenate("LINES BUYSTOPn",Magic);

   sl = StringConcatenate("LINES SELLLIMIT",Magic);

   bl = StringConcatenate("LINES BUYLIMIT",Magic);

   sln = StringConcatenate("LINES SELLLIMITn",Magic);

   bln = StringConcatenate("LINES BUYLIMITn",Magic);



   STOPLEVEL = MarketInfo(Symbol(),MODE_STOPLEVEL);

   if (Stoploss <STOPLEVEL && Stoploss!=0) {Stoploss=0;Alert("Stoploss ",Stoploss,"  <5=55 4>?CAB8<>3> C@>2=O ",STOPLEVEL," Stoploss >B:;NG5=");}

   if (Takeprofit <STOPLEVEL && Takeprofit!=0) {Takeprofit=0;Alert("Takeprofit ",Takeprofit,"  <5=55 4>?CAB8<>3> C@>2=O ",STOPLEVEL," Takeprofit >B:;NG5=");}

   if (StopOrders)

   {  



      if (ObjectFind(ss)==-1) 

      {

         if (PriceSellStop==0) {drawline(ss,Red,iLow(NULL,1440,1)-5*Point);drawtext(ssn,"",Time[10],iLow(NULL,1440,1)-5*Point);}

         else {drawline(ss,Red,PriceSellStop);drawtext(ssn,"",Time[10],PriceSellStop);}

      }



      if (ObjectFind(bs)==-1) 

      {

         if (PriceBuyStop==0) {drawline(bs,Blue,iHigh(NULL,1440,1)+5*Point);drawtext(bsn,"",Time[10],iHigh(NULL,1440,1)+5*Point);}

         else {drawline(bs,Blue,PriceBuyStop);drawtext(bsn,"",Time[10],PriceBuyStop);}

      }

   }

   else 

   {

      ObjectDelete(ss);

      ObjectDelete(bs);

      ObjectDelete(ssn);

      ObjectDelete(bsn);

   }

   if (LimitOrders)

   {  

      if (ObjectFind(sl)==-1) 

      {

         if (PriceSellLimit==0) {drawline(sl,Red,iHigh(NULL,1440,1));drawtext(sln,"",Time[10],iHigh(NULL,1440,1));}

         else {drawline(sl,Red,PriceSellLimit);drawtext(sln,"",Time[10],PriceSellLimit);}

      }

      if (ObjectFind(bl)==-1) 

      {

         if (PriceBuyLimit==0) {drawline(bl,Blue,iLow(NULL,1440,1));drawtext(bln,"",Time[10],iLow(NULL,1440,1));}

         else {drawline(bl,Blue,PriceBuyLimit);drawtext(bln,"",Time[10],PriceBuyLimit);}

      }

   }

   else 

   {

      ObjectDelete(sl);

      ObjectDelete(bl);

      ObjectDelete(bln);

      ObjectDelete(bln);

   }

   return(INIT_SUCCEEDED);

}

//-------------------------------------------------------------------

void OnTick()

{

   STOPLEVEL = MarketInfo(Symbol(),MODE_STOPLEVEL);

   int TicketSellLimit=0,TicketBuyLimit=0,TicketBuyStop=0,TicketSellStop=0;

   double SELLLIMIT=0,BUYLIMIT=0,BUYSTOP=0,SELLSTOP=0;//F5=K >B:@KBKE >B;>65:

   double OOP;

   int tip,Ticket=0;

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

   {    

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

      { 

         if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)

         { 

            tip = OrderType(); 

            OOP   = NormalizeDouble(OrderOpenPrice(),Digits);

            Ticket = OrderTicket();

            SL=0;TP=0;

            if (tip==OP_BUYSTOP)             

            {

               BUYSTOP = OOP;

               TicketBuyStop = Ticket;

            }                                         

            if (tip==OP_SELLSTOP)        

            {

               SELLSTOP = OOP;

               TicketSellStop = Ticket;

            } 

            if (tip==OP_BUYLIMIT)             

            {

               BUYLIMIT = OOP;

               TicketBuyLimit = Ticket;

            }                                         

            if (tip==OP_SELLLIMIT)        

            {

               SELLLIMIT = OOP;

               TicketSellLimit = Ticket;

            } 

         }

      }

   } 

   //---

   double Price,StopLevel=NormalizeDouble(STOPLEVEL*Point,Digits);

   if (ObjectFind(sl)!=-1) 

   {

      Price = NormalizeDouble(ObjectGetValueByShift(sl,0),Digits);

      if (Price==0)

      {

         if (ObjectFind(sln)!=-1)ObjectDelete(sln);

         if (OrderSelect(TicketSellLimit,SELECT_BY_TICKET)) 

            if (!OrderDelete(TicketSellLimit)) Comment("Error OrderDelete ",GetLastError(),"  Ticket ",TicketSellLimit);

      }

      else

      {

         if (Price-StopLevel>=Ask)

         {

            if (Takeprofit!=0) TP = NormalizeDouble(Price - Takeprofit * Point,Digits); else TP=0;

            if (Stoploss!=0)   SL = NormalizeDouble(Price + Stoploss   * Point,Digits); else SL=0;

            if (SELLLIMIT==0)//SellLimit

            {

               if (OrderSend(Symbol(),OP_SELLLIMIT ,Lot,Price,0,SL,TP,txt,Magic,0,Blue)==-1)

                  Comment("Error OrderSend SellLimit ",GetLastError()," Lot=",Lot," Price=",Price," SL=",SL," TP=",TP);

            }

            else

            {

               if (OrderSelect(TicketSellLimit,SELECT_BY_TICKET))

                  if (NormalizeDouble(OrderOpenPrice(),Digits)!=Price)

                     if (!OrderModify(TicketSellLimit,Price,SL,TP,0,Green)) Comment("Error OrderModify SellLimit ",GetLastError()," Lot=",Lot," Price=",Price," SL=",SL," TP=",TP);

            }

         }

         else drawtext(sln,"=5?@028;L=0O F5=0 4;O SELLLIMIT",Time[0],Price);

         if (ObjectGet(sl,OBJPROP_TIME1)!=ObjectGet(sln,OBJPROP_TIME1) || ObjectGet(sln,OBJPROP_PRICE1)!=Price) drawtext(sln,"",(datetime)ObjectGet(sl,OBJPROP_TIME1),Price);

      }

   }

   else

   {

      if (OrderSelect(TicketSellLimit,SELECT_BY_TICKET)) if (!OrderDelete(TicketSellLimit)) Print("Error OrderDelete ",GetLastError(),"  Ticket ",TicketSellLimit);

      if (ObjectFind(sln)!=-1)ObjectDelete(sln);

   }

   

   //---

   

   if (ObjectFind(bl)!=-1) 

   {

      Price = NormalizeDouble(ObjectGetValueByShift(bl,0),Digits);

      if (Price==0)

      {

         if (ObjectFind(bln)!=-1) ObjectDelete(bln);

         if (OrderSelect(TicketBuyLimit,SELECT_BY_TICKET)) 

            if (!OrderDelete(TicketBuyLimit)) Comment("Error OrderDelete ",GetLastError(),"  Ticket ",TicketBuyLimit);

      }

      else

      {

         if (Price+StopLevel<=Bid)

         {

            if (Takeprofit!=0) TP = NormalizeDouble(Price + Takeprofit * Point,Digits); else TP=0;

            if (Stoploss!=0)   SL = NormalizeDouble(Price - Stoploss   * Point,Digits); else SL=0;

            if (BUYLIMIT==0)//BayLimit

            {

               if (OrderSend(Symbol(),OP_BUYLIMIT,Lot,Price,0,SL,TP,txt,Magic,0,Red )==-1)

                  Comment("Error OrderSend BuyLimit ",GetLastError()," Lot=",Lot," Price=",Price," SL=",SL," TP=",TP);

            }

            else

            {

               if (OrderSelect(TicketBuyLimit,SELECT_BY_TICKET))

                  if (NormalizeDouble(OrderOpenPrice(),Digits)!=Price)

                     if (!OrderModify(TicketBuyLimit,Price,SL,TP,0,Green)) Comment("Error OrderModify BuyLimit ",GetLastError()," Lot=",Lot," Price=",Price," SL=",SL," TP=",TP);

            }

         }

         else drawtext(bln,"=5?@028;L=0O F5=0 4;O BUYLIMIT",Time[0],Price);

         if (ObjectGet(bl,OBJPROP_TIME1)!=ObjectGet(bln,OBJPROP_TIME1) || ObjectGet(bln,OBJPROP_PRICE1)!=Price) drawtext(bln,"",(datetime)ObjectGet(bl,OBJPROP_TIME1),Price);

      }

   }

   else

   {

      if (ObjectFind(bln)!=-1) ObjectDelete(bln);

      if (OrderSelect(TicketBuyLimit,SELECT_BY_TICKET)) 

         if (!OrderDelete(TicketBuyLimit)) Print("Error OrderDelete ",GetLastError(),"  Ticket ",TicketBuyLimit);

   }

   

   //---

   

   if (ObjectFind(bs)!=-1) 

   {

      Price = NormalizeDouble(ObjectGetValueByShift(bs,0),Digits);

      if (Price==0)

      {

         if (ObjectFind(bsn)!=-1) ObjectDelete(bsn);

         if (OrderSelect(TicketBuyStop,SELECT_BY_TICKET)) 

            if (!OrderDelete(TicketBuyStop)) Comment("Error OrderDelete ",GetLastError(),"  Ticket ",TicketBuyStop);

      }

      else

      {

         if (Price-StopLevel>=Ask)

         {

            if (Takeprofit!=0) TP = NormalizeDouble(Price + Takeprofit * Point,Digits); else TP=0;

            if (Stoploss!=0)   SL = NormalizeDouble(Price - Stoploss   * Point,Digits); else SL=0;

            if (BUYSTOP==0)//BayStop

            {

               if (OrderSend(Symbol(),OP_BUYSTOP ,Lot,Price,0,SL,TP,txt,Magic,0,Blue)==-1)

                  Comment("Error OrderSend BuyStop ",GetLastError()," Lot=",Lot," Price=",Price," SL=",SL," TP=",TP);

            }

            else

            {

               if (OrderSelect(TicketBuyStop,SELECT_BY_TICKET))

                  if (NormalizeDouble(OrderOpenPrice(),Digits)!=Price)

                     if (!OrderModify(TicketBuyStop,Price,SL,TP,0,Green)) Comment("Error OrderModify BuyStop ",GetLastError()," Lot=",Lot," Price=",Price," SL=",SL," TP=",TP);

            }

         }

         else drawtext(bsn,"=5?@028;L=0O F5=0 4;O BUYSTOP",Time[0],Price);

         if (ObjectGet(bs,OBJPROP_TIME1)!=ObjectGet(bsn,OBJPROP_TIME1) || ObjectGet(bsn,OBJPROP_PRICE1)!=Price) drawtext(bsn,"",(datetime)ObjectGet(bs,OBJPROP_TIME1),Price);

      }

   }

   else

   {

      if (ObjectFind(bsn)!=-1) ObjectDelete(bsn);

      if (OrderSelect(TicketBuyStop,SELECT_BY_TICKET)) 

         if (!OrderDelete(TicketBuyStop)) Print("Error OrderDelete ",GetLastError(),"  Ticket ",TicketBuyStop);

   }

   

   //---

   

   if (ObjectFind(ss)!=-1) 

   {

      Price = NormalizeDouble(ObjectGetValueByShift(ss,0),Digits);

      if (Price==0)

      {

         if (ObjectFind(ssn)!=-1) ObjectDelete(ssn);

         if (OrderSelect(TicketSellStop,SELECT_BY_TICKET)) 

            if (!OrderDelete(TicketSellStop)) Comment("Error OrderDelete ",GetLastError(),"  Ticket ",TicketSellStop);

      }

      else

      {

         if (Price+StopLevel<=Bid)

         {

            if (Takeprofit!=0) TP = NormalizeDouble(Price - Takeprofit * Point,Digits); else TP=0;

            if (Stoploss!=0)   SL = NormalizeDouble(Price + Stoploss   * Point,Digits); else SL=0;

            if (SELLSTOP==0)//SellStop

            {

               if (OrderSend(Symbol(),OP_SELLSTOP,Lot,Price,0,SL,TP,txt,Magic,0,Red )==-1)

                  Comment("Error OrderSend SellStop ",GetLastError()," Lot=",Lot," Price=",Price," SL=",SL," TP=",TP);

            }

            else

            {

               if (OrderSelect(TicketSellStop,SELECT_BY_TICKET))

                  if (NormalizeDouble(OrderOpenPrice(),Digits)!=Price)

                     if (!OrderModify(TicketSellStop,Price,SL,TP,0,Green)) Comment("Error OrderModify SellStop ",GetLastError()," Lot=",Lot," Price=",Price," SL=",SL," TP=",TP);

            }

         }

         else drawtext(ssn,"=5?@028;L=0O F5=0 4;O SELLSTOP",Time[0],Price);

         if (ObjectGet(ss,OBJPROP_TIME1)!=ObjectGet(ssn,OBJPROP_TIME1) || ObjectGet(ssn,OBJPROP_PRICE1)!=Price) drawtext(ssn,"",(datetime)ObjectGet(ss,OBJPROP_TIME1),Price);

      }

   }

   else

   {

      if (ObjectFind(ssn)!=-1) ObjectDelete(ssn);

      if (OrderSelect(TicketSellStop,SELECT_BY_TICKET)) 

         if (!OrderDelete(TicketSellStop)) Print("Error OrderDelete ",GetLastError(),"  Ticket ",TicketSellStop);

   }

}

//--------------------------------------------------------------------

void drawline(string NameL, color col, double Y1)

{

   ObjectCreate(NameL, OBJ_TREND, 0,Time[10],Y1,Time[0]+Period()*600,Y1);

   ObjectSet   (NameL, OBJPROP_COLOR, col);

   ObjectSet   (NameL, OBJPROP_STYLE, STYLE_SOLID);

   ObjectSet   (NameL, OBJPROP_WIDTH, 1);

   ObjectSet   (NameL, OBJPROP_BACK,  false);

   ObjectSet   (NameL, OBJPROP_RAY,   false);

}

//--------------------------------------------------------------------

void drawtext(string NameL, string info, datetime X1, double Y1)

{

   if (info=="") 

   {

      info=StringConcatenate(StringSubstr(NameL,6,0),"  lot ",DoubleToStr(Lot,2));

      if (Stoploss!=0) info=StringConcatenate(info,"  SL " ,DoubleToStr(Stoploss ,0));

      if (Takeprofit!=0) info=StringConcatenate(info,"  TP " ,DoubleToStr(Takeprofit ,0));

   }

   ObjectDelete (NameL);

   ObjectCreate (NameL, OBJ_TEXT,0,X1,Y1,0,0,0,0);

   ObjectSetText(NameL, info ,FontSize,"Arial");

   ObjectSet    (NameL, OBJPROP_COLOR, ColorText);

}

//--------------------------------------------------------------------



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