MT4 Coding Snippets

2
Time filter ...

Code: Select all

bool checkTimeLimits(uint startHour, uint endHour, datetime timeToCheck) { return(checkTimeLimits(startHour,0,0,endHour,0,0,timeToCheck)); }
bool checkTimeLimits(uint startHour, uint startMinute, uint endHour, uint endMinute, datetime timeToCheck) { return(checkTimeLimits(startHour,startMinute,0,endHour,endMinute,0,timeToCheck)); }
bool checkTimeLimits(uint startHour, uint startMinute, uint startSecond, uint endHour, uint endMinute, uint endSecond, datetime timeToCheck)
{
   bool answer = false;
   MqlDateTime tempTime; TimeToStruct(timeToCheck,tempTime);
      datetime startTime,endTime,checkTime;
               startTime = _ctMinMax(startHour,0,24)    *3600+_ctMinMax(startMinute,0,60) *60+_ctMinMax(startSecond,0,60);
               endTime   = _ctMinMax(endHour,0,24)      *3600+_ctMinMax(endMinute,0,60)   *60+_ctMinMax(endSecond,0,60);
               checkTime = _ctMinMax(tempTime.hour,0,24)*3600+_ctMinMax(tempTime.min,0,60)*60+_ctMinMax(tempTime.sec,0,60);
               
   if (startTime>endTime)
          answer = (checkTime>=startTime || checkTime<=endTime);
   else   answer = (checkTime>=startTime && checkTime<=endTime);
   return(answer);
}
uint _ctMinMax(uint value, uint min, uint max) { return((uint)MathMax(MathMin(value,max),min)); }
example of time filter usage (both mt4 and mt5 versions) :
These users thanked the author mladen for the post (total 2):
ChuChu Rocket, GelindoP

Re: MT4 Coding Snippets

3
Dearest MLADEN
Thanks for posting today needs snippet.
May i know is it only used within EAs or is for indicators too,my second query,if we needs 2 different codes for ea and indicator.
regards
Indicator is just a tool.

Use it only if it can benefit you. Leave it if you don't know how to use it optimally.

Re: MT4 Coding Snippets

4
An idea to try to expand the capacity of the hour filter.

I can not fit Sunday in the filter correctly, but surely something happens to us all.

I hope that the template with the parameters that I attach is interpreted correctly and is easy to understand.

Code: Select all

//+------------------------------------------------------------------+
//|                                      time_filter_example_1.1.mq4 |
//|                                                           mladen |
//|                                    https://www.forex-station.com |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "https://www.forex-station.com"
#property version   "1.1"
#property strict


input string      ———————FilterTime———————   = "===== FILTER TIME =====";
input Filter_Time UseFilterTime              = 0;
//input string     UFT_0                        = "UFT = 0, Off";
//input string     UFT_1                        = "UFT = 1, Filter Weekend";
//input string     UFT_2                        = "UFT = 2, Filter Intraday";
input Time_Use    ServerOrLocalTime          = 1;
//input string     SOLT_0                          = "SOLT = 0, Server Time";
//input string     SOLT_1                          = "SOLT = 1, Local Time";
input string      Monday                     = "MONDAY";
input string      Note_Start_Monday          = "Filter Weekend - Filter Intraday";     
input string      Note_End_Monday            = "Filter Intraday"; 
input string      Tuesday                    = "TUESDAY";
input string      Note_Start_Tuesday         = "Filter Intraday";     
input string      Note_End_Tuesday           = "Filter Intraday"; 
input string      Wednesday                  = "WEDNESDAY";
input string      Note_Start_Wednesday       = "Filter Intraday";     
input string      Note_End_Wednesday         = "Filter Intraday"; 
input string      Thursday                   = "THURSDAY";
input string      Note_Start_Thursday        = "Filter Intraday";     
input string      Note_End_Thursday          = "Filter Intraday";
input string      Friday                     = "FRIDAY";
input string      Note_Start_Friday          = "Filter Intraday";
input string      Note_End_Friday            = "Filter Weekend - Filter Intraday"; 
//--------------------------------------------------------------------------------------------------
input uint        StartHour                  =  0; // Start hour
input uint        StartMinute                =  0; // Start minute
input uint        StartSecond                =  0; // Start second
input uint        EndHour                    = 24; // Ending hour
input uint        EndMinute                  =  0; // Ending minute
input uint        EndSecond                  =  0; // Ending second
//--------------------------------------------------------------------------------------------------
input CloseActiveTrader  UseCloseActiveTrader   = 0;
//input string     UCAT_0                          = "UCAT = 0, Off";
//input string     UCAT_1                          = "UCAT = 1, Filter Hour Close Active Trader Intraday";

input uint        CloseTraderEndHour            = 24; // Close Active Trader Ending hour
input uint        CloseTraderEndMinute          =  0; // Close Active Trader Ending minute
input uint        CloseTraderEndSecond          =  0; // Close Active Trader Ending second
I insist, it is an idea, feel free to program it or omit it Mr. mladen

a cordial greeting

Hermo

I take this opportunity to wish you the best of luck with this new project.


GetPrice() function (universal version)

8
Get price function - universal (mt4 and mt5 version)

Price enum :

Code: Select all

enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_tbiased2,   // Trend biased (extreme) price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased,  // Heiken ashi trend biased price
   pr_hatbiased2, // Heiken ashi trend biased (extreme) price
   pr_habclose,   // Heiken ashi (better formula) close
   pr_habopen ,   // Heiken ashi (better formula) open
   pr_habhigh,    // Heiken ashi (better formula) high
   pr_hablow,     // Heiken ashi (better formula) low
   pr_habmedian,  // Heiken ashi (better formula) median
   pr_habtypical, // Heiken ashi (better formula) typical
   pr_habweighted,// Heiken ashi (better formula) weighted
   pr_habaverage, // Heiken ashi (better formula) average
   pr_habmedianb, // Heiken ashi (better formula) median body
   pr_habtbiased, // Heiken ashi (better formula) trend biased price
   pr_habtbiased2 // Heiken ashi (better formula) trend biased (extreme) price
};
Function code :

Code: Select all

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#define _priceInstances     1
#define _priceInstancesSize 4
double  _priceWorkHa[][_priceInstances*_priceInstancesSize];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int bars, int instanceNo=0)
{
  if (tprice>=pr_haclose)
   {
      if (ArrayRange(_priceWorkHa,0)!= bars) ArrayResize(_priceWorkHa,bars); instanceNo*=_priceInstancesSize; #ifdef __MQL4__ int r = bars-i-1; #else int r=i; #endif
         
         //
         //
         //
         //
         //
         
         double haOpen  = (r>0) ? (_priceWorkHa[r-1][instanceNo+2] + _priceWorkHa[r-1][instanceNo+3])/2.0 : (open[i]+close[i])/2;;
         double haClose = (open[i]+high[i]+low[i]+close[i]) / 4.0;
         if (tprice>=pr_habclose)
               if (high[i]!=low[i])
                     haClose = (open[i]+close[i])/2.0+(((close[i]-open[i])/(high[i]-low[i]))*fabs((close[i]-open[i])/2.0));
               else  haClose = (open[i]+close[i])/2.0; 
         double haHigh  = fmax(high[i], fmax(haOpen,haClose));
         double haLow   = fmin(low[i] , fmin(haOpen,haClose));

         //
         //
         //
         //
         //
         
         if(haOpen<haClose) { _priceWorkHa[r][instanceNo+0] = haLow;  _priceWorkHa[r][instanceNo+1] = haHigh; } 
         else               { _priceWorkHa[r][instanceNo+0] = haHigh; _priceWorkHa[r][instanceNo+1] = haLow;  } 
                              _priceWorkHa[r][instanceNo+2] = haOpen;
                              _priceWorkHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (tprice)
         {
            case pr_haclose:
            case pr_habclose:    return(haClose);
            case pr_haopen:   
            case pr_habopen:     return(haOpen);
            case pr_hahigh: 
            case pr_habhigh:     return(haHigh);
            case pr_halow:    
            case pr_hablow:      return(haLow);
            case pr_hamedian:
            case pr_habmedian:   return((haHigh+haLow)/2.0);
            case pr_hamedianb:
            case pr_habmedianb:  return((haOpen+haClose)/2.0);
            case pr_hatypical:
            case pr_habtypical:  return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:
            case pr_habweighted: return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:  
            case pr_habaverage:  return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
            case pr_habtbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
            case pr_hatbiased2:
            case pr_habtbiased2:
               if (haClose>haOpen)  return(haHigh);
               if (haClose<haOpen)  return(haLow);
                                    return(haClose);        
         }
   }
   
   //
   //
   //
   //
   //
   
   switch (tprice)
   {
      case pr_close:     return(close[i]);
      case pr_open:      return(open[i]);
      case pr_high:      return(high[i]);
      case pr_low:       return(low[i]);
      case pr_median:    return((high[i]+low[i])/2.0);
      case pr_medianb:   return((open[i]+close[i])/2.0);
      case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
      case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
      case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
      case pr_tbiased:   
               if (close[i]>open[i])
                     return((high[i]+close[i])/2.0);
               else  return((low[i]+close[i])/2.0);        
      case pr_tbiased2:   
               if (close[i]>open[i]) return(high[i]);
               if (close[i]<open[i]) return(low[i]);
                                     return(close[i]);        
   }
   return(0);
}
These users thanked the author mladen for the post:
ChuChu Rocket

Re: MT4 Coding Snippets

10
Waal9 wrote: Mon Nov 20, 2017 10:37 pm Hello
Can anyone give a snippet for indicator expiration time? Also an explanation good enough for a novice programmer. Thanks.
Why? I mean that is usually used for code protection. If you are not familiar with that (the way how it is done, and it is very simple), then what exactly are you going to "protect"?

Why don't you do the usual coders steps : learn to code, make your own code and then protect your own code, instead of "protecting" some code that has nothing in common with your own work?
These users thanked the author mladen for the post:
ChuChu Rocket


Who is online

Users browsing this forum: No registered users and 11 guests