share utility code - page 2

 
RaptorUK:
Why aren't you checking if the object already exists,  you do in your WriteText() function ?
? ? ?
 

Because you've already written, I just use it to change its value.

Like the time showing on the chart 

 

 

 writeText("time"," TIME : "+TimeToString(TimeCurrent(),TIME_MINUTES),clrWhite,20,90);
 


Macro definition  - eliminate division by zero errors 

//+------------------------------------------------------------------+
//| Macro definitions.                                               |
//+------------------------------------------------------------------+
//--- eliminate division by zero errors
#define DivZero(a,b) (b==0.) ?0 :1.0*a/b


Macro definition  - eliminate division by zero errors , output Warning.

//+------------------------------------------------------------------+
//| Macro definitions.                                               |
//+------------------------------------------------------------------+
//--- eliminate division by zero errors, output Warning.
#define DivZero(a,b) (b==0.) ?Print("Warning: division by zero in "+__FUNCTION__) :1.0*a/b 


Example:

//+------------------------------------------------------------------+
//| Macro definitions.                                               |
//+------------------------------------------------------------------+
//--- eliminate division by zero errors
#define DivZero(a,b) (b==0.) ?0 :1.0*a/b 

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
  int x,y;
  Print(DivZero(x,y));
  }
 
FinGeR:


Macro definition  - eliminate division by zero errors 


Example:

Good Idea
 
FinGeR:


Macro definition  - eliminate division by zero errors 


Example:

Thanks for sharing. As a remark, be cautious using such function as DivZero(), you don't get a runtime error causing an interruption of your program, BUT that can mask a logical error, much more difficult to find.
 
angevoyageur:
Thanks for sharing. As a remark, be cautious using such function as DivZero(), you don't get a runtime error causing an interruption of your program, BUT that can mask a logical error, much more difficult to find.

Thanks for the note.  've Added code example: eliminate division by zero errors , output Warning.

Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
MQL5 programs / Runtime Errors - Documentation on MQL5
 


Macro definition  - Return (number of digits or number of symbols after a decimal point)

//+------------------------------------------------------------------+
//| Macro definitions.                                               |
//+------------------------------------------------------------------+
//--- Returns number of digits or number of symbols after a decimal point
#define GetDigits(a) (StringFind(a,".")<0) ?0 :StringLen(StringSubstr(a,(StringFind(a,"."))+1,-1)) 


Example:

//+------------------------------------------------------------------+
//| Macro definitions.                                               |
//+------------------------------------------------------------------+
//--- Returns number of digits or number of symbols after a decimal point
#define GetDigits(a) (StringFind(a,".")<0) ?0 :StringLen(StringSubstr(a,(StringFind(a,"."))+1,-1))

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   double Numbers = 1235.22446235;
   string Name    = "_tmp.xxxxxx";

   Print(GetDigits(Numbers));
   Print(GetDigits(Name));
   
   }


Example 2:  Returns number of digits of the volume for a deal(lots)

#include <Trade\SymbolInfo.mqh>

CSymbolInfo       m_sym;

//+------------------------------------------------------------------+
//| Macro definitions.                                               |
//+------------------------------------------------------------------+
//--- Returns number of digits or number of symbols after a decimal point
#define GetDigits(a) (StringFind(a,".")<0) ?0 :StringLen(StringSubstr(a,(StringFind(a,"."))+1,-1))


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   // Returns number of digits of the volume for a deal(lots)   
   m_sym.Name(Symbol());
   Print(GetDigits(m_sym.LotsStep()));
 
   }
 

Delete pending order 

void DeletePendingOrder(int magic, string sym ,ENUM_ORDER_TYPE type =ORDER_TYPE_BUY_LIMIT )
{
    
    long order_ticket;
    
    HistorySelect(0,TimeCurrent());
   
    for (int i=OrdersTotal()-1;i>=0;i--)
    order_ticket=OrderGetTicket(i);
 
    if (OrderGetInteger(ORDER_MAGIC) == magic && OrderGetString(ORDER_SYMBOL) == sym && OrderGetInteger(ORDER_TYPE) == type )
    {
        MqlTradeResult result;
        MqlTradeRequest request;
        request.order=order_ticket;
        request.action=TRADE_ACTION_REMOVE;
        OrderSend(request,result);
     }
    }
 
kourosh1347:

Delete pending order 

I suggest you to add errors processing and to initialize your structure MqlTradeRequest.
 

hi;

below code can use for " send email & send massage to mobile & send sound and alarm " .i write below and use it in all my indicator.

uint counterBUY=0;
uint counterSELL=0;
double Ask,Bid;
string text,sAsk,sBid,sPeriod;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
input string s99="-----------------------------------------------";      // ----------- ALARM Settings -----------------
input uint Numberof_Alerts_Maximum_Iterations=2;
input bool On_Push = true;                        //allow to send push-messages
input bool On_Email = true;                       //allow to send e-mail messages
input bool On_Alert= true;                        //allow to put alert
input bool On_Play_Sound = true;                  //allow to put sound signal
input string NameFileSound="request.wav";          //name of the file with sound in "...\MetaTrader 5\Sounds"
input string  CommentSirName="your indicator name:";  //the first part of the allert comment
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,const int prev_calculated,
                const datetime &Time[],
                const double   &Open[],
                const double   &High[],
                const double   &Low[],
                const double   &Close[],
                const long     &TickVolume[],
                const long     &Volume[],
                const int      &Spread[])
{

//----------------------------------------------------------------------//
   MqlDateTime tm;
   TimeToStruct(TimeCurrent(),tm);
   text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min);
   Ask=Close[0];
   Bid=Close[0]+Spread[0];
   sAsk=DoubleToString(Ask,_Digits);
   sBid=DoubleToString(Bid,_Digits);
   sPeriod=EnumToString(ChartPeriod());
   if(rates_total!=prev_calculated) {counterBUY=0;counterSELL=0;}
//----------------------------------------------------------------------//      

//----------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------- PlaySound Alert SendNotification SendMail 
//+++
     {
      if(counterBUY<Numberof_Alerts_Maximum_Iterations)
        {
         if(Indicator_Signal[0]==1&&Indicator_Signal[1]!=1) {BUY_ALARM();counterBUY++;}
        }
      if(counterSELL<Numberof_Alerts_Maximum_Iterations)
        {
         if(Indicator_Signal[0]==-1&&Indicator_Signal[1]!=-1) {SELL_ALARM();counterSELL++;}
        }
     }
//----------------------------------------------------------------------------------------------------------//
//--- done
   return(rates_total);
}
//+------------------------------------------------------------------+
//----------------------------------------------------------------------------------------------------------//
//********************************************************************************************************************
void BUY_ALARM()
  {
   if(On_Alert) Alert("BUY # ",Symbol(),"\n Period=",sPeriod,"\n Ask=",sAsk,"\n Bid=",sBid,"\n currtime=",text);
   if(On_Play_Sound) PlaySound("matrixrevolution.wav");
   if(On_Email) SendMail("BUY #",Symbol()+" Period="+sPeriod+", Ask="+sAsk+", Bid="+sBid+", currtime="+text);
   if(On_Push) SendNotification("BUY #"+Symbol()+" Period="+sPeriod+", Ask="+sAsk+", Bid="+sBid+", currtime="+text);
  }
//+------------------------------------------------------------------+
void SELL_ALARM()
  {
   if(On_Alert) Alert("SELL # ",Symbol(),"\n Period=",sPeriod,"\n Ask=",sAsk,"\n Bid=",sBid,"\n currtime=",text);
   if(On_Play_Sound) PlaySound("matrixrevolution.wav");
   if(On_Email) SendMail("SELL #",Symbol()+" Period="+sPeriod+", Ask="+sAsk+", Bid="+sBid+", currtime="+text);
   if(On_Push) SendNotification("SELL #"+Symbol()+" Period="+sPeriod+", Ask="+sAsk+", Bid="+sBid+", currtime="+text);
  }
//********************************************************************************************************************
//----------------------------------------------------------------------------------------------------------//

example : https://www.mql5.com/en/code/1868

Awesome Oscillator Divergence
Awesome Oscillator Divergence
  • votes: 31
  • 2013.10.07
  • Mehrdad Shiri
  • www.mql5.com
This indicator will plot divergence lines on the Awesome_Oscillator indicator and will give buy and sell signal by displaying arrows.
Reason: