#property indicator_chart_window // в окне инструмента #property indicator_buffers 2 #property indicator_color1 Blue // входные параметры Parabolic SAR extern double StepFrom=0.005; // начальное значение и шаг фактора ускорения от extern double StepTo=0.02; // до extern double Maximum=0.2; // конечное значение фактора ускорения // Воходные параметры MasterSlave extern int Source=1; // 0 - объем; 1 - ATR; 2 - ст.девиация цены; extern int SourcePeriod=33; // период входного индикатора extern int Window=88; // длина выборки нормирования extern double Sensitivity=0; // чувствительность extern int Signal=33; // сглаживание нормированного индикатора (сигнальная) //-- int History=0; // индикаторные буферы double SarBuffer[], // параболик Step[]; // шаг фактора ускорения // общие переменные для параболика int save_lastreverse; bool save_dirlong; double save_start; double save_last_high; double save_last_low; double save_ep; double save_sar; int bartime; // bool First=1; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexBuffer(0,SarBuffer); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,159); SetIndexLabel(0,"SAR"); SetIndexBuffer(1,Step); SetIndexStyle(1,DRAW_NONE,2); SetIndexEmptyValue(0,0.0); SetIndexEmptyValue(1,0.0); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int ic=IndicatorCounted(); if(!First && Bars-ic>2) ic=reinit(); // если есть пропущенные бары не на подключении (в работе) First=0; // сброс флага первого запуска // Мастер int limit=Bars-ic-1; for(int i=limit; i>0; i--) { // управляющий сигнал double ms=iCustom(NULL,0,"_MasterSlave",Source,SourcePeriod,Window,Sensitivity,Signal, 0,i); Step[i-1]=StepFrom+ms*(StepTo-StepFrom); } //===========параболик======================== static bool first = true; bool dirlong; double start, last_high, last_low; double ep, sar, price_low, price_high, price; int counted_bars = IndicatorCounted(); //---- if(Bars < 3) return(0); //---- initial settings i = Bars - 2; //---- if(counted_bars == 0 || first) { first = false; dirlong = true; start = Step[i]; last_high = -10000000.0; last_low = 10000000.0; //---- while(i > 0) { save_lastreverse = i; price_low = Low[i]; //---- if(last_low > price_low) last_low = price_low; price_high = High[i]; //---- if(last_high < price_high) last_high = price_high; //---- if(price_high > High[i+1] && price_low > Low[i+1]) break; //---- if(price_high < High[i+1] && price_low < Low[i+1]) { dirlong = false; break; } i--; } //---- initial zero int k = i; //---- while(k < Bars) { SarBuffer[k] = 0.0; k++; } //---- check further if(dirlong) { SarBuffer[i] = Low[i+1]; ep = High[i]; } else { SarBuffer[i] = High[i+1]; ep = Low[i]; } i--; } else { i = save_lastreverse; start = save_start; dirlong = save_dirlong; last_high = save_last_high; last_low = save_last_low; ep = save_ep; sar = save_sar; // Fix1 start // If new bar increment index if(Time[0] != bartime) { bartime = Time[0]; i++; } //Fix1 end } //---- while(i >= 0) { price_low = Low[i]; price_high = High[i]; //--- check for reverse from long to short if(dirlong && price_low < SarBuffer[i+1]) { SaveLastReverse(i, true, start, price_low, last_high, ep, sar); start = Step[i]; dirlong = false; ep = price_low; last_low = price_low; SarBuffer[i] = last_high; i--; continue; } //--- check for reverse from short to long if(!dirlong && price_high > SarBuffer[i+1]) { SaveLastReverse(i, false, start, last_low, price_high, ep, sar); start = Step[i]; dirlong = true; ep = price_high; last_high = price_high; SarBuffer[i] = last_low; i--; continue; } //sar(i) = sar(i+1)+start*(ep-sar(i+1)) price = SarBuffer[i+1]; sar = price + start*(ep - price); //---- if(dirlong) { if(ep < price_high && (start + Step[i]) <= Maximum) start += Step[i]; //---- if(price_high < High[i+1] && i == Bars - 2) sar = SarBuffer[i+1]; price = Low[i+1]; //---- if(sar > price) sar = price; price = Low[i+2]; //---- if(sar > price) sar = price; //---- if(sar > price_low) { SaveLastReverse(i, true, start, price_low, last_high, ep, sar); start = Step[i]; dirlong = false; ep = price_low; last_low = price_low; SarBuffer[i] = last_high; i--; continue; } //---- if(ep < price_high) { last_high = price_high; ep = price_high; } } //dir-long else { if(ep > price_low && (start + Step[i]) <= Maximum) start += Step[i]; //---- if(price_low < Low[i+1] && i == Bars - 2) sar = SarBuffer[i+1]; price = High[i+1]; //---- if(sar < price) sar = price; price = High[i+2]; //---- if(sar < price) sar = price; //---- if(sar < price_high) { SaveLastReverse(i, false, start, last_low, price_high, ep,sar); start = Step[i]; dirlong = true; ep = price_high; last_high = price_high; SarBuffer[i] = last_low; i--; continue; } //---- if(ep > price_low) { last_low = price_low; ep = price_low; } } //dir-short SarBuffer[i] = sar; i--; } //while // sar=SarBuffer[0]; // price=iSAR(NULL,0,Step,Maximum,0); // if(sar!=price) Print("custom=",sar," SAR=",price," counted=",counted_bars); // if(sar==price) Print("custom=",sar," SAR=",price," counted=",counted_bars); //---- //---- return(0); } //===================================================================================== void SaveLastReverse(int last, int dir, double start, double low, double high, double ep, double sar) { save_lastreverse = last; save_dirlong = dir; save_start = start; save_last_low = low; save_last_high = high; save_ep = ep; save_sar = sar; } int reinit() { // дополнительная инициализация ArrayInitialize(SarBuffer,0.0); ArrayInitialize(Step,0.0); return(0); }