Download MetaTrader 5

Simple Bar Timer - converting to MQL5 - problem

To add comments, please log in or register
Warble68
40
Warble68  

Hi all,

here a very simple Bar Timer that i am trying to convert from MQL4 to MQL5. In Metatrader 4 it works well on all timeframes.

The problem -

in MT5 it works properly on timeframes below an hour, but anything above an hour it refuses to update. Anybody got an idea of what i may be doing wrong?


//+------------------------------------------------------------------+
//| BarTimer.mq5 |
//+------------------------------------------------------------------+
#property indicator_chart_window
//
input int FontSize1 = 12;
input color FontColor1 = Black;
//+------------------------------------------------------------------+
void OnInit()
{
     ObjectCreate(0, "BarPercent" , OBJ_LABEL,0,0,0);
     ObjectSetInteger(0, "BarPercent", OBJPROP_XDISTANCE, 55);
     ObjectSetInteger(0, "BarPercent", OBJPROP_YDISTANCE, 30);
     ObjectSetInteger(0, "BarPercent", OBJPROP_CORNER, 3);
     ObjectSetInteger(0, "BarPercent", OBJPROP_HIDDEN,1);
     ObjectSetInteger(0, "BarPercent", OBJPROP_ANCHOR, ANCHOR_LEFT);     
     ObjectSetString(0, "BarPercent", OBJPROP_TEXT, "0%");
     ObjectSetInteger(0, "BarPercent", OBJPROP_COLOR, FontColor1); 
     ObjectSetInteger(0, "BarPercent", OBJPROP_FONTSIZE, FontSize1);
}           
//+------------------------------------------------------------------+
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 &tick_volume[],
                const long &volume[],
                const int &spread[])
{
     ArraySetAsSeries(time, true);
//     
     int sec = 0;
     int pc = 0;
     int pct = 0;  
//
     sec = TimeCurrent() - time[0];
     pc = (100 * sec) / (_Period * 60);
//     
     if (pc > 100)
{  
     pct = 100;
} 
     else 
     pct = pc;
//
     ObjectSetString(0, "BarPercent", OBJPROP_TEXT, pct + "%");
// 
     return(0);           
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
     ObjectDelete(0, "BarPercent");
}
//+------------------------------------------------------------------+


Here the MQL4 version that works properly;


//+------------------------------------------------------------------+
//| BarTimer.mq4 |
//+------------------------------------------------------------------+
#property indicator_chart_window
//
input int FontSize1 = 12;
input color FontColor1 = Black;
//+------------------------------------------------------------------+
void OnInit()
{
     ObjectCreate(0, "BarPercent" , OBJ_LABEL,0,0,0);
     ObjectSetInteger(0, "BarPercent", OBJPROP_XDISTANCE, 55);
     ObjectSetInteger(0, "BarPercent", OBJPROP_YDISTANCE, 30);
     ObjectSetInteger(0, "BarPercent", OBJPROP_CORNER, 1);
     ObjectSetInteger(0, "BarPercent", OBJPROP_HIDDEN,1);
     ObjectSetInteger(0, "BarPercent", OBJPROP_ANCHOR, ANCHOR_LEFT);     
     ObjectSetString(0, "BarPercent", OBJPROP_TEXT, "0%");
     ObjectSetInteger(0, "BarPercent", OBJPROP_COLOR, FontColor1); 
     ObjectSetInteger(0, "BarPercent", OBJPROP_FONTSIZE, FontSize1);
}           
//+------------------------------------------------------------------+
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 &tick_volume[],
                const long &volume[],
                const int &spread[])
{
     int sec = 0;
     int pc = 0;
     int pct = 0;  
//
     sec = TimeCurrent() - Time[0];
     pc = (100 * sec) / (Period() * 60);
//     
     if (pc > 100)
{  
     pct = 100;
} 
     else 
     pct = pc;
//
     ObjectSetString(0, "BarPercent", OBJPROP_TEXT, pct + "%"); 
     return(0);           
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
     ObjectDelete(0, "BarPercent");
}
//+------------------------------------------------------------------+


Thanks in advance!

Alain Verleyen
Moderator
33770
Alain Verleyen  
 pc = (100 * sec) / (_Period * 60);

Bad practice. What is the value of _Period ?

Use PeriodSeconds() instead.

Warble68
40
Warble68  
Alain Verleyen:

Bad practice. What is the value of _Period ?

Use PeriodSeconds() instead.

Thank you very much for putting me on the right track! Works perfectly now.
whroeder1
16502
whroeder1  
     pc = (100 * sec) / (Period() * 60);
Int/Int is an int. On MT4 v434, division quotient don't give floating point values(Bug??) - MQL4 forum
To add comments, please log in or register