//+------------------------------------------------------------------+ //| Indicator Candles.mq4 | //| Copyright 2005-2014, MetaQuotes Software Corp. | //| http://www.mql4.com | //+------------------------------------------------------------------+ #property copyright "2005-2014, MetaQuotes Software Corp." #property link "http://www.mql4.com" #property description "Indicator Candles by pipPod" #property strict //--- indicator settings #property indicator_separate_window #property indicator_buffers 4 #property indicator_color4 clrMagenta #property indicator_width4 2 #property indicator_levelcolor clrLightSlateGray double indicator_level1 = 20; double indicator_level2 = 50; double indicator_level3 = 80; double indicator_level4 = 30; double indicator_level5 = 50; double indicator_level6 = 70; double indicator_level7 = -100; double indicator_level8 = 0; double indicator_level9 = 100; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum indicators { INDICATOR_PRI, //Percent Range Index INDICATOR_RSI, //Relative Strength Index INDICATOR_CCI, //Commodity Channel Index INDICATOR_PRICE //Prices }; enum ENUM_RANGEMODE { HIGH_LOW, //High/Low CLOSE_CLOSE, //Close/Close HIGH_LOW_CLOSE //High/Low/Close }; input indicators Indicator=INDICATOR_PRI; //--- indicator parameters input string _ = "Chart = Chart Symbol"; extern string Symbol_="Chart"; input int PRIPeriod=14; // Percent Range Index Period input int PRISignal=3; // PRI Signal Period input ENUM_APPLIED_PRICE PRIPrice=PRICE_CLOSE; // PRI Price input ENUM_RANGEMODE PRIMode=HIGH_LOW; input int RSIPeriod=14; // Relative Strength Index Period input int RSISignal=3; // RSI Signal Period input ENUM_APPLIED_PRICE RSIPrice=PRICE_CLOSE; // RSI Price input int CCIPeriod=14; // Commodity Channel Index Period input int CCISignal=3; // CCI Signal Period input ENUM_APPLIED_PRICE CCIPrice=PRICE_CLOSE; // CCI Price input int PriceSignal=14; //Price MA Period input ENUM_TIMEFRAMES TimeFrame=0; input int indicator_width1=3; //CandleBody Width input int indicator_width2=1; //CandleWick Width input bool SignalLine=true; // Show signal line for indicator input bool PriceLine=true; // Show horizontal price line input bool AutoColor=true; //--- indicator buffers double _High[]; double _Low[]; double _Close[]; double _Signal[]; double _high, _low, _close, _signal; string candle_1, candle_2, body="body", wick="wick", priceLine="PriceLine"; color candleColor; color color_1; color color_2; int barsWindow; long chartID; int window; //--- right input parameters flag bool ExtParameters=false; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit(void) { if(Symbol_=="Chart") Symbol_=_Symbol; else StringToUpper(Symbol_); color_1=clrLimeGreen; color_2=clrFireBrick; if(AutoColor) { string Base = StringSubstr(Symbol_,0,3); //Base currency name string Quote= StringSubstr(Symbol_,3,3); //Quote currency name if(Base=="EUR") color_1=clrRoyalBlue; if(Quote=="EUR") color_2=clrRoyalBlue; if(Base=="GBP") color_1=clrSilver; if(Quote=="GBP") color_2=clrSilver; if(Base=="AUD") color_1=clrDarkOrange; if(Quote=="AUD") color_2=clrDarkOrange; if(Base=="NZD") color_1=clrDarkViolet; if(Quote=="NZD") color_2=clrDarkViolet; if(Base=="CAD") color_1=clrWhite; if(Quote=="CAD") color_2=clrWhite; if(Base=="CHF") color_1=clrFireBrick; if(Quote=="CHF") color_2=clrFireBrick; if(Base=="JPY") color_1=clrYellow; if(Quote=="JPY") color_2=clrYellow; if(Base=="USD") color_1=clrLimeGreen; if(Quote=="USD") color_2=clrLimeGreen; if(Base=="XAU") color_1=clrGold; } //--- drawing settings SetIndexStyle(0,INDICATOR_DATA,EMPTY,EMPTY,clrNONE); SetIndexStyle(1,INDICATOR_DATA,EMPTY,EMPTY,clrNONE); SetIndexStyle(2,INDICATOR_DATA,EMPTY,EMPTY,clrNONE); SetIndexStyle(3,DRAW_LINE); //--- indicator buffers mapping SetIndexBuffer(0,_High); SetIndexBuffer(1,_Low); SetIndexBuffer(2,_Close); SetIndexBuffer(3,_Signal); SetLevelStyle(STYLE_DOT,1,indicator_levelcolor); string timeFrame; switch(TimeFrame) { case PERIOD_MN1: timeFrame = " MN1 ";break; case PERIOD_W1: timeFrame = " W1 "; break; case PERIOD_D1: timeFrame = " D1 "; break; case PERIOD_H4: timeFrame = " H4 "; break; case PERIOD_H1: timeFrame = " H1 "; break; case PERIOD_M30: timeFrame = " M30 ";break; case PERIOD_M15: timeFrame = " M15 ";break; case PERIOD_M5: timeFrame = " M5 "; break; case PERIOD_M1: timeFrame = " M1 "; break; default: timeFrame = " "; } string shortName, label_1, label_2, label_3, label_4; switch(Indicator) { case INDICATOR_PRI: shortName="PRI "+Symbol_+timeFrame+"("+ IntegerToString(PRIPeriod)+","+ IntegerToString(PRISignal)+")"; label_1="PRI High "+timeFrame; label_2="PRI Low "+timeFrame; label_3="PRI Close "+timeFrame; label_4="PRI Signal "+timeFrame; SetLevelValue(0,indicator_level1); SetLevelValue(1,indicator_level2); SetLevelValue(2,indicator_level3); IndicatorDigits(0); break; case INDICATOR_RSI: shortName="RSI "+Symbol_+timeFrame+"("+ IntegerToString(RSIPeriod)+","+ IntegerToString(RSISignal)+")"; label_1="RSI High "+timeFrame; label_2="RSI Low "+timeFrame; label_3="RSI Close "+timeFrame; label_4="RSI Signal "+timeFrame; SetLevelValue(0,indicator_level4); SetLevelValue(1,indicator_level5); SetLevelValue(2,indicator_level6); IndicatorDigits(0); break; case INDICATOR_CCI: shortName="CCI "+Symbol_+timeFrame+"("+ IntegerToString(CCIPeriod)+","+ IntegerToString(CCISignal)+")"; label_1="CCI High "+timeFrame; label_2="CCI Low "+timeFrame; label_3="CCI Close "+timeFrame; label_4="CCI Signal "+timeFrame; SetLevelValue(0,indicator_level7); SetLevelValue(1,indicator_level8); SetLevelValue(2,indicator_level9); IndicatorDigits(0); break; case INDICATOR_PRICE: shortName="Price "+Symbol_+timeFrame+"("+ IntegerToString(PriceSignal)+")"; label_1="Price High "+timeFrame; label_2="Price Low "+timeFrame; label_3="Price Close "+timeFrame; label_4="Price Signal "+timeFrame; IndicatorDigits(_Digits-1); } //--- name for DataWindow and indicator subwindow label IndicatorShortName(shortName); window=WindowFind(shortName); chartID=ChartID(); SetIndexLabel(0,label_1); SetIndexLabel(1,label_2); SetIndexLabel(2,label_3); SetIndexLabel(3,label_4); //--- check for input parameters if(PRIPeriod<=1 || RSIPeriod<=1 || CCIPeriod<=1) { Print("Wrong input parameters"); ExtParameters=false; return(INIT_FAILED); } else ExtParameters=true; //--- initialization done return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ 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[]) { int i,limit/*,shift*/; static int candle; //--- barsWindow=WindowBarsPerChart(); if(rates_total<=barsWindow || !ExtParameters) return(0); //--- limit=rates_total-prev_calculated; //--- if(prev_calculated==0) { limit=barsWindow; candle=0; double price; switch(Indicator) { case INDICATOR_PRI: _High[limit+1]=50; _Low[limit+1]=50; _Close[limit+1]=50; _Signal[limit+1]=50; break; case INDICATOR_RSI: _High[limit+1]=50; _Low[limit+1]=50; _Close[limit+1]=50; _Signal[limit+1]=50; break; case INDICATOR_CCI: _High[limit+1]=0.0; _Low[limit+1]=0.0; _Close[limit+1]=0.0; _Signal[limit+1]=0.0; break; case INDICATOR_PRICE: price=iClose(Symbol_,TimeFrame,limit); _High[limit+1]=price; _Low[limit+1]=price; _Close[limit+1]=price; _Signal[limit+1]=price; } } for(i=limit; i>=0;i--) { //shift=iBarShift(Symbol_,TimeFrame,time[i]); switch(Indicator) { case INDICATOR_PRI: _high =iPRI(Symbol_,TimeFrame,PRIPeriod,PRISignal,PRICE_HIGH,MODE_MAIN,i); _low =iPRI(Symbol_,TimeFrame,PRIPeriod,PRISignal,PRICE_LOW,MODE_MAIN,i); _close=iPRI(Symbol_,TimeFrame,PRIPeriod,PRISignal,PRIPrice,MODE_MAIN,i); if(SignalLine) _signal=iPRI(Symbol_,TimeFrame,PRIPeriod,PRISignal,PRIPrice,MODE_SIGNAL,i); break; case INDICATOR_RSI: _high =iRSI(Symbol_,TimeFrame,RSIPeriod,RSISignal,PRICE_HIGH,MODE_MAIN,i); _low =iRSI(Symbol_,TimeFrame,RSIPeriod,RSISignal,PRICE_LOW,MODE_MAIN,i); _close=iRSI(Symbol_,TimeFrame,RSIPeriod,RSISignal,RSIPrice,MODE_MAIN,i); if(SignalLine) _signal=iRSI(Symbol_,TimeFrame,RSIPeriod,RSISignal,RSIPrice,MODE_SIGNAL,i); break; case INDICATOR_CCI: _high =iCCI(Symbol_,TimeFrame,CCIPeriod,CCISignal,PRICE_HIGH,MODE_MAIN,i); _low =iCCI(Symbol_,TimeFrame,CCIPeriod,CCISignal,PRICE_LOW,MODE_MAIN,i); _close=iCCI(Symbol_,TimeFrame,CCIPeriod,CCISignal,CCIPrice,MODE_MAIN,i); if(SignalLine) _signal=iCCI(Symbol_,TimeFrame,CCIPeriod,CCISignal,CCIPrice,MODE_SIGNAL,i); break; case INDICATOR_PRICE: _high =iHigh(Symbol_,TimeFrame,i); _low =iLow(Symbol_,TimeFrame,i); _close=iClose(Symbol_,TimeFrame,i); if(SignalLine) _signal=iMA(Symbol_,TimeFrame,PriceSignal,0,MODE_SMA,PRICE_CLOSE,i); } _High[i]=_high; _Low[i]=_low; _Close[i]=_close; _High[i]=MathMax(_High[i],_Close[i+1]); _Low[i]=MathMin(_Low[i],_Close[i+1]); if(_Close[i]>=_Close[i+1]) candleColor=color_1; else candleColor=color_2; candle_1=body+string(candle); candle_2=wick+string(candle); ObjectCreate(candle_1,candle_2,time[i],_Close[i+1],_High[i],_Low[i],_Close[i],candleColor); if(SignalLine) _Signal[i]=_signal; if(prev_calculated==0 || NewBar(time[0])) candle++; //--- done } return(rates_total); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ #define OBJ_NONE -1 void OnDeinit(const int reason) { //---- check & delete candles for(int i=0;i