Code snippets - page 10

 

Enums and a function to handle the 22 types of prices

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

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

#define priceInstances 1
double workHa[][priceInstances*4];
double getPrice(int tprice, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
  if (tprice>=pr_haclose)
   {
      if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars); instanceNo*=4;
         int r = Bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen;
         if (r>0)
                haOpen  = (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0;
         else   haOpen  = (open[i]+close[i])/2;
         double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
         double haHigh  = MathMax(high[i], MathMax(haOpen,haClose));
         double haLow   = MathMin(low[i] , MathMin(haOpen,haClose));

         if(haOpen  <haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; } 
         else                 { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  } 
                                workHa[r][instanceNo+2] = haOpen;
                                workHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (tprice)
         {
            case pr_haclose:     return(haClose);
            case pr_haopen:      return(haOpen);
            case pr_hahigh:      return(haHigh);
            case pr_halow:       return(haLow);
            case pr_hamedian:    return((haHigh+haLow)/2.0);
            case pr_hamedianb:   return((haOpen+haClose)/2.0);
            case pr_hatypical:   return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:  return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:   return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
            case pr_hatbiased2:
               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);
}   
 
mladen:

Enums and a function to handle the 22 types of prices

Dearest MLADEN

Pretty thanks for being kind human.

regards

 

As simple as it gets open line display of any chose time frame :

//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  clrDeepSkyBlue
#property indicator_style1  STYLE_DOT
#property strict

extern ENUM_TIMEFRAMES TimeFrame      = PERIOD_D1; // Time frame to use
extern int             TimeZoneOFData = 0;         // TimeZone
double line[];

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

int OnInit() { SetIndexBuffer(0, line); TimeFrame = MathMax(TimeFrame,_Period); return(0); }
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 counted_bars = prev_calculated;
      if(counted_bars < 0) return(-1);
      if(counted_bars > 0) counted_bars--;
           int limit=MathMin(rates_total-counted_bars,rates_total-1);

           for(int i=limit; i>=0; i--) line[i] = (i<Bars-1) ? (iBarShift(NULL,TimeFrame,time[i]+TimeZoneOFData*3600)==iBarShift(NULL,TimeFrame,time[i+1]+TimeZoneOFData*3600)) ? line[i+1] : open[i] : open[i];
   return(rates_total);
}
Files:
Open line.mq4  2 kb
 

Two ways of calculating standard deviation - both are single pass ways of calculating it (unlike the usual calculation mode that is used - which makes these two ways faster in calculation than the usual ways). Both include sample correction as an option (which makes these ways able of doing what should be done with standard deviation in the first place)

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


double workDev[];
double iDeviation(double value, int length, int i, bool isSample=false)
{
   if (ArraySize(workDev)!=Bars) ArrayResize(workDev,Bars);  i = Bars-i-1; workDev[i] = value;
      double sumx=0,sumxx=0; for (int k=0; k<length && (i-k)>=0; sumx+=workDev[i-k],sumxx+=workDev[i-k]*workDev[i-k],k++) {}
   return(MathSqrt((sumxx-sumx*sumx/length)/MathMax(length-isSample,1)));
}

// 
//
//
//
//

double workMDev[];
double iMDeviation(double value, int length, int i, bool isSample=false)
{
   if (ArraySize(workMDev)!=Bars) ArrayResize(workMDev,Bars); i=Bars-i-1; workMDev[i] = value;
                 
   //
   //
   //
   //
   //
   
      double oldMean   = value;
      double newMean   = value;
      double squares   = 0; int k;
      for (k=1; k<length && (i-k)>=0; k++)
      {
         newMean  = (workMDev[i-k]-oldMean)/(k+1)+oldMean;
         squares += (workMDev[i-k]-oldMean)*(workMDev[i-k]-newMean);
         oldMean  = newMean;
      }
      return(MathSqrt(squares/MathMax(k-isSample,1)));
}
 

For those looking for an "old way" of getting prices in metatrader 5, here is this function

double getPrice(int price,int pos)
{
   MqlRates rates[]; int result = CopyRates(_Symbol,_Period,pos,1,rates);
                     if (result!=1) return(0);
   switch (price)
   {
      case PRICE_CLOSE    : return(rates[0].close);
      case PRICE_OPEN     : return(rates[0].open);
      case PRICE_HIGH     : return(rates[0].high);
      case PRICE_LOW      : return(rates[0].low);
      case PRICE_MEDIAN   : return((rates[0].high+rates[0].low)/2.0);
      case PRICE_TYPICAL  : return((rates[0].high+rates[0].low+rates[0].close)/3.0);
      case PRICE_WEIGHTED : return((rates[0].high+rates[0].low+rates[0].close+rates[0].close)/4.0);
   }
   return(0);
}

Using it is simple :

When you had, for example, Close[n], you should use getPrice(PRICE_CLOSE,n), and so on. It works exactly the same as in metatrader 4 (ie: when using 0 for n, it does not return the oldest price - as it would do if used in mt5 way of accessing buffers, but the used to current - newest - price, the way we are used to in metatrader 4)

 
Any more snippets that could help us using mt5?
 
whisperer:
Any more snippets that could help us using mt5?
They will be posted as they come :)
 
Nice thread , how come i never saw this thread before .
 

There is one huge misunderstanding when it comes to what is accessible by using iCostom() calls

A lot of people think that if something is not displayed in the data window, it can not be retrieved using iCustom(). Which is simply not true. Any buffer used by a custom indicator can be accessed using iCustom(). Any. Here is a very simple code that will help you test that - and an example - the buffer displayed is eight buffer (buffer No. 7 in metatrader numbering) and,as it is obvious, even though that value is not displayed anywhere, it is containing values +1 and -1 for each and every color state

#property indicator_separate_window
#property indicator_buffers  3
#property indicator_color1   clrSilver
#property indicator_color2   clrGray
#property indicator_color3   clrDimGray
#property indicator_width1   2
#property indicator_width2   2
#property indicator_width3   2
#property strict


extern int    BufferNumber1 = 0;  // First buffer number (<0 to skip it)
extern int    BufferNumber2 = -1; // Second buffer number (<0 to skip it)
extern int    BufferNumber3 = -1; // Third buffer number (<0 to skip it)
extern string IndicatorName = "Enter indicator name"; // Indicator name to use

double Buffer1[],Buffer2[],Buffer3[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
int init()
{
   SetIndexBuffer(0,Buffer1);
   SetIndexBuffer(1,Buffer2);
   SetIndexBuffer(2,Buffer3);
   IndicatorShortName(IndicatorName+" buffers ("+(string)BufferNumber1+","+(string)BufferNumber2+","+(string)BufferNumber3+")");
   return(0);
}
int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         static datetime lastChecked=0;
         static bool     checked    =false;
                if (!checked&&lastChecked!=Time[0])
                {
                     ResetLastError(); double dummy = iCustom(NULL,0,IndicatorName,0,0); checked = (GetLastError()==0); lastChecked=Time[0];
                }
                if (!checked) { Comment("Problem loading "+IndicatorName+"\nCheck the indicator name"); return(0); }
        
   //
   //
   //
   //
   //
            
   for(int i=limit; i>=0; i--)
   {
      if (BufferNumber1>=0) Buffer1[i] = iCustom(NULL,0,IndicatorName,BufferNumber1,i);
      if (BufferNumber2>=0) Buffer2[i] = iCustom(NULL,0,IndicatorName,BufferNumber2,i);
      if (BufferNumber3>=0) Buffer3[i] = iCustom(NULL,0,IndicatorName,BufferNumber3,i);
   }
   return(0);


Files:
 
mladen:

A time filter that will for any case of time limitation. It is an overloaded function (to call it using just hours, hours and minutes or hours, minutes and seconds). It will not work with old builds of metatrader. Works with new metatrader 4 and metatrader 5. Handles all the time combination cases within a day with no problem

ATTENTION: Video should be reuploaded
_time_filter.mq4

Seeing how easy is to overload a function, look at the way how IndicatorSetInteger(), IndicatorSetDouble() and IndicatorSetString (and all sort of similar cases are made) and try to guess why was it necessary to make 3 different calling ways when it could simply be overloaded

Hi,

 

I tried it in MT5 but fails..

after add the time code , the experts compile like an indicator... I don't know what happened 

Reason: