iCustom on differents Period

 
Hi everyone,

I am using iCustom into indicator to retrieve values, and use them as signals emitter into my EA (better visual than put all code in Expert).
I am using M1 Period Chart, and I want to obtain other period values ( Day, Week, Month, ...).

So here, I try to retrieve "Daily MMA20" on a "M1 Period Chart" in my custom indicator. (for convenience, I use basic iMA indicator in my example instead of iCustom into my indicator)

My problem : I tried the code below, and that doesn't work,
1) ExtMaDayBuffer values change every minute
2) I don't undertstand ExtMaDayBuffer values (seems to be virulent result )


Code below,

ExtMaBuffer     => Normal MMA Chart

ExtMaDayBuffer  => My try of DAILY MMA

 

//+------------------------------------------------------------------+
//|                        Moving Average ChartPeriod, DayPeriod.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property version   "1.00"
#property strict

#property indicator_chart_window
#property indicator_buffers 2

double ExtMaBuffer[];               // Chart Moving Average
double ExtMaDayBuffer[];            // Day Moving Average

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|

void OnInit(void)
{
   IndicatorShortName("Moving Average ChartPeriod, DayPeriod");
   IndicatorDigits(4);
  
   // Lines
   SetIndexStyle(0,DRAW_LINE,0,4,clrYellow);
   SetIndexBuffer(0,ExtMaBuffer);
   SetIndexStyle(1,DRAW_LINE,0,4,clrBlue);
   SetIndexBuffer(1,ExtMaDayBuffer);

   // Labels
   SetIndexLabel(0,"Moving Average Chart");
   SetIndexLabel(1,"Moving Average Day");
      
   // DrawBegin
   SetIndexDrawBegin(0,10);
   SetIndexDrawBegin(1,10);
  
   // Buffers
   SetIndexBuffer(0,ExtMaBuffer);
   SetIndexBuffer(1,ExtMaDayBuffer);

}

//+------------------------------------------------------------------+
//| Main                                                             |
//+------------------------------------------------------------------+
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 pos, i;

   if( rates_total <= 10 )
   {
      return(0);
   }    
      
   // ArraySetAsSeries
   ArraySetAsSeries(ExtMaBuffer,false);
   ArraySetAsSeries(ExtMaDayBuffer,false);
  
   //--- preliminary calculation
   if( prev_calculated > 1 )
   {
      pos = prev_calculated - 1;
   }
   else
   {
      // --- set first candle

      ExtMaBuffer[0]       = EMPTY_VALUE;
      ExtMaDayBuffer[0]    = EMPTY_VALUE;
        
      pos=1;
   }
      
   // Main loop of calculations
   for(i=pos; i<rates_total; i++)
   {
      ExtMaBuffer[i]     = iMA(NULL,0, 8, 0, MODE_SMA, PRICE_CLOSE, (rates_total-i-1));  
      ExtMaDayBuffer[i]  = iMA(NULL,PERIOD_D1, 20, 0, MODE_SMA, PRICE_CLOSE, (rates_total-i-1));  
   }

   return(rates_total);
}
//+------------------------------------------------------------------+

 

I tried some search on websites/forum, but I did not found this kind of situation.

Thanks for help !

 

 

 

 

 

It's necessary to use iBarShift() for the higher time frame.

   ArraySetAsSeries(time,false);
   ArraySetAsSeries(ExtMaBuffer,false);
   ArraySetAsSeries(ExtMaDayBuffer,false);
  
   //--- preliminary calculation
   if( prev_calculated > 1 )
   {
      pos = prev_calculated - 1;
   }
   else
   {
      // --- set first candle

      ExtMaBuffer[0]       = EMPTY_VALUE;
      ExtMaDayBuffer[0]    = EMPTY_VALUE;
        
      pos=1;
   }
      
   int shift;
   // Main loop of calculations
   for(i=pos; i<rates_total; i++)
   {
      ExtMaBuffer[i]     = iMA(NULL,0, 8, 0, MODE_SMA, PRICE_CLOSE, (rates_total-i-1));  
      shift = iBarShift(NULL,PERIOD_D1,time[i]);
      ExtMaDayBuffer[i]  = iMA(NULL,PERIOD_D1, 20, 0, MODE_SMA, PRICE_CLOSE, shift);  
   }
 
  1. You must use iBarShift. Don't mix apples and oranges.
  2. If you count down and use series buffers you wouldn't have to use rates_total-1 - i just i. But either way, you still have to Do your lookbacks correctly.
 

First of all, great thanks for your answers. My "Daily MMA20" on a "M1 Period Chart" works !

 

whroeder1:
  1. You must use iBarShift. Don't mix apples and oranges.
  2. If you count down and use series buffers you wouldn't have to use rates_total-1 - i just i. But either way, you still have to Do your lookbacks correctly.

 @whroeder1, I really appreciate your point 2. When I studied iCustom 3 months ago, each custom indicator I saw had different  OnCalculate body. Really, there is plenty of examples, from ~2007 to today, while/for(;;i++)/for(;;i--), even mql4 examples are differents each other. So, I decided to take recent one, to understand it, test it, and use it. It's kind of example you see in my first post.

The post (lookbacks correctly) refer also some methods, can you advise me one ? One that will be considered as good practice. Can you modify my first code to show it ? 

Range not behaving as expected
Range not behaving as expected
  • www.mql5.com
Goodmorning all...