//+------------------------------------------------------------------+ //| Support Resistance.mq5 | //| Copyright 2021, ernst | //| https://www.mql5.com/en/users/pippod | //+------------------------------------------------------------------+ #property copyright "Copyright 2021, ernst" #property link "https://www.mql5.com/en/users/pippod" #property version "1.00" #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 2 //--- plot Res #property indicator_label1 "Resistance" #property indicator_type1 DRAW_LINE #property indicator_color1 clrPaleGreen #property indicator_style1 STYLE_DASH #property indicator_width1 1 //--- plot Sup #property indicator_label2 "Support" #property indicator_type2 DRAW_LINE #property indicator_color2 clrTomato #property indicator_style2 STYLE_DASH #property indicator_width2 1 //--- input bool Redraw=true; //--- indicator buffers double ResBuffer[]; double SupBuffer[]; double Upper[]; double Lower[]; //--- int handle=INVALID_HANDLE; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- if((handle=iFractals(_Symbol,_Period))==INVALID_HANDLE) return(INIT_FAILED); //--- indicator buffers mapping SetIndexBuffer(0,ResBuffer,INDICATOR_DATA); ArraySetAsSeries(ResBuffer,true); SetIndexBuffer(1,SupBuffer,INDICATOR_DATA); ArraySetAsSeries(SupBuffer,true); //--- ArraySetAsSeries(Upper,true); ArraySetAsSeries(Lower,true); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[]) { //--- const int toCopy=(prev_calculated<1)?rates_total:(rates_total!=prev_calculated)?rates_total-prev_calculated+2:3; //--- if(CopyBuffer(handle,0,0,toCopy,Upper)!=toCopy || CopyBuffer(handle,1,0,toCopy,Lower)!=toCopy) { Print("Failed to copy values: ",(string)_LastError); return(0); } //--- const int limit=Redraw?(prev_calculated<1)?rates_total-2:(rates_total!=prev_calculated)?rates_total-prev_calculated+1:2: (prev_calculated<1)?rates_total-4:(rates_total!=prev_calculated)?rates_total-prev_calculated-1:0; //--- double upper,lower; for(int i=limit;i>=0 && !_StopFlag;i--) { if(Redraw) { upper=Upper[i]; lower=Lower[i]; } else { upper=Upper[i+2]; lower=Lower[i+2]; } ResBuffer[i]=(upper!=EMPTY_VALUE)?upper:ResBuffer[i+1]; SupBuffer[i]=(lower!=EMPTY_VALUE)?lower:SupBuffer[i+1]; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+