//+------------------------------------------------------------------+ //| WhittakerHourlyATR.mq4 | //| Copyright © 2010, Jeremy R. Whittaker | //| http://www.JeremyWhittaker.com/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, Jeremy R. Whittaker" #property link "http://www.JeremyWhittaker.com/" #property indicator_separate_window #property indicator_buffers 5 #property indicator_color1 Red #property indicator_color2 DodgerBlue #property indicator_color3 Orange #property indicator_color4 Green #property indicator_color5 White //---- input parameters //atr period for drawing line recommend 1. daysLookback is the amount of days to average that specific hour extern int AtrPeriod=1; extern int daysLookback=5; //---- buffers Creating 2 CustomAtrBuffer so that can change color double AtrBuffer[]; double CustomAtrBuffer[]; double CustomAtrBuffer2[]; double CustomAtrBuffer3[]; double TempBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { if(Period()!=PERIOD_H1){ Alert("This indicator only runs on hourly charts"); Print("This indicator only runs on hourly charts"); return(0); } string short_name; string short_name2; //---- 1 additional buffer used for counting. IndicatorBuffers(5); //---- indicator line SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2,Blue); SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2,Red); SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,2,Green); SetIndexStyle(4,DRAW_LINE); SetIndexBuffer(0,CustomAtrBuffer); SetIndexBuffer(1,CustomAtrBuffer2); SetIndexBuffer(3,CustomAtrBuffer3); SetIndexBuffer(4,AtrBuffer); SetIndexBuffer(2,TempBuffer); short_name="ATR("+AtrPeriod+")"; IndicatorShortName(short_name); SetIndexLabel(3,short_name); short_name2="HourlyATR("+daysLookback+")"; IndicatorShortName(short_name2); SetIndexLabel(0,short_name2); SetIndexLabel(1,short_name2); SetIndexLabel(3,short_name2); //---- SetIndexDrawBegin(0,AtrPeriod); SetIndexDrawBegin(4,daysLookback); SetIndexDrawBegin(3,daysLookback); SetIndexDrawBegin(1,AtrPeriod); //---- return(0); } //+------------------------------------------------------------------+ //| Average True Range | //+------------------------------------------------------------------+ int start() { //decarling an array for every hour of the day these are temp arrays because they will not be indexed correctly due to the empty 23 indexes double Hour0Temp[],Hour1Temp[],Hour2Temp[],Hour3Temp[],Hour4Temp[],Hour5Temp[],Hour6Temp[],Hour7Temp[],Hour8Temp[],Hour9Temp[],Hour10Temp[],Hour11Temp[],Hour12Temp[],Hour13Temp[], Hour14Temp[],Hour15Temp[],Hour16Temp[],Hour17Temp[],Hour18Temp[],Hour19Temp[],Hour20Temp[],Hour21Temp[],Hour22Temp[],Hour23Temp[]; //counted_bars=amount of bars not changed since last launch of indicator int i,k,l,counted_bars=IndicatorCounted(); //---- //not enough bars to calculate atr then exit if(Bars<=AtrPeriod) return(0); //---- initial zero // if(counted_bars<1) // for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0; //---- //i=total bars - bars not changed - 1 //This sets i typically to 2 so that you are not recalculating all bars //creating 2 loops to calculate the specific hour range. //First we must calculate the temp arrays then we will create another array that does not contain blank indexes i=Bars-counted_bars-1; k=Bars-counted_bars-1; //resizing all the hourly arrays to the number of bars on the screen ArrayResize(Hour0Temp,i); ArrayResize(Hour1Temp,i); ArrayResize(Hour2Temp,i); ArrayResize(Hour3Temp,i); ArrayResize(Hour4Temp,i); ArrayResize(Hour5Temp,i); ArrayResize(Hour6Temp,i); ArrayResize(Hour7Temp,i); ArrayResize(Hour8Temp,i); ArrayResize(Hour9Temp,i); ArrayResize(Hour10Temp,i); ArrayResize(Hour11Temp,i); ArrayResize(Hour12Temp,i); ArrayResize(Hour13Temp,i); ArrayResize(Hour14Temp,i); ArrayResize(Hour15Temp,i); ArrayResize(Hour16Temp,i); ArrayResize(Hour17Temp,i); ArrayResize(Hour18Temp,i); ArrayResize(Hour19Temp,i); ArrayResize(Hour20Temp,i); ArrayResize(Hour21Temp,i); ArrayResize(Hour22Temp,i); ArrayResize(Hour23Temp,i); //first loop will set the range for all of the bars high-low or the close[1]-low whichever is greater. This accounts for gaps in the market while(i>=0) { double high=High[i]; double low =Low[i]; //if i==Total bars on chart - 1 then first bar is high-low if(i==Bars-1) TempBuffer[i]=high-low; else { //What is the pervious close double prevclose=Close[i+1]; //TempBuffer is basically a 1 day atr //TempBuffer = what is lower the low of this bar or the previous period close price //subtract this number from the high of this bar or the previous close high if it is greater //I'm assuming this is to account for gaps in the market TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose); //This loops goes through all the possible hours in the day. When it matches the current bar it assigns the TempBuffer value previously determined //to be the range to the specific hour array and breaks the loop int j; for(j=0;j<23;j++){ if(TimeHour(Time[i])==0){Hour0Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==1){Hour1Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==2){Hour2Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==3){Hour3Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==4){Hour4Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==5){Hour5Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==6){Hour6Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==7){Hour7Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==8){Hour8Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==9){Hour9Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==10){Hour10Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==11){Hour11Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==12){Hour12Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==13){Hour13Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==14){Hour14Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==15){Hour15Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==16){Hour16Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==17){Hour17Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==18){Hour18Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==19){Hour19Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==20){Hour20Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==21){Hour21Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==22){Hour22Temp[i]=TempBuffer[i];break;} if(TimeHour(Time[i])==23){Hour23Temp[i]=TempBuffer[i];break;} } } i--; } //---- //if the bars unchanged since indicator ran last > 0 then subtract 1 so that you recalculate the last 2 bars instead of just last one if(counted_bars>0) counted_bars--; //limit=total bars - bars unchanged since indicator ran last int limit=Bars-counted_bars; double HourlyATR[]; ArrayResize(HourlyATR,Bars); for(i=0; i0){sum[i]=sum[i]+Hour0Temp[i+j];}}} if(TimeHour(Time[i])==1){for(j=0;j0){sum[i]=sum[i]+Hour1Temp[i+j];}}} if(TimeHour(Time[i])==2){for(j=0;j0){sum[i]=sum[i]+Hour2Temp[i+j];}}} if(TimeHour(Time[i])==3){for(j=0;j0){sum[i]=sum[i]+Hour3Temp[i+j];}}} if(TimeHour(Time[i])==4){for(j=0;j0){sum[i]=sum[i]+Hour4Temp[i+j];}}} if(TimeHour(Time[i])==5){for(j=0;j0){sum[i]=sum[i]+Hour5Temp[i+j];}}} if(TimeHour(Time[i])==6){for(j=0;j0){sum[i]=sum[i]+Hour6Temp[i+j];}}} if(TimeHour(Time[i])==7){for(j=0;j0){sum[i]=sum[i]+Hour7Temp[i+j];}}} if(TimeHour(Time[i])==8){for(j=0;j0){sum[i]=sum[i]+Hour8Temp[i+j];}}} if(TimeHour(Time[i])==9){for(j=0;j0){sum[i]=sum[i]+Hour9Temp[i+j];}}} if(TimeHour(Time[i])==10){for(j=0;j0){sum[i]=sum[i]+Hour10Temp[i+j];}}} if(TimeHour(Time[i])==11){for(j=0;j0){sum[i]=sum[i]+Hour11Temp[i+j];}}} if(TimeHour(Time[i])==12){for(j=0;j0){sum[i]=sum[i]+Hour12Temp[i+j];}}} if(TimeHour(Time[i])==13){for(j=0;j0){sum[i]=sum[i]+Hour13Temp[i+j];}}} if(TimeHour(Time[i])==14){for(j=0;j0){sum[i]=sum[i]+Hour14Temp[i+j];}}} if(TimeHour(Time[i])==15){for(j=0;j0){sum[i]=sum[i]+Hour15Temp[i+j];}}} if(TimeHour(Time[i])==16){for(j=0;j0){sum[i]=sum[i]+Hour16Temp[i+j];}}} if(TimeHour(Time[i])==17){for(j=0;j0){sum[i]=sum[i]+Hour17Temp[i+j];}}} if(TimeHour(Time[i])==18){for(j=0;j0){sum[i]=sum[i]+Hour18Temp[i+j];}}} if(TimeHour(Time[i])==19){for(j=0;j0){sum[i]=sum[i]+Hour19Temp[i+j];}}} if(TimeHour(Time[i])==20){for(j=0;j0){sum[i]=sum[i]+Hour20Temp[i+j];}}} if(TimeHour(Time[i])==21){for(j=0;j0){sum[i]=sum[i]+Hour21Temp[i+j];}}} if(TimeHour(Time[i])==22){for(j=0;j0){sum[i]=sum[i]+Hour22Temp[i+j];}}} if(TimeHour(Time[i])==23){for(j=0;j0){sum[i]=sum[i]+Hour23Temp[i+j];}}} //Here we create another array labled HourlyATR this averages the previous X bars HourlyATR[i]=sum[i]/daysLookback; //in this if else statement we are looking for the range of the bar where it is greater than 150% of it's anticipated average //if one is found the bar changes color for visual effects if(AtrBuffer[i]>=HourlyATR[i]*1.5){ CustomAtrBuffer[i]=HourlyATR[i]; CustomAtrBuffer2[i]=EMPTY_VALUE; CustomAtrBuffer3[i]=EMPTY_VALUE; } if(AtrBuffer[i]<=HourlyATR[i]*0.5){ CustomAtrBuffer[i]=EMPTY_VALUE; CustomAtrBuffer2[i]=HourlyATR[i]; CustomAtrBuffer3[i]=EMPTY_VALUE; } if(AtrBuffer[i]HourlyATR[i]*0.5){ CustomAtrBuffer[i]=EMPTY_VALUE; CustomAtrBuffer2[i]=EMPTY_VALUE; CustomAtrBuffer3[i]=HourlyATR[i]; } } return(0); } //+------------------------------------------------------------------+