//+------------------------------------------------------------------+ //| Nearest Neighbor.mq5 | //| Copyright 2010, gpwr | //| vlad1004@yahoo.com | //+------------------------------------------------------------------+ #property copyright "gpwr" #property version "1.00" #property description "Prediction of future based on the nearest neighbor in the past" #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 2 //--- future model outputs #property indicator_label1 "NN future" #property indicator_type1 DRAW_LINE #property indicator_color1 Red #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- past model outputs #property indicator_label2 "NN past" #property indicator_type2 DRAW_LINE #property indicator_color2 Blue #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- global constants #define pi 3.141592653589793238462643383279502884197169399375105820974944592 //--- indicator inputs input int Npast =300; // Past bars in a pattern input int Nfut =50; // Future bars in a pattern (must be < Npast) //--- global variables int bars,PrevBars; double mx[],sxx[],denx[]; bool FirstTime; //--- indicator buffers double ynn[],xnn[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- initialize global variables PrevBars=Bars(_Symbol,_Period)-1; FirstTime=true; //--- indicator buffers mapping SetIndexBuffer(0,ynn,INDICATOR_DATA); SetIndexBuffer(1,xnn,INDICATOR_DATA); IndicatorSetInteger(INDICATOR_DIGITS,_Digits); IndicatorSetString(INDICATOR_SHORTNAME,"1NN("+string(Npast)+")"); PlotIndexSetInteger(0,PLOT_SHIFT,Nfut); } //+------------------------------------------------------------------+ //| 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[]) { //--- check for insufficient data and new bar int bars=rates_total; if(barscorrMax) { corrMax=corr; knn=k; b=num/denx[k]; } } Print("Nearest neighbor is dated ",Time[knn]," and has correlation with current pattern of ",corrMax); //--- Compute xm[] and ym[] by scaling the nearest neighbor double delta=Open[bars-1]-b*Open[knn+Npast-1]; for(int i=0;i=Npast-1) ynn[bars-Npast-Nfut+i]=b*Open[knn+i]+delta; } return(rates_total); } //+------------------------------------------------------------------+