//+------------------------------------------------------------------+ //| Momentum_Histo.mq5 | //| Marcelo Castro | //| https://www.mql5.com/pt/users/zemo | //+------------------------------------------------------------------+ //This indicator is based on code from the original author Iurii Tokman. //https://www.mql5.com/ru/code/19158 //https://www.mql5.com/ru/users/satop //and translated to mt5 from the optimized mt4 version by Iurii Tokman //https://www.mql5.com/en/code/19868 #property copyright "Marcelo Castro" #property link "https://www.mql5.com/pt/users/zemo" #property version "1.24" #property strict #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 1 //--- plotar Histograma #property indicator_type1 DRAW_LINE #property indicator_color1 clrGreen,clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 2 //#property indicator_type2 DRAW_HISTOGRAM //#property indicator_color2 clrRed #property indicator_style2 STYLE_SOLID //#property indicator_width2 3 #property indicator_level1 0 #property indicator_level2 0 #property indicator_level3 0 //#property indicator_level1 0 //#property indicator_levelcolor clrBlue #property indicator_levelstyle STYLE_DASHDOT #property indicator_levelwidth 1 //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum ENUM_TYPE_DRAWING { DRAWING_TYPE_LINE,// Draw Line DRAWING_TYPE_HISTOGRAM // Draw Histogram }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum IndList { IndList1 = 1, //Momentum IndList2 = 2, //CCI (Commodity Channel Index) IndList3 = 3, //RSI (Relative Strength Index) IndList4 = 4, //WPR (Williams' Percent Range) IndList5 = 5, //DEM (DeMarKer) IndList6 = 6 //RVI (Relative Vigor Index) }; //--- input parameter input IndList MName= 1;//Indicator Name input int MPeriod = 14;//Indicator Period input double MLevel=100.0;//Level indicator input ENUM_APPLIED_PRICE MAppliedPrice=PRICE_CLOSE;//Applied Price for Momentum, CCI or RSI input ENUM_TYPE_DRAWING InpTypeDrawing=DRAWING_TYPE_LINE; // Type of indicator: input bool MANUAL_LEVELS=false;//Set levels manually input double MANUAL_LEVEL1 = 0;//manual level_up input double MANUAL_LEVEL2 = 0;//manual level_down input bool Alerts= false;//Enable Alerts input bool Email = false;//Enable Email Notification input bool Push=false;//Enable Push Notifiction string selectedName="Momentum"; //--- buffers //double BU0[],BU1[]; double par=0.0; int handle;//indicator handle //Declaration of buffers double buffer_line[],buffer_color_line[]; //int min_rates_total; //--- global variable int ExtPeriod; int initial_limit=0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- check for input value if(MPeriod<=0) { ExtPeriod=14; printf("Incorrect input parameter InpPeriods = %d. Indicator will use value %d for calculations.",MPeriod,ExtPeriod); } else ExtPeriod=int(MPeriod); string short_name; if(MName == 2){selectedName = "CCI";}else if(MName == 3){selectedName = "RSI";}else if(MName == 4){selectedName = "WPR";}else if(MName == 5){selectedName = "DeMarKer";}else if(MName == 6){selectedName = "RVI";} IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrDodgerBlue); IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrDodgerBlue); IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrDodgerBlue); if(MName == 1){handle=iMomentum(Symbol(),Period(),ExtPeriod,MAppliedPrice);Set3Levels(-0.30,0,0.30);}else if(MName == 2){handle=iCCI(Symbol(),Period(),ExtPeriod,MAppliedPrice);Set3Levels(-120,0,120);}else if(MName == 3){handle=iRSI(Symbol(),Period(),ExtPeriod,MAppliedPrice);Set3Levels(-8,0,8);}else if(MName == 4){handle=iWPR(Symbol(),Period(),ExtPeriod);Set3Levels(-25,0,25);}else if(MName == 5){handle=iDeMarker(Symbol(),Period(),ExtPeriod);Set3Levels(-20,0,20);}else if(MName == 6){handle=iRVI(Symbol(),Period(),ExtPeriod);Set3Levels(-0.10,0,0.10);} //Assign the data array with indicator's buffer SetIndexBuffer(0,buffer_line,INDICATOR_DATA); PlotIndexSetInteger(0,PLOT_DRAW_TYPE,(InpTypeDrawing ? DRAW_COLOR_HISTOGRAM:DRAW_COLOR_LINE)); //PlotIndexSetInteger(0,PLOT_LINE_WIDTH,0,(InpTypeDrawing ? 3:2)); ArraySetAsSeries(buffer_line,true); ArraySetAsSeries(buffer_color_line,true); //Assign the color indexes array with indicator's buffer SetIndexBuffer(1,buffer_color_line,INDICATOR_COLOR_INDEX); PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriod); PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE); short_name=selectedName+"_Histo("+IntegerToString(ExtPeriod)+") "; IndicatorSetString(INDICATOR_SHORTNAME,short_name); IndicatorSetInteger(INDICATOR_DIGITS,2); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| 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(handle)rates_total || prev_calculated<=0) { limit=rates_total-ExtPeriod-1; } else limit=rates_total-prev_calculated; for(bar=limit; bar>=0 && !IsStopped(); bar--) { if(MName==1){par=iMomentumGet(bar)-MLevel; }else //if(MName == 2){par=iATRGet(bar)-MLevel;}else if(MName == 2){par=iCCIGet(bar)-MLevel;}else if(MName == 3){par=50+iRSIGet(bar)-MLevel;}else if(MName == 4){par=iCustomGet(bar)+MLevel-50;}else if(MName == 5){par=iCustomGet(bar)*MLevel-50;}else if(MName == 6){par=iCustomGet(bar);} //Print(MName+" "+par); if(par>0) { buffer_line[bar]=par; if(buffer_line[bar+1]==EMPTY_VALUE) BuySignal(selectedName); buffer_color_line[bar]=0; } else { buffer_line[bar]=par; if(buffer_line[bar+1]==EMPTY_VALUE) SellSignal(selectedName); buffer_color_line[bar]=1; } //i--; //Calculating index of the next bar }//end while return(rates_total); } //+------------------------------------------------------------------+ void SellSignal(string _Name) { if(Alerts)Alert(_Name+"_Histo: SELL SIGNAL ON "+Symbol()); if(Email)SendMail(_Name+"_Histo: SELL SIGNAL ON "+Symbol(),"Signal Sent From "+_Name+"_Histo Indicator"); if(Push)SendNotification(_Name+"_Histo: SELL SIGNAL ON "+Symbol()); } //+------------------------------------------------------------------+ void BuySignal(string _Name) { if(Alerts)Alert(_Name+"_Histo: BUY SIGNAL ON "+Symbol()); if(Email)SendMail(_Name+"_Histo: BUY SIGNAL ON "+Symbol(),"Signal Sent From "+_Name+"_Histo Indicator"); if(Push)SendNotification(_Name+"_Histo: BUY SIGNAL ON "+Symbol()); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Get value of buffers for the iCCI | //+------------------------------------------------------------------+ double iMomentumGet(const int index) { double MOMENTUM[1]; //--- reset error code ResetLastError(); //--- fill a part of the iCCIBuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(handle,0,index,1,MOMENTUM)<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iMOMENTUM indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(0.0); } return(MOMENTUM[0]); } //+------------------------------------------------------------------+ //| Get value of buffers for the iRSI | //+------------------------------------------------------------------+ double iRSIGet(const int index) { double RSI[1]; //--- reset error code ResetLastError(); //--- fill a part of the iCCIBuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(handle,0,index,1,RSI)<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iRSI indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(0.0); } //Print(RSI[0]); return(RSI[0]); } //+------------------------------------------------------------------+ //| Get value of buffers for the iCCI | //+------------------------------------------------------------------+ double iCCIGet(const int index) { double CCI[1]; //--- reset error code ResetLastError(); //--- fill a part of the iCCIBuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(handle,0,index,1,CCI)<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iCCI indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(0.0); } return(CCI[0]); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Get value of buffers for the iCustom | //| the buffer numbers are the following: | //+------------------------------------------------------------------+ double iCustomGet(const int index) { double Custom[1]; //--- reset error code ResetLastError(); //--- fill a part of the iCCIBuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(handle,0,index,1,Custom)<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iCCI indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(0.0); } return(Custom[0]); } //+------------------------------------------------------------------+ //| Set Level Indicator | //+------------------------------------------------------------------+ void Set3Levels(double _levelup,double _levelmid,double _leveldn) { if(!MANUAL_LEVELS) { IndicatorSetDouble(INDICATOR_LEVELVALUE,0,_levelup); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,_levelmid); IndicatorSetDouble(INDICATOR_LEVELVALUE,2,_leveldn); } else { IndicatorSetDouble(INDICATOR_LEVELVALUE,0,MANUAL_LEVEL2); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,0); IndicatorSetDouble(INDICATOR_LEVELVALUE,2,MANUAL_LEVEL1); } } //+------------------------------------------------------------------+