Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Ichimoku Cloud with Trendline for Amibroker (AFL)

Rating:
3 / 5 (Votes 4)
Tags:
trendline, amibroker, ichimoku

It is an Ichimoku cloud indicator plus trendlines for better signals to buy or sell.

Similar Indicators / Formulas

Automatic Trend-line
Submitted by kaiji almost 15 years ago
Advanced Trend Lines
Submitted by Arun over 13 years ago
trend price
Submitted by ashokwins over 11 years ago
Ichimoku Cloud Charts
Submitted by Alok over 14 years ago
Trendline Multiple TimeFrame
Submitted by vargasc1 over 12 years ago
Price With Regression and Trend
Submitted by Boknoy9999 almost 13 years ago

Indicator / Formula

Copy & Paste Friendly
// E.M.Pottasch, 9/20/2010
// using fractals for trendlines
// Version 6a: 
// each trendline has 3 segments, see explanations 
// between code. Segments are not combined because 
// some trendlines partially overlap

SetBarsRequired(sbrAll,sbrAll);
xx = Cum(1);
nbar = Param("nbar",5,2,50,1); 

// define fractals
PHigh = H > Ref(HHV(H,nbar),-1) AND Ref(HHV(H,nbar),nbar) <= H;
PHighPrice = ValueWhen(PHigh,H);
PLow = L < Ref(LLV(L,nbar),-1) AND Ref(LLV(L,nbar),nbar) >= L;
PLowPrice = ValueWhen(PLow,L); 

// lower trendline, segment 1: between fractal points
startval_L = ValueWhen(PLow,L,1); 
endval_L = ValueWhen(PLow,L,0); 
startbar_L = ValueWhen(PLow,xx,1); 
endbar_L = ValueWhen(PLow,xx,0); 
aa_L = (endval_L-startval_L)/(endbar_L-startbar_L);
bb_L = startval_L;
trendline_L = aa_L * (xx - startbar_L) + bb_L;
// slope calculations for display purpose only
dtrendline_L = trendline_L - Ref(trendline_L,-1);

// lower extended trendline, segment 2: extend segment 1 to next fractals pivot 
startval_L_extend = ValueWhen(PLow,L,2); 
endval_L_extend = ValueWhen(PLow,L,1); 
startbar_L_extend = ValueWhen(PLow,xx,2); 
endbar_L_extend = ValueWhen(PLow,xx,1); 
aa_L_extend = (endval_L_extend-startval_L_extend)/(endbar_L_extend-startbar_L_extend);
bb_L_extend = startval_L;
trendline_L_extend = aa_L_extend * (xx - startbar_L) + endval_L_extend; 
// slope calculations for display purpose only
dtrendline_L_extend = trendline_L_extend - Ref(trendline_L_extend,-1);
dtrendline_L_extend = IIf(PLow,Ref(dtrendline_L,-1),dtrendline_L_extend);

// lower extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_L_extend2 = ValueWhen(PLow,L,3); 
endval_L_extend2 = ValueWhen(PLow,L,2); 
startbar_L_extend2 = ValueWhen(PLow,xx,3); 
endbar_L_extend2 = ValueWhen(PLow,xx,2); 
aa_L_extend2 = (endval_L_extend2-startval_L_extend2)/(endbar_L_extend2-startbar_L_extend2);
bb_L_extend2 = endval_L_extend2;
trendline_L_extend2 = aa_L_extend2 * (xx - endbar_L_extend2) + endval_L_extend2; 
// slope calculations for display purpose only
dtrendline_L_extend2 = trendline_L_extend2 - Ref(trendline_L_extend2,-1);
dtrendline_L_extend2 = IIf(PLow,Ref(dtrendline_L_extend,-1),dtrendline_L_extend2);

// upper trendline, segment 1: between fractal points
startval_H = ValueWhen(PHigh,H,1); 
endval_H = ValueWhen(PHigh,H,0); 
startbar_H = ValueWhen(PHigh,xx,1); 
endbar_H = ValueWhen(PHigh,xx,0); 
aa_H = (endval_H-startval_H)/(endbar_H-startbar_H);
bb_H = startval_H;
trendline_H = aa_H * (xx - startbar_H) + bb_H; 
// slope calculations for display purpose only
dtrendline_H = trendline_H - Ref(trendline_H,-1);

// upper extended trendline, segment 2: extend segment 1 to next fractals pivot 
startval_H_extend = ValueWhen(PHigh,H,2); 
endval_H_extend = ValueWhen(PHigh,H,1); 
startbar_H_extend = ValueWhen(PHigh,xx,2); 
endbar_H_extend = ValueWhen(PHigh,xx,1); 
aa_H_extend = (endval_H_extend-startval_H_extend)/(endbar_H_extend-startbar_H_extend);
bb_H_extend = startval_H;
trendline_H_extend = aa_H_extend * (xx - startbar_H) + endval_H_extend; 
// slope calculations for display purpose only
dtrendline_H_extend = trendline_H_extend - Ref(trendline_H_extend,-1);
dtrendline_H_extend = IIf(PHigh,Ref(dtrendline_H,-1),dtrendline_H_extend);

// upper extended trendline, segment 3: extend segment 2 nbars past the fractal pivot
startval_H_extend2 = ValueWhen(PHigh,H,3); 
endval_H_extend2 = ValueWhen(PHigh,H,2); 
startbar_H_extend2 = ValueWhen(PHigh,xx,3); 
endbar_H_extend2 = ValueWhen(PHigh,xx,2); 
aa_H_extend2 = (endval_H_extend2-startval_H_extend2)/(endbar_H_extend2-startbar_H_extend2);
bb_H_extend2 = endval_H_extend2;
trendline_H_extend2 = aa_H_extend2 * (xx - endbar_H_extend2) + endval_H_extend2; 
// slope calculations for display purpose only
dtrendline_H_extend2 = trendline_H_extend2 - Ref(trendline_H_extend2,-1);
dtrendline_H_extend2 = IIf(PHigh,Ref(dtrendline_H_extend,-1),dtrendline_H_extend2);

tld = ParamToggle("All trendlines","show|hide",1);
if (tld)
{
	// omit uptrending upper trendlines and downtrending lower trendlines
	trendline_L = IIf(dtrendline_L > 0,trendline_L,Null);
	trendline_L_extend = IIf(dtrendline_L_extend > 0,trendline_L_extend,Null);
	trendline_L_extend2 = IIf(dtrendline_L_extend2 > 0,trendline_L_extend2,Null);
	trendline_H = IIf(dtrendline_H < 0,trendline_H,Null);
	trendline_H_extend = IIf(dtrendline_H_extend < 0,trendline_H_extend,Null);
	trendline_H_extend2 = IIf(dtrendline_H_extend2 < 0,trendline_H_extend2,Null);
}

trendline_L_extend2 = IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null);
trendline_H_extend2 = IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null);

// chart
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
chartflag = ParamToggle("Heikin Ashi","show|hide",1);
if (chartFlag)
{
	Plot( C, "\nPrice",colorWhite, styleCandle );
} 
else
{
	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 ) ); 
	PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "Modified " + Name(), colorWhite, styleCandle );	
}

PlotShapes(shapeSmallCircle*PLow,colorGreen,0,L,-10);
PlotShapes(shapeSmallCircle*PHigh,colorRed,0,H,10);
// segment 1
Plot(trendline_L, "\nLower Trendline", colorBrightGreen,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend,Null), "", colorLightGrey,styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(Plow) > nbar,trendline_L_extend,Null), "",colorDarkGreen, styleNoLine | styleDots | styleThick);
// segment 3
Plot(IIf(BarsSince(Plow) <= nbar,trendline_L_extend2,Null), "",colorDarkGreen,styleNoLine | styleDots| styleThick);
// segment 1
Plot(trendline_H, "\nUpper Trendline", colorRed,styleLine);
// segment 2, grey dots unconfirmed trend, green confirmed trend
Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend,Null), "", colorLightGrey, styleNoLine | styleDots | styleThick);
Plot(IIf(BarsSince(PHigh) > nbar,trendline_H_extend,Null), "",colorOrange, styleNoLine | styleDots | styleThick);
// segment 3
Plot(IIf(BarsSince(PHigh) <= nbar,trendline_H_extend2,Null), "",colorOrange, styleNoLine | styleDots | styleThick);

Title = Name() + 
" | " + EncodeColor( colorYellow ) + "nbar: " + nbar + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorBrightGreen ) + "Lower Trendline: " + trendline_L + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorRed ) + "Upper Trendline: " + trendline_H + EncodeColor( colorWhite );

signalFlag = ParamToggle("Possible Signals","show|hide",1);
if (signalFlag)
{
// ***** possible buy and short signal *****
Buy = (!IsEmpty(trendline_H_extend) AND Cross(C,trendline_H_extend) AND BarsSince(PHigh) > nbar) OR 
(!IsEmpty(trendline_H_extend2) AND Cross(C,trendline_H_extend2) AND !PHigh) OR
(PHigh AND C > trendline_H_extend2 AND Ref(C,-1) < Ref(trendline_H_extend,-1) AND !IsEmpty(trendline_H_extend) AND !IsEmpty(trendline_H_extend2) );
BuyPrice = C;
Short = (!IsEmpty(trendline_L_extend) AND Cross(trendline_L_extend,C)  AND BarsSince(PLow) > nbar)  OR 
(!IsEmpty(trendline_L_extend2) AND Cross(trendline_L_extend2,C) AND !PLow) OR
(PLow AND C < trendline_L_extend2 AND Ref(C,-1) > Ref(trendline_L_extend,-1) AND !IsEmpty(trendline_L_extend) AND !IsEmpty(trendline_L_extend2)); 
ShortPrice = C;
Sell = 0;
Cover = 0;

PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeHollowUpArrow,shapeNone),colorWhite,0,L,-15);
PlotShapes(IIf(Buy,shapeHollowSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);

PlotShapes(IIf(Short,shapeDownTriangle,shapeNone),colorYellow,0,H,IIf(Short AND Sell,-30,-15));
PlotShapes(IIf(Short,shapeHollowDownTriangle,shapeNone),colorWhite,0,H,IIf(Short AND Sell,-30,-15));
PlotShapes(IIf(Short,shapeHollowCircle,shapeNone),colorWhite,0,ShortPrice,0);
}



_SECTION_BEGIN("Price");
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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();


_SECTION_BEGIN("Ichimoku Hayo Kinko");
GraphXSpace =1;
prds = Param("Standard Line Periods?", 12,5,26,1);
prds1 = Param("Turning Line Periods?", 3,3,10,1);
prds2 = Param("Delayed Line Periods?", 11,4,25,1);
prds3 = Param("Spans Periods?", 18,10,52,1);


TL = ( HHV( H, prds1) + LLV( L, prds1) )/2;
SL = ( HHV( H, prds) + LLV( L, prds) )/2;
DL = Ref( C, prds2);
Sp1 = Ref( ( SL + TL )/2, -prds2)-(C*0.003);
Sp2 = Ref( (HHV( H, prds3) + LLV(L, prds3))/2, -prds2)+(C*0.003);

STPL=(C+sp1+sp2)/3;
Plot (STPL,"STPL",ParamColor("STPL", colorBlack), styleThick);


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", colorBlack, styleCandle | styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

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 ) ) );
}
Buy = Cross(Close,IIf(sp1>sp2,sp1,sp2));
Sell = Cross(IIf(sp1<sp2,sp1,sp2),Close);
/* exrem is one method to remove surplus strade signals*/
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Filter = Buy OR Sell;

AddTextColumn( FullName(), "Company Name" );
AddColumn( Buy, "Buy", 1 );
AddColumn( Sell, "Sell", 1 );
AddColumn( C, "Close", 1.3 );
AddColumn( H, "High", 1.3 );
PlotOHLC (Sp1,Sp1,Sp2,Sp2,"Cloud",IIf(Sp1>Sp2,ParamColor("Span1 Color", ColorRGB(0,255,0)),ParamColor("Span2 Color",ColorRGB(255,104,32))),styleCloud);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

if( Status("action") == actionIndicator ) 
(
Title = EncodeColor(colorWhite)+ "NICK MA Swing System" + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
 "  - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+"  "+"Hi-"+H+"  "+"Lo-"+L+"  "+
"Cl-"+C+"  "+ "Vol= "+ WriteVal(V)+"\n"+ 
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+"  ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+"  ","")+"\n");



_SECTION_END(); 

2 comments

1. RaviNazre

Error in Line 8: Variable sbrall is used without having been initialized :(

2. Rakesh Kumar

Dear Ravi

There is no problem with the formula. It’s working fine.

Leave Comment

Please login here to leave a comment.

Back