//+------------------------------------------------------------------+ //| Interpolation.mq5 | //| Copyright 2018, MetaQuotes Software Corp. | //| https://mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, MetaQuotes Software Corp." #property link "https://mql5.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 1 //--- plot MA #property indicator_label1 "Interpolation" #property indicator_type1 DRAW_LINE #property indicator_color1 clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 2 //--- input parameters input uint InpLength = 100; // Length input uint InpPower = 5; // Power input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price input uint InpNodeDrawStyle=STYLE_DOT; // Drawing style of node line //--- indicator buffers double BufferRegression[]; double BufferMA[]; //--- global variables int handle_ma; int length_def; int power; int period_ma; string prefix; datetime last_time; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- setting global variables length_def=int(InpLength<5 ? 5 : InpLength); power=int(InpPower<1 ? 1 : InpPower>9 ? 9 : InpPower); last_time=0; //--- prefix=MQLInfoString(MQL_PROGRAM_NAME)+"_"; //--- indicator buffers mapping SetIndexBuffer(0,BufferRegression,INDICATOR_DATA); SetIndexBuffer(1,BufferMA,INDICATOR_CALCULATIONS); //--- settings indicators parameters IndicatorSetInteger(INDICATOR_DIGITS,Digits()); string descr="Interpolation("+(string)power+")"; IndicatorSetString(INDICATOR_SHORTNAME,descr); PlotIndexSetString(0,PLOT_LABEL,descr); //--- setting buffer arrays as timeseries ArraySetAsSeries(BufferRegression,true); ArraySetAsSeries(BufferMA,true); //--- create MA's handle ResetLastError(); handle_ma=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpAppliedPrice); if(handle_ma==INVALID_HANDLE) { Print("The iMA(1) object was not created: Error ",GetLastError()); return INIT_FAILED; } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0,prefix,0); ChartRedraw(); } //+------------------------------------------------------------------+ //| 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_totalTimeCurrent()) && res==Bars(symbol_name,timeframe,time-PeriodSeconds(timeframe)+1,UINT_MAX)) return(WRONG_VALUE); return(res); } //+------------------------------------------------------------------+ //| Рассчитывает линию | //+------------------------------------------------------------------+ bool Calculate(const int rates_total) { //--- Подготовка данных ArrayInitialize(BufferMA,0); int copied=CopyBuffer(handle_ma,0,0,rates_total,BufferMA); if(copied!=rates_total) return false; //--- ArrayInitialize(BufferRegression,EMPTY_VALUE); int StartBar=GetStartPoint(); int EndBar=GetEndPoint(); if(StartBar=0; i--) { if(j==0) constant[i]=summ_y_value[i]/matrix[i][i]; else { summ=0; for(int k=j; k>=1; k--) summ+=constant[i+k]*matrix[i][i+k]; constant[i]=(summ_y_value[i]-summ)/matrix[i][i]; } j++; } int k=1; for(int i=StartBar; i>=EndBar; i--) { summ=0; for(int n=0; n<=power; n++) summ+=constant[n]*MathPow(k,n); BufferRegression[i]=(summ); k++; } ChartRedraw(); return true; } //+------------------------------------------------------------------+