// Downloaded From https://www.WiseStockTrader.com

//asfatul.alam@yahoo.com

SetChartOptions(0,chartShowArrows|chartShowDates);


bs=Param("BUY Sensitivity",3,2,20,1);
bf=Param("BUY Finetune",2,0.1,20,0.1);

ss=Param("SELL Sensitivity",3,2,20,1);
sf=Param("SELL Finetune",1,0.1,20,0.1);

MyCL = Param( "CL", 10, 10, 100, 10 );
MyVK = Param( "VK", 30, 10, 100, 10 );
MyTL = Param( "TL", 300, 100, 1000, 100 );

fast = 2/(2+1);
slow = 2/(30+1);

dirb=abs(Close-Ref(Close,-bs));
volb=Sum(abs(Close-Ref(Close,-1)),bs);
ERb=dirb/volb;
scb =( ERb*(fast-slow)+slow)^2; 
xb = AMA( C, scb ); 
flb=bf*StDev(xb-Ref(xb,-1),20);
j=xb-Ref(xb,-3);


dirs=abs(Close-Ref(Close,-ss));
vols=Sum(abs(Close-Ref(Close,-1)),ss);
ERs=dirs/vols;
scs =( ERs*(fast-slow)+slow)^2; 
xs = AMA( C, scs ); 
fls=sf*StDev(xs-Ref(xs,-1),20);
k=Ref(Xs,-3)-Xs;

Buy=Cross(j,flb) ;
Sell=Cross(k,fls);
mycolor=IIf(C>xb,colorLime,colorRed);
//Plot( C, "Close", mycolor,styleNoTitle | styleBar|styleThick   ); 








   SetChartOptions(0,chartShowArrows|chartShowDates);
   GraphXSpace=Param("GraphXSpace",10,0,300,1);
   SetChartBkColor(ParamColor("Outer Panel",53));
   SetChartBkGradientFill(ParamColor("Upper Chart",16),ParamColor("Lower Chart",16));

   global hO,hH,hL,hC,fO,fH,fL,fC,bc,Lx,x;
   x=BarIndex();  bc=BarCount;  Lx=LastValue(x);

   fC=EMA((O+H+L+C)/4,3);         fO=EMA((Ref(O,-1)+Ref(fC,-1))/2,3);
   fH=EMA(Max(Max(H,fO),fC),3);   fL=EMA(Min(Min(L,fO),fC),3);

   hC=EMA((O+H+L+C)/4,3);         hO=AMA(Ref(hC,-1),0.5); 
   hH=Max(H,Max(hC,hO));          hL=Min(L,Min(hC,hO)); 
   function ti3(y,t)  {
    s=0.84;
    e1=EMA(y,t);  e2=EMA(e1,t);  e3=EMA(e2,t);
    e4=EMA(e3,t); e5=EMA(e4,t);  e6=EMA(e5,t);
    c1=-s*s*s;    c2=3*s*s+3*s*s*s;
    c3=-6*s*s-3*s-3*s*s*s;
    c4=1+3*s+s*s*s+3*s*s;
   return T3=c1*e6+c2*e5+c3*e4+c4*e3; }


   bsl=ParamToggle("Candle Type","Heikin-Ashi,Candle ",0);
   olb=ParamToggle("Overlay Bars","Off|On",0);
   sco= ParamToggle("CandleFill Color","Std|Macd",1);
   r1=5;   r2=10;   r3=5;//MACD Fast,Slow & Signal Periods
   ap=4;//ATR & Hi Lo Periods
   pd=7;//HHV LLV Lookback
   atv=ATR(ap);  sup=HHV(LLV(L,ap)+atv,pd);  res=LLV(HHV(H,ap)-atv,pd);
   fC=EMA((O+H+L+C)/4,3);//Flower Close
   hC=(O+H+L+C)/4;        hO=AMA(Ref(fC,-1),0.5);  
   hH=Max(H,Max(fC,hO));  hL=Min(L,Min(fC,hO)); 
   m1=MACD(r1,r2);   s1=Signal(r1,r2,r3);  uc=ColorRGB(230,230,0);
   mco=IIf(m1<0 AND m1>s1,uc,IIf(m1>0 AND m1>s1,55,IIf(m1>0 AND m1<s1,25,24)));
   if(sco)SetBarFillColor(mco);
   if(bsl==1)PlotOHLC(O,H,L,C,"",IIf(C>O,55,32),64);
   if(bsl==0)PlotOHLC(hO,hH,hL,fC,"",IIf(C>sup,55,IIf(C<res,32,42)),64);
   bco=IIf(m1<0 AND m1>s1,uc,IIf(m1>0 AND m1>s1,43,IIf(m1>0 AND m1<s1,32,42)));
   if(bsl==0 AND olb)
Plot(C,"",bco,128|4096,0,0,0,1);

_SECTION_END();

Plot(xb,"BUY",colorYellow,1);
Plot(xs,"SELL",colorOrange,1);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

shape = Buy * shapeUpArrow +Sell * shapeDownArrow ;

PlotShapes( shape, IIf( Buy, colorGreen, colorYellow ),0, IIf( Buy, Low, High ) );

GraphXSpace = 5;
dist = 1.5*ATR(20); 

for( i = 0; i < BarCount; i++ ) 
{ 
if( Buy[i] ) PlotText( "Buy\n@" + C[ i ], i, L[ i ]-dist[i], colorLime ); 
if( Sell[i] ) PlotText( "sell\n@" + C[ i ], i, L[ i ]+dist[i], colorYellow ); 
} 
Filter= Buy OR Sell;

PositionScore=100/C;
PositionSize = - 20;
SetBarsRequired(10000, 10000);
SetFormulaName("Otithi lozz");

_SECTION_END();

_SECTION_BEGIN("IIR2");

function IIR2( input, f0, f1, f2 )

{
result[ 0 ] = input[ 0 ];
result[ 1 ] = input[ 1 ];
for( i = 2; i < BarCount; i++ )
{
result[i] = f0 * input[i] + f1 * result[i-1] + f2 * result[i-2];
}

return result;
}

SmoothedClose = IIR2(Close, 0.2, 1.4, -0.6 );
//Plot( Close, "Price", 2, styleCandle );
Plot( SmoothedClose, "function example", colorRed );

_SECTION_END();

_SECTION_BEGIN("GSMA");
SetBarsRequired(100000,0);
PI = 3.1415926;

function jIIR2( input, f0, f1, f2 ) 
{ 
    result[ 0 ] = input[ 0 ]; 
    result[ 1 ] = input[ 1 ]; 

    for( i = 2; i < BarCount; i++ ) 
    { 
       result[ i ] = f0 * input[ i ] + 
                     f1 * result[ i - 1 ] + 
                     f2 * result[ i - 2 ]; 
    } 

   return result; 
} 

function GSMA( input, Period )
{
  N = 0;
  an = 2 * PI / Period;
  c0 = b0 = 1;
  c1 = b1 = b2 = a1 = a2 = gamma1 = 0;
  beta1 = 2.415 * ( 1- cos( an ) );
  alpha = -beta1 + sqrt( beta1 ^ 2 + 2 * beta1 );
  alpha1 = ( cos( an ) + sin( an ) - 1 )/cos( an );
   {
    fo = alpha ^ 2;
    f1 = 2 * ( 1- alpha ); f2 = -( 1 - alpha )*( 1 - alpha );
  }
  

  return jIIR2( input, fo,f1,f2);
}
period=Param("period",13,1,40,1);

//Plot( Close, "Price", colorBlack, styleCandle ); 
Plot( GSMA( C,period), "GSMA", colorLime );


P = ParamField("Price field",-1);
Daysback = Param("Period for Liner Regression Line",21,1,240,1);
shift = Param("Look back period",0,0,240,1); 



x = Cum(1);
lastx = LastValue( x ) - shift; 
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) ); 
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) ); 
y = Aa + bb * ( x - (Lastx - DaysBack +1 ) ); 




LRColor = ParamColor("LR Color", colorCycle ); 
LRStyle = ParamStyle("LR Style");

LRLine =  IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE ); //  styleDots ); 


SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);
SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET  
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCustom11 ); 
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", SDColor,SDStyle ); 
Plot( SDL , "Lower Lin Reg", SDColor,SDStyle ); 


SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
SD2 = SDP2/2;

width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) );   // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET  
SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ;

SDColor2 = ParamColor("2 SD Color", colorPink ); 
SDStyle2 = ParamStyle("2 SD Style");

Plot( SDU2 , "Upper Lin Reg", SDColor2,SDStyle2 ); 
Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 ); 

Title = ( Name()+"  " + Date() +"  "+Interval(2) +"  "+ EncodeColor(colorLime)+",Open "+Open +" ,High "+H+" ,Low "+L+" ,Close "+C+" "+"{{VALUES}}");