// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("HEIKENASHI");
SetChartBkGradientFill( ParamColor("BgTop", colorWhite),ParamColor("BgBottom", colorWhite),ParamColor("Titleblock",colorLightGrey )); 
SetChartOptions(0,chartShowArrows|chartShowDates);
GraphXSpace=5;
p=Param("Period",6,2,30,1);
Om=MA(O,p);
hm=MA(H,p);
lm=MA(L,p);
Cm=MA(C,p);
HACLOSE=(Om+Hm+Lm+Cm)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( Hm, Max( HaClose, HaOpen ) );
HaLow = Min( Lm, Min( HaClose, HaOpen ) );
//PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "" + Name(), colorWhite, styleCandle | styleNoLabel );
_SECTION_END();


_SECTION_BEGIN("4");
SetChartOptions(0,chartShowArrows|chartShowDates);
//_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
//Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

TP = (H + L)/2;
r = WMA(HACLOSE, 4);


// *BEGIN Parabolic SAR Indicator

HH = HHV(r, 2);
LL = LLV(r, 2);

EnableScript ( "vbscript" );
psar = LL;

<%

Function Min( x, y, z )

If x < y Then
 Min = x
Else
 Min = y
End If

If z < Min Then
   Min = z
End If

End Function



Function Max( x, y, z )

If x > y Then
 Max = x
Else
 Max = y
End If

If z > Max Then
   Max = z
End If

End Function


StartAF = 0.02   '                           //acceleration factor
MaxAF = 0.1  '                          //max acceleration

r = AFL ( "r" )
HH   = AFL ( "HH" )
LL    = AFL ( "LL" )
psar  =  AFL ( "psar" )

psar( 0 ) = r( 0 )      ' initialize
LongPos = 1                    ' assume long for initial conditions
af = StartAF               ' init acelleration factor
ep = LL( 0 )                 ' init extreme point
hp = HH( 0 )
lp = LL( 0 )

For i = 2 To UBound( r )

     If LongPos Then
        psar( i ) = psar( i-1 ) + af * ( hp - psar( i-1 ) )
     Else
       psar( i ) = psar( i-1 ) + af * ( lp - psar ( i-1 ) )
     End If

     reverse =  0

     ' check for reversal
     
     If LongPos Then
       If  LL( i ) < psar ( i ) Then 
              LongPos = 0
  		reverse = 1 '            //reverse position to short
              psar( i ) =  hp '                        //sar is high point in prev trade
              lp = LL( i )
              af = StartAF
       End If
     
     Else
     
         If HH( i ) > psar( i ) Then
              LongPos = 1    
              reverse = 1 '        //reverse position to long
              psar( i ) =  lp 
              hp = HH( i ) 
              af = StartAF
         End If  
     End If

     If reverse = 0 Then
         If LongPos Then
             If HH( i ) > hp Then
                  hp = HH( i )
                  af = af + StartAF 
                  af = Min ( af, MaxAF, MaxAF )
             End If
                  
             psar( i ) = Min ( psar( i ), LL( i - 1 ), LL ( i-2 ) )
         Else
         
             If  LL( i ) < lp Then
                lp = LL( i )
                af = af + StartAF 
                af = Min ( af, MaxAF, MaxAF ) 
             End If
             psar( i ) = Max ( psar( i ), HH( i - 1 ), HH( i-2 ) )
         End If
     End If


Next

AFL( "psar" ) = psar
%>

//Graph0 = Close;
//Graph0Style = 64 +32 ;
//graph0Style = 128 +32 ;
//Graph0BarColor=1;
Graph1 = psar;
//Graph1Style = 8 + 16 + 32;
//Graph1Color = 8;



Title=Name() + " - Custom PSAR = "+WriteVal(psar);


Plot(psar,"",colorGreen,styleNoLine|styleDots);





mid = WMA(r, 50);
a = StDev(r, 50); 

a2 = a * 2.1;
a1 = WMA(a2, 50);


ub = mid + a1;
lb = mid - a1; 

//Plot( mid, "mid", colorGrey40 );
//Plot( ub, "ub", colorGrey40 );
//Plot( lb, "lb", colorGrey40 );

line = ub;
line1 = lb;
//Plot(line,"ma"+WriteVal(line,0),IIf(Ref(line,-1)>line,colorRed,IIf(ref(line,-1)<line,colorGreen,coloryellow)),styleLine,Null,Null,Null,Null,3);
//Plot(line1,"ma"+WriteVal(line,0),IIf(Ref(line,-1)>line,colorRed,IIf(ref(line,-1)<line,colorGreen,coloryellow)),styleLine,Null,Null,Null,Null,3);

TEMA2 = TEMA(r, 100);
//Plot( TEMA2, "TEMA2", colorGrey40 );

_SECTION_END();