//+------------------------------------------------------------------+ //| Example iBands values on a chart.mq5 | //| Copyright © 2020, Vladimir Karputov | //+------------------------------------------------------------------+ #property copyright "Copyright © 2020, Vladimir Karputov" #property version "1.00" //--- input parameters input int Inp_Bands_bands_period = 20; // Bands: period for average line calculation input int Inp_Bands_bands_shift = 0; // Bands: horizontal shift of the indicator input double Inp_Bands_deviation = 2.0; // Bands: number of standard deviations input ENUM_APPLIED_PRICE Inp_Bands_applied_price = PRICE_CLOSE; // Bands: type of price //--- int handle_iBands; // variable for storing the handle of the iBands indicator //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create handle of the indicator iBands handle_iBands=iBands(Symbol(),Period(),Inp_Bands_bands_period, Inp_Bands_bands_shift,Inp_Bands_deviation,Inp_Bands_applied_price); //--- if the handle is not created if(handle_iBands==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iBands indicator for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early return(INIT_FAILED); } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- string text=""; double upper[],lower[],middle[]; ArraySetAsSeries(upper,true); ArraySetAsSeries(lower,true); ArraySetAsSeries(middle,true); int start_pos=0,count=3; if(!iGetArray(handle_iBands,UPPER_BAND,start_pos,count,upper) || !iGetArray(handle_iBands,LOWER_BAND,start_pos,count,lower) || !iGetArray(handle_iBands,BASE_LINE,start_pos,count,middle)) { return; } string text_upper="",text_lower="",text_middle=""; for(int i=count-1; i>=0; i--) { text_upper = text_upper + "Upper" + "["+IntegerToString(i) + "]" +" " + DoubleToString(upper[i],Digits()+1) +" | "; text_lower = text_lower + "Lower" + "["+IntegerToString(i) + "]" +" " + DoubleToString(lower[i],Digits()+1) +" | "; text_middle = text_middle + "Middle"+ "["+IntegerToString(i) + "]" +" " + DoubleToString(middle[i],Digits()+1) +" | "; } Comment(text_upper+"\n"+text_lower+"\n"+text_middle); } //+------------------------------------------------------------------+ //| Get value of buffers | //+------------------------------------------------------------------+ bool iGetArray(const int handle,const int buffer,const int start_pos, const int count,double &arr_buffer[]) { bool result=true; if(!ArrayIsDynamic(arr_buffer)) { PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__); return(false); } ArrayFree(arr_buffer); //--- reset error code ResetLastError(); //--- fill a part of the iBands array with values from the indicator buffer int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer); if(copied!=count) { //--- if the copying fails, tell the error code PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d", __FILE__,__FUNCTION__,count,copied,GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(false); } return(result); } //+------------------------------------------------------------------+