//+------------------------------------------------------------------+ //| MA_AvgOfFour.mq4 | //| Copyright © 2010, Robert Hill | //| Written for Pete Hadleybased on post on Yahoo MT E & I | //| Takes 4 different moving averages, takes the average | //| and plots the resulting MA from the average | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, Robert Hill" #property link "None" //---- indicator settings #property indicator_chart_window #property indicator_buffers 5 #property indicator_color1 Yellow #property indicator_color2 Orange #property indicator_color3 Red #property indicator_color4 Aqua #property indicator_color5 Blue #property indicator_width1 2 #property indicator_width2 1 #property indicator_width3 1 #property indicator_width4 1 #property indicator_width5 1 extern bool DisplayMAs = true; extern int MAPeriodHigh1 = 14; extern int MAPeriodHigh2 = 25; extern int MAPeriodLow1 = 18; extern int MAPeriodLow2 = 34; extern int MAPeriod=14; extern string m = "--Moving Average Types--"; extern string m0 = " 0 = SMA"; extern string m1 = " 1 = EMA"; extern string m2 = " 2 = SMMA"; extern string m3 = " 3 = LWMA"; extern int MATypeHigh1=0; extern int MATypeHigh2=0; extern int MATypeLow1=0; extern int MATypeLow2=0; extern int MAType=0; //---- buffers double MA_Buffer[]; double MA_High1[]; double MA_High2[]; double MA_Low1[]; double MA_Low2[]; double MA_Avg[]; //---- variables int MAModeH1; int MAModeH2; int MAModeL1; int MAModeL2; int MAMode; string strMATypeH1; string strMATypeH2; string strMATypeL1; string strMATypeL2; string strMAType; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { IndicatorBuffers(6); //---- drawing settings SetIndexBuffer(0,MA_Buffer); SetIndexBuffer(1,MA_High1); SetIndexBuffer(2,MA_High2); SetIndexBuffer(3,MA_Low1); SetIndexBuffer(4,MA_Low2); SetIndexBuffer(5, MA_Avg); SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2); SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1); SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1); SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1); SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,1); switch (MATypeHigh1) { case 1: strMATypeH1="EMA"; MAModeH1=MODE_EMA; break; case 2: strMATypeH1="SMMA"; MAModeH1=MODE_SMMA; break; case 3: strMATypeH1="LWMA"; MAModeH1=MODE_LWMA; break; default: strMATypeH1="SMA"; MAModeH1=MODE_SMA; break; } switch (MATypeHigh2) { case 1: strMATypeH2="EMA"; MAModeH2=MODE_EMA; break; case 2: strMATypeH2="SMMA"; MAModeH2=MODE_SMMA; break; case 3: strMATypeH2="LWMA"; MAModeH2=MODE_LWMA; break; default: strMATypeH2="SMA"; MAModeH2=MODE_SMA; break; } switch (MATypeLow1) { case 1: strMATypeL1="EMA"; MAModeL1=MODE_EMA; break; case 2: strMATypeL1="SMMA"; MAModeL1=MODE_SMMA; break; case 3: strMATypeL1="LWMA"; MAModeL1=MODE_LWMA; break; default: strMATypeL1="SMA"; MAModeL1=MODE_SMA; break; } switch (MATypeLow2) { case 1: strMATypeL2="EMA"; MAModeL2=MODE_EMA; break; case 2: strMATypeL2="SMMA"; MAModeL2=MODE_SMMA; break; case 3: strMATypeL2="LWMA"; MAModeL2=MODE_LWMA; break; default: strMATypeL2="SMA"; MAModeL2=MODE_SMA; break; } switch (MAType) { case 1: strMAType="EMA"; MAMode=MODE_EMA; break; case 2: strMAType="SMMA"; MAMode=MODE_SMMA; break; case 3: strMAType="LWMA"; MAMode=MODE_LWMA; break; default: strMAType="SMA"; MAMode=MODE_SMA; break; } IndicatorShortName( strMAType+ " (" +MAPeriod + ") "); SetIndexLabel(0,strMAType+ " (" +MAPeriod + ") "); SetIndexLabel(1,strMATypeH1+ " (" +MAPeriodHigh1 + ") High"); SetIndexLabel(2,strMATypeH2+ " (" +MAPeriodHigh2 + ") High"); SetIndexLabel(3,strMATypeL1+ " (" +MAPeriodLow1 + ") Low"); SetIndexLabel(4,strMATypeL2+ " (" +MAPeriodLow2 + ") Low"); //---- initialization done return(0); } int start() { double MA_H1, MA_H2, MA_L1, MA_L2; int i, limit; int counted_bars = IndicatorCounted(); //---- check for possible errors if (counted_bars<0) return(-1); //---- last counted bar will be recounted if (counted_bars>0) counted_bars--; limit = Bars - counted_bars; for(i=limit; i>=0; i--) { MA_H1 = iMA(NULL,0,MAPeriodHigh1,0,MAModeH1, PRICE_HIGH,i); MA_H2 = iMA(NULL,0,MAPeriodHigh2,0,MAModeH2, PRICE_HIGH,i); MA_L1 = iMA(NULL,0,MAPeriodLow1,0,MAModeL1, PRICE_LOW,i); MA_L2 = iMA(NULL,0,MAPeriodLow2,0,MAModeL2, PRICE_LOW,i); MA_Avg[i] = (MA_H1 + MA_H2 + MA_L1 + MA_L2) / 4.0; if (DisplayMAs) { MA_High1[i] = MA_H1; MA_High2[i] = MA_H2; MA_Low1[i] = MA_L1; MA_Low2[i] = MA_L2; } } for(i=limit; i>=0; i--) { MA_Buffer[i] = iMAOnArray(MA_Avg,0,MAPeriod,0,MAMode, i); } return(0); } //+------------------------------------------------------------------+