//+------------------------------------------------------------------+ //| Demo_iIchimoku.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2000-2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property description "The indicator demonstrates how to obtain data" #property description "of indicator buffers for the iIchimoku technical indicator." #property description "A symbol and timeframe used for calculation of the indicator," #property description "are set by the symbol and period parameters." #property description "The method of creation of the handle is set through the 'type' parameter (function type)." #property description "All other parameters just like in the standard Ichimoku Kinko Hyo." #property indicator_chart_window #property indicator_buffers 5 #property indicator_plots 4 //--- the Tenkan_sen plot #property indicator_label1 "Tenkan_sen" #property indicator_type1 DRAW_LINE #property indicator_color1 clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- the Kijun_sen plot #property indicator_label2 "Kijun_sen" #property indicator_type2 DRAW_LINE #property indicator_color2 clrBlue #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- the Senkou_Span plot #property indicator_label3 "Senkou Span A;Senkou Span B" // two fields will be shown in Data Window #property indicator_type3 DRAW_FILLING #property indicator_color3 clrSandyBrown, clrThistle #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- the Chikou_Span plot #property indicator_label4 "Chinkou_Span" #property indicator_type4 DRAW_LINE #property indicator_color4 clrLime #property indicator_style4 STYLE_SOLID #property indicator_width4 1 //+------------------------------------------------------------------+ //| Enumeration of the methods of handle creation | //+------------------------------------------------------------------+ enum Creation { Call_iIchimoku, // use iIchimoku Call_IndicatorCreate // use IndicatorCreate }; //--- input parameters input Creation type=Call_iIchimoku; // type of the function input int tenkan_sen=9; // period of Tenkan-sen input int kijun_sen=26; // period of Kijun-sen input int senkou_span_b=52; // period of Senkou Span B input string symbol=" "; // symbol input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // timeframe //--- indicator buffer double Tenkan_sen_Buffer[]; double Kijun_sen_Buffer[]; double Senkou_Span_A_Buffer[]; double Senkou_Span_B_Buffer[]; double Chinkou_Span_Buffer[]; //--- variable for storing the handle of the iIchimoku 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 Ichimoku Kinko Hyo indicator int bars_calculated=0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- assignment of arrays to indicator buffers SetIndexBuffer(0,Tenkan_sen_Buffer,INDICATOR_DATA); SetIndexBuffer(1,Kijun_sen_Buffer,INDICATOR_DATA); SetIndexBuffer(2,Senkou_Span_A_Buffer,INDICATOR_DATA); SetIndexBuffer(3,Senkou_Span_B_Buffer,INDICATOR_DATA); SetIndexBuffer(4,Chinkou_Span_Buffer,INDICATOR_DATA); //--- set the shift for the Senkou Span channel of kijun_sen bars in the future direction PlotIndexSetInteger(2,PLOT_SHIFT,kijun_sen); //--- setting a shift for the Chikou Span line is not required, since the Chinkou data Span //--- is already stored with a shift in iIchimoku //--- 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(type==Call_iIchimoku) handle=iIchimoku(name,period,tenkan_sen,kijun_sen,senkou_span_b); else { //--- fill the structure with parameters of the indicator MqlParam pars[3]; //--- periods and shifts of the Alligator lines pars[0].type=TYPE_INT; pars[0].integer_value=tenkan_sen; pars[1].type=TYPE_INT; pars[1].integer_value=kijun_sen; pars[2].type=TYPE_INT; pars[2].integer_value=senkou_span_b; //--- create handle handle=IndicatorCreate(name,period,IND_ICHIMOKU,3,pars); } //--- if the handle is not created if(handle==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iIchimoku 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 Ichimoku Kinko Hyo indicator is calculated for short_name=StringFormat("iIchimoku(%s/%s, %d, %d ,%d)",name,EnumToString(period), tenkan_sen,kijun_sen,senkou_span_b); IndicatorSetString(INDICATOR_SHORTNAME,short_name); //--- normal initialization of the indicator 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[]) { //--- number of values copied from the iIchimoku 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 iIchimoku 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_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) { //--- if the Tenkan_sen_Buffer array is greater than the number of values in the iIchimoku 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 arrays with values of the Ichimoku Kinko Hyo indicator //--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operation if(!FillArraysFromBuffers(Tenkan_sen_Buffer,Kijun_sen_Buffer,Senkou_Span_A_Buffer,Senkou_Span_B_Buffer,Chinkou_Span_Buffer, kijun_sen,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 Ichimoku Kinko Hyo indicator bars_calculated=calculated; //--- return the prev_calculated value for the next call return(rates_total); } //+------------------------------------------------------------------+ //| Filling indicator buffers from the iIchimoku indicator | //+------------------------------------------------------------------+ bool FillArraysFromBuffers(double &tenkan_sen_buffer[], // indicator buffer of the Tenkan-sen line double &kijun_sen_buffer[], // indicator buffer of the Kijun_sen line double &senkou_span_A_buffer[], // indicator buffer of the Senkou Span A line double &senkou_span_B_buffer[], // indicator buffer of the Senkou Span B line double &chinkou_span_buffer[], // indicator buffer of the Chinkou Span line int senkou_span_shift, // shift of the Senkou Span lines in the future direction int ind_handle, // handle of the iIchimoku indicator int amount // number of copied values ) { //--- reset error code ResetLastError(); //--- fill a part of the Tenkan_sen_Buffer array with values from the indicator buffer that has 0 index if(CopyBuffer(ind_handle,0,0,amount,tenkan_sen_buffer)<0) { //--- if the copying fails, tell the error code PrintFormat("1.Failed to copy data from the iIchimoku indicator, 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 Kijun_sen_Buffer array with values from the indicator buffer that has index 1 if(CopyBuffer(ind_handle,1,0,amount,kijun_sen_buffer)<0) { //--- if the copying fails, tell the error code PrintFormat("2.Failed to copy data from the iIchimoku indicator, 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 Chinkou_Span_Buffer array with values from the indicator buffer that has index 2 //--- if senkou_span_shift>0, the line is shifted in the future direction by senkou_span_shift bars if(CopyBuffer(ind_handle,2,-senkou_span_shift,amount,senkou_span_A_buffer)<0) { //--- if the copying fails, tell the error code PrintFormat("3.Failed to copy data from the iIchimoku indicator, 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 Senkou_Span_A_Buffer array with values from the indicator buffer that has index 3 //--- if senkou_span_shift>0, the line is shifted in the future direction by senkou_span_shift bars if(CopyBuffer(ind_handle,3,-senkou_span_shift,amount,senkou_span_B_buffer)<0) { //--- if the copying fails, tell the error code PrintFormat("4.Failed to copy data from the iIchimoku indicator, 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 Senkou_Span_B_Buffer array with values from the indicator buffer that has 0 index //--- when copying Chinkou Span, we don't need to consider the shift, since the Chinkou Span data //--- is already stored with a shift in iIchimoku if(CopyBuffer(ind_handle,4,0,amount,chinkou_span_buffer)<0) { //--- if the copying fails, tell the error code PrintFormat("5.Failed to copy data from the iIchimoku indicator, 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 | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(handle!=INVALID_HANDLE) IndicatorRelease(handle); //--- clear the chart after deleting the indicator Comment(""); }