//+------------------------------------------------------------------+ //| DRAW_FILLING.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2011, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property description "An indicator to demonstrate DRAW_FILLING" #property description "It draws a channel between two MAs in a separate window" #property description "The fill color is changed randomly" #property description "after every N ticks" #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 1 //--- plot Intersection #property indicator_label1 "Intersection" #property indicator_type1 DRAW_FILLING #property indicator_color1 clrRed,clrBlue #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- input parameters input int Fast=13; // The period of a fast MA input int Slow=21; // The period of a slow MA input int shift=1; // A shift of MAs towards the future (positive) input int N=5; // Number of ticks to change //--- indicator buffers double IntersectionBuffer1[]; double IntersectionBuffer2[]; int fast_handle; int slow_handle; //--- an array to store colors color colors[]={clrRed,clrBlue,clrGreen,clrAquamarine,clrBlanchedAlmond,clrBrown,clrCoral,clrDarkSlateGray}; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,IntersectionBuffer1,INDICATOR_DATA); SetIndexBuffer(1,IntersectionBuffer2,INDICATOR_DATA); //--- PlotIndexSetInteger(0,PLOT_SHIFT,shift); //--- fast_handle=iMA(_Symbol,_Period,Fast,0,MODE_SMA,PRICE_CLOSE); slow_handle=iMA(_Symbol,_Period,Slow,0,MODE_SMA,PRICE_CLOSE); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { static int ticks=0; //--- Calculate ticks to change the style, color and width of the line ticks++; //--- If a sufficient number of ticks has been accumulated if(ticks>=N) { //--- change the line properties ChangeLineAppearance(); //--- Reset the counter of ticks to zero ticks=0; } //--- Make the first calculation of the indicator, or data has changed and requires a complete recalculation if(prev_calculated==0) { //--- Copy all the values of the indicators to the appropriate buffers int copied1=CopyBuffer(fast_handle,0,0,rates_total,IntersectionBuffer1); int copied2=CopyBuffer(slow_handle,0,0,rates_total,IntersectionBuffer2); } else // Fill only those data that are updated { //--- Get the difference in bars between the current and previous start of OnCalculate() int to_copy=rates_total-prev_calculated; //--- If there is no difference, we still copy one value - on the zero bar if(to_copy==0) to_copy=1; //--- copy to_copy values to the very end of indicator buffers int copied1=CopyBuffer(fast_handle,0,0,to_copy,IntersectionBuffer1); int copied2=CopyBuffer(slow_handle,0,0,to_copy,IntersectionBuffer2); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| Change the colors of the channel filling | //+------------------------------------------------------------------+ void ChangeLineAppearance() { //--- A string for the formation of information about the line properties string comm=""; //--- A block for changing the color of the line int number=MathRand(); // Get a random number //--- The divisor is equal to the size of the colors[] array int size=ArraySize(colors); //--- Get the index to select a new color as the remainder of integer division int color_index1=number%size; //--- Set the first color as the PLOT_LINE_COLOR property PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,colors[color_index1]); //--- Write the first color comm=comm+"\r\nColor1 "+(string)colors[color_index1]; //--- Get the index to select a new color as the remainder of integer division number=MathRand(); // Get a random number int color_index2=number%size; //--- Set the second color as the PLOT_LINE_COLOR property PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,colors[color_index2]); //--- Write the second color comm=comm+"\r\nColor2 "+(string)colors[color_index2]; //--- Show the information on the chart using a comment Comment(comm); } //+------------------------------------------------------------------+