Show daily MA on any timeframe?

 

So I'm trying to write this as an indicator first. I want to be able to use the current daily Moving Average as a trade signal but it doesn't seem to be giving me the correct results. Any ideas on what I need to change to get this to work? Thanks.

//+------------------------------------------------------------------+
//|                                                 _MA + IB Ind.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LightBlue
#property indicator_color2 DodgerBlue

extern int iHistory = 1000;                            //Bars to use
extern double MA_Period_1 = 10;                       
extern double MA_Period_2 = 10;
                     
int iPeriod = PERIOD_D1;

double Line_10[], Line_20[];      

                      

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   
  SetIndexBuffer(0,Line_10);                  
  SetIndexBuffer(1,Line_20);

  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  
   DrawMA();                                          //Draw MA function
   ErrorCheck();                                      //Check for errors
                
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
  
   DeleteObjects();                                   //Delete objects on close

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


   void DeleteObjects()
     {
      for (int i=iHistory; i>=0; i--)
         {
          if (ObjectFind("signal" + i) != -1)
            {
             ObjectDelete("signal" + i);
            }     
         }
      }
   
//-------------------------------------------------------------------

   void DrawMA()
      {
         int i,ind ;
         int limit=Bars;
   
      double MA_1, MA_2;
   
   for(i=limit; i>=0; i--)
         {
          MA_1=iMA(0,0,MA_Period_1,0,MODE_EMA,PRICE_TYPICAL,i);                   //Current TimeFrame MA
            //Print("MA_1 = ",MA_1," @ bar: ",i);
          Line_10[i]=MA_1;
       
          MA_2=iMA(0,iPeriod,MA_Period_2,0,MODE_EMA,PRICE_TYPICAL,i);             //Daily Moving Average
          Line_20[i]=MA_2;


         }
      }

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

   void ErrorCheck()
      {
       int err = GetLastError();
       if(err != 0)
         Alert("Error: ",err);
      }
 

i checked it & it's giving the correct result

 

 
qjol:

i checked it & it's giving the correct result

This is what it looks like on H4 chart on my screen.

I'm pretty sure it has something to do wtih using iBarShift but haven't figured it out yet.

 

Gjol, Thanks for trying to help me figure this one out. I saw the same thing that youre pointing out about the final value being correct. But I'm pretty sure i need to use "shift" in some way to stretch that Daily MA out on the 4hr chart so it looks correct.

 
I think I may have figured it out, I'm at least getting closer. My goal is once I get this working properly is to be able to use the daily MA (10,20,50,100) as a trade signal dependent on their order and slope.
//+------------------------------------------------------------------+
//|                                                 _MA + IB Ind.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 8

#property indicator_color1 Red
#property indicator_color2 LightBlue
#property indicator_color3 Red
#property indicator_color4 CornflowerBlue
#property indicator_color5 Red
#property indicator_color6 Blue
#property indicator_color7 Red
#property indicator_color8 DarkViolet



extern int PeriodMA1      = 10;             //10 day MA
extern int PeriodMA2      = 20;             //20 day MA
extern int PeriodMA3      = 50;             //20 day MA
extern int PeriodMA4      = 100;             //20 day MA

extern int applied_price = 0;
extern int iTimeFrame     = PERIOD_D1;        //Moving Average Timeframe

//---- Buffers
double MA_10[];
double MA_10Smooth[];

double MA_20[];
double MA_20Smooth[];

double MA_50[];
double MA_50Smooth[];

double MA_100[];
double MA_100Smooth[];

int iBarsPerPeriod;


/*
PERIOD_M1   1     1 minute 
PERIOD_M5   5     5 minute 
PERIOD_M15  15    15 minute 
PERIOD_M30  30    30 minute 
PERIOD_H1   60    1 hour 
PERIOD_H4   240   4 hour 
PERIOD_D1   1440  1 day 
PERIOD_W1   10080
PERIOD_MN1  43200
*/

//+------------------------------------------------------------------+
int init()
  {
   if (iTimeFrame<Period()) iTimeFrame=Period();        //if Moving average period is less than current period, set timeframe to current period.
   
   //---- indicator lines
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MA_10);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,MA_10Smooth);
   
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,MA_20);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,MA_20Smooth);
   
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,MA_50);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,MA_50Smooth);
   
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,MA_100);
   SetIndexStyle(7,DRAW_LINE);
   SetIndexBuffer(7,MA_100Smooth);
   
//---- 

   iBarsPerPeriod = iTimeFrame/Period();               //Number of bars within each Timeframe (hours per day) -  smooth average 
   Print("iBarsPerPeriod = ",iBarsPerPeriod);
   
   return(0);
  }
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   
   
   for(int i=0; i<limit; i++)
   {
      MA_10[i]  = iMA(NULL,iTimeFrame,PeriodMA1,0,MODE_EMA,PRICE_TYPICAL,iBarShift(NULL,iTimeFrame,Time[i],false));
      MA_20[i]  = iMA(NULL,iTimeFrame,PeriodMA2,0,MODE_EMA,PRICE_TYPICAL,iBarShift(NULL,iTimeFrame,Time[i],false));
      MA_50[i]  = iMA(NULL,iTimeFrame,PeriodMA3,0,MODE_EMA,PRICE_TYPICAL,iBarShift(NULL,iTimeFrame,Time[i],false));
      MA_100[i]  = iMA(NULL,iTimeFrame,PeriodMA4,0,MODE_EMA,PRICE_TYPICAL,iBarShift(NULL,iTimeFrame,Time[i],false));
   }


   for(i=0; i<limit; i++)
   {
      MA_10Smooth[i]  = iMAOnArray(MA_10,0,iBarsPerPeriod,0,MODE_EMA,i);
      MA_20Smooth[i]  = iMAOnArray(MA_20,0,iBarsPerPeriod,0,MODE_EMA,i);
      MA_50Smooth[i]  = iMAOnArray(MA_50,0,iBarsPerPeriod,0,MODE_EMA,i);
      MA_100Smooth[i]  = iMAOnArray(MA_100,0,iBarsPerPeriod,0,MODE_EMA,i);       
      
   }
   return(0);
}
//+------------------------------------------------------------------+


Reason: