//+------------------------------------------------------------------+ //| AnchoredMomentumCandle.mq5 | //| Copyright © 2015, Nikolay Kositsin | //| Khabarovsk, farria@mail.redcom.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2015, Nikolay Kositsin" #property link "farria@mail.redcom.ru" #property description "AnchoredMomentumCandle" //---- номер версии индикатора #property version "1.01" //+----------------------------------------------+ //| Параметры отрисовки индикатора | //+----------------------------------------------+ //---- отрисовка индикатора в отдельном окне #property indicator_separate_window //---- для расчета и отрисовки индикатора использовано пять буферов #property indicator_buffers 5 //---- использовано всего одно графическое построение #property indicator_plots 1 //---- в качестве индикатора использованы цветные свечи #property indicator_type1 DRAW_COLOR_CANDLES #property indicator_color1 clrDeepPink,clrGray,clrBlue //---- отображение метки индикатора #property indicator_label1 "Open;High;Low;Close" //+----------------------------------------------+ //| Объявление констант | //+----------------------------------------------+ #define RESET 0 // константа для возврата терминалу команды на пересчет индикатора //+----------------------------------------------+ //| Входные параметры индикатора | //+----------------------------------------------+ input uint MomPeriod=8; // Период SMA input uint SmoothPeriod=6; // Период EMA input double UpLevel=+0.025; // Верхний пробойный уровень input double DnLevel=-0.025; // Нижний пробойный уровень //+----------------------------------------------+ //---- объявление динамических массивов, которые будут в //---- дальнейшем использованы в качестве индикаторных буферов double ExtOpenBuffer[]; double ExtHighBuffer[]; double ExtLowBuffer[]; double ExtCloseBuffer[]; double ExtColorBuffer[]; //---- объявление целочисленных переменных начала отсчета данных int min_rates_total; //---- объявление целочисленных переменных для хендлов индикаторов int Ind_Handle[4]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //---- инициализация глобальных переменных min_rates_total=int(MomPeriod+1); //---- получение хендлов индикатора iAnchoredMomentum Ind_Handle[0]=iCustom(NULL,0,"AnchoredMomentum",MomPeriod,SmoothPeriod,PRICE_OPEN,0,0,0); if(Ind_Handle[0]==INVALID_HANDLE) { Print(" Не удалось получить хендл индикатора iAnchoredMomentum["+string(0)+"]!"); return(INIT_FAILED); } //--- Ind_Handle[1]=iCustom(NULL,0,"AnchoredMomentum",MomPeriod,SmoothPeriod,PRICE_HIGH,0,0,0); if(Ind_Handle[1]==INVALID_HANDLE) { Print(" Не удалось получить хендл индикатора iAnchoredMomentum["+string(1)+"]!"); return(INIT_FAILED); } //--- Ind_Handle[2]=iCustom(NULL,0,"AnchoredMomentum",MomPeriod,SmoothPeriod,PRICE_LOW,0,0,0); if(Ind_Handle[2]==INVALID_HANDLE) { Print(" Не удалось получить хендл индикатора iAnchoredMomentum["+string(2)+"]!"); return(INIT_FAILED); } //--- Ind_Handle[3]=iCustom(NULL,0,"AnchoredMomentum",MomPeriod,SmoothPeriod,PRICE_CLOSE,0,0,0); if(Ind_Handle[3]==INVALID_HANDLE) { Print(" Не удалось получить хендл индикатора iAnchoredMomentum["+string(3)+"]!"); return(INIT_FAILED); } //---- превращение динамических массивов в индикаторные буферы SetIndexBuffer(0,ExtOpenBuffer,INDICATOR_DATA); SetIndexBuffer(1,ExtHighBuffer,INDICATOR_DATA); SetIndexBuffer(2,ExtLowBuffer,INDICATOR_DATA); SetIndexBuffer(3,ExtCloseBuffer,INDICATOR_DATA); //---- превращение динамического массива в цветовой, индексный буфер SetIndexBuffer(4,ExtColorBuffer,INDICATOR_COLOR_INDEX); //---- индексация элементов в буферах как в таймсериях ArraySetAsSeries(ExtOpenBuffer,true); ArraySetAsSeries(ExtHighBuffer,true); ArraySetAsSeries(ExtLowBuffer,true); ArraySetAsSeries(ExtCloseBuffer,true); ArraySetAsSeries(ExtColorBuffer,true); //---- осуществление сдвига начала отсчета отрисовки индикатора 1 PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,min_rates_total); //---- установка формата точности отображения индикатора IndicatorSetInteger(INDICATOR_DIGITS,3); //---- имя для окон данных и метка для субъокон string short_name="AnchoredMomentumCandle"; IndicatorSetString(INDICATOR_SHORTNAME,short_name); //---- количество горизонтальных уровней индикатора 2 IndicatorSetInteger(INDICATOR_LEVELS,2); //---- значения горизонтальных уровней индикатора IndicatorSetDouble(INDICATOR_LEVELVALUE,0,UpLevel); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,DnLevel); //---- в качестве цветов линий горизонтальных уровней использованы синий и розовый цвета IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrBlue); IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrMagenta); //---- в линии горизонтального уровня использован короткий штрих-пунктир IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,STYLE_DASHDOTDOT); IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_DASHDOTDOT); //--- завершение инициализации 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[]) { //---- проверка количества баров на достаточность для расчета if(BarsCalculated(Ind_Handle[0])rates_total || prev_calculated<=0)// проверка на первый старт расчета индикатора { limit=rates_total-1; // стартовый номер для расчета всех баров } else { limit=rates_total-prev_calculated; // стартовый номер для расчета новых баров } //--- to_copy=limit+1; //---- копируем вновь появившиеся данные в массивы if(CopyBuffer(Ind_Handle[0],0,0,to_copy,ExtOpenBuffer)<=0) return(RESET); if(CopyBuffer(Ind_Handle[1],0,0,to_copy,ExtHighBuffer)<=0) return(RESET); if(CopyBuffer(Ind_Handle[2],0,0,to_copy,ExtLowBuffer)<=0) return(RESET); if(CopyBuffer(Ind_Handle[3],0,0,to_copy,ExtCloseBuffer)<=0) return(RESET); //---- основной цикл исправления и окрашивания свечей for(bar=limit; bar>=0 && !IsStopped(); bar--) { double Max=MathMax(ExtOpenBuffer[bar],ExtCloseBuffer[bar]); double Min=MathMin(ExtOpenBuffer[bar],ExtCloseBuffer[bar]); //--- ExtHighBuffer[bar]=MathMax(Max,ExtHighBuffer[bar]); ExtLowBuffer[bar]=MathMin(Min,ExtLowBuffer[bar]); //--- if(ExtOpenBuffer[bar]ExtCloseBuffer[bar]) ExtColorBuffer[bar]=0.0; else ExtColorBuffer[bar]=1.0; } //---- return(rates_total); } //+------------------------------------------------------------------+