Simple Coding Help - No Promise.

Try this

Code:
UP = C > tdstba*1.05 OR C < tdstba*0.95;
DN = C > tdstsa*1.05 OR C < tdstsa*0.95;
BUY  = UP AND NOT DN;
SELL = DN AND NOT UP;
Filter =  Buy OR Sell;
AddColumn(Writeif(Buy, "TDST Buy", TDST Sell", "Signal", 1.2, IIF(Buy,colorGreen,colorRed));
AddColumn(Close, "close", 1.2, colorBlack);

In your code/logic there are many instances when both the conditions are true,
In the above code snippet, i have modified the code/logic to eliminate that . . .
Before using it, please verify if the integrity of you logic is not compromised by this change



EDIT 2 or else try using

Code:
BUY  = C > tdstba*1.05 OR C < tdstba*0.95;
SELL = C > tdstsa*1.05 OR C < tdstsa*0.95;
Filter =  Buy OR Sell;
AddColumn(Writeif(Buy,  "TDST Buy",  "NA", "Signal", 1.2, colorGreen);
AddColumn(Writeif(Sell, "TDST Sell", "NA", "Signal", 1.2, colorRed);
AddColumn(Close, "Close", 1.2, colorBlack);



EDIT 3,

not really satisfied with either of the code above,
so giving a version 3 . . .


Code:
CON2 = C > tdstba*1.05 OR C > tdstsa*1.05;
CON1 = C < tdstba*0.95 OR C < tdstsa*0.95;
Filter =  CON1 OR CON2;
AddColumn(Writeif(CON1, "TDST Buy", "NA", "Signal", 1.2, colorGreen);
AddColumn(Writeif(CON2, "TDST Sell", "NA", "Signal", 1.2, colorRed);
AddColumn(Close, "Close", 1.2, colorBlack);

I don't know your logic or not tested for errors . . .
so please remove/correct any logical or syntax errors etc that maybe there in the code



:) Happy




with your guidance i was able to come up with this code

Condition1= C>tdstba*1.05 OR C<tdstba*0.95;
Condition2= C>tdstsa*1.05 OR C<tdstsa*0.95;

Filter= Condition1 OR Condition2;

AddColumn( Condition1, "tdst buy", 1.2, colorBlack);
AddColumn( Condition2, "tdst sell", 1.2, colorBlack);


AddColumn(C, "close", 1.2, colorBlack);

result of exploration using the above code is:



the tdst buy and tdst sell column (bolded for reference in the above code) shows 1 and 0

can u tweak this to make the exploration to show actual price instead of "1" or "0"

the exploration shows stock that do not fit into the criteria denoted by 1

as against the exploration shows stock that fit into the criteria denoted by 0

how can i filter out these "1" stock..??

i hope my question is not confusing you.

thanks!!!
 
Last edited:

Nehal_s143

Well-Known Member
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);
 

johnnypareek

Well-Known Member
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);


value and value2 variable used without intialized
 
Try this

Code:
UP = C > tdstba*1.05 OR C < tdstba*0.95;
DN = C > tdstsa*1.05 OR C < tdstsa*0.95;
BUY  = UP AND NOT DN;
SELL = DN AND NOT UP;
Filter =  Buy OR Sell;
AddColumn(Writeif(Buy, "TDST Buy", TDST Sell", "Signal", 1.2, IIF(Buy,colorGreen,colorRed));
AddColumn(Close, "close", 1.2, colorBlack);

In your code/logic there are many instances when both the conditions are true,
In the above code snippet, i have modified the code/logic to eliminate that . . .
Before using it, please verify if the integrity of you logic is not compromised by this change



EDIT 2 or else try using

Code:
BUY  = C > tdstba*1.05 OR C < tdstba*0.95;
SELL = C > tdstsa*1.05 OR C < tdstsa*0.95;
Filter =  Buy OR Sell;
AddColumn(Writeif(Buy,  "TDST Buy",  "NA", "Signal", 1.2, colorGreen);
AddColumn(Writeif(Sell, "TDST Sell", "NA", "Signal", 1.2, colorRed);
AddColumn(Close, "Close", 1.2, colorBlack);



EDIT 3,

not really satisfied with either of the code above,
so giving a version 3 . . .


Code:
CON2 = C > tdstba*1.05 OR C > tdstsa*1.05;
CON1 = C < tdstba*0.95 OR C < tdstsa*0.95;
Filter =  CON1 OR CON2;
AddColumn(Writeif(CON1, "TDST Buy", "NA", "Signal", 1.2, colorGreen);
AddColumn(Writeif(CON2, "TDST Sell", "NA", "Signal", 1.2, colorRed);
AddColumn(Close, "Close", 1.2, colorBlack);

I don't know your logic or not tested for errors . . .
so please remove/correct any logical or syntax errors etc that maybe there in the code



:) Happy
this is what i came up with using your codes

and it meets my criterias

UP = C > tdstba*1.05 OR C < tdstba*0.95;
DN = C > tdstsa*1.05 OR C < tdstsa*0.95;
Buy = UP AND NOT DN;
Sell = DN AND NOT UP;
Filter = Buy OR Sell;
AddTextColumn(WriteIf(Sell, "TDST Buy", "TDST Sell"), "Signal", 1.2, IIf(Sell,colorGreen,colorRed));
AddColumn(IIf(Sell, tdstba, tdstsa), "value");
AddColumn(Close, "close", 1.2, colorBlack);


thanks singhji...

Regards CA. Ritesh Bafna
 
the addcolumn does not seems to work with writeif

did some google... found out addtextcolumn works with writeif

and addcolumn works with IIF

i dont know how true is it but this helped the code to work.

Regards
 

Nehal_s143

Well-Known Member
value and value2 variable used without intialized
problem is in this portion, if toggle is removed problem will be solved, due to toggle, instead of chart values heat map is getting activated, i dont know how to remove toggle condition...

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 );
}
 

XRAY27

Well-Known Member
Please try this, let me know if this is what you want

_SECTION_BEGIN("Candle Identification");
Plot(C,"",1,64);
O1 = Ref(O,-1);O2 = Ref(O,-2);
H1 = Ref(H,-1);H2 = Ref(H,-2);
L1 = Ref(L,-1);L2 = Ref(L,-2);
C1 = Ref(C,-1);C2 = Ref(C,-2);
function CandlePattern(P)
{
global PatternName;
if(P == 0) { PatternName = "NearDoji"; Pv = (abs(O-C)<= ((H-L)*0.1)); }
else if(P == 1) { PatternName = "BlackCandle"; Pv = (O>C); }
else if(P == 2) { PatternName = "LongBlackCandle"; Pv = (O>C AND
(O-C)/(.001+H-L)>.6); }
else if(P == 3) { PatternName = "SmallBlackCandle"; Pv = ((O>C) AND
((H-L)>(3*(O-C)))); }
else if(P == 4) { PatternName = "WhiteCandle"; Pv = (C>O); }
else if(P == 5) { PatternName = "LongWhiteCandle"; Pv = ((C>O) AND
((C-O)/(.001+H-L)>.6)); }
else if(P == 6) { PatternName = "SmallWhiteCandle"; Pv = ((C>O) AND
((H-L)>(3*(C-O)))); }
else if(P == 7) { PatternName = "BlackMaubozu"; Pv = (O>C AND H==O AND
C==L); }
else if(P == 8) { PatternName = "WhiteMaubozu"; Pv = (C>O AND H==C AND
O==L); }
else if(P == 9) { PatternName = "BlackClosingMarubozu"; Pv = (O>C AND
C==L); }
else if(P == 10) { PatternName = "WhiteClosingMarubozu"; Pv = (C>O AND
C==H); }
else if(P == 11) { PatternName = "BlackOpeningMarubozu"; Pv = (O>C AND
O==H); }
else if(P == 12) { PatternName = "WhiteOpeningMarubozu"; Pv = (C>O AND
O==L); }
else if(P == 13) { PatternName = "HangingMan"; Pv = (((H-L)>4*(O-C)) AND
((C-L)/(.001+H-L)>= 0.75) AND ((O-L)/(.001+H-L)>= 0.75)); }
else if(P == 14) { PatternName = "Hammer"; Pv = (((H-L)>3*(O-C)) AND
((C-L)/(.001+H-L)>0.6) AND ((O-L)/(.001+H-L)>0.6)); }
else if(P == 15) { PatternName = "InvertedHammer"; Pv = (((H-L)>3*(O-C))
AND ((H-C)/(.001+H-L)>0.6) AND ((H-O)/(.001+H-L)>0.6)); }
else if(P == 16) { PatternName = "ShootingStar"; Pv = (((H-L)>4*(O-C))
AND ((H-C)/(.001+H-L)>= 0.75) AND ((H-O)/(.001+H-L)>= 0.75)); }
else if(P == 17) { PatternName = "BlackSpinningTop"; Pv = ((O>C) AND
((H-L)>(3*(O-C))) AND (((H-O)/(.001+H-L))<.4) AND
(((C-L)/(.001+H-L))<.4)); }
else if(P == 18) { PatternName = "WhiteSpinningTop"; Pv = ((C>O) AND
((H-L)>(3*(C-O))) AND (((H-C)/(.001+H-L))<.4) AND
(((O-L)/(.001+H-L))<.4)); }
else if(P == 19) { PatternName = "BearishAbandonedBaby"; Pv = ((C1 == O1)
AND (C2>O2) AND (O>C) AND (L1>H2) AND (L1>H)); }
else if(P == 20) { PatternName = "BearishEveningDojiStar"; Pv = ((C2>O2)
AND ((C2-O2)/(.001+H2-L2)>.6) AND (C2<O1) AND (C1>O1) AND
((H1-L1)>(3*(C1-O1))) AND (O>C) AND (O<O1)); }
else if(P == 21) { PatternName = "DarkCloudCover"; Pv = (C1>O1 AND
((C1+O1)/2)>C AND O>C AND O>C1 AND C>O1 AND (O-C)/(.001+(H-L)>0.6)); }
else if(P == 22) { PatternName = "BearishEngulfing"; Pv = ((C1>O1) AND
(O>C) AND (O>= C1) AND (O1>= C) AND ((O-C)>(C1-O1))); }
else if(P == 23) { PatternName = "ThreeOutsideDownPattern"; Pv = ((C2>O2)
AND (O1>C1) AND (O1>= C2) AND (O2>= C1) AND ((O1-C1)>(C2-O2)) AND (O>C) AND
(C<C1)); }
else if(P == 24) { PatternName = "BullishAbandonedBaby"; Pv = ((C1 == O1)
AND (O2>C2) AND (C>O) AND (L2>H1) AND (L>H1)); }
else if(P == 25) { PatternName = "BullishMorningDojiStar"; Pv = ((O2>C2)
AND ((O2-C2)/(.001+H2-L2)>.6) AND (C2>O1) AND (O1>C1) AND
((H1-L1)>(3*(C1-O1))) AND (C>O) AND (O>O1)); }
else if(P == 26) { PatternName = "BullishEngulfing"; Pv = ((O1>C1) AND
(C>O) AND (C>= O1) AND (C1>= O) AND ((C-O)>(O1-C1))); }
else if(P == 27) { PatternName = "ThreeOutsideUpPattern"; Pv = ((O2>C2)
AND (C1>O1) AND (C1>= O2) AND (C2>= O1) AND ((C1-O1)>(O2-C2)) AND (C>O) AND
(C>C1)); }
else if(P == 28) { PatternName = "BullishHarami"; Pv = ((O1>C1) AND (C>O)
AND (C<= O1) AND (C1<= O) AND ((C-O)<(O1-C1))); }
else if(P == 29) { PatternName = "ThreeInsideUpPattern"; Pv = ((O2>C2)
AND (C1>O1) AND (C1<= O2) AND (C2<= O1) AND ((C1-O1)<(O2-C2)) AND (C>O) AND
(C>C1) AND (O>O1)); }
else if(P == 30) { PatternName = "PiercingLine"; Pv = ((C1<O1) AND
(((O1+C1)/2)<C) AND (O<C) AND (O<C1) AND (C<O1) AND
((C-O)/(.001+(H-L))>0.6)); }
else if(P == 31) { PatternName = "BearishHarami"; Pv = ((C1>O1) AND (O>C)
AND (O<= C1) AND (O1<= C) AND ((O-C)<(C1-O1))); }
else if(P == 32) { PatternName = "ThreeInsideDownPattern"; Pv = ((C2>O2)
AND (O1>C1) AND (O1<= C2) AND (O2<= C1) AND ((O1-C1)<(C2-O2)) AND (O>C) AND
(C<C1) AND (O<O1)); }
else if(P == 33) { PatternName = "ThreeWhiteSoldiers"; Pv = (C>O*1.01)
AND (C1>O1*1.01) AND (C2>O2*1.01) AND (C>C1) AND (C1>C2) AND (O<C1) AND
(O>O1) AND (O1<C2) AND (O1>O2) AND (((H-C)/(H-L))<.2) AND
(((H1-C1)/(H1-L1))<.2) AND (((H2-C2)/(H2-L2))<.2); }
else if(P == 34) { PatternName = "DarkCloudCover"; Pv = (C1>O1*1.01) AND
(O>C) AND (O>H1) AND (C>O1) AND (((C1+O1)/2)>C) AND (C>O1) AND
(MA(C,13)-Ref(MA(C,13),-4)>0); }
else if(P == 35) { PatternName = "ThreeBlackCrows"; Pv = (O>C*1.01) AND
(O1>C1*1.01) AND (O2>C2*1.01) AND (C<C1) AND (C1<C2) AND (O>C1) AND (O<O1)
AND (O1>C2) AND (O1<O2) AND (((C-L)/(H-L))<.2) AND (((C1-L1)/(H1-L1))<.2)
AND (((C2-L2)/(H2-L2))<.2); }
else if(P == 36) { PatternName = "doji"; Pv = (O == C); }
else if(P == 37) { PatternName = "GapUp"; Pv = GapUp(); }
else if(P == 38) { PatternName = "GapDown"; Pv = GapDown(); }
else if(P == 39) { PatternName = "BigGapUp"; Pv = L>1.01*H1; }
else if(P == 40) { PatternName = "BigGapDown"; Pv = H<0.99*L1; }
else if(P == 41) { PatternName = "HugeGapUp"; Pv = L>1.02*H1; }
else if(P == 42) { PatternName = "HugeGapDown"; Pv = H<0.98*L1; }
else if(P == 43) { PatternName = "DoubleGapUp"; Pv = GapUp() AND
Ref(GapUp(),-1); }
else if(P == 44) { PatternName = "DoubleGapDown"; Pv = GapDown() AND
Ref(GapDown(),-1); }
return Pv;
}

PatternNameList = "";
for(Cp=0; Cp<=44; Cp++)
{
VarSet("Pattern"+NumToStr(Cp,1.0),CandlePattern(cP));
PatternNameList = PatternNameList +PatternName+",";
}

BI = BarIndex();
SelectedBar = SelectedValue(BI) -BI[0];
//Selectedbar = Status("lastvisiblebar")-1;
PStr="";
for(Cp=0; Cp<=44; Cp++)
{
Temp = VarGet("Pattern"+NumToStr(Cp,1.0));
if(temp[SelectedBar]) Pstr=Pstr+"#"+NumToStr(Cp,1.0)+" -
"+StrExtract(PatternNameList,Cp)+"n";
}

Title = "nCandle Demostration n"+ Pstr;
_SECTION_END();
nehal ji
just i want only following
bullish/bearish engulfing
dark cloud cover
piercing pattern
morning and evening star...
any how thanks for the afl
 
the addcolumn does not seems to work with writeif

did some google... found out addtextcolumn works with writeif

and addcolumn works with IIF

i dont know how true is it but this helped the code to work.

Regards
Sorry Ritesh

That was a blooper on my part . . . that's due to not testing the code on Ami and directly typing it here . . . my apologies . . .


this is what i came up with using your codes
.......

and it meets my criterias
That's what is satisfying :)
end of day if you get what you are looking for :thumb:


seems in my above codes I have also inverted the logic for buy/sell :eek:


Code:
DN = C > tdstba*1.05 OR C < tdstba*0.95;
UP = C > tdstsa*1.05 OR C < tdstsa*0.95;
Buy  = UP AND NOT DN;
Sell = DN AND NOT UP;
Filter =  Buy OR Sell;
AddTextColumn(WriteIf(Buy, "TDST Buy", "TDST Sell"), "Signal", 1.2, IIf(Buy,colorGreen,colorRed));
AddColumn(IIf(Buy, tdstba, tdstsa), "value");
AddColumn(Close, "close", 1.2, colorBlack);

:) Happy
 
nehal ji
just i want only following
bullish/bearish engulfing
dark cloud cover
piercing pattern
morning and evening star...
any how thanks for the afl
Delete or comment out the code for rest of patterns . . .


:) Happy
 

Nehal_s143

Well-Known Member
nehal ji
just i want only following
bullish/bearish engulfing
dark cloud cover
piercing pattern
morning and evening star...
any how thanks for the afl
I dont have for all patters, i had afl for bullish/bearish engulfing, search in laptop, could not relocate, will try to search for engulfing pattern afl.
 

Similar threads