EA using indicator, applied to second indicator, applied to first indicator

 

Hello All,

I am still hoping someone here has much knowledge of MQL4 and can please help me program this idea.

If you place for example an MACD oscillator on an MT4 chart, you can then apply another indicator (example: Moving Average- purple) to the MACD. You can then apply another indicator (example: CCI - yellow) to the Moving Average of the MACD.

I know you must use arrays etc. to program this in an EA, but I have'nt been able to do this successfully.

Maybe someone could please help me.

Please see the attached picture of what I'm trying to do.

I would like to use the final indicator (the CCI indicator in the example above) to open buy/sell positions in an EA

Thanks for your time,

Andre


EA_MACD_MA_CCI

 
Powerpack2008 wrote >>

Hello All,

I am still hoping someone here has much knowledge of MQL4 and can please help me program this idea.

If you place for example an MACD oscillator on an MT4 chart, you can then apply another indicator (example: Moving Average- purple) to the MACD. You can then apply another indicator (example: CCI - yellow) to the Moving Average of the MACD.

I know you must use arrays etc. to program this in an EA, but I have'nt been able to do this successfully.

Maybe someone could please help me.

Please see the attached picture of what I'm trying to do.

I would like to use the final indicator (the CCI indicator in the example above) to open buy/sell positions in an EA

Thanks for your time,

Andre


EA_MACD_MA_CCI

//+------------------------------------------------------------------+
//|                                                     Momentum.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Parmenides contact: his1220@gmail.com"


#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_color3 Yellow
#property indicator_color4 Green
//---- input parameters
extern int MACD_FastPeriod=12;
extern int MACD_SlowPeriod=26;
extern int MACD_SignalPeriod=9;
// scaling
extern int Scaling = 8000; // fine for Euro USD  // 1 for Ger30
//---- buffers
double MACDBuffer[];
double MACDSignalBuffer[];
double SMABuffer[];

//
//---- indicator parameters for MA
extern int SMA_Period=20;
extern int SMA_Shift=0;
//---- indiactor parameters for CCI
extern int CCI_Period=2;
extern int CCI_Shift=0;
//---- indicator buffers
double ExtMapBufferSMA[];
double ExtMapBufferCCI[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- indicator short name
   short_name="MACD ("+MACD_SlowPeriod+"-"+MACD_FastPeriod+"-"+MACD_SignalPeriod+") - SMA ("+SMA_Period+") - CCI("+CCI_Period+")";
   IndicatorShortName(short_name);
//---- Mom indicator line
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,MACDBuffer);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,MACD_SignalPeriod);
//----

//---- drawing settings
   SetIndexStyle(1,DRAW_LINE);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- indicator buffers mapping
   SetIndexBuffer(1,MACDSignalBuffer);
//---- initialization done

// drawing setting Indicator SMA
   int    draw_begin;
   if(SMA_Period<2) SMA_Period=13;
   draw_begin=SMA_Period-1;
   SetIndexStyle(2,DRAW_LINE);
   SetIndexShift(2,SMA_Shift);
   SetIndexBuffer(2,ExtMapBufferSMA);
   SetIndexDrawBegin(2,draw_begin);
   
// drawing settings for CCI
   SetIndexStyle(3,DRAW_LINE);
   SetIndexShift(3,CCI_Shift);
   SetIndexBuffer(3,ExtMapBufferCCI);
   SetIndexDrawBegin(3,draw_begin);
   return(0);
  }
//+------------------------------------------------------------------+
//| MACD                                                       |
//+------------------------------------------------------------------+
int start()
  {
//----
   bool error=false;
   if (macd() == -1) error = true;
   if (sma() == -1) error = true;
   if (cci() == -1) error = true;
   if (error)
      Print("Error found");
   return(0);
  }
  

int macd()
{
   int i,counted_bars=IndicatorCounted();
//----
   if(Bars<=MACD_SlowPeriod) return(-1);
//---- initial zero
   if(counted_bars<1)
   {
      for(i=1;i<=MACD_SignalPeriod;i++) MACDSignalBuffer[Bars-i]=0.0;
      for(i=1;i<=MACD_FastPeriod;i++) MACDBuffer[Bars-i]=0.0;
   }
//----
   i=Bars-MACD_SignalPeriod-1;
   if(counted_bars>=MACD_SignalPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      MACDSignalBuffer[i] = iMACD(Symbol(),0,MACD_FastPeriod,MACD_SlowPeriod,MACD_SignalPeriod,0,MODE_SIGNAL,i);
      i--;
     }
   i=Bars-MACD_SlowPeriod-1;
   if(counted_bars>=MACD_SlowPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
      MACDBuffer[i] = iMACD(Symbol(),0,MACD_FastPeriod,MACD_SlowPeriod,MACD_SignalPeriod,0,MODE_MAIN,i);
      i--;
     }
      
return(0);  
}
  
//+------------------------------------------------------------------+
//| CCI
int cci()
{
   if(Bars<=CCI_Period) return(-1);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
  //---
   int i=Bars-ExtCountedBars-1;
//---- initial accumulation
   
   while(i>0)
   {
        ExtMapBufferCCI[i] = iCCIOnArray(ExtMapBufferSMA, ArraySize(ExtMapBufferSMA), CCI_Period,i)/Scaling;
        i--;
   }
   return(0);
}

//+------------------------------------------------------------------+
//| Simple Moving Average                                            |
//+------------------------------------------------------------------+
int  sma()
  {
  
  if(Bars<=SMA_Period) return(-1);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
  //---
   double sum=0;
   int    i,pos=Bars-ExtCountedBars-1;
//---- initial accumulation
   if(pos<SMA_Period) pos=SMA_Period;
   for(i=1;i<SMA_Period;i++,pos--)
      sum+=MACDSignalBuffer[pos];
//---- main calculation loop
   while(pos>=0)
     {
      sum+=MACDSignalBuffer[pos];
      ExtMapBufferSMA[pos]=sum/SMA_Period;
   sum-=MACDSignalBuffer[pos+SMA_Period-1];
   pos--;
     }
//---- zero initial bars
   if(ExtCountedBars<1)
      for(i=1;i<SMA_Period;i++) ExtMapBufferSMA[Bars-i]=0;
   return(0);
  }

Hi!

This should do it.

If you need modifications or help just give me a call

 
Parmenides wrote >>

Hi!

This should do it.

If you need modifications or help just give me a call

Hi there,

Thanks very much for the quick reply, will try it out and let you know either way.

Much appreciated,

Regards,

Andre

 
Powerpack2008 wrote >>

Hi there,

Thanks very much for the quick reply, will try it out and let you know either way.

Much appreciated,

Regards,

Andre

Hi there Parmenides,

I also sent you email, as per below....


Thanks again for your help on the MQL Forum regarding the indicator below.

Just two questions:

- Can I use Weighted MA rather than SMA ?

- How do I use the final signal of this indicator, i.e. the "CCI" signal in an Expert Advisor? How would I BUY if the CCI signal goes from below zero (negative) to above zero (positive)? And also the other way round, how do I SELL when the final CCI signal goes from postive to negative?
.....or How do I call this indicator in and EA and use the CCI signal for BUY and SELL orders

Your help is much appreciated,

Best Regards,
Andre

Reason: