//+------------------------------------------------------------------+ //| CrossIndex.mq5 | //| Copyright © 2010, Nikolay Kositsin | //| Khabarovsk, farria@mail.redcom.ru | //+------------------------------------------------------------------+ //---- author of the indicator #property copyright "Copyright © 2010, Nikolay Kositsin" //---- link to the website of the author #property link "farria@mail.redcom.ru" //---- indicator version #property version "1.00" //+----------------------------------------------+ //| Indicator drawing parameters | //+----------------------------------------------+ //---- drawing indicator in a separate window #property indicator_separate_window //---- 6 buffers are used for calculation and drawing the indicator #property indicator_buffers 6 //---- only two plots are used #property indicator_plots 2 //---- color candlesticks are used as an indicator #property indicator_type2 DRAW_COLOR_CANDLES #property indicator_color2 LightSeaGreen, DeepPink //---- displaying the indicator label #property indicator_label2 "ExtOpenBuffer; ExtHighBuffer; ExtLowBuffer; ExtCloseBuffer" //+-----------------------------------+ //| Declaration of constants | //+-----------------------------------+ #define RESET 0 //+-----------------------------------+ //| Declaration of enumerations | //+-----------------------------------+ enum Applied_price_ // Type of constant { PRICE_CLOSE_ = 1, // Close PRICE_OPEN_, // Open PRICE_HIGH_, // High PRICE_LOW_, // Low PRICE_MEDIAN_, // Median Price (HL/2) PRICE_TYPICAL_, // Typical Price (HLC/3) PRICE_WEIGHTED_, // ExchIndWeighted Close (HLCC/4) PRICE_SIMPLE, // Simple Price (OC/2) PRICE_QUARTER_, // Quarted Price (HLOC/4) PRICE_TRENDFOLLOW0_, // TrendFollow_1 Price PRICE_TRENDFOLLOW1_ // TrendFollow_2 Price }; //+----------------------------------------------+ //| Indicator input parameters | //+----------------------------------------------+ input string CrossIndex="EURJPY"; // Currency pair input color BidColor=Red; input ENUM_LINE_STYLE BidStyle=STYLE_SOLID; input Applied_price_ IPC=PRICE_CLOSE; // Price constant in a zero buffer input int IndicatorDigits=3; // Indicator display accuracy format input bool Direct=true; // Chart inversion //+----------------------------------------------+ bool InitResult; int prev_calculated_=0; //---- declaration of dynamic arrays that //---- will be used as indicator buffers double ExtBuffer[]; double ExtOpenBuffer[]; double ExtHighBuffer[]; double ExtLowBuffer[]; double ExtCloseBuffer[]; double ExtColorsBuffer[]; //+------------------------------------------------------------------+ //| Getting the minimum number of bars for time series | //+------------------------------------------------------------------+ int Rates_Total(string symbol,int Rates_total) { //---- static datetime LastTime[1]; int bars=Bars(symbol,PERIOD_CURRENT); //---- int error=GetLastError(); ResetLastError(); if(error==4401) return(RESET); int rates_total_=MathMin(Rates_total,bars); datetime Time[1]; if(CopyTime(symbol,0,bars-1,1,Time)<=0) return(RESET); if(Time[0]!=LastTime[0]) { LastTime[0]=Time[0]; return(RESET); } //---- return(rates_total_); } //+----------------------------------------------------------------------------+ //| Checking of the time series synchronization by the current bar time | //+----------------------------------------------------------------------------+ bool SynchroCheck(string symbol,datetime BarTime,int Bar) { //---- datetime TimeN[1]; //---- if(!BarTime) return(RESET); if(CopyTime(symbol,0,Bar,1,TimeN)<=0) return(RESET); else if(TimeN[0]!=BarTime) return(RESET); //---- return(true); } //+------------------------------------------------------------------+ //| PriceSeries() function | //+------------------------------------------------------------------+ double PriceSeries(uint applied_price, // price constant uint bar, // index of shift relative to the current bar for a specified number of periods back or forward). const double &Open[], const double &Low[], const double &High[], const double &Close[]) { //----+ switch(applied_price) { //----+ Price constant from the enumeration ENUM_APPLIED_PRICE case PRICE_CLOSE: return(Close[bar]); case PRICE_OPEN: return(Open [bar]); case PRICE_HIGH: return(High [bar]); case PRICE_LOW: return(Low[bar]); case PRICE_MEDIAN: return((High[bar]+Low[bar])/2.0); case PRICE_TYPICAL: return((Close[bar]+High[bar]+Low[bar])/3.0); case PRICE_WEIGHTED: return((2*Close[bar]+High[bar]+Low[bar])/4.0); //----+ case 8: return((Open[bar] + Close[bar])/2.0); case 9: return((Open[bar] + Close[bar] + High[bar] + Low[bar])/4.0); //---- case 10: { if(Close[bar]>Open[bar])return(High[bar]); else { if(Close[bar]Open[bar])return((High[bar]+Close[bar])/2.0); else { if(Close[bar]rates_total || prev_calculated_<=0) {prev_calculated_=0; return;}; limit=rates_total-prev_calculated_; for(bar=limit-1; bar>=0; bar--) { ExtColorsBuffer[bar]=1.0; ExtBuffer[bar]=ExtBuffer[bar+1]; ExtOpenBuffer [bar]=ExtCloseBuffer[bar+1]; ExtCloseBuffer[bar]=ExtCloseBuffer[bar+1]; ExtHighBuffer [bar]=ExtCloseBuffer[bar+1]; ExtLowBuffer [bar]=ExtCloseBuffer[bar+1]; } return; } //---- calculation of the 'limit' starting index for the bars recalculation loop //---- and preliminary initialization of buffers with empty values if(prev_calculated_>rates_total || prev_calculated_<=0) // checking for the first start of the indicator calculation { limit=cross_rates_total-1; int draw_begin=rates_total-limit+1; //---- performing shift of the beginning of counting of drawing the indicator by draw_begin PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,draw_begin); PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,draw_begin); PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,draw_begin); PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,draw_begin); PlotIndexSetInteger(4,PLOT_DRAW_BEGIN,draw_begin); PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,draw_begin); for(bar=limit; barcross_rates_total-1) {prev_calculated_=0; return;}; } //---- calculation of the data amount to be copied to_copy=limit+1; //--- copy newly appeared data in the arrays if(CopyOpen (CrossIndex,PERIOD_CURRENT,0,to_copy,ExtOpenBuffer)<=0) {prev_calculated_=0; return;}; if(CopyHigh (CrossIndex,PERIOD_CURRENT,0,to_copy,ExtHighBuffer)<=0) {prev_calculated_=0; return;}; if(CopyLow (CrossIndex,PERIOD_CURRENT,0,to_copy,ExtLowBuffer)<=0) {prev_calculated_=0; return;}; if(CopyClose(CrossIndex,PERIOD_CURRENT,0,to_copy,ExtCloseBuffer)<=0) {prev_calculated_=0; return;}; //---- painting the candlesticks for(bar=limit; bar>=0; bar--) if(ExtOpenBuffer[bar]=0; bar--) ExtBuffer[bar]=PriceSeries(IPC,bar,ExtOpenBuffer,ExtLowBuffer,ExtHighBuffer,ExtCloseBuffer); //---- Bid line shift parameters IndicatorSetDouble(INDICATOR_LEVELVALUE,0,ExtCloseBuffer[0]); //---- if(prev_calculated_==0) ChartRedraw(0); prev_calculated_=rates_total; } //+------------------------------------------------------------------+ //| 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[]) { //---- return(rates_total); //---- } //+------------------------------------------------------------------+