//+------------------------------------------------------------------+ //| Demo_iFractals.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #propertycopyright'Copyright 2011, MetaQuotes Software Corp.' #propertylink'https://www.mql5.com' #propertyversion'1.00' #propertydescription'The indicator demonstrates how to obtain data' #propertydescription'of indicator buffers for the iFractals technical indicator.' #propertydescription'A symbol and timeframe used for calculation of the indicator,' #propertydescription'are set by the symbol and period parameters.' #propertydescription'The method of creation of the handle is set through the 'type' parameter (function type).' #propertyindicator_chart_window #propertyindicator_buffers 1 #propertyindicator_plots 1 #propertyindicator_chart_window #propertyindicator_buffers 2 #propertyindicator_plots 2 //--- the FractalUp plot #property indicator_label1 'FractalUp' #property indicator_type1 DRAW_ARROW #property indicator_color1 clrBlue //--- the FractalDown plot #property indicator_label2 'FractalDown' #property indicator_type2 DRAW_ARROW #property indicator_color2 clrRed //+------------------------------------------------------------------+ //| Enumeration of the methods of handle creation | //+------------------------------------------------------------------+ enum Creation { Call_iFractals, // use iFractals Call_IndicatorCreate // use IndicatorCreate }; //--- input parameters input Creation type=Call_iFractals; // type of the function inputstringsymbol=' '; // symbol inputENUM_TIMEFRAMESperiod=PERIOD_CURRENT; // timeframe //--- indicator buffers double FractalUpBuffer[]; double FractalDownBuffer[]; //--- variable for storing the handle of the iFractals indicator int handle; //--- variable for storing string name=symbol; //--- name of the indicator on a chart string short_name; //--- we will keep the number of values in the Fractals indicator int bars_calculated=0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ intOnInit() { //--- assignment of arrays to indicator buffers SetIndexBuffer(0,FractalUpBuffer,INDICATOR_DATA); SetIndexBuffer(1,FractalDownBuffer,INDICATOR_DATA); //--- set codes using a symbol from the Wingdings charset for the PLOT_ARROW property PlotIndexSetInteger(0,PLOT_ARROW,217); // arrow up PlotIndexSetInteger(1,PLOT_ARROW,218); // arrow down //--- determine the symbol the indicator is drawn for name=symbol; //--- delete spaces to the right and to the left StringTrimRight(name); StringTrimLeft(name); //--- if it results in zero length of the 'name' string if(StringLen(name)0) { //--- take the symbol of the chart the indicator is attached to name=_Symbol; } //--- create handle of the indicator if(typeCall_iFractals) handle=iFractals(name,period); else handle=IndicatorCreate(name,period,IND_FRACTALS); //--- if the handle is not created if(handleINVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat('Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d', name, EnumToString(period), GetLastError()); //--- the indicator is stopped early return(INIT_FAILED); } //--- show the symbol/timeframe the Fractals indicator is calculated for short_name=StringFormat('iFractals(%s/%s)',name,EnumToString(period)); IndicatorSetString(INDICATOR_SHORTNAME,short_name); //--- normal initialization of the indicator return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ intOnCalculate(constint rates_total, constint prev_calculated, constdatetime &time[], constdouble &open[], constdouble &high[], constdouble &low[], constdouble &close[], constlong &tick_volume[], constlong &volume[], constint &spread[]) { //--- number of values copied from the iFractals indicator int values_to_copy; //--- determine the number of values calculated in the indicator int calculated=BarsCalculated(handle); if(calculated<=0) { PrintFormat('BarsCalculated() returned %d, error code %d',calculated,GetLastError()); return(0); } //--- if it is the first start of calculation of the indicator or if the number of values in the iFractals indicator changed //---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history) if(prev_calculated0 || calculated!=bars_calculated || rates_total>prev_calculated+1) { //--- if the FractalUpBuffer array is greater than the number of values in the iFractals indicator for symbol/period, then we don't copy everything //--- otherwise, we copy less than the size of indicator buffers if(calculated>rates_total) values_to_copy=rates_total; else values_to_copy=calculated; } else { //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate() //--- for calculation not more than one bar is added values_to_copy=(rates_total-prev_calculated)+1; } //--- fill the FractalUpBuffer and FractalDownBuffer arrays with values from the Fractals indicator //--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation if(!FillArraysFromBuffers(FractalUpBuffer,FractalDownBuffer,handle,values_to_copy)) return(0); //--- form the message string comm=StringFormat('%s > Updated value in the indicator %s: %d', TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), short_name, values_to_copy); //--- display the service message on the chart Comment(comm); //--- memorize the number of values in the Fractals indicator bars_calculated=calculated; //--- return the prev_calculated value for the next call return(rates_total); } //+------------------------------------------------------------------+ //| Filling indicator buffers from the iFractals indicator | //+------------------------------------------------------------------+ bool FillArraysFromBuffers(double &up_arrows[], // indicator buffer for up arrows double &down_arrows[], // indicator buffer for down arrows int ind_handle, // handle of the iFractals indicator int amount // number of copied values ) { //--- reset error code ResetLastError(); //--- fill a part of the FractalUpBuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(ind_handle,0,0,amount,up_arrows)<0) { //--- if the copying fails, tell the error code PrintFormat('Failed to copy data from the iFractals indicator to the FractalUpBuffer array, error code %d', GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(false); } //--- fill a part of the FractalDownBuffer array with values from the indicator buffer that has index 1 if(CopyBuffer(ind_handle,1,0,amount,down_arrows)<0) { //--- if the copying fails, tell the error code PrintFormat('Failed to copy data from the iFractals indicator to the FractalDownBuffer array, error code %d', GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(false); } //--- everything is fine return(true); } //+------------------------------------------------------------------+ //| Indicator deinitialization function | //+------------------------------------------------------------------+ voidOnDeinit(constint reason) { if(handle!=INVALID_HANDLE) IndicatorRelease(handle); //--- clear the chart after deleting the indicator Comment('); } |