#property copyright "asghar rostami (omidrstm@gmail.com)" #property link "https://www.mql5.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 2 #property indicator_color1 clrGreen #property indicator_type1 DRAW_NONE #property indicator_width1 2 #property indicator_color2 clrBlue #property indicator_type2 DRAW_NONE #property indicator_width2 2 #property indicator_label1 "ГУ U" #property indicator_label2 "ГУ D" input int TDLevel = 1; //Nível de pontos input int NumberOfDots = 3; //Quantidade de pontos input double Delta = 0.001; //Distância máxima entre dois pontos double TDLU[], TDLD[]; int hHandle; datetime iTime(string symbol,int tf,int index) { if(index < 0) return(-1); ENUM_TIMEFRAMES timeframe=TFMigrate(tf); datetime Arr[]; if(CopyTime(symbol, timeframe, index, 1, Arr)>0) return(Arr[0]); else return(-1); } ENUM_TIMEFRAMES TFMigrate(int tf) { switch(tf) { case 0: return(PERIOD_CURRENT); case 1: return(PERIOD_M1); case 5: return(PERIOD_M5); case 15: return(PERIOD_M15); case 30: return(PERIOD_M30); case 60: return(PERIOD_H1); case 240: return(PERIOD_H4); case 1440: return(PERIOD_D1); case 10080: return(PERIOD_W1); case 43200: return(PERIOD_MN1); case 2: return(PERIOD_M2); case 3: return(PERIOD_M3); case 4: return(PERIOD_M4); case 6: return(PERIOD_M6); case 10: return(PERIOD_M10); case 12: return(PERIOD_M12); case 16385: return(PERIOD_H1); case 16386: return(PERIOD_H2); case 16387: return(PERIOD_H3); case 16388: return(PERIOD_H4); case 16390: return(PERIOD_H6); case 16392: return(PERIOD_H8); case 16396: return(PERIOD_H12); case 16408: return(PERIOD_D1); case 32769: return(PERIOD_W1); case 49153: return(PERIOD_MN1); default: return(PERIOD_CURRENT); } } //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { SetIndexBuffer(0,TDLU,INDICATOR_DATA); SetIndexBuffer(1,TDLD,INDICATOR_DATA); PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE); PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE); ArrayInitialize(TDLU,EMPTY_VALUE); ArrayInitialize(TDLD,EMPTY_VALUE); hHandle = iCustom(NULL, 0, "itddots", NumberOfDots); ObjectCreate(0, "Nível horizontal U", OBJ_HLINE, 0, iTime(NULL, 0, 0), 0); ObjectCreate(0, "Nível horizontal D", OBJ_HLINE, 0, iTime(NULL, 0, 0), 0); 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 != Bars(NULL,0)) return(rates_total); TDLD[rates_total-2] = 0; TDLU[rates_total-2] = 0; TDLD[rates_total-1] = GetLevelPrice(1, TDLevel, Delta, NumberOfDots); TDLU[rates_total-1] = GetLevelPrice(0, TDLevel, Delta, NumberOfDots); if(ObjectFind(0,"Nível horizontal U") < 0) { ObjectCreate(0, "Nível horizontal U", OBJ_HLINE, 0, iTime(NULL, 0, 0), 0); } if(ObjectFind(0,"Nível horizontal D") < 0) { ObjectCreate(0, "Nível horizontal D", OBJ_HLINE, 0, iTime(NULL, 0, 0), 0); } ObjectSetDouble(0, "Nível horizontal U", OBJPROP_PRICE, TDLU[rates_total-1]); ObjectSetDouble(0, "Nível horizontal D", OBJPROP_PRICE, TDLD[rates_total-1]); return(rates_total); } //+------------------------------------------------------------------+ double GetLevelPrice(int ud,int n,double delta,int level) { /* ud - indicador do tipo de linha. 0 - U, outro valor - D. n - número de pontos. delta - distância máxima entre eles level - nível de pontos.*/ //São armazenadas matrizes para armazenamento dos preços dos pontos double TDU[]; double TDD[]; ArrayResize(TDU,n+1,n+1); ArrayResize(TDD,n+1,n+1); ArrayInitialize(TDU,0); ArrayInitialize(TDD,0); int N=0; for(int Buffer=0; Buffer<2; Buffer++) { N=0; int Fails=0; bool r=false; double buff[]; CopyBuffer(hHandle,Buffer,0,Bars(_Symbol,_Period),buff); for(int i=0; r==false && i0.0001) { if(N>0) { double cp; if(Buffer==0) cp=TDU[N-1]; else cp=TDD[N-1]; if(MathAbs(d-cp)<=delta) { if(Buffer == 0) TDU[N] = d; else TDD[N]=d; N++; } //Se a distância for muito grande, será adicionado 1 ao erro else { Fails++; } } else { if(Buffer == 0) TDU[N] = d; else TDD[N]=d; N++; } } //Se forem coletados muitos erros, o ciclo será concluído if(Fails>2 || N>n) r=true; } } //Obtenção do valor médio double ATDU = 0; double ATDD = 0; N=0; for(int i=0; i