Converting Minutes Integer to Hours & Minutes ...

 

Hello to everyone here on the mql4 Forums.


I have an EA that uses FFCal.ex4 forex factory news calendar to avoid trading before and after high and medium impact news times.

The EA allows me to set the amount of time before and after the news to stop trading and I am trying to display the remaining time until news starts or the remaining time until the news finishes in the chart.


I am able to obtain the value as in integer in minutes:

eg Minutes until news starts: 84


However, I would like to format the output like this instead:

eg News starts in: 1hr 24mins


For the hour value I was going to divide the minutes by 60 then remove the decimals:

NormalizeDouble(result, 0)


I can't think of a way to obtain the remaining digits and convert them to the minutes value. I wrote this myself but I am very new to mql4 (1 day old) so I expect it to be a bit messy.


Snippet #1:

extern bool AvoidNews=true;
extern bool High_Impact=true;
extern int MinsUntilNextHighNews=120;
extern int MinsSincePrevHighNews=420;
extern bool Medium_Impact=true;
extern int MinsUntilNextMediumNews=90;
extern int MinsSincePrevMediumNews=240;
extern bool Low_Impact=false;
extern int MinsUntilNextLowNews=60;
extern int MinsSincePrevLowNews=60;

Snippet #2:

  int MinSinceHighNews=iCustom(NULL,0,"FFCal",true,false,false,true,true,1,0);
      int MinUntilHighNews=iCustom(NULL,0,"FFCal",true,false,false,true,true,1,1);
     
      int MinSinceMediumNews=iCustom(NULL,0,"FFCal",false,true,false,true,true,1,0);
      int MinUntilMediumNews=iCustom(NULL,0,"FFCal",false,true,false,true,true,1,1);
     
      int MinSinceLowNews=iCustom(NULL,0,"FFCal",false,false,true,true,true,1,0);
      int MinUntilLowNews=iCustom(NULL,0,"FFCal",false,false,true,true,true,1,1);

      if(Minute()!=PrevMinute)
      {
          AllowTrading=true;
          PrevMinute=Minute();
          if (High_Impact)
             if (MinUntilHighNews <= MinsUntilNextHighNews || MinSinceHighNews <= MinsSincePrevHighNews) AllowTrading=false;
          if (Medium_Impact)
             if (MinUntilMediumNews <= MinsUntilNextMediumNews || MinSinceMediumNews <= MinsSincePrevMediumNews) AllowTrading=false;
          if (Low_Impact)
             if (MinUntilLowNews <= MinsUntilNextLowNews || MinSinceLowNews <= MinsSincePrevLowNews) AllowTrading=false; 


Snippet #3:

 else {
        PrintLN("Not trading! Avoiding News");
       
        int HighCome=(MinsUntilNextHighNews - MinUntilHighNews);
        int HighGone=(MinsSincePrevHighNews - MinSinceHighNews);
        int MediumCome=(MinsUntilNextMediumNews - MinUntilMediumNews);
        int MediumGone=(MinsSincePrevMediumNews - MinSinceMediumNews);
        int LowCome=(MinsUntilNextLowNews - MinUntilLowNews);
        int LowGone=(MinsSincePrevLowNews - MinSinceLowNews);
       
        if (High_Impact) {
        if (MinUntilHighNews <= MinsUntilNextHighNews) {
            PrintLN("Minutes until High Impact News Arrives:");
            PrintLN(HighCome, TextColor2, gi_664, gi_660 - 1, 200); } }
        if (MinSinceHighNews <= MinsSincePrevHighNews) {
            PrintLN("Minutes until High Impact News Finishes:");
            PrintLN(HighGone, TextColor2, gi_664, gi_660 - 1, 200); }
        if (Medium_Impact) {
        if (MinUntilMediumNews <= MinsUntilNextMediumNews) {
            PrintLN("Minutes until Medium Impact News Arrives:");
            PrintLN(MediumCome, TextColor2, gi_664, gi_660 - 1, 200); } }
        if (MinSinceMediumNews <= MinsSincePrevMediumNews) {
            PrintLN("Minutes until Medium Impact News Finishes:");
            PrintLN(MediumGone, TextColor2, gi_664, gi_660 - 1, 200); }
        if (Low_Impact) {
        if (MinUntilLowNews <= MinsUntilNextLowNews){
            PrintLN("Minutes until Low Impact News Arrives:");
            PrintLN(LowCome, TextColor2, gi_664, gi_660 - 1, 200); }
        if (MinSinceLowNews <= MinsSincePrevLowNews) {
            PrintLN("Minutes until Low Impact News Finishes:");
            PrintLN(LowGone, TextColor2, gi_664, gi_660 - 1, 200); } }
    }
   PrintSep();

Regards,

John

 

take this example again
number is 350 mins
step1 ) first divide 350/60 == 5.83
then take int value means 5
it is show hours
step2)
take decmial no. : .83333
again multiply by 60

.833333 * 60 =49.99

approx value is 50

http://www.codetoad.com/forum/15_26133.asp

 

Thank you gjol.


To obtain the decimal value I'm guessing I should subtract the whole number?


eg:

int hrs=(minutes / 60)

int mins=(minutes / 60)

NormalizeDouble(mins, 5)

int mins=(mins - hrs)

int mins=(mins * 60)

PrintLN("Trading resumes in: ");

PrintLN(hrs);

PrintLN(" hours and ");

PrintLN(mins);

PrintLN(" minutes.");

Regards,

John

 
int min = 350;
int x = 0;

while (min >= 60)
{
min = min - 60;
x ++;
}
Alert ("hours is " + (x)+ " Minutes is " +(min));
 

Thank you qjol. I've added comments so others know whats happening.

Regards,

John

int min = 350;  // The minutes value.
int x = 0;  // The hour value set to 0.
  
while (min > 60) // Loops until min value is less than 60 (1 Hour).
{
min = min - 60; // subtracts 60 (1 Hour) from min value each loop until min value is less than 60 (1 Hour).
x ++; // Increases hour value by 1 each successful loop.
}
Alert ("hours is " + (x)+ " Minutes is " +(min)); // Result is an hour value and the remaining min value thats less than 60 (1 Hour).
 

Here is how I have applied your code (tested and working).

To see the code prior to gjol's solution, see post #1. This requires FFCal.ex4 indicator.

Here are the external variables:

extern bool AvoidNews=true;
extern bool High_Impact=true;
extern int MinsUntilNextHighNews=120;
extern int MinsSincePrevHighNews=420;
extern bool Medium_Impact=true;
extern int MinsUntilNextMediumNews=90;
extern int MinsSincePrevMediumNews=240;
extern bool Low_Impact=false;
extern int MinsUntilNextLowNews=60;
extern int MinsSincePrevLowNews=60;
bool AllowTrading=true;


Here is the call to FFCal.ex4 and the switch for trading:

int start() {
   static bool AllowTrading=true;
   if(AvoidNews && !IsTesting())
   {
      static int PrevMinute=-1;  
   
      int MinSinceHighNews=iCustom(NULL,0,"FFCal",true,false,false,true,true,1,0);
      int MinUntilHighNews=iCustom(NULL,0,"FFCal",true,false,false,true,true,1,1);
      
      int MinSinceMediumNews=iCustom(NULL,0,"FFCal",false,true,false,true,true,1,0);
      int MinUntilMediumNews=iCustom(NULL,0,"FFCal",false,true,false,true,true,1,1);
      
      int MinSinceLowNews=iCustom(NULL,0,"FFCal",false,false,true,true,true,1,0);
      int MinUntilLowNews=iCustom(NULL,0,"FFCal",false,false,true,true,true,1,1);

      if(Minute()!=PrevMinute)
      {
          AllowTrading=true;
          PrevMinute=Minute();
          if (High_Impact)
             if (MinUntilHighNews <= MinsUntilNextHighNews || MinSinceHighNews <= MinsSincePrevHighNews) AllowTrading=false;
          if (Medium_Impact)
             if (MinUntilMediumNews <= MinsUntilNextMediumNews || MinSinceMediumNews <= MinsSincePrevMediumNews) AllowTrading=false;
          if (Low_Impact)
             if (MinUntilLowNews <= MinsUntilNextLowNews || MinSinceLowNews <= MinsSincePrevLowNews) AllowTrading=false;    
      }
   }


Here is the code to show data in the chart: PrintSep();

   else { 
                PrintLN("Not trading! Avoiding News");
                        
                int HighCome=(MinUntilHighNews + MinsSincePrevHighNews);                
                int HighGone=(MinsSincePrevHighNews - MinSinceHighNews);
                int MediumCome=(MinUntilMediumNews + MinsSincePrevMediumNews);  
                int MediumGone=(MinsSincePrevMediumNews - MinSinceMediumNews);
                int LowCome=(MinUntilLowNews + MinsSincePrevLowNews);           
                int LowGone=(MinsSincePrevLowNews - MinSinceLowNews);   
                
                if (High_Impact) {
                
                int set1 = MinsUntilNextHighNews;
                int y1 = 0;
                while (set1 >= 60) {
                set1 = set1 - 60;
                y1 ++; }
                
                int set2 = MinsSincePrevHighNews;
                int y2 = 0;
                while (set2 >= 60) {
                set2 = set2 - 60;
                y2 ++; }
                
                PrintLN("-- High Setting -----");
                PrintLN("Before: ");
                PrintLN((y1) + " hrs " + (set1) + " mins", TextColor2, gi_664, gi_660 - 1, 40); 
                PrintLN("After: ");
                PrintLN((y2) + " hrs " + (set2) + " mins", TextColor2, gi_664, gi_660 - 1, 40);
                PrintLN("Total: ");
                PrintLN((y1+y2) + " hrs " + (set1+set2) + " mins", TextColor2, gi_664, gi_660 - 1, 40);
                                        
                if (MinSinceHighNews <= MinsSincePrevHighNews) {
                        int min1 = HighGone;
                        int x1 = 0;
                        while (min1 >= 60) {
                        min1 = min1 - 60;
                        x1 ++; }
                        PrintLN("High ", Red);
                        PrintLN("Impact News finshes in: ", TextColor1, gi_664, gi_660 - 1, 25);
                        PrintLN((x1) + " hrs " + (min1) + " mins", TextColor2, gi_664, gi_660 - 1, 135); }
                else {
                if (MinUntilHighNews <= MinsUntilNextHighNews) {
                        int min2 = HighCome;
                        int x2 = 0;
                        while (min2 >= 60) {
                        min2 = min2 - 60;
                        x2 ++; }
                        PrintLN("High ", Red);
                        PrintLN("Impact News finshes in: ", TextColor1, gi_664, gi_660 - 1, 25);
                        PrintLN((x2) + " hrs " + (min2) + " mins", TextColor2, gi_664, gi_660 - 1, 135); } } }
                        
                if (Medium_Impact) {
                
                int set3 = MinsUntilNextMediumNews;
                int y3 = 0;
                while (set3 >= 60) {
                set3 = set3 - 60;
                y3 ++; }
                
                int set4 = MinsSincePrevMediumNews;
                int y4 = 0;
                while (set4 >= 60) {
                set4 = set4 - 60;
                y4 ++; }
                
                PrintLN("-- Medium Setting --");
                PrintLN("Before: ");
                PrintLN((y3) + " hrs " + (set3) + " mins", TextColor2, gi_664, gi_660 - 1, 40); 
                PrintLN("After: ");
                PrintLN((y4) + " hrs " + (set4) + " mins", TextColor2, gi_664, gi_660 - 1, 40);
                PrintLN("Total: ");
                PrintLN((y3+y4) + " hrs " + (set3+set4) + " mins", TextColor2, gi_664, gi_660 - 1, 40);
                                
                if (MinSinceMediumNews <= MinsSincePrevMediumNews) {
                        int min3 = MediumGone;
                        int x3 = 0;
                        while (min3 >= 60) {
                        min3 = min3 - 60;
                        x3 ++; }
                        PrintLN("Medium ", Yellow);
                        PrintLN("Impact News finshes in: ", TextColor1, gi_664, gi_660 - 1, 40);
                        PrintLN((x3) + " hrs " + (min3) + " mins", TextColor2, gi_664, gi_660 - 1, 150); }
                else {
                if (MinUntilMediumNews <= MinsUntilNextMediumNews) {
                        int min4 = MediumCome;
                        int x4 = 0;
                        while (min4 >= 60) {
                        min4 = min4 - 60;
                        x4 ++; }
                        PrintLN("Medium ", Yellow);
                        PrintLN("Impact News finshes in: ", TextColor1, gi_664, gi_660 - 1, 40);
                        PrintLN((x4) + " hrs " + (min4) + " mins", TextColor2, gi_664, gi_660 - 1, 150); } } }  
                        
                if (Low_Impact) {
                
                int set5 = MinsUntilNextLowNews;
                int y5 = 0;
                while (set5 >= 60) {
                set5 = set5 - 60;
                y5 ++; }
                
                int set6 = MinsSincePrevLowNews;
                int y6 = 0;
                while (set6 >= 60) {
                set6 = set6 - 60;
                y6 ++; }
                
                PrintLN("-- Medium Setting --");
                PrintLN("Before: ");
                PrintLN((y5) + " hrs " + (set5) + " mins", TextColor2, gi_664, gi_660 - 1, 40); 
                PrintLN("After: ");
                PrintLN((y6) + " hrs " + (set6) + " mins", TextColor2, gi_664, gi_660 - 1, 40);
                PrintLN("Total: ");
                PrintLN((y5+y6) + " hrs " + (set5+set6) + " mins", TextColor2, gi_664, gi_660 - 1, 40);
                                
                if (MinSinceLowNews <= MinsSincePrevLowNews) {
                        int min5 = LowGone;
                        int x5 = 0;
                        while (min5 >= 60) {
                        min5 = min5 - 60;
                        x5 ++; }
                        PrintLN("Low ", Green);
                        PrintLN("Impact News finshes in: ", TextColor1, gi_664, gi_660 - 1, 20);
                        PrintLN((x5) + " hrs " + (min5) + " mins", TextColor2, gi_664, gi_660 - 1, 130); }
                else {
                if (MinUntilLowNews <= MinsUntilNextLowNews) {
                        int min6 = LowCome;
                        int x6 = 0;
                        while (min6 >= 60) {
                        min6 = min6 - 60;
                        x6 ++; }
                        PrintLN("Low ", Green);
                        PrintLN("Impact News finshes in: ", TextColor1, gi_664, gi_660 - 1, 20);
                        PrintLN((x6) + " hrs " + (min6) + " mins", TextColor2, gi_664, gi_660 - 1, 130); } } }
        }
   PrintSep();

Here's a picture of the result:


Regards,

John

 
int Hr = xInMin/60;
int Min= XInMin%60;
 

WHRoeder:

int Hr = xInMin/60;
int Min= XInMin%60;

this one is Great
 
WHRoeder:
int Hr = xInMin/60;
int Min= XInMin%60;


Hello WHRoeder, can you explain these a bit for me?

Regards,

John

Reason: