TriggerLines

#51
Hi Kelvin,

But this AFL of Johny gives the delayed triggers. If we use LSMA alone, we get better triggers, of course, some whipsaws also will be there, but it is easy to avoid them.

-Anant
EASY TO AVOID WHIPSAWS.........probably a very important function in every traders life........can u please share your system of avoiding these whipsaws........cos there are many systems with good sucess rate when thers a proper rule to avoid whipsaws..........lets see how your rule to avoid whipsaws work.
 
#52
SetChartBkGradientFill( ParamColor( "TopColor", ColorRGB( 217, 197, 137 ) ), ParamColor( "BottomColor", ColorRGB( 254, 244, 224 ) ) );
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

_SECTION_BEGIN("SingleMA");

SetChartOptions(0, chartShowDates | chartWrapTitle);
Plot(C,"",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
kk = Optimize( "mult", Param( "mult", 2, 1, 6, 0.25 ), 1, 2, 0.25 );
Per = Optimize( "period", Param( "period", 10, 5, 50, 1 ), 5, 50, 1 );
sdfact = Param( "Standard Deviation Factor", 2, 0.5, 5, 0.1 );
offset = Param( "Offset", 2, 1, 50, 1 );
tc = ParamList( "Channel Display", List = "No Channel|Channel|ChannelRT|Both
Channels", 1 );
ms = ParamToggle( "Trend", "Regular|Smoothed", 1 );

x = Cum( 1 );
HaClose = ( O + H + L + C ) / 4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );

if ( ms == 0 )
{
nm = ( H - L );
j = ( O + H + L + C ) / 4;
}
else
{
nm = ( HaHigh - HaLow );
j = ( HaOpen + HaHigh + HaLow + HaClose ) / 4;
}

rfsctor = WMA( nm, Per );

revers = kk * rfsctor;
Trend = 1;
NW[0] = 0;

for ( i = 1;i < BarCount;i++ )
{
if ( Trend[i-1] == 1 )
{
if ( j < NW[i-1] )
{
Trend = -1;
NW = j + Revers;
}
else
{
Trend = 1;

if ( ( j - Revers ) > NW[i-1] )
{
NW = j - Revers;
}
else
{
NW = NW[i-1];
}
}
}

if ( Trend[i-1] == -1 )
{
if ( j > NW[i-1] )
{
Trend = 1;
NW = j - Revers;
}
else
{
Trend = -1;

if ( ( j + Revers ) < NW[i-1] )
{
NW = j + Revers;
}
else
{
NW = NW[i-1];
}
}
}
}

cp = ( H + L ) / 2;

Plot( IIf( NW < j, NW, Null ), "\ntrailLong", ParamColor( "ColorTrailLong",
colorBlue ), styleStaircase | styleDots );
Plot( IIf( NW > j, NW, Null ), "\ntrailShort", ParamColor( "ColorTrailShort",
colorRed ), styleStaircase | styleDots );

Buy = NW < j;
Short = NW > j;

Cover=Buy = ExRem(Buy, Short);
Sell=Short = ExRem(Short, Buy);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorBlue, 0,NW);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,NW);



_SECTION_END();

_SECTION_BEGIN("Small Triggers");
p1 = Param("TL 1 Periods", 55, 5, 50, 1);
p2 = Param("TL 2 Periods", 2, 3, 25, 1);
TL1 = LinearReg(C, p1);
TL2 = EMA(TL1, p2);
Col1 = IIf(TL1 > TL2, ParamColor("TL Up Colour", colorBrightGreen), ParamColor("TL Dn Colour", colorCustom12));
Plot(TL1, "TriggerLine 1", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL2, "TriggerLine 2", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();
 
#53
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{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 )) ));

trendup = IIf(MACD(12,26) > 0 AND MACD(12,26) > Signal(12,26,9), colorBlue, colorWhite);
trendcolor = IIf(MACD(12,26) < 0 AND MACD(12,26) < Signal(12,26,9), colorRed, trendup);
Plot( C, "Close", trendcolor, styleBar | styleThick );

//RSIup = RSI(7) > 70;
//RSIdown = RSI(7) < 30;

sp = Param( "RSI Period", 7, 1, 100 );
r = RSI( sp );
RSIup = r > 70;
RSIdown = r < 30;

shape = RSIup * shapeNone + RSIdown * shapeNone;
PlotShapes(shape, IIf( RSIup, colorBrightGreen, colorRed ), 0, IIf( RSIup, Low, High ) );

if( ParamToggle("Tooltip shows", "All Values | Only Prices" ) )
{
ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C, SelectedValue( ROC( C, 1 )));
}
_SECTION_END();

SetChartBkColor(ParamColor("Panel color ",colorBlack));

PlotOHLC( Open, High, Low, Close, "", colorLime, styleBar| styleThick );



_SECTION_BEGIN("trailstops");
EntrySignal = C > ( LLV( L, 20 ) + 2 * ATR( 10 ) );
ExitSignal = C < ( HHV( H, 20 ) - 2 * ATR( 10 ) );
Color = IIf( EntrySignal, colorBlue, IIf( ExitSignal, colorOrange, colorGrey50 ));
TrailStop = HHV( C - 2 * ATR(10), 15 );
ProfitTaker = EMA( H, 13 ) + 2 * ATR(10);

/* plot price chart and stops */
Plot( TrailStop, "Trailing stop", colorGold, styleThick | styleLine );
Plot( C, "Price", color, styleBar );

/* plot color ribbon */
Plot( 2, "", Color, styleArea | styleOwnScale | styleNoLabel, -0.1, 50 );


_SECTION_BEGIN("GFX EMA");
procedure Plotlinewidth(pvalue,ptitle,pcolor,pstyle,pmin,pmax,pxshift,plinewidth,pshowdate)
{
local pvalue,ptitle,pcolor,pstyle,pmin,pmax,pxshift,plinewidth,ppenstyle,pshowdate;
local Miny,Maxy; local Lvb,fvb; local pxwidth,pxheight; local TotalBars,axisarea; local i,x,y;
if(plinewidth>0 && Status("action") == 1 && (pstyle & styleLine==styleLine)){
GfxSetOverlayMode(0);
Miny=Status("axisminy"); Maxy=Status("axismaxy");
lvb=Status("lastvisiblebar"); fvb=Status("firstvisiblebar");
pxwidth=Status("pxwidth"); pxheight=Status("pxheight");
TotalBars=Lvb-fvb; xaxisarea=56;
if(pshowdate)yaxisarea=10;
else
yaxisarea=0; i=0;
x=5+i*(pxwidth-xaxisarea-10)/( TotalBars+1);
y=5+yaxisarea+(pvalue[i+fvb]-Miny)*(pxheight-yaxisarea-10)/(Maxy-Miny);
GfxMoveTo(x,pxheight - y);
for(i=1; i<TotalBars AND i<(BarCount-fvb); i++)
{
GfxSelectPen(pcolor[i + fvb],plinewidth,0);
x=5+i*(pxwidth-xaxisarea-10)/(TotalBars+1);
y=5+yaxisarea+(pvalue[i+fvb]-Miny)*(pxheight-yaxisarea-10)/(Maxy-Miny);
GfxLineTo(x,pxheight - y);
}}}
RequestTimedRefresh(2);
_SECTION_END();



_SECTION_BEGIN("Small Triggers");
p1 = Param("TL 1 Periods", 20, 5, 50, 1);
p2 = Param("TL 2 Periods", 5, 3, 25, 1);
TL1 = LinearReg(C, p1);
TL2 = EMA(TL1, p2);
Col1 = IIf(TL1 > TL2, ParamColor("TL Up Colour", colorBrightGreen), ParamColor("TL Dn Colour", colorCustom12));
Plot(TL1, "TriggerLine 1", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL2, "TriggerLine 2", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("Large Triggers");
p3 = Param("TL 3 Periods", 80, 5, 100, 1);
p4 = Param("TL 4 Periods", 20, 3, 100, 1);
TL3 = LinearReg(C, p3);
TL4 = EMA(TL3, p4);
Col1 = IIf(TL3 > TL4, ParamColor("TLL Up Colour", colorBlue), ParamColor("TLL Dn Colour", colorRed));
Plot(TL3, "TriggerLine 3", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL4, "TriggerLine 4", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("Fibo Retrace and Extensions");
fibs = ParamToggle("Plot Fibs","Off|On",1);
pctH = Param ("Pivot Hi %", 0.325,0.001,2.0,0.002);
HiLB = Param ("Hi LookBack",1,1,BarCount-1,1);
pctL = Param ("Pivot Lo %", 0.325,0.001,2.0,0.002);
LoLB = Param ("Lo LookBack",1,1,BarCount-1,1);
Back = Param ("Extend Left = 2",1,1,500,1);
Fwd = Param("Plot Forward", 0, 0, 500, 1);
text = ParamToggle("Plot Text","Off|On",1);
hts = Param ("Text Shift", -33.5,-50,50,0.10);
style =ParamStyle("Line Style",styleLine,styleNoLabel);
x = BarIndex();
pRp = PeakBars( H, pctH, 1) == 0;
yRp0 = SelectedValue(ValueWhen( pRp, H, HiLB));
xRp0 = SelectedValue(ValueWhen( pRp, x, HiLB));
pSp = TroughBars( L, pctL, 1) == 0;
ySp0 = SelectedValue(ValueWhen( pSp, L, LoLB));
xSp0 = SelectedValue(ValueWhen( pSp, x, LoLB));
Delta = yRp0 - ySp0;

function fib(ret)
{
retval = (Delta * ret);
Fibval = IIf(ret < 1.0
AND xSp0 < xRp0, yRp0 - retval, IIf(ret < 1.0
AND xSp0 > xRp0, ySp0 + retval,IIf(ret > 1.0
AND xSp0 < xRp0, yRp0 - retval, IIf(ret > 1.0
AND xSp0 > xRp0, ySp0 + retval, Null))));
return FibVal;
}

x0 = Min(xSp0,xRp0)-Back;
x1 = (BarCount -1);

r236 = fib(0.236); r236I = LastValue (r236,1);
r382 = fib(0.382); r382I = LastValue (r382,1);
r050 = fib(0.50); r050I = LastValue (r050,1);
r618 = fib(0.618); r618I = LastValue (r618,1);
r786 = fib(0.786); r786I = LastValue (r786,1);
e127 = fib(1.27); e127I = LastValue (e127,1);
e162 = fib(1.62); e162I = LastValue (e162,1);
e200 = fib(2.00); e200I = LastValue (e200,1);
e262 = fib(2.62); e262I = LastValue (e262,1);
e424 = fib(4.24); e424I = LastValue (e424,1);

p00 = IIf(xSp0 > xRp0,ySp0,yRp0); p00I = LastValue (p00,1);
p100 = IIf(xSp0 < xRp0,ySp0,yRp0); p100I = LastValue (p100,1);
color00 =IIf(xSp0 > xRp0,colorLime,colorRed);
color100 =IIf(xSp0 < xRp0,colorLime,colorRed);

numbars = LastValue(Cum(Status("barvisible")));
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);

if(fibs==1)
{
Plot(LineArray(xRp0-Fwd,yRp0,x1,yRp0,Back),"PR",32,8|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(xSp0-Fwd,ySp0,x1,ySp0,Back),"PS",27,8|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r236,x1,r236,Back),"",45,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r382,x1,r382,Back),"",44,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r050,x1,r050,Back),"",41,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r618,x1,r618,Back),"",43,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,r786,x1,r786,Back),"",42,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e127,x1,e127,Back),"e127",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e162,x1,e162,Back),"e162",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e200,x1,e200,Back),"p200",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e262,x1,e262,Back),"p262",47,style|styleNoRescale,Null, Null,Fwd);
Plot(LineArray(x0-Fwd,e424,x1,e424,Back),"p424",25,style|styleNoRescale,Null, Null,Fwd);
}

if(text==1)
{
PlotText(" 0% = " + WriteVal(p00,fraction), LastValue(BarIndex())-(numbars/hts), p00I + 0.05, color00);
PlotText("23% = " + WriteVal(r236,fraction), LastValue(BarIndex())-(numbars/hts), r236I + 0.05, 45);
PlotText("38% = " + WriteVal(r382,fraction), LastValue(BarIndex())-(numbars/hts), r382I + 0.05, 44);
PlotText("50% = " + WriteVal(r050,fraction), LastValue(BarIndex())-(numbars/hts), r050I + 0.05, 41);
PlotText("62% = " + WriteVal(r618,fraction), LastValue(BarIndex())-(numbars/hts), r618I + 0.05, 43);
PlotText("78% = " + WriteVal(r786,fraction), LastValue(BarIndex())-(numbars/hts), r786I + 0.05, 42);
PlotText("100% = " + WriteVal(p100,fraction), LastValue(BarIndex())-(numbars/hts),p100I + 0.05, color100);
PlotText("127% = " + WriteVal(e127,fraction), LastValue(BarIndex())-(numbars/hts),e127I + 0.05, 47);
PlotText("162% = " + WriteVal(e162,fraction), LastValue(BarIndex())-(numbars/hts),e162I + 0.05, 47);
PlotText("200% = " + WriteVal(e200,fraction), LastValue(BarIndex())-(numbars/hts),e200I + 0.05, 47);
PlotText("262% = " + WriteVal(e262,fraction), LastValue(BarIndex())-(numbars/hts),e262I + 0.05, 47);
PlotText("424% = " + WriteVal(e424,fraction), LastValue(BarIndex())-(numbars/hts),e424I + 0.05, 25);
}
_SECTION_END();

/* **********************************

Code to automatically identify pivots

********************************** */

// -- what will be our lookback range for the hh and ll?
farback=Param("How Far back to go",100,50,5000,10);
nBars = Param("Number of bars", 12, 5, 40);

// -- Title.

Title = Name() + " (" + StrLeft(FullName(), 15) + ") O: " + Open + ",

H: " + High + ", L: " + Low + ", C: " + Close;

// -- Plot basic candle chart

PlotOHLC(Open, High, Low, Close,

"BIdx = " + BarIndex() +

"\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L = " + L

+ "\n"+"C ",

colorBlack, styleBar);

GraphXSpace=7;

// -- Create 0-initialized arrays the size of barcount

aHPivs = H - H;

aLPivs = L - L;

// -- More for future use, not necessary for basic plotting

aHPivHighs = H - H;

aLPivLows = L - L;

aHPivIdxs = H - H;

aLPivIdxs = L - L;

nHPivs = 0;

nLPivs = 0;

lastHPIdx = 0;

lastLPIdx = 0;

lastHPH = 0;

lastLPL = 0;

curPivBarIdx = 0;

// -- looking back from the current bar, how many bars

// back were the hhv and llv values of the previous

// n bars, etc.?

aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);

// -- Would like to set this up so pivots are calculated back from

// last visible bar to make it easy to "go back" and see the pivots

// this code would find. However, the first instance of

// _Trace output will show a value of 0

aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);

// -- Initialize value of curTrend

curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}

// -- Loop through bars. Search for

// entirely array-based approach

// in future version

for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;

// -- Have we identified a pivot? If trend is down...

if (aLLVBars[curBar] < aHHVBars[curBar]) {

// ... and had been up, this is a trend change

if (curTrend == "U") {

curTrend = "D";

// -- Capture pivot information

curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}

// -- or current trend is up

} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

}

// -- loop through bars

}

// -- Basic attempt to add a pivot this logic may have missed

// -- OK, now I want to look at last two pivots. If the most

// recent low pivot is after the last high, I could

// still have a high pivot that I didn't catch

// -- Start at last bar

curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {

// -- Bar and price info for candidate pivot

candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar];

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aHPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nHPivs; j++) {

aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-

(j+1)];

aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];

}

aHPivHighs[0] = candPrc ;

aHPivIdxs[0] = candIdx;

nHPivs++;

}

} else {


// -- Bar and price info for candidate pivot

candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar];

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aLPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nLPivs; j++) {

aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];

aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];

}

aLPivLows[0] = candPrc;

aLPivIdxs[0] = candIdx;

nLPivs++;

}

}

// -- Dump inventory of high pivots for debugging

/*

for (k=0; k<nHPivs; k++) {

_TRACE("High pivot no. " + k

+ " at barindex: " + aHPivIdxs[k] + ", "

+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],

DateTime(), 1), formatDateTime)

+ ", " + aHPivHighs[k]);

}

*/

// -- OK, let's plot the pivots using arrows

PlotShapes(

IIf(aHPivs==1, shapeHollowDownArrow, shapeNone), colorRed, 0,

High, Offset=-15);

PlotShapes(

IIf(aLPivs==1, shapeHollowUpArrow , shapeNone), colorBrightGreen, 0,

Low, Offset=-15);
_SECTION_END();

_SECTION_BEGIN("Dots Color");
acc = Param("Acceleration", 0.01, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.1, 0, 1, 0.001 );
myColor = IIf (C > SAR( acc, accm ) , colorTurquoise , colorRed);
Plot( SAR( acc, accm ), _DEFAULT_NAME(), myColor , ParamStyle("Style", styleDots |styleThick | styleNoLine, maskDefault | styleDots | styleThick | styleNoLine ) );
_SECTION_END();

SetChartBkGradientFill( ParamColor("BgTop", ColorRGB( 172,172,172 )),

ParamColor("BgBottom", ColorRGB( 172,172,172 )),ParamColor("titleblock",ColorRGB( 172,172,172 )));
 
#54
Re: TriggerLines plus two stops

SetChartBkGradientFill( ParamColor( "TopColor", ColorRGB( 217, 197, 137 ) ), ParamColor( "BottomColor", ColorRGB( 254, 244, 224 ) ) );
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

_SECTION_BEGIN("SingleMA");

SetChartOptions(0, chartShowDates | chartWrapTitle);
Plot(C,"",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
kk = Optimize( "mult", Param( "mult", 2, 1, 6, 0.25 ), 1, 2, 0.25 );
Per = Optimize( "period", Param( "period", 10, 5, 50, 1 ), 5, 50, 1 );
sdfact = Param( "Standard Deviation Factor", 2, 0.5, 5, 0.1 );
offset = Param( "Offset", 2, 1, 50, 1 );
tc = ParamList( "Channel Display", List = "No Channel|Channel|ChannelRT|Both
Channels", 1 );
ms = ParamToggle( "Trend", "Regular|Smoothed", 1 );

x = Cum( 1 );
HaClose = ( O + H + L + C ) / 4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );

if ( ms == 0 )
{
nm = ( H - L );
j = ( O + H + L + C ) / 4;
}
else
{
nm = ( HaHigh - HaLow );
j = ( HaOpen + HaHigh + HaLow + HaClose ) / 4;
}

rfsctor = WMA( nm, Per );

revers = kk * rfsctor;
Trend = 1;
NW[0] = 0;

for ( i = 1;i < BarCount;i++ )
{
if ( Trend[i-1] == 1 )
{
if ( j < NW[i-1] )
{
Trend = -1;
NW = j + Revers;
}
else
{
Trend = 1;

if ( ( j - Revers ) > NW[i-1] )
{
NW = j - Revers;
}
else
{
NW = NW[i-1];
}
}
}

if ( Trend[i-1] == -1 )
{
if ( j > NW[i-1] )
{
Trend = 1;
NW = j - Revers;
}
else
{
Trend = -1;

if ( ( j + Revers ) < NW[i-1] )
{
NW = j + Revers;
}
else
{
NW = NW[i-1];
}
}
}
}

cp = ( H + L ) / 2;

Plot( IIf( NW < j, NW, Null ), "\ntrailLong", ParamColor( "ColorTrailLong",
colorBlue ), styleStaircase | styleDots );
Plot( IIf( NW > j, NW, Null ), "\ntrailShort", ParamColor( "ColorTrailShort",
colorRed ), styleStaircase | styleDots );

Buy = NW < j;
Short = NW > j;

Cover=Buy = ExRem(Buy, Short);
Sell=Short = ExRem(Short, Buy);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorBlue, 0,NW);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,NW);



_SECTION_END();

_SECTION_BEGIN("Small Triggers");
p1 = Param("TL 1 Periods", 55, 5, 50, 1);
p2 = Param("TL 2 Periods", 2, 3, 25, 1);
TL1 = LinearReg(C, p1);
TL2 = EMA(TL1, p2);
Col1 = IIf(TL1 > TL2, ParamColor("TL Up Colour", colorBrightGreen), ParamColor("TL Dn Colour", colorCustom12));
Plot(TL1, "TriggerLine 1", Col1, styleLine|styleThick|styleNoLabel);
Plot(TL2, "TriggerLine 2", Col1, styleLine|styleThick|styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("SmoothHeikin MA Swing");
SetBarsRequired(200,0);
GraphXSpace = 5;
SetChartOptions(0,chartShowArrows|chartShowDates);
k = Param("K", 1.5, 1, 5, 0.1);
Per = Param("ATR", 3, 1, 30, 0.50);
k1 = Optimize("K", 1, 0.1, 5, 0.1);
Per1 = Optimize("ATR", 3, 1, 30, 0.50);
HACLOSE=(O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
j=Haclose;
//=======================================================================================================================
//=========================Indicator==============================================================================================
f=ATR(14);

rfsctor = WMA(H-L, Per);

revers = k * rfsctor;

Trend = 1;
NW[0] = 0;


for(i = 1; i < BarCount; i++)
{
if(Trend[i-1] == 1)
{
if(j < NW[i-1])
{
Trend = -1;
NW = j + Revers;
}
else
{
Trend = 1;
if((j - Revers) > NW[i-1])
{
NW = j - Revers;
}
else
{
NW = NW[i-1];
}
}
}
if(Trend[i-1] == -1)
{
if(j > NW[i-1])
{
Trend = 1;
NW = j - Revers;
}
else
{
Trend = -1;
if((j + Revers) < NW[i-1])
{
NW = j + Revers;
}
else
{
NW = NW[i-1];
}
}
}
}

//===============system================
Plot(NW, "", IIf(Trend == 1, 3, 25), 1);

Buy=Cover=Cross(j,nw);
Sell=Short=Cross(nw,j);
SWING =WriteIf(j > nw, "UPTREND","DOWNTREND");
GfxSetTextColor( colorLightBlue );
GfxSelectFont("Arial", 8 );
shape = Buy * shapeCircle + Sell * shapeCircle;
_SECTION_END();

_SECTION_BEGIN( "KBrain" );
//========================Initiation================ ========
bts = ParamToggle( "BrainTrend1 signal", "No|Yes" , 1 );
btst = ParamToggle( "BrainTrend1 stop", "No|Yes" , 1 );
btsl = ParamToggle( "BrainTrend1 stop line", "No|Yes" , 1 );
period = Param( "Period", 14, 6, 20, 1 );
x1 = 53;
x2 = 47;
d = 2.3;
f = 7;
s = 1.5;
range = ATR( f );
Range1 = ATR( f ) / d;
Range2 = ( ATR( f ) * s ) / 4;
range3 = ATR( 10 );
R = ( ( HHV( H, period ) - C ) / ( HHV ( H, period ) - LLV ( L, period ) ) ) * -100;
EMA1 = EMA( R, Period );
EMA2 = EMA( EMA1, 5 );
Difference = EMA1 - EMA2;
ZeroLagEMA = EMA1 + Difference;
value2 = abs( ZeroLagEMA );


function PercentR( periods )
{
return -100 * ( HHV( H, periods ) - C ) / ( HHV( H, periods ) - LLV( L, periods ) );
}


for ( i = 1; i < Period - 1; i++ )
{


tm = 1;
bstop = 1;
sstop = 1;
p = 1;
}

for ( i = period + 10; i < BarCount; i++ )
{
C[0] = 0;
Value2[0] = 0;
p[0] = 0;
Plot1[0] = 0;
Plot2[0] = 0;
Val1 = 0;
Val2 = 0;
temp[0] = 0;
Value3[0] = 0;
tm[0] = 0;
p1 = 0;
bt1[0] = 0;
bt2[0] = 0;
r[0] = 0;
bt1a[0] = 0;
bt2a[0] = 0;
istop[0] = Val1[0];
stop = Val1[0];
bstop[0] = 0;
sstop = 0;
//==================================Indicators====== ======================
{
if ( value2 < x2 AND abs( Close - Close[i-2] ) > range1 )
p = 1 ;
else
{
if ( value2 > x1 AND abs( Close - Close[i-2] ) > Range1 )
p = 2 ;

else

p = 0;
}
}

if ( ( value2 < x2 AND p == 1 ) OR ( value2 < x2 AND p == 0 ) )

{
if ( abs( Close - Close[i-2] ) > Range1 )
{
Plot1 = H;
Plot2 = L;
}
else
{
Plot1 = Plot1[i-1];
Plot2 = Plot2[i-1];
}
}
else
{
if ( ( value2 > x1 AND p == 2 ) OR ( value2 > x1 AND p == 0 ) )

{
Plot1 = L;
Plot2 = H;
}
else
{
Plot1 = Plot1[i-1];
Plot2 = Plot2[i-1];
}
}

//==================KBrain Signal ====================================
{
if ( value2 < x2 AND ( abs( Close - Close[i-2] ) > Range1 ) )
{
if ( p == 1 OR p == 0 )
Value3 = L - range3;

val1 = Value3;

p = 1;

temp = 1;
}
else
{
temp = temp[i-1];
}

{
if ( value2 > x1 AND ( abs( Close - Close[i-2] ) > Range1 ) )
{
if ( p == 2 OR p == 0 )
Value3 = H + range3;

val2 = Value3;

p = 2;

temp = 2;;
}

}
}

{

if ( temp == 1 AND Plot1 > 0 AND tm != 1 )
tm = 1;

if ( temp == 2 AND Plot2 > 0 AND tm != 2 )
tm = 2;

}

//===============================stop=============== =========================
{
if ( value2 < x2 AND ( abs( Close - Close[i-2] ) > Range1 )AND p != 2 )

{
value3 = L - range3;
va1 = Value3;
p1 = 2;
r = Va1;
bstop = Va1;
bt2 = bt2[i-1];
}

if ( value2 > x1 AND ( abs( Close - Close[i-2] ) > Range1 )AND p != 1 )

{
Value3 = H + range3;
va2 = Value3;
p1 = 1;

r = Va2;
sstop = Value3;
bt1 = bt1[i-1];
}

}



if ( val1 == 0 AND val2 == 0 AND p == 0 )
{
bstop = bstop[i-1];
sstop = sstop[i-1];
}


if ( bstop < bstop[i-1] AND tm == 1 AND tm[i-1] == 1 )
bstop = bstop[i-1];

if ( sstop > sstop[i-1] AND tm == 2 AND tm[i-1] == 2 )
sstop = sstop[i-1];

}

//=============================SYSTEM=============== =======================
Mycolor = IIf( p == 1, colorLime, IIf( p == 2, colorRed, colorBlue ) );

PlotShapes( shapeCircle* ( bts AND tm == 1 AND Ref( tm, -1 ) == 2 ), colorTurquoise, 0, bstop, 0 );

PlotShapes( shapeCircle* ( bts AND tm == 2 AND Ref( tm, -1 ) == 1 ), colorCustom12, 0, sstop, 0 );

PlotShapes( IIf( btst AND p == 1, shapeSmallCircle, Null ), colorTurquoise, 0, bstop, 0 );

Plot( IIf( btsl AND tm == 1, bstop, Null ), "", colorPaleBlue, 1 );

PlotShapes( IIf( btst AND p == 2, shapeSmallCircle, Null ), colorCustom12, 0, sstop, 0 );

Plot( IIf( btsl AND tm == 2, sstop, Null ), "", colorLightYellow, 1 );

Buy = Cover = ( bts AND tm == 1 AND Ref( tm, -1 ) == 2 );

Sell = Short = ( bts AND tm == 2 AND Ref( tm, -1 ) == 1 );

SellPrice = ValueWhen( Sell, C, 1 );

BuyPrice = ValueWhen( Buy, C, 1 );

Long = Flip( Buy, Sell );

Shrt = Flip( Sell, Buy );

_SECTION_END();