added few more filters to reduce the whipsaws
_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowDates | chartShowArrows | chartLogarithmic | chartWrapTitle );
_N( Title = StrFormat( "{{NAME}} - " + SectorID( 1 ) + " - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " + WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", colorRose, styleCandle | styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("TSI");
r = Param( "TSI period 'R':", 25, 1, 100, 1 );
s = Param( "TSI period 'S':", 13, 1, 100, 1 );
u = Param( "TSI period 'U':", 1, 1, 100, 1 );
sig = Param( "Signal period:", 7, 1, 100, 1 );
Mtm = C - Ref ( C, -1 );
AbsMtm = abs ( Mtm );
Num_T = EMA ( EMA( EMA ( Mtm, r ), s ), u );
Den_T = EMA ( EMA( EMA ( AbsMtm, r ), s ), u);
TSI = 100 * Nz ( Num_T / Den_T );
TSIL = 100 * Nz ( Num_T / Den_T );
//Green TSIL Line=Rising; Red TSIL Line=Falling
col = IIf( TSIL > Ref( TSIL, -1 ), colorGreen, colorRed );
//Plot( TSIL, "TSI("+r+","+s+","+u+")", col, styleLine | styleThick);
//Green EMA TSI,sig Line=Rising; Red EMA TSI,sig Line=Falling
col = IIf( EMA(TSI,sig) > Ref( EMA(TSI,sig), -1 ), colorGreen, colorRed );
//Plot( EMA(TSI,sig), "", col, styleThick);
//Plot(0,"ZeroLine",ParamColor( "ColorZero", colorBlueGrey ),styleLine);
/*
Buy = Cross(EMA(TSI,sig), TSIL);
Sell = Cross(TSIL, EMA(TSI,sig));
PlotShapes( IIf( Buy, shapeSmallSquare, shapeNone ), 4, layer = 0, yposition = 0, offset = 3 );
PlotShapes( IIf( Buy, shapeSmallSquare, shapeNone ), 4, layer = 0, yposition = 0, offset = -4 );
PlotShapes( IIf( Sell, shapeSmallSquare, shapeNone ), 8, layer = 0, yposition = 0, offset = 3 );
PlotShapes( IIf( Sell, shapeSmallSquare, shapeNone ), 8, layer = 0, yposition = 0, offset = -4 );
Filter=Buy OR Sell;
Sell=ExRem(Sell,Buy); Buy=ExRem(Buy,Sell);
*/
_SECTION_END();
CY1 = Param("Short Cycle Length?" ,10, 1 ,1000 ,1 )/2;
CY2 = Param("Medium Cycle Length?" ,80, 1 ,1000 ,1 )/2;
M1 = Param("Short Cycle Multiplyer?" ,1, 0.01 ,10 ,1 );
M2 = Param("Medium Cycle Multiplyer?" ,3, 0.01 ,10 ,1 );
T1 = Ref(MA(Close ,CY1 ),-CY1/2)+ M1*ATR(CY1 );
B1 = Ref(MA( Close ,CY1 ),-CY1/2)- M1*ATR(CY1 );
T2 = Ref(MA(Close ,CY2 ),-CY2/2)+ M2*ATR(CY2 );
B2 = Ref(MA( Close ,CY2 ),-CY2/2)- M2*ATR(CY2 );
//Plot(T1, "", colorLime);
//Plot(B1, "", colorLime);
Plot(T2, "", colorLime);
Plot(B2, "", colorLime);
GraphXSpace = 5;
Period = Param("Period", 25, 2, 100 );
bi = BarIndex();
procedure PlotLevel( array, nam, Color )
{
x = SelectedValue( bi );
y = SelectedValue( array );
Plot( IIf( bi >= x AND bi < x + Period, y, Null ),
nam, Color, styleDashed );
PlotText( nam, x + Period - 1, y, Color );
}
pl = Ref( LLV( Low, Period ), -1 );
ph = Ref( HHV( High, Period ), -1 );
range = ph - pl;
pc = Ref( Close, -1 );
R5 = ph/pl * pc;
R4 = pc + Range * 1.1/2;
R3 = pc + Range * 1.1/4;
R2 = pc + Range * 1.1/6;
R1 = pc + Range * 1.1/12;
S1 = pc - Range * 1.1/12;
S2 = pc - Range * 1.1/6;
S3 = pc - Range * 1.1/4;
S4 = pc - Range * 1.1/2;
S5 = pc - ( R5 - pc );
Plot( C, Name() + " " + Date(), colorDefault, styleCandle );
PlotLevel( R5, "R5", colorBlue );
PlotLevel( R4, "R4", colorBlue );
PlotLevel( R3, "R3", colorBlue );
PlotLevel( R2, "R2", colorBlue );
//PlotLevel( R1, "R1", colorBlue );
//PlotLevel( S1, "S1", colorRed );
PlotLevel( S2, "S2", colorRed );
PlotLevel( S3, "S3", colorRed );
PlotLevel( S4, "S4", colorRed );
PlotLevel( S5, "S5", colorRed );
SetChartBkGradientFill( ParamColor("BgTop", colorDarkGrey),ParamColor("BgBottom", colorDarkGrey));
_SECTION_BEGIN("Ehlers laguerre RSI");
//Ehlers laguerre RSI
/*
The Laguerre transform provides a time warp such that the Low frequency components are delayed
much more than the High frequency components. This enables very smooth filters to be built
using a Short amount of data.
The Laguerre RSI operates on four data points AND is smoother than an RSI(4).
*/
SetChartBkColor(ParamColor("Panel color ",colorDarkGrey));
SetBarsRequired(200, 0);
// Ehlers formulas
// from Ehlers, John F. Cybernetic Analysis for Stocks and Futures. Wiley. 2004.
// Chapter 14, p. 213. Code on p. 221.
function LRSI(array, gamma)
// Figure 14.8 on p. 221.
{
L0 = array; // Initialize as array
L1 = array;
L2 = array;
L3 = array;
LRSIValue = array;
for(i = 1; i < BarCount; i++)
{
L0 = (1 - gamma)*array + gamma*L0[i-1];
L1 = - gamma * L0 + L0[i-1] + gamma * L1[i-1];
L2 = - gamma * L1 + L1[i-1] + gamma * L2[i-1];
L3 = - gamma * L2 + L2[i-1] + gamma * L3[i-1];
CU = 0;
CD = 0;
if (L0 >= L1) CU = L0 - L1; else (CD = L1 - L0);
if (L1 >= L2) CU = CU + L1 - L2; else CD = CD + L2 - L1;
if (L2 >= L3) CU = CU + L2 - L3; else CD = CD + L3 - L2;
if (CU + CD != 0) LRSIValue = CU / (CU + CD);
}
return LRSIValue;
}
//Plot(LRSI(C, 0.5), "Laguerre RSI", colorWhite, styleLine);
//PlotGrid(0.5);
myc=IIf(LRSI(C,0.5)>0.5,colorBlue,
IIf(LRSI(C,0.5)<0.5,colorRed,31));
// IIf(HaClose<smmaH AND HaClose>smmaL ,colorYellow,31)));
//Plot(6, "", myC, styleOwnScale| styleArea|styleNoLabel,-0.5,100);
_SECTION_END();
_SECTION_BEGIN("cycle121");
SetChartBkColor(ParamColor("Panel color ",colorDarkGrey));
PI = 3.1415926;
Data = (H+L)/2;
// detrending ( high-pass filter )
HFPeriods = Param("HP filter cutoff", 40, 20, 100 );
alpha1 = ( 1-sin(2*pi/HFPeriods) ) / cos( 2 * pi / HFPeriods );
HP = AMA2( Data - Ref( Data, -1 ), 0.5 * ( 1 + alpha1 ), alpha1 );
// 6-tap low-pass FIR filter
SmoothHP = ( HP + 2 * Ref( HP, -1 ) + 3 * Ref( HP, -2 ) +
3 * Ref( HP, -3 ) + 2 * Ref( HP, -4 ) + Ref( HP, -5 ) )/12;
SmoothHPDiff = SmoothHP - Ref( SmoothHP, -1 );
x = BarIndex();
delta = -0.015 * x + 0.5;
delta = Max( delta, 0.15 );
Q = 0;
Real = 0;
Imag = 0;
Ampl = 0;
DB = 0;
I = SmoothHP;
MaxAmpl = 0;
for( N = 8; N <= 50; N++ )
{
beta = cos( 2 * PI / N );
Q = ( N / ( 2 * PI ) ) * SmoothHPDiff;
for( bar = 8; bar < BarCount; bar++ )
{
gamma = 1 / cos( 4 * PI * delta[ bar ] / N );
alpha = gamma - sqrt( gamma ^ 2 - 1 );
Real[ bar ] = 0.5 * ( 1 - alpha ) * ( I[ bar ] - I[ bar - 1 ] ) +
beta * ( 1 + alpha ) * Real[ bar - 1 ] -
alpha * Real[ bar - 2 ];
Imag[ bar ] = 0.5 * ( 1- alpha ) * ( Q[ bar ] - Q[ bar - 1 ] ) +
beta * ( 1 + alpha ) * Imag[ bar - 1 ] -
alpha * Imag[ bar - 2 ];
}
Ampl = Real ^ 2 + Imag ^ 2;
MaxAmpl = Max( MaxAmpl, Ampl );
VarSet("Ampl"+N, Ampl );
}
TunedFilterDisplay = ParamToggle("Dom Cycle Tuned Filter", "No|Yes" );
// Plot Heat Map ( Spectrogram )
// and find dominant cycle
DcNum = DcDenom = 0;
for( N = 8; N <= 50; N++ )
{
Ampl = VarGet("Ampl"+N);
db = Nz( -10 * log10( 0.01 / ( 1 - 0.99 * Ampl / MaxAmpl ) ) );
db = Min( db, 20 ) ;
Red = IIf( db <= 10, 255, 255 * ( 2 - db/10 ) );
Green = IIf( db <= 10, 255 * ( 1 - db/10 ), 0 );
if( NOT TunedFilterDisplay )
PlotOHLC( N, N, N-1, N-1, "", ColorRGB( Red, Green, 0 ),
styleCloud | styleNoLabel );
DcNum = DcNum + (db < 3 ) * N * ( 20 - db );
DcDenom = DcDenom + ( db < 3 ) * ( 20 - db );
}
DC = DcNum / DcDenom;
/*
if( ParamToggle("Show Dom. Cycle?", "No|Yes" ) )
{
DomCycle = Median( DC, 10 );
// Plot( DomCycle, "Dominant Cycle", colorBlue );
}
if( TunedFilterDisplay )
{
DomCycle = Median( DC, 10 );
DomCycle = Max( DomCycle, 8 );
Value = 0;
for( bar = 10; bar < BarCount; bar++ )
{
beta = cos( 2 * PI / domCycle[ bar ] );
gamma = 1 / cos( 4 * PI * delta[ bar ] / DomCycle[ bar ] );
alpha = gamma - sqrt( gamma ^ 2 - 1 );
Value[ bar ] = 0.5 * ( 1 - alpha ) * SmoothHPDiff[ bar ] +
beta * ( 1 + alpha ) * Value[ bar - 1 ] -
alpha * Value[ bar - 2 ];
}
Value2 = ( domCycle / ( 2 * PI ) ) * ( Value - Ref( Value, -1 ) );
// Plot( Value, "Sine", colorBlue );
// Plot( Value2, "Cosine", colorWhite );
}
*/
GraphZOrder = 1;
//myc=IIf(Value2>Value,colorBlue,
// IIf(Value2<Value,colorRed,31));
// IIf(HaClose<smmaH AND HaClose>smmaL ,colorYellow,31)));
//Plot(6, "", myC, styleOwnScale| styleArea|styleNoLabel,-0.5,100);
_SECTION_END();
_SECTION_BEGIN("Matrix");
Smoother = Param("Trend Smoother", 5, 5, 20);
upcolor = ParamColor( "UpTrend Color", colorGreen );
Downcolor = ParamColor( "DownTrend Color", colorRed );
n=Smoother;
ys1=(High+Low+Close*2)/4;
rk3=EMA(ys1,n);
rk4=StDev(ys1,n);
rk5=(ys1-rk3)*200/rk4;
rk6=EMA(rk5,n);
MUP=EMA(rk6,n);
MDOWN=EMA(mup,n);
Oo=IIf(Mup<Mdown,Mup,Mdown);
Hh=Oo;
Ll=IIf(mup<mdown,mdown,mup);
Cc=Ll;
barcolor2=IIf(Ref(oo,-1)<Oo AND Cc<Ref(Cc,-1),upcolor,IIf(mup>mdown,upcolor,downcolor));
//PlotOHLC( Oo,hh,ll,Cc, "Matrix", barcolor2, styleCandle);
//Buy=Cross(up,OverSold);
//Sell=Cross(OverBought,up);
//PlotShapes (IIf(Buy, shapeSmallCircle, shapeNone) ,EntryColor, layer = 0, yposition = -220, offset = 1 );
//PlotShapes (IIf(Sell, shapeSmallCircle, shapeNone) ,ExitColor, layer = 0, yposition = 220, offset = 1 );
//Plot(6, "ribbon", barcolor2, styleOwnScale| styleArea|styleNoLabel,-0.5,100);
_SECTION_END();
Buy= C>O AND (LRSI(C,0.5)>0.5) AND Value2>Value AND mup>mdown AND (Cross(EMA(TSI,sig), TSIL)); //Buy Condition
Sell=(LRSI(C,0.5)<0.5);
Short = C<O AND (LRSI(C,0.5)<0.5) AND Value2<Value AND mup<mdown AND (Cross(TSIL, EMA(TSI,sig));//Sell Condition
Cover=(LRSI(C,0.5)>0.5);
Buy=ExRem(Buy,Sell);
Short=ExRem(Short,Cover);
Sell=ExRem(Sell,Buy);
Cover=ExRem(Cover,Short);
PlotShapes(Buy*shapeUpArrow,colorBlue,0,L,-35);
PlotShapes(Sell*shapeHollowDownArrow,colorRed,0,L,-60);
PlotShapes(Short*shapeDownArrow,colorRed,0,H,-35);
PlotShapes(Cover*shapeHollowUpArrow,colorBlue,0,H,-60);