//+------------------------------------------------------------------+ //| Linear Prediction.mq4 | //| AIS Forex | //| https://www.mql5.com/ru/users/aleksej1966 | //+------------------------------------------------------------------+ #property copyright "AIS Forex" #property link "https://www.mql5.com/ru/users/aleksej1966" #property version "1.00" #property strict #property indicator_chart_window #property indicator_buffers 3 #property indicator_plots 3 #property indicator_type1 DRAW_LINE #property indicator_label1 "Linear Prediction" #property indicator_color1 clrGreen #property indicator_width1 1 #property indicator_style1 STYLE_SOLID #property indicator_type2 DRAW_LINE #property indicator_label2 "Error Up" #property indicator_color2 clrBlue #property indicator_width2 1 #property indicator_style2 STYLE_SOLID #property indicator_type3 DRAW_LINE #property indicator_label3 "Error Dn" #property indicator_color3 clrRed #property indicator_width3 1 #property indicator_style3 STYLE_SOLID input ushort iPeriod=14, Parametr=0; ushort N=2,p; double buffer[],buffer_up[],buffer_dn[],coeff[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,buffer,INDICATOR_DATA); ArraySetAsSeries(buffer,true); SetIndexEmptyValue(0,EMPTY_VALUE); SetIndexShift(0,1); SetIndexBuffer(1,buffer_up,INDICATOR_DATA); ArraySetAsSeries(buffer_up,true); SetIndexEmptyValue(1,EMPTY_VALUE); SetIndexShift(1,1); SetIndexBuffer(2,buffer_dn,INDICATOR_DATA); ArraySetAsSeries(buffer_dn,true); SetIndexEmptyValue(2,EMPTY_VALUE); SetIndexShift(2,1); N=MathMax(N,iPeriod); ArrayResize(coeff,N); ArrayInitialize(coeff,1.0/N); p=Parametr==0?N:Parametr; //--- 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(rates_total>prev_calculated) { ArraySetAsSeries(open,true); static int sum_dn,cnt_dn,sum_up,cnt_up; int bars=prev_calculated>0? rates_total-prev_calculated:rates_total-N-1; for(int i=bars; i>=0; i--) { double indicator=0,M=p; for(int j=1; j<=N; j++) { indicator=indicator+coeff[j-1]*open[i+j]; M=M+MathPow(open[i+j],2); } double error=open[i]-indicator; for(int j=0; j0) { sum_up=sum_up+diff; cnt_up++; } if(cnt_dn>0 && cnt_up>0) { buffer_dn[i]=indicator+sum_dn*_Point/cnt_dn; buffer_up[i]=indicator+sum_up*_Point/cnt_up; } } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+