Download MetaTrader 5

Getting normalised price from another timeframe

To add comments, please log in or register
Hyun Hwang
367
Hyun Hwang  

Hi

I normalised the distance between (EURGBP-EURUSD) and (EURUSD-GBPUSD). Below is the code;

Now, I want to create separate variables to store normalised values of other timeframes i.e 15min 60min regardless which timeframe I am currently on so I can comment those on the chart. 

Can someone help on how I could retrieve normalised values from another timeframe please?


//+------------------------------------------------------------------+
//|                                         Normalised Fly Price.mq4 |
//|                                                   Hyun soo Hwang |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Hyun soo Hwang"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window

#property indicator_level1     2
#property indicator_level2     1.5
#property indicator_level3     0
#property indicator_level4     -1.5
#property indicator_level5     -2.0

#property indicator_buffers 2
#property indicator_color1 Black
#property indicator_color2 Red

//--- Extern variables
extern int period = 20;

//--- Buffers
double ExtNormalised[];
double ExtNormalisedMA[];
double ExtFlyBuffer[];
double ExtMABuffer[];
double ExtStdevBuffer[];

double Normalised15;
double Fly15[];
double MA15[];
double Stdev15[];

double ExtNormalised60;
double ExtNormalised240;

//--- variables
int limit, i, x, y;
double total;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(5);
   IndicatorDigits(5);
   IndicatorShortName("Normalised Fly Price");
   
   SetIndexBuffer(0, ExtNormalised);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1);
   SetIndexLabel(0, "Normalised");
   
   SetIndexBuffer(1, ExtNormalisedMA);
   SetIndexStyle(1, DRAW_LINE, STYLE_DOT, 1);
   SetIndexLabel(1, "MA");
   
   SetIndexBuffer(2, ExtFlyBuffer);
   SetIndexBuffer(3, ExtMABuffer);
   SetIndexBuffer(4, ExtStdevBuffer);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {

//--- Find number of bars to fill (aka find Limit)
   //int counted_bars=IndicatorCounted(); // IndicatorCounted returns number of bars not changed since the indicator had been launched last
   if(rates_total<60 || prev_calculated<0) return(0); // To check for errors
   //if(counted_bars>0) counted_bars--; // Minus 1 so as to always recount the last bar. Last bar may be formed incompletely

   limit=(prev_calculated==0)?rates_total-period:rates_total-prev_calculated; // Bars represent number of bars in the chart

//--- Main Calculation 
   for(i=limit; i>=0 && !_StopFlag; i--) {
   
      ///--- Fly price
      ExtFlyBuffer[i] = flyPrice(i,0,time);
      
      //--- Moving average
      ExtMABuffer[i] = MA(i, period, ExtFlyBuffer);
      
      //--- Standard deviation
      ExtStdevBuffer[i] = Stdev(i, ExtFlyBuffer, ExtMABuffer[i], period);

      //--- Normalised fly price
      ExtNormalised[i] = ExtStdevBuffer[i]!=0.0?(ExtFlyBuffer[i]-ExtMABuffer[i])/ExtStdevBuffer[i]:0.0;
  }
      
   limit=(prev_calculated==0)?rates_total-period*2:rates_total-prev_calculated; // Bars represent number of bars in the chart
   
   for(i=limit; i>=0 && !_StopFlag; i--) {
      
      //-- Moving average on Normalised price
      ExtNormalisedMA[i] = MA(i, period, ExtNormalised);
   }

//-- Alerts
   if(ExtNormalised[1]<=-2){
      Alert("Alert Buy"," ",DoubleToString(ExtNormalised[1],5));
      SendNotification("Alert Buy");
   }
   if(ExtNormalised[1]>=2){
      Alert("Alert Sell"," ",DoubleToString(ExtNormalised[1],5));
      SendNotification("Alert Sell");
   }
   
//-- Comment the current (bar[0]) normalised price for 15min, 60min, 4hour on the chart
   limit=(prev_calculated==0)?rates_total-period*4:rates_total-prev_calculated;
   Normalised15 = Normalised(limit, time, 15, period, Fly15, MA15, Stdev15, 2) ;
   
   Comment("\n","15 min: ",DoubleToString(Normalised15,5));
   
//--- return value of prev_calculated for next call
   return(rates_total);
 }
 
//indicator value 5dp
//script or ea for creating a button that executes buy/sell market order
//comment 

//+------------------------------------------------------------------+

//--- Functions

double flyPrice(int k, int time1, const datetime &time[]) {

   int shift1=iBarShift("EURGBP",time1,time[k]);
   int shift2=iBarShift("EURUSD",time1,time[k]);
   int shift3=iBarShift("GBPUSD",time1,time[k]);
   
   double flyPrice = (iClose("EURGBP", time1, shift1)-iClose("EURUSD", time1, shift2))-(iClose("EURUSD", time1, shift2)-iClose("GBPUSD", time1, shift3));
   
   return(flyPrice);

}

double MA(int m, int period1, double &array[]) {
   
   double sum1 = 0;
   double ma1;
   for(x=m; x<m+period1; x++) {
            sum1 = sum1 + array[x];
         }

   ma1 = sum1/period1; // Sum of prices divided number of bars gives us the SMA on closing price 
   return(ma1);        
   
}

double Stdev(int pos, double &price[], double MA, int length) {

   double StdevTemp =0.0;
   
   for (y=pos; y<pos+length; y++) {
      StdevTemp +=MathPow(price[y]-MA,2);    
   }
   StdevTemp = MathSqrt(StdevTemp/length);
   
   return(StdevTemp);
}

double Normalised(int a, const datetime &time[], int timeframe, int ma_stdev_period, double &fly[], double &ma[], double &stdev[], int normalised_shift) {
   
   double normalised [];
   
   for(int z=a; i>=0 && !_StopFlag; z--) {
   
      ///--- Fly price
      fly[z] = flyPrice(i,timeframe,time);
      
      //--- Moving average
      ma[z] = MA(i, period, ExtFlyBuffer);
      
      //--- Standard deviation
      stdev[z] = Stdev(i, ExtFlyBuffer, ExtMABuffer[i], period);

      //--- Normalised fly price
      normalised[z] = ExtStdevBuffer[z]!=0.0?(ExtFlyBuffer[z]-ExtMABuffer[z])/ExtStdevBuffer[z]:0.0;
  }
  return(normalised[normalised_shift]);
   
} 


I tried creating a function,  but I am getting array out of range error. Please correct me if I can use this method or let me know if there is a better way.

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
whroeder1
15726
whroeder1  
Hyun Hwang: I am getting array out of range error.
   double normalised [];
Of course you do, array has no size.
Hyun Hwang
367
Hyun Hwang  
whroeder1:
Of course you do, array has no size.
What size do i need to assign it?
whroeder1
15726
whroeder1  
Hyun Hwang: What size do i need to assign it?
How many values are you storing in it?
Hyun Hwang
367
Hyun Hwang  
whroeder1:
How many values are you storing in it?
Dont need many, just two i guess. One for the bar[0] and one for bar [1]
whroeder1
15726
whroeder1  
Hyun Hwang: Dont need many, just two i guess. One for the bar[0] and one for bar [1]
You guess? Why don''t you know? Look at your code:
   limit=(prev_calculated==0)?rates_total-period*4:rates_total-prev_calculated;
   Normalised15 = Normalised(limit, time, 15, period, Fly15, MA15, Stdev15, 2) ;
:
double Normalised(int a, ...) {
   double normalised [];

   for(int z=a; i>=0 && !_StopFlag; z--) { ...
      normalised[z] = 
  1. You call your function with a=rates_total-period*4
  2. Z begins at a.
  3. You store a value at element z
  4. Therefor you need z+1 elements. Resize your array to a+1.

not two but thousands (slightly less than Bars)

Hyun Hwang
367
Hyun Hwang  
whroeder1:
You guess? Why don''t you know? Look at your code:
  1. You call your function with a=rates_total-period*4
  2. Z begins at a.
  3. You store a value at element z
  4. Therefor you need z+1 elements. Resize your array to a+1.

not two but thousands (slightly less than Bars)

So after the for loop, i need to put 

for {

}
ArrayResize(normalised, a+1,0)

return normalised[normalised_shift]

Is that correct?


Hyun Hwang
367
Hyun Hwang  
whroeder1:
You guess? Why don''t you know? Look at your code:
  1. You call your function with a=rates_total-period*4
  2. Z begins at a.
  3. You store a value at element z
  4. Therefor you need z+1 elements. Resize your array to a+1.

not two but thousands (slightly less than Bars)

Actually, without having to resize the array, can't i just lookback only the required amount of bars to compute two normalised values? This will depend on what period i use for ma and stdev so,

normalise[]=2;

And use period+1 when calling normalise function instead of using 'limit'

Is that correct?
whroeder1
15726
whroeder1  
Hyun Hwang: So after the for loop ... Is that correct?
Hyun Hwang: " required amount of bars to compute two normalised values" Is that correct?
No and no. learn to code it, or pay (Freelance) someone to code it.
To add comments, please log in or register