Pendings Monitor (no dll)

Author: Igor Zakharov, IgorNewMail@yandex.ru
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 strategyIt Closes Orders by itself
Miscellaneous
It opens Message Boxes to the userIt issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Pendings Monitor (no dll)
ÿþ#property copyright   "Igor Zakharov, IgorNewMail@yandex.ru"

#property version     "1.0"

#property link        "https://www.mql5.com/ru/users/igor.i"

#property description " "

#property strict



extern int    shift = 100;     //Pending step, pts

extern double factor= 2;       //Martigale step

input string  Cap3    ="";     //... Profit control:

extern double ProfBux = 20;    //Profit for Exit, Account currency

extern int    GridSize= 2;     //Grid Size for exit

/*input string  Cap4    ="";     //... Timer:

extern bool   LetGoToSleep = false;    //Allow hybernation after closing

extern int    T1 = 20;    //Fall asleep after...

extern int    T2 = 8;     //... 'till

*/

string obj_pref="PC_";

int FS=10;



/*

#import "kernel32.dll"

 int WinExec(uchar &command[],uint window_show_mode);

#import

*/



void OnInit()

{ 



   //if(!IsDllsAllowed() && LetGoToSleep) Alert("Let EA to use DLL in settings!!!");



   ObjectCreate(ChartID(),obj_pref+"Show",OBJ_BUTTON,0,0,0); 

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_XDISTANCE,FS*6);

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_YDISTANCE,20);

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_XSIZE,int(FS*5.6));     //--- CAB0=>28< @07<5@ :=>?:8

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_YSIZE,FS*2);

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_CORNER,CORNER_RIGHT_UPPER);   //--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

   ObjectSetString (ChartID(),obj_pref+"Show",OBJPROP_TEXT,"Show TP");//--- CAB0=>28< B5:AB

   ObjectSetString (ChartID(),obj_pref+"Show",OBJPROP_FONT,"Arial");

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_FONTSIZE,FS);

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_COLOR,clrBlack);

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_BGCOLOR,clrGray);

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_BORDER_COLOR,clrGray);//--- CAB0=>28< F25B 3@0=8FK

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_BACK,false);      //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(ChartID(),obj_pref+"Show",OBJPROP_STATE,false);     //--- ?5@52545< :=>?:C 2 7040==>5 A>AB>O=85



   ObjectCreate(ChartID(),obj_pref+"SetTP",OBJ_BUTTON,0,0,0); 

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_XDISTANCE,FS*12);

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_YDISTANCE,20);

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_XSIZE,int(FS*5.6));     //--- CAB0=>28< @07<5@ :=>?:8

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_YSIZE,FS*2);

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_CORNER,CORNER_RIGHT_UPPER);   //--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

   ObjectSetString (ChartID(),obj_pref+"SetTP",OBJPROP_TEXT,"Set TP");//--- CAB0=>28< B5:AB

   ObjectSetString (ChartID(),obj_pref+"SetTP",OBJPROP_FONT,"Arial");

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_FONTSIZE,FS);

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_COLOR,clrBlack);

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_BGCOLOR,clrWhite);

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_BORDER_COLOR,clrWhite);//--- CAB0=>28< F25B 3@0=8FK

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_BACK,false);      //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(ChartID(),obj_pref+"SetTP",OBJPROP_STATE,false);     //--- ?5@52545< :=>?:C 2 7040==>5 A>AB>O=85

   

   ObjectCreate(ChartID(),obj_pref+"Add",OBJ_BUTTON,0,0,0); 

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_XDISTANCE,FS*18);

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_YDISTANCE,20);

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_XSIZE,int(FS*5.6));     //--- CAB0=>28< @07<5@ :=>?:8

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_YSIZE,FS*2);

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_CORNER,CORNER_RIGHT_UPPER);   //--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

   ObjectSetString (ChartID(),obj_pref+"Add",OBJPROP_TEXT,"Add");//--- CAB0=>28< B5:AB

   ObjectSetString (ChartID(),obj_pref+"Add",OBJPROP_FONT,"Arial");

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_FONTSIZE,FS);

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_COLOR,clrBlack);

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_BGCOLOR,clrWhite);

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_BORDER_COLOR,clrWhite);

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_BACK,false);      //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(ChartID(),obj_pref+"Add",OBJPROP_STATE,false);     //--- ?5@52545< :=>?:C 2 7040==>5 A>AB>O=85



   ObjectCreate(ChartID(),obj_pref+"Stop",OBJ_BUTTON,0,0,0); 

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_XDISTANCE,FS*24);

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_YDISTANCE,20);

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_XSIZE,int(FS*5.6));     //--- CAB0=>28< @07<5@ :=>?:8

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_YSIZE,FS*2);

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_CORNER,CORNER_RIGHT_UPPER);   //--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

   ObjectSetString (ChartID(),obj_pref+"Stop",OBJPROP_TEXT,"STOP");//--- CAB0=>28< B5:AB

   ObjectSetString (ChartID(),obj_pref+"Stop",OBJPROP_FONT,"Arial");

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_FONTSIZE,clrBlack);

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_COLOR,clrWhite);

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_BGCOLOR,clrBlack);

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_BORDER_COLOR,clrDarkOrange);

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_BACK,false);      //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_STATE,false);     //--- ?5@52545< :=>?:C 2 7040==>5 A>AB>O=85



   ObjectCreate(ChartID(),obj_pref+"StopALL",OBJ_BUTTON,0,0,0); 

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_XDISTANCE,FS*33);

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_YDISTANCE,20);

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_XSIZE,int(FS*5.6));     //--- CAB0=>28< @07<5@ :=>?:8

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_YSIZE,FS*2);

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_CORNER,CORNER_RIGHT_UPPER);   //--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

   ObjectSetString (ChartID(),obj_pref+"StopALL",OBJPROP_TEXT,"STOP");//--- CAB0=>28< B5:AB

   ObjectSetString (ChartID(),obj_pref+"StopALL",OBJPROP_FONT,"Arial");

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_BORDER_TYPE,BORDER_FLAT);

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_FONTSIZE,clrBlack);

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_COLOR,clrWhite);

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_BGCOLOR,clrBlack);

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_BORDER_COLOR,clrDarkOrange);

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_BACK,false);      //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_STATE,false);     //--- ?5@52545< :=>?:C 2 7040==>5 A>AB>O=85



   EventSetTimer(1);

}



void OnDeinit(const int reason)

{

   ObjectsDeleteAll(0,obj_pref);

}



void OnChartEvent(const int id,         // 845=B8D8:0B>@ A>1KB8O  

                  const long& lparam,   // ?0@0<5B@ A>1KB8O B8?0 long

                  const double& dparam, // ?0@0<5B@ A>1KB8O B8?0 double

                  const string& sparam) // ?0@0<5B@ A>1KB8O B8?0 string

{

  if(ObjectGetInteger(0,obj_pref+"Stop",OBJPROP_STATE))

   {

    ObjectSetInteger(0,obj_pref+"Stop",OBJPROP_STATE,false);

    int ret=MessageBox("Close ALL "+Symbol()+" orders?",

       "Are you sure?", MB_OKCANCEL);

      if (ret==IDOK) CloseALL();

   }

   

  if(ObjectGetInteger(0,obj_pref+"StopALL",OBJPROP_STATE))

   {

    ObjectSetInteger(0,obj_pref+"StopALL",OBJPROP_STATE,false);

    int ret=MessageBox("Close ALL orders of this account?",

       "Are you sure?", MB_OKCANCEL);

      if (ret==IDOK) CloseALL(true);

   }



   if(ObjectGetInteger(0,obj_pref+"Add",OBJPROP_STATE))

   {

    ObjectSetInteger(0,obj_pref+"Add",OBJPROP_STATE,false);

    int ret=MessageBox("Add pending to "+Symbol()+" ?",

       "Are you sure?", MB_OKCANCEL);

    if (ret==IDOK) 

     {

      double OP=-1,lot=-1;

      int OT=-1;

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

       if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol() && lot<OrderLots()) 

        {

          lot=OrderLots();

          OP=OrderOpenPrice();

          OT=OrderType();

        }

      if(OT>-1)

        int res=OrderSend(_Symbol,OT%2==0?OP_BUYLIMIT:OP_SELLLIMIT,NormalizeDouble(lot*factor,2),ND(OP+(OT%2==0?-1:1)*shift*_Point),9999,0,0,"",0,0,clrNONE);

     }

   }



  if(ObjectGetInteger(0,obj_pref+"Show",OBJPROP_STATE))

   {

    ObjectSetInteger(0,obj_pref+"Show",OBJPROP_BORDER_COLOR,clrWhite);

    ObjectSetInteger(0,obj_pref+"Show",OBJPROP_BGCOLOR,clrWhite);

    if(ObjectCreate(0,obj_pref+"TP",OBJ_ARROW_RIGHT_PRICE,0,0,0))

     {

      ObjectSetInteger(ChartID(),obj_pref+"TP",OBJPROP_COLOR,clrWhite);

      ObjectSetInteger(ChartID(),obj_pref+"TP",OBJPROP_WIDTH,3);

      ObjectSetInteger(ChartID(),obj_pref+"TP",OBJPROP_SELECTABLE,true);

      ObjectSetInteger(ChartID(),obj_pref+"TP",OBJPROP_SELECTED,true);

      ObjectSetInteger(ChartID(),obj_pref+"TP",OBJPROP_BACK,false);    

      ObjectSetInteger(ChartID(),obj_pref+"TP",OBJPROP_TIME,Time[0]);

      ObjectSetDouble (ChartID(),obj_pref+"TP",OBJPROP_PRICE,FindLoseless());

     }

    else

     {

      ObjectSetInteger(ChartID(),obj_pref+"TP",OBJPROP_SELECTED,true);  

      ObjectSetInteger(ChartID(),obj_pref+"TP",OBJPROP_TIME,Time[0]);

     }

   }

  else

   {

    ObjectSetInteger(0,obj_pref+"Show",OBJPROP_BORDER_COLOR,clrGray);

    ObjectSetInteger(0,obj_pref+"Show",OBJPROP_BGCOLOR,clrGray);

    ObjectDelete(0,obj_pref+"TP");

   }

  

  if(ObjectGetInteger(0,obj_pref+"SetTP",OBJPROP_STATE))

   {

    ObjectSetInteger(0,obj_pref+"SetTP",OBJPROP_STATE,false);

    if(ObjectFind(0,obj_pref+"TP")<0) return;

    double TP=ObjectGetDouble (ChartID(),obj_pref+"TP",OBJPROP_PRICE);



    int ret=MessageBox("Set TP = "+DoubleToString(TP,_Digits)+" to all "+Symbol()+" orders?",

       "Are you sure?", MB_OKCANCEL);

    if (ret==IDOK) 

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

       if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && 

        OrderSymbol() == Symbol())// && OrderTakeProfit()!=TP)

         bool res=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),ND(TP),OrderExpiration(),clrNONE);

    

    ObjectSetInteger(0,obj_pref+"Show",OBJPROP_STATE,false);

    ObjectSetInteger(0,obj_pref+"Show",OBJPROP_BORDER_COLOR,clrGray);

    ObjectSetInteger(0,obj_pref+"Show",OBJPROP_BGCOLOR,clrGray);

    ObjectDelete(0,obj_pref+"TP");

   }



  if(id==CHARTEVENT_KEYDOWN) 

   {

    switch((int)lparam)

     {

      case 85: if(ObjectFind(0,obj_pref+"TP")<0) return;

               ObjectSetDouble (ChartID(),obj_pref+"TP",OBJPROP_PRICE,ObjectGetDouble(ChartID(),obj_pref+"TP",OBJPROP_PRICE)+_Point);

               return;

      case 68: if(ObjectFind(0,obj_pref+"TP")<0) return;

               ObjectSetDouble (ChartID(),obj_pref+"TP",OBJPROP_PRICE,ObjectGetDouble(ChartID(),obj_pref+"TP",OBJPROP_PRICE)-_Point);

               return;

     }

   }



} 



double ND(double Value)

{

 return(NormalizeDouble(Value,_Digits));

}



void CloseALL(int ignore_symbol=false)

{

  ulong counter=GetMicrosecondCount();



  //close the real

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

    if( OrderSelect(i,SELECT_BY_POS) 

     && (ignore_symbol || (!ignore_symbol && OrderSymbol()==_Symbol))

     && OrderType()<2

      )

       {

         RefreshRates();

         if(!OrderClose(OrderTicket(),OrderLots(),(OrderType()==OP_BUY?Bid:Ask),9999,clrNONE)) 

               Alert("Problem with closing #",OrderTicket(),"; Error: ",GetLastError());

       }

  

  ulong counter2=GetMicrosecondCount();



  //delete pendings

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

   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && 

      (ignore_symbol || (!ignore_symbol && OrderSymbol()==_Symbol))

      && OrderType()>1)

        bool res=OrderDelete(OrderTicket(),clrNONE);

  

  Print ("Closing time: ",(GetMicrosecondCount()-counter)/1000," ms (real: ",(counter2-counter)/1000," + pendings: ",(GetMicrosecondCount()-counter2)/1000,")");

}



void OnTick()

 {

  int real=0, pend=0;

  double prof_bux=0;

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

   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && 

    OrderSymbol() == Symbol())

     {

      if(OrderType()<2) real++;

      else              pend++;

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

     }

   

   ObjectSetString (ChartID(),obj_pref+"Stop",OBJPROP_TEXT,DoubleToString(prof_bux,fabs(prof_bux)>=100?0:fabs(prof_bux)>10?1:2));

   ObjectSetInteger(ChartID(),obj_pref+"Stop",OBJPROP_BORDER_COLOR,prof_bux<0?clrDarkOrange:clrWhite);

  

   if((real==0 && pend>0) || (real>=GridSize && prof_bux>=ProfBux)) CloseALL();



   //if(LetGoToSleep && OrdersTotal()==0 && !TimeToTrade()) GoToSleep();

 }



void OnTimer()

 {

   ObjectSetString (ChartID(),obj_pref+"StopALL",OBJPROP_TEXT,DoubleToString(AccountProfit(),fabs(AccountProfit())>=100?0:fabs(AccountProfit())>10?1:2));

   ObjectSetInteger(ChartID(),obj_pref+"StopALL",OBJPROP_BORDER_COLOR,AccountProfit()<0?clrDarkOrange:clrWhite);

 }

/*

bool TimeToTrade()

// 5 B>@3C5< 2 AC11>BC 8 2>A:@5A5=L5. ">@3C5< <564C "1 8 "2.

{

 double h=TimeHour     (TimeCurrent())+TimeMinute(TimeCurrent())*1.0/60;

 int    d=TimeDayOfWeek(TimeCurrent());



 if(T2>T1) return((h>=T1 && h<T2));

 else      return((h>=T1 || h<T2));

}





void GoToSleep(string key="/h")

 {

   Print("Called: shutdown.exe "+key," ...See you later :)");

   uchar u_command[];

   string s_command = "shutdown.exe "+key;

   ArrayResize(u_command, StringLen(s_command)+1);

   StringToCharArray(s_command, u_command);

   int res = WinExec(u_command,0);

   if (res<32) Alert("Failed to go to sleep: ",res);

 }

*/



double FindLoseless()

{

double lot_real=0,prof_real=0,

       lot_potenc=0,prof_potenc=0,

       tickvalue = MarketInfo(Symbol(), MODE_TICKVALUE)/MarketInfo(Symbol(), MODE_TICKSIZE);



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

    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&

       OrderSymbol()==Symbol())

    {

      if(OrderType()==OP_SELL)

       {

        lot_real-=OrderLots();

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

        continue;

       }

      if(OrderType()==OP_BUY)

       {

        lot_real+=OrderLots();

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

        continue;

       }

    }



 if(fabs(lot_real)<MarketInfo(Symbol(),MODE_MINLOT)) return(0);

 

 if(lot_real>0) return(Bid-prof_real/lot_real/tickvalue);

 else           return(Ask-prof_real/lot_real/tickvalue);

}

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