Visual_market_pending_orders_alertMagic

Author: Roll
Orders Execution
Checks for the total of open ordersChecks for the total of closed orders
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Visual_market_pending_orders_alertMagic
//+------------------------------------------------------------------+
//|                      Visual_market_pending_orders_alertMagic.mq4 |
//|                                                             Roll |
//+------------------------------------------------------------------+
#property copyright "Roll"
#property indicator_chart_window
#include <Events.mq4>
extern bool  lShowTargets=true;
extern int   Magic=0;
extern int   IndGroupOrders=0; //0-all;1-market orders;2-pending orders.
extern color OpenColor=Aqua;//Color Op
extern color TakeProfitColor=Lime;//Color TakeProfit
extern color StopLossColor=Gold;//Color StopLoss
int i,j,t,Tip;
string Symb,n;
//----
int init(){Symb=Symbol();return(0);} 
int deinit()
{for(i=0;i<OrdersTotal();i++)
 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&(OrderSymbol()==Symb))delete();
 DeleteHistory();
 for(i=0;i<30;i++)
 {ObjectDelete(Symb+DoubleToStr(Period(),0)+"topen"+i); 
  ObjectDelete(Symb+DoubleToStr(Period(),0)+"ttake"+i); 
  ObjectDelete(Symb+DoubleToStr(Period(),0)+"tstop"+i);}
 DeleteLabels();return(0);}
//--------------------------------------------------------------
int start()
{i=0;j=0;
 DeleteHistory();
 int cnt=OrdersTotal();
 DeleteLabels();
 int MagicNumber=Magic;
 CheckEvents(MagicNumber);
 if(eventBuyClosed_SL>0)Alert(Symb,": Buy-ïîçèöèÿ çàêðûòà ïî ÑòîïËîññó! Magic - ",Magic);
 if(eventBuyClosed_TP>0)Alert(Symb,": Buy-ïîçèöèÿ çàêðûòà ïî ÒåéêÏðîôèòó! Magic - ",Magic);
 if(eventSellClosed_SL>0)Alert(Symb,": Sell-ïîçèöèÿ çàêðûòà ïî ÑòîïËîññó! Magic - ",Magic);
 if(eventSellClosed_TP>0)Alert(Symb,": Sell-ïîçèöèÿ çàêðûòà ïî ÒåéêÏðîôèòó! Magic - ",Magic);	
 if(eventBuyLimitOpened>0)Alert(Symb,": ñðàáîòàë BuyLimit îðäåð! Magic - ",Magic);
 if(eventBuyStopOpened>0)Alert(Symb,": ñðàáîòàë BuyStop îðäåð! Magic - ",Magic); 
 if(eventSellLimitOpened>0)Alert(Symb,": ñðàáîòàë SellLimit îðäåð! Magic - ",Magic);
 if(eventSellStopOpened>0)Alert(Symb,": ñðàáîòàë SellStop îðäåð! Magic - ",Magic); 
 for(i=0;i<OrdersTotal();i++)  
 {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&(OrderSymbol()==Symb))
  {if(lShowTargets)PROF();
  if(cnt!=OrdersTotal())continue;
   Tip=-1;Tip=OrderType();
   if(IndGroupOrders==1)
    {if(Tip<2)F();if(Tip>1)delete();}
   if(IndGroupOrders==2)
    {if(Tip>1)F();if(Tip<2)delete();}
  if(IndGroupOrders==0)F();}}
  ShowLabels();if(lShowTargets){PROF();fShowTargets();}return(0);}
//----------------------------------------------------
void delete()
{n=OrderSymbol()+"_"+DoubleToStr(OrderTicket(),0)+"_"+DoubleToStr(Period(),0);
 if(ObjectFind(n+"open")!=-1)ObjectDelete(n+"open");     
 if(ObjectFind(n+"stop")!=-1)ObjectDelete(n+"stop");
 if(ObjectFind(n+"take")!=-1)ObjectDelete(n+"take");
 if(ObjectFind("Text_Label")!=-1)ObjectDelete("Text_Label");}
//----------------------------------------------------
void F()
{double OPos,SLpos,TPpos,popen,ptake,pstop;int awh[];
 int myTicket=OrderTicket();    
 n=OrderSymbol()+"_"+DoubleToStr(OrderTicket(),0)+"_"+DoubleToStr(Period(),0);
 OPos=OrderOpenPrice();
 SLpos=ND(OrderStopLoss());
 TPpos=ND(OrderTakeProfit());
 if(ObjectFind(n+"open")==-1)
  {ObjectCreate(n+"open",OBJ_HLINE,awh[j],Time[0],OPos);
   ObjectSet(n+"open",OBJPROP_COLOR,OpenColor);
   ObjectSet(n+"open",OBJPROP_WIDTH,2);}
 if((ObjectFind(n+"stop")==-1)&&(SLpos!=0))
  {ObjectCreate(n+"stop",OBJ_HLINE,awh[j],Time[0],SLpos);
   ObjectSet(n+"stop",OBJPROP_COLOR,StopLossColor);
   ObjectSet(n+"stop",OBJPROP_WIDTH,2);}
 if((ObjectFind(n+"take")==-1)&&(TPpos!=0))
  {ObjectCreate(n+"take",OBJ_HLINE,awh[j],Time[0],TPpos);
   ObjectSet(n+"take",OBJPROP_COLOR,TakeProfitColor);
   ObjectSet(n+"take",OBJPROP_WIDTH,2);}
 popen=ND(ObjectGet(n+"open",OBJPROP_PRICE1));
 ptake=ND(ObjectGet(n+"take",OBJPROP_PRICE1));
 pstop=ND(ObjectGet(n+"stop",OBJPROP_PRICE1));
if((popen!=OPos)||(pstop!=SLpos)||(ptake!=TPpos))
  {if(ND(popen)!=OPos)ObjectSet(n+"open",OBJPROP_PRICE1,OPos);
   if(ND(pstop)!=SLpos)ObjectSet(n+"stop",OBJPROP_PRICE1,SLpos);
   if(ND(ptake)!=TPpos)ObjectSet(n+"take",OBJPROP_PRICE1,TPpos);}}
//----------------------------------------------------
void DeleteHistory()
{for(i=0;i<OrdersHistoryTotal();i++)
 if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)&&(OrderSymbol()==Symb))delete();}
//-----------
void fShowTargets()      
{i=0;Tip=-1;
 int period=Period(),prof_pts=0,ticket=0,FS=14;
 double dist_stop_pts,dist_stop,dist_take_pts,dist_take,SL,TP,Price;
 double point=MarketInfo(Symbol(),MODE_POINT);
 double pr=(Ask+Bid)/2,prof=0;             
 datetime CT=Time[0]+period*60*(WindowBarsPerChart()/20+2);
 double CP=5*point;
 color  prof_clr=Blue;
 string txt="";
 for(i=0;i<10;i++)
 {ObjectDelete(Symb+DoubleToStr(period,0)+"topen"+i);
  ObjectDelete(Symb+DoubleToStr(period,0)+"ttake"+i);
  ObjectDelete(Symb+DoubleToStr(period,0)+"tstop"+i);}
 for(i=0;i<OrdersTotal();i++)
 {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
  {Tip=OrderType();
   Price=OrderOpenPrice();
   TP=OrderTakeProfit();
   SL=OrderStopLoss();
   prof=OrderProfit();
  if(Tip==0){pr=Bid;prof_pts=ND((pr-Price)/point);}//"ïðîôèò â ïóíêòàõ"
  if(Tip==2){pr=Ask;prof_pts=ND((pr-Price)/point);}
  if(Tip==4){pr=Ask;prof_pts=ND((pr-Price)/point);}
  if(Tip==1){pr=Ask;prof_pts=ND((Price-pr)/point);}
  if(Tip==3){pr=Bid;prof_pts=ND((Price-pr)/point);}
  if(Tip==5){pr=Bid;prof_pts=ND((Price-pr)/point);}
  prof_clr=Blue;
  if(prof_pts<0)prof_clr=OrangeRed;//åñëè ïðîôèò â óáûòêå öâåò ïðîôèòà êðàñíûé
  if(prof_pts>0)prof_clr=Lime;     //åñëè ïðîôèò â ïðèáûëè öâåò ïðîôèòà çåëåíûé
  ObjectCreate(Symb+DoubleToStr(period,0)+"topen"+i,OBJ_TEXT,0,CT,Price+CP);//îáðàçîâàòü òåêñòîâûé îáúåêò.ãäå i - íîìåð îðäåðà
  txt=""; 
  if(Tip==0||Tip==1)txt="("+DoubleToStr(MathAbs(prof),2)+"$)";
  ObjectSetText(Symb+DoubleToStr(period,0)+"topen"+i,DoubleToStr(MathAbs(prof_pts),0)+txt,FS,"Arial Black",prof_clr);
  dist_take_pts=0;dist_take=0;
  if(Tip==2)pr=Bid;
  if(Tip==4)pr=Bid;
  if(Tip==3)pr=Ask;
  if(Tip==5)pr=Ask;
  if(TP!=0) 
  {dist_take_pts=MathAbs(ND((pr-TP)/point));
   txt=DoubleToStr(dist_take_pts,0); 
   dist_take_pts=MathAbs(ND((Price-TP)/point));
   txt=txt+"("+DoubleToStr(dist_take_pts,0)+"/"+DoubleToStr(dist_take_pts*OrderLots()*10,0)+"$ )";
   ObjectCreate(Symb+DoubleToStr(period,0)+"ttake"+i,OBJ_TEXT,0,CT,TP+CP);
   ObjectSetText(Symb+DoubleToStr(period,0)+"ttake"+i,txt,FS,"Arial Black",Lime);}
  dist_stop_pts=0;dist_stop=0;
  if(SL!=0)
  {dist_stop_pts=MathAbs(ND((pr-SL)/point));
   txt=DoubleToStr(dist_stop_pts,0);
   dist_stop_pts=MathAbs(ND((Price-SL)/point));
   txt=txt+"("+DoubleToStr(dist_stop_pts,0)+"/"+DoubleToStr(dist_stop_pts*OrderLots()*10,0)+"$)";
   ObjectCreate(Symb+DoubleToStr(period,0)+"tstop"+i,OBJ_TEXT,0,CT,SL+CP);
   ObjectSetText(Symb+DoubleToStr(period,0)+"tstop"+i,txt,FS,"Arial Black",OrangeRed);}}}}
//----------------------------------------------------------------------------
void DeleteLabels()
{i=0;n="";t=Time[WindowFirstVisibleBar()];
for(i=0;i<OrdersHistoryTotal();i++)
 {if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)&&OrderSymbol()==Symb)
  {n=OrderSymbol()+Period()+DoubleToStr(OrderTicket(),0);
   ObjectDelete(n+"stoplbl");
   ObjectDelete(n+"takelbl");}}
for(i=0;i<OrdersTotal();i++)
 {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&OrderSymbol()==Symb)
  {n=OrderSymbol()+Period()+DoubleToStr(OrderTicket(),0);
   ObjectDelete(n+"stoplbl");
   ObjectDelete(n+"takelbl");}}}
//---------------------------------------------
void ShowLabels()  
{i=0;n="";t=Time[WindowFirstVisibleBar()-(WindowBarsPerChart()/20)];
for(i=0;i<OrdersTotal();i++)
{if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)&&OrderSymbol()==Symb)
 {n=OrderSymbol()+Period()+DoubleToStr(OrderTicket(),0);
  ObjectDelete(n+"stoplbl");
  ObjectCreate(n+"stoplbl",OBJ_TEXT,0,t,OrderStopLoss()); 
  ObjectSetText(n+"stoplbl","#"+DoubleToStr(OrderTicket(),0)+"  stop",12,"Arial Black",StopLossColor);
  ObjectDelete(n+"takelbl");
  ObjectCreate(n+"takelbl",OBJ_TEXT,0,t,OrderTakeProfit()); 
  ObjectSetText(n+"takelbl","#"+DoubleToStr(OrderTicket(),0)+"  take",12,"Arial Black",TakeProfitColor);}}}
//---------------------------------------------
void PROF()
{double Profit=0;color Profit_clr=Blue;
 Profit=AccountProfit();
 if(Profit<0)Profit_clr=Magenta;
 if(Profit>0)Profit_clr=Yellow; 
 ObjectCreate("Text_Label",OBJ_LABEL,0,0,0);   
 ObjectSetText("Text_Label",("Profit =  "+(DoubleToStr(Profit,2))+" $"),16,"Times New Roman",Profit_clr);   
 ObjectSet("Text_Label",OBJPROP_CORNER,2);   
 ObjectSet("Text_Label",OBJPROP_XDISTANCE,5);   
 ObjectSet("Text_Label",OBJPROP_YDISTANCE,5);}
//---------NORMALIZATION-------------------+
double ND(double v){double digits=MarketInfo(Symb,MODE_DIGITS);return(NormalizeDouble(v,digits));}

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