#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Lime
//---- input parameters
extern double Step=0.02;
extern double Maximum=0.2;
//---- buffers
double SarBuffer[];
//----
int save_lastreverse;
bool save_dirlong;
double save_start;
double save_last_high;
double save_last_low;
double save_ep;
double save_sar;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,159);
SetIndexBuffer(0,SarBuffer);
//----
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| SaveLastReverse |
//+------------------------------------------------------------------+
void SaveLastReverse(int last,int dir,double start_index,double low,double high,double ep,double sar)
{
save_lastreverse=last;
save_dirlong=dir;
save_start=start_index;
save_last_low=low;
save_last_high=high;
save_ep=ep;
save_sar=sar;
}
//+------------------------------------------------------------------+
//| Parabolic Sell And Reverse system |
//+------------------------------------------------------------------+
int start()
{
static bool first=true;
bool dirlong;
double start_index,last_high,last_low;
double ep,sar,price_low,price_high,price;
int i;
int limit,cb;
//----
if(Bars<3) return(0);
int counted_bars=IndicatorCounted();
if(counted_bars < 0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
if(counted_bars==0) limit-=2;
//---- initial settings
i=limit;
if(counted_bars==0 || first)
{
first=false;
dirlong=true;
start_index=Step;
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_index=save_start;
dirlong=save_dirlong;
last_high=save_last_high;
last_low=save_last_low;
ep=save_ep;
sar=save_sar;
}
//----
while(i>=0)
{
price_low=Low[i];
price_high=High[i];
//--- check for reverse
if(dirlong && price_low<SarBuffer[i+1])
{
SaveLastReverse(i,true,start_index,price_low,last_high,ep,sar);
start_index=Step; dirlong=false;
ep=price_low; last_low=price_low;
SarBuffer[i]=last_high;
i--;
continue;
}
if(!dirlong && price_high>SarBuffer[i+1])
{
SaveLastReverse(i,false,start_index,last_low,price_high,ep,sar);
start_index=Step; dirlong=true;
ep=price_high; last_high=price_high;
SarBuffer[i]=last_low;
i--;
continue;
}
//---
price=SarBuffer[i+1];
sar=price+start_index*(ep-price);
if(dirlong)
{
if(ep<price_high && (start_index+Step)<=Maximum) start_index+=Step;
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_index,price_low,last_high,ep,sar);
start_index=Step; 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; }
}
else
{
if(ep>price_low && (start_index+Step)<=Maximum) start_index+=Step;
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_index,last_low,price_high,ep,sar);
start_index=Step; 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; }
}
SarBuffer[i]=sar;
i--;
}
//---
for(cb=limit;cb>=0;cb--)
{
if(GreaterDoubles(SarBuffer[cb],0,Digits))
{
string object_name="price"+string(Time[cb]);
//--- first find object by name
if(ObjectFind(object_name)<0)
{
//--- if not found, create it
if(ObjectCreate(object_name,OBJ_ARROW,0,Time[cb],SarBuffer[cb]))
{
//--- set object properties
//--- arrow code
ObjectSet(object_name,OBJPROP_ARROWCODE,SYMBOL_LEFTPRICE);
//--- color
ObjectSet(object_name,OBJPROP_COLOR,DodgerBlue);
//--- price
ObjectSet(object_name,OBJPROP_PRICE1,SarBuffer[cb]);
//--- time
ObjectSet(object_name,OBJPROP_TIME1,Time[cb]);
}
}
else
{
//--- if the object exists, just modify its price coordinate
ObjectSet(object_name,OBJPROP_PRICE1,SarBuffer[cb]);
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| GreaterDoubles |
//+------------------------------------------------------------------+
bool GreaterDoubles(double number1,double number2,int dig)
{
if(NormalizeDouble(number1-number2,dig)>0) return(true);
else return(false);
}
//+------------------------------------------------------------------+ |