// Downloaded From https://www.WiseStockTrader.com
/* Fibonacci Internal & External Retracements */
// This is a Modified Version that shows Fibonacci 00% & 100% instead of WRONG Sup & Res

_SECTION_BEGIN("Background");
	SetChartOptions(0,chartShowArrows|chartShowDates);
	SetChartBkColor(ParamColor("Outer panel",colorTan)); // color of outer border 
	SetChartBkGradientFill( ParamColor("Inner panel upper",colorBlack),ParamColor("Inner panel lower",colorBlack));
	tchoice=Param("Title Selection ",2,1,2,1);

Plot(C, "", IIf(O>=C, colorRed, colorGreen), ParamStyle("Price Style",styleCandle,maskPrice));
//////////////////////////////////////////////////////////////////
_SECTION_BEGIN("Fib Retracements");
	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();
//////////////////////////////////////////////////////////////////
if (tchoice==1 ) 
{
_N(Title = EncodeColor(colorWhite)+StrFormat(" {{NAME}} -   {{INTERVAL}}     
{{Date}}    Open:  %g,    High:  %g,     Low:  %g,     Close:  %g    
{{VALUES}}",O, H, L, C, SelectedValue( ROC( C, 1   ) ) ));
}
//////////////////////////////////////////////////////////////////
if (tchoice==2 ) 
{
Title = EncodeColor(colorYellow) + "Fibonacci Internal & External Retracements  " +  EncodeColor(colorWhite) + Name() + "  " + EncodeColor(colorWhite)+  Date() + "   Tick = " + EncodeColor(5) +
Interval()+
EncodeColor(colorWhite) + "     Open = " + EncodeColor(colorWhite) + O + 
EncodeColor(colorWhite) + "     High = " + EncodeColor(5) + H +
EncodeColor(colorWhite) + "      Low = " + EncodeColor(colorRed) + L + 
EncodeColor(colorWhite) + "     Close = " + EncodeColor(colorWhite) + C + "\n"
+
EncodeColor( colorWhite) +"_______________"+"\n"+
EncodeColor( colorWhite)  + "424%   =  "	+ 	EncodeColor(25)+ e424 + " " +"\n"+
EncodeColor( colorWhite)  + "262%   =  "	+ 	EncodeColor(47)+ e262 + " " +"\n"+
EncodeColor( colorWhite)  + "200%   =  "	+ 	EncodeColor(47)+ e200 + " " +"\n"+
EncodeColor( colorWhite)  + "162%   =  "	+ 	EncodeColor(47)+ e162 + " " +"\n"+
EncodeColor( colorWhite)  + "127%   =  "	+ 	EncodeColor(47)+ e127 + " " +"\n"+
EncodeColor( Color100)  + "100%   =  "	+ EncodeColor(Color100) + p100 + " "+"\n"+
EncodeColor( colorWhite)  + "78.6%  =  "	+	EncodeColor(42)+ r786 + " " +"\n"+
EncodeColor( colorWhite)  + "61.8%  =  "	+ 	EncodeColor(43)+ r618 + " "+"\n"+
EncodeColor( colorWhite)  + "50%     =  "	+ 	EncodeColor(41)+ r050 + " "+"\n"+
EncodeColor( colorWhite)  + "38.2%  =  "	+ 	EncodeColor(44)+ r382 + " "+"\n"+
EncodeColor( colorWhite)  + "23.6%  =  "	+ 	EncodeColor(45)+ r236+ " " +"\n"+
EncodeColor( Color00)  + "00%    =  " + 	EncodeColor(Color00) + p00 + " " ;
}
GraphXSpace=5;

////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("MOB simulation shiree"); 
  
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", colorPink ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );  
PlotOHLC(O,H,L,C,"Price",IIf(C>O,colorGreen,colorRed),styleCandle); 
  
  
//Date: 12-January 2002 by Thomas Zmuck 
//thomas.zm@ aon.at 
Offset = 5; //use two sheets: one with 5 and another with 7, or maybe other offset value 
Avgmov = Offset*MA(abs( ROC(C,1)) ,20); 
per = LastValue(Avgmov) ;  
x=Cum(1);  
Range = 0.01; 
PS = TroughBars(L, per,1) == 0; 
xa = LastValue(ValueWhen (PS,x,1)) ;//x from last trough 
Ya = LastValue(ValueWhen (PS,L,1)) ;//y (Low) last trough 
PR = PeakBars(H,per, 1) == 0; 
xb = LastValue(ValueWhen (PR,x,1)) ;//x from last peak 
Yb = LastValue(ValueWhen (PR,H,1)) ;//y (High) last peak 
Trough_ReTest = abs((L/ya)-1) <Range; 
Peak_ReTest = abs((H/yb)-1) <Range; 
Trough_Cross = Cross(ya,C); 
Peak_Cross = Cross(C,yb); 
//UP = upSwing DN = downSwing 
UP = xb>xa;//upSwing 
DN = xa>xb;//DownSwing 
RT23_6 = IIf(UP,yb-(yb- ya)*0.236, IIf(DN,ya+ (yb-ya)*0.236,-1e10) ); 
RT38_2 = IIf(UP,yb-(yb- ya)*0.382, IIf(DN,ya+ (yb-ya)*0.382,-1e10) ); 
RT50_0 = IIf(UP,yb-(yb- ya)*0.500, IIf(DN,ya+ (yb-ya)*0.500,-1e10) ); 
RT61_8 = IIf(UP,yb-(yb- ya)*0.618, IIf(DN,ya+ (yb-ya)*0.618,-1e10) ); 
RT78_6 = IIf(UP,yb-(yb- ya)*0.786, IIf(DN,ya+ (yb-ya)*0.786,-1e10) ); 
  
RT12_7 = IIf(UP,yb-(yb- ya)*1.27, IIf(DN,ya+ (yb-ya)*1.27,-1e10) ); 
RT16_1 = IIf(UP,yb-(yb- ya)*1.61, IIf(DN,ya+ (yb-ya)*1.61,-1e10) ); 
  
RT=  
IIf(UP,-100* (yb-L)/(yb- ya), 
100*(H-ya)/( yb-ya));//Retracement_ Value 
InZone = C<yb & C>ya;//use it for filter to receive only signals that are in in the Retracement zone. 
Buy = Trough_ReTest OR peak_Cross; 
Sell = Peak_ReTest OR trough_Cross; 
Filter = 1; 
AddColumn(RT, "RT%"); 
AddColumn(Trough_ReTest,"TR- Test",1.0) ; 
AddColumn(Peak_ReTest,"PK-Test",1.0) ; 
AddColumn(Trough_Cross,"TR-Cross",1.0); 
AddColumn(Peak_Cross,"PK-Cross",1.0); 
//AddColumn(CdDoji( )OR CHammer(),"Candle" ,1.0); 
Plot(C,"C",1, 64); 
Plot(IIf(x>xa, ya,-1e10) ,"Bottom" ,colorBrown, 1+8); 
Plot(IIf(x>xb, yb,-1e10) ,"Top",colorBrown,1+8); 
xab = IIf(xb>xa,xb, xa); 
//Retracements 
Plot(IIf(x>= xab+1,RT23_6,-1e10), "23,6% Retr.",5,1); 
Plot(IIf(x>= xab+1,RT38_2,-1e10), "38,2% Retr.",5,1); 
Plot(IIf(x>= xab+1,RT50_0,-1e10), "50,0% Retr.",colorBlue, 1); 
Plot(IIf(x>= xab+1,RT61_8,-1e10), "61,8% Retr.",colorYellow, 1); 
Plot(IIf(x>= xab+1,RT78_6,-1e10), "78,6% Retr.",colorYellow, 1); 
  
Plot(IIf(x>= xab+1,RT12_7,-1e10), "127% ext.",colorBrightGreen, styleDots); 
Plot(IIf(x>= xab+1,RT16_1,-1e10), "161% ext.",colorBrightGreen, styleDots); 
CondA=IIf(x>= xab+1,RT12_7,-1e10); 
CondB=IIf(x>= xab+1,RT16_1,-1e10); 
PlotOHLC(Condb,Condb,Conda,Conda,"",ColorRGB(30,130,30),styleCloud,styleNoLabel); 
  
  
GraphXSpace = 0.5; 
  
  
  
  
//Plot(Gauss2ord(C,5),"M5",4,1); 
//GraphXSpace = 1.5; 
//Title = Name()+" per = "+WriteVal(per, 1.0) +" Close = "+WriteVal(C, 1.2)+ " ("+WriteVal( ROC(C,1), 1.2)+"%)" +" Current Correction = "+WriteVal(RT, 1.0)+"%"; 
  
//Plot( Volume,"V", ParamColor("Color", colorBlueGrey ), ParamStyle( "Style", stylehidden| styleOwnScale | styleThick, maskHistogram  ), 2 ); 
  
  
_SECTION_END( );



//---- Ramesh.S.R
HaClose = (O+H+L+C)/4; 
HaOpen = AMA( Ref( HaClose, -1 ), .40); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) ); 
xDiff = (HaHigh - Halow) * IIf(StrFind(Name(),"JPY"),100,10000);
barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);
// ****PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", barcolor, styleCandle ); 
// Plot(EMA(HaClose,9),"",colorWhite, styleLine);
// Plot(EMA(HaClose,18),"",colorBlack, styleLine);




_SECTION_BEGIN("Ramesh Swing1");
no=Param( "Swing", 10, 1, 55 );
tsl_col=ParamColor( "Color", colorDarkRed );

res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);

// line code
Plot(tsl, _DEFAULT_NAME(), colorDarkRed, styleStaircase); 

// ribbon code
//  Plot( 2, "Ribbon",IIf(C>tsl,colorBlue,colorRed),styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

// arrows code
Buy=Cross(C,Ref(res,-1));
Sell=Cross(Ref(sup,-1),C);
shape=Buy*shapeUpArrow + Sell*shapeDownArrow;
PlotShapes(shape,colorBlack,0,IIf(Buy,Low,High));

// scanner code
Buy=Cross(C,tsl);
Sell=Cross(tsl,C);

_SECTION_END();



_SECTION_BEGIN("Ramesh Swing2");
no=Param( "Swing", 20, 1, 55 );
tsl_col=ParamColor( "Color", colorBlue );

res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);

Plot(tsl, _DEFAULT_NAME(), tsl_col, styleStaircase); // or styleaArea

Buy=Cross(C,tsl);
Sell=Cross(tsl,C);
shape=Buy*shapeUpArrow + Sell*shapeDownArrow;
PlotShapes(shape,IIf(Buy,tsl_col,tsl_col),0,IIf(Buy,Low,High));
_SECTION_END();




_SECTION_BEGIN("Ramesh Swing3");
no=Param( "Swing", 30, 1, 55 );
tsl_col=ParamColor( "Color", colorGreen );

res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);

Plot(tsl, _DEFAULT_NAME(), tsl_col, styleStaircase); // or styleaArea

Buy=Cross(C,tsl);
Sell=Cross(tsl,C);
shape=Buy*shapeUpArrow + Sell*shapeDownArrow;
PlotShapes(shape,IIf(Buy,tsl_col,tsl_col),0,IIf(Buy,Low,High));
_SECTION_END();




_SECTION_BEGIN("Time Left");

function GetSecondNum()

{

Time = Now( 4 );

Seconds = int( Time % 100 );

Minutes = int( Time / 100 % 100 );

Hours = int( Time / 10000 % 100 );

SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );

return SecondNum;

}

RequestTimedRefresh( 1 );

TimeFrame = Interval();

SecNumber = GetSecondNum();

Newperiod = SecNumber % TimeFrame == 0;

SecsLeft = SecNumber - int( SecNumber / TimeFrame ) * TimeFrame;

SecsToGo = TimeFrame - SecsLeft;

 

x=Param("xposn",300,0,1000,1);

y=Param("yposn",50,0,1000,1);

 

GfxSelectSolidBrush( ColorRGB( 230, 230, 230 ) );

GfxSelectPen( ColorRGB( 230, 230, 230 ), 2 );

if ( NewPeriod )

{

GfxSelectSolidBrush( colorYellow );

GfxSelectPen( colorYellow, 2 );

Say( "New period" );

}

GfxRoundRect( x+45, y+17, x-3, y-2, 0, 0 );

GfxSetBkMode(1);

GfxSelectFont( "Arial", 12, 700, False );

GfxSetTextColor( colorBlack );

GfxTextOut( ""+SecsToGo+" / "+NumToStr( TimeFrame, 1.0 ), x, y );

_SECTION_END();

_SECTION_BEGIN("Price2");
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("Pop-Up");


//  function GetSecondNum()
{
    Time = Now( 4 );
    Seconds = int( Time % 100 );
    Minutes = int( Time / 100 % 100 );
    Hours = int( Time / 10000 % 100 );
    SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
//    return SecondNum;
}
 
function PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
{
    displayText = bodytext + captiontext;
    if ( ( StaticVarGetText( "prevPopup" + popupID ) != displayText) OR ( StaticVarGet( "prevPopupTime" + popupID ) < GetSecondNum() ) )
    {
        StaticVarSetText( "prevPopup" + popupID, displayText);
        StaticVarSet( "prevPopupTime" + popupID, GetSecondNum() + timeout );
        PopupWindow( bodytext, Captiontext + popupID, timeout, Left, top );
    }
}
 
//PopupWindowEx( "ID:1", "BUY", "test alert ", 5, -1, -1 );
//PopupWindowEx( "ID:2", "testing", "test alert ", 5, 0, 0 );


_SECTION_END();




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Name : MA Ribbon
// Date   : 20100112
// Release : 1.0
// Language : AFL for AmiBroker

// Author :       TAMS
// http://www.traderslaboratory.com/forums/f46/ma-ribbon-6992.html
// Adapted from Easy Language : aaa 
// Color : Head2K

// Public license
// Terms of use:
// You are required to include this header if you incorporate 
// all or parts of the concepts or codes of this indicator.

// The MA Ribbon cuts through the noise to display the strength of a trend
//
// The strength of the trend is shown via the difference between current bar's MA
// and the MA of X bars ago. (usually half of the MA length)
//
// When the trend strength is positive and increasing, 
// the indicator will display an upward ribbon that is increasing in width.
// When the trend strength is decreasing, the ribbon narrows.
// Eventually when the market reverses, the ribbon will change color,
// then reverses direction

// Differences with TradeStation or Multicharts
// 2) The lenght is adjustable with a slider in a floating window in Real Time
// 1) The loocback is projected in the future

Plot(C,"Price",colorBlack,styleBar);

Periods      = Param("Periods",      20, 2, 150, 1, 0 );
Displacement = Param("Displacement", 10, -150, 150 );

MAactual = MA( C, Periods );
MAdisp   = Ref( MA( C, Periods ), -Displacement);

ColorMA = MA( C , Periods ) < Ref( MA( C , Periods ), -1) ;

Plot( MA( C , Periods ), "MA", 
IIf( ColorMA, ParamColor( "ColorUp.MA", colorBlue ), ParamColor( "ColorDn.MA", colorYellow ) ), 
ParamStyle( "Style.MA") | styleNoRescale ); 

Plot( MA( Close, Periods ), "MA2", 
IIf( ColorMA, ParamColor( "ColorUp.MA", colorBlue ), ParamColor( "ColorDn.MA", colorYellow ) ), 
ParamStyle( "StyleMA2") | styleNoRescale , Null, Null, Displacement  );

PlotOHLC( MAdisp , MAactual, MAdisp, MAactual, "Cloud", 
IIf( MAactual > MAdisp, ParamColor( "ColorCloud.Up", ColorRGB( 193 , 189 , 152 )), ParamColor( "ColorCloud.Dn", ColorRGB( 125 , 151 , 192 ))), 
styleCloud | styleNoRescale | styleNoLabel );

GraphXSpace=5;


_SECTION_END();

//--------------------------------------------------------------//
//  Kenzie SR System - 09/2010
//  Modified By Kenzie Sebastian (kenziesr@yahoo.co.id)
//  Modified By vargaso_s@yahoo.com
//
// - fixed the % change to display correctly
// - modified buy/sell signals display above/below candles
// - added Market trend S&P 500
// - change color of characters to display with black background.
//--------------------------------------------------------------
SetBarsRequired( 800, 0 );

GraphXSpace = 7;
SetChartOptions( 0, chartShowArrows | chartShowDates );

// set criteria to scan for big stock only;
BigStock = MA( V, 10 ) * MA( C, 10 ) > 1000000;

//---------------Color------------------------
per1 = 6;
per2 = 2;
Om = MA( O, per1 );
hm = MA( H, per1 );
lm = MA( L, per1 );
Cm = MA( C, per1 );

// 1. Heiken Ashi
HACLOSE = ( Om + Hm + Lm + Cm ) / 4;
HaOpen = AMA( Ref( HaClose,  -1 ),  0.5 );
HaHigh = Max( Hm,  Max( HaClose,  HaOpen ) );
HaLow = Min( Lm,  Min( HaClose,  HaOpen ) );


Of = MA( Haopen, per2 );
Cf = MA( Haclose, per2 );
Lf = IIf( haOpen < haClose, MA( Halow, per2 ), MA( Hahigh, per2 ) );
Hf = IIf( haOpen < haClose, MA( Hahigh, per2 ), MA( Halow, per2 ) );
//Color = IIf( Cf > Of, colorGreen, colorRed );


//----------------------------------------------------

TrailStop = HHV( C - 2 * ATR( 10 ), 15 );
ProfitTaker = EMA( H, 13 ) + 2 * ATR( 10 );

/* **********************************
Code to automatically identify pivots
********************************** */

// -- what will be our lookback range for the hh and ll?
farback = 140; //How Far back to go
nBars = 12; //Number of bars

// -- 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 < BarCount; 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++;
    }
}

//============== EXPLORATION ==============
Buy = Cover = BigStock AND aLPivs == 1;

Sell = Short = BigStock AND aHPivs == 1;

SellPrice = ValueWhen( Sell, C, 1 );

BuyPrice = ValueWhen( Buy, C, 1 );

Long = Flip( Buy, Sell );

Shrt = Flip( Sell, Buy );

//============== Plot price ==============
n = 15;

a =  C > ( MA( H, n ) + MA( L, n ) ) / 2;// then Buy next bar at market;

b = C < ( MA( H, n ) + MA( L, n ) ) / 2;// then Sell Short next bar at market;

state = IIf( BarsSince( a ) < BarsSince( b ), 1, 0 );

Longs = state == 1;

shorts = state == 0;

//Chart
Colorbar = IIf( Longs, colorGreen, IIf( Shorts, colorRed, colorGrey40 ) );

Plot( C, "Close", colorbar, styleCandle = 64 | styleNoTitle );

//============== Plot Shape ==============
PlotShapes( IIf( aHPivs == 1, shapeDownArrow, shapeNone ), colorOrange, 0, High, Offset = -45 );

PlotShapes( IIf( aLPivs == 1, shapeUpArrow , shapeNone ), colorLime, 0, Low, Offset = -20 );
PlotShapes( IIf(Buy, shapeSmallCircle, shapeNone),colorGreen, 0, BuyPrice, Offset = -15 );
PlotShapes( IIf(Sell, shapeSmallCircle, shapeNone),colorYellow, 0 ,SellPrice,  Offset = 45  );
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status("LastVisibleBar");
for( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++)
{
if( Buy[b] ) PlotText("\n\n\n\n Buy\n "+NumToStr(BuyPrice[b],1.2),b,BuyPrice[b],colorGreen);
else if( Sell[b] ) PlotText("Sell  "+NumToStr(SellPrice[b], 1.2),b,SellPrice[b],colorYellow);
}

//============== EMA(13) ==============
Plot( EMA( C, 13 ), "" , colorSkyblue + styleLine + styleNoRescale );

//============== TRENDING ==============
DTL = 150; // DTL = Define Trend Long

DTM = 70;	// DTM = Define Trend Medium

DTS = 14;  // DTS = Define Trend Short

TL = LinRegSlope( MA( C, DTL ), 2 );	 // TL = Trend Long

TM = LinRegSlope( MA( C, DTM ), 2 );  // TM = Trend Medium

TS = LinRegSlope( MA( C, DTS ), 2 );  // TS = Trend Short

TLL = IIf( LinRegSlope( MA( C, DTL ), 2 ) > 0, True, False );

TMM = IIf( LinRegSlope( MA( C, DTM ), 2 ) > 0, True, False );

TSS = IIf( LinRegSlope( MA( C, DTS ), 2 ) > 0, True, False );

//============== VOLUME ==============
Vlp = 30; //Volume lookback period

Vrg = MA( V, Vlp );

St = StDev( Vrg, Vlp );

Vp3 = Vrg + 3 * st;

Vp2 = Vrg + 2 * st;

Vp1 = Vrg + 1 * st;

Vn1 = Vrg - 1 * st;

Vn2 = Vrg - 2 * st;

//============== WILLIAM'S %R ==============
WR = ( ( HHV( H, 14 ) - C ) / ( HHV ( H, 14 ) - LLV ( L, 14 ) ) ) * -100;

//============== A/D ==============
TRH = IIf( Ref( C, -1 ) > H, Ref( C, -1 ), H );

TRL = IIf( Ref( C, -1 ) < L, Ref( C, -1 ), L );

ad = IIf( C > Ref( C, -1 ), C - TRL, IIf( C < Ref( C, -1 ), C - TRH, 0 ) );

WAD = Cum( ad );

wu = wad > Ref( wad, -1 );

wd = wad < Ref( wad, -1 );

//============== MACD ==============
MB = Cross ( MACD(), Signal() );

MS = Cross( Signal(), MACD() );

MB = ExRem( MB, MS );

MS = ExRem( MS, MB );

MB1 = MACD() > Signal();

MS1 = MACD() < Signal();

//============== STOCH ==============
StochKval = StochK( 10, 5 );

StochDval = StochD( 10, 5, 5 );

StochBuy = Cross( StochK( 10, 5 ), StochD( 10, 5, 5 ) );

StochSell = Cross ( StochD( 10, 5, 5 ), StochK( 10, 5 ) );

StBuy = StochK( 10, 5 ) > StochD( 10, 5, 5 );

StSell = StochK( 10, 5 ) < StochD( 10, 5, 5 );

//============== ADX ==============
adxBuy =  Cross( PDI( 14 ), MDI( 14 ) );

adxSell = Cross( MDI( 14 ), PDI( 14 ) );

adxBuy = ExRem( adxBuy, adxSell );

adxSell = ExRem( adxSell, adxBuy );

adxbuy1 = PDI( 14 ) > MDI( 14 );

adxsell1 = MDI( 14 ) > PDI( 14 );

//==============Zero Lag TMA ==============
function ZeroLagTEMA( array, period )
{
    TMA1 = TEMA( array, period );
    TMA2 = TEMA( TMA1, period );
    Diff = TMA1 - TMA2;
    return TMA1 + Diff ;
}

haClose = ( haClose + haOpen + haHigh + haLow ) / 4;

periodtm = 55;
ZLHa = ZeroLagTEMA( haClose, periodtm );
ZLTyp = ZeroLagTEMA( Avg, periodtm );
TMBuy = Cross( ZLTyp, ZLHa );
TMSell = Cross( ZLHa, ZLTyp );
TMBuy1 = ZLTyp > ZLHa ;
TMSell1 = ZLHa > ZLTyp ;

//============== ZLW ==============
R = ( ( HHV( H, 14 ) - C ) / ( HHV ( H, 14 ) - LLV ( L, 14 ) ) ) * -100;
MaxGraph = 10;
PeriodZ = 10;
EMA1 = EMA( R, PeriodZ );
EMA2 = EMA( EMA1, 5 );
Difference = EMA1 - EMA2;
ZeroLagEMA = EMA1 + Difference;
PR = 100 - abs( ZeroLagEMA );
MoveAvg = MA( PR, 5 );
ZBuy = Cross( PR, moveAvg ) AND PR < 30;
ZSell = Cross( moveAvg, PR ) AND PR > 70;
ZBuy1 = PR >= MoveAvg AND PR >= Ref( PR, -1 ) ;
ZSell1 = ( PR < MoveAvg ) OR PR >= MoveAvg AND PR < Ref( PR, -1 ) ;

//============== RS ==============
p = ( H + L + C ) / 3;
r1 = ( 2 * p ) - L;
s1 = ( 2 * p ) - H;
r2 = p + ( r1 - s1 );
s2 = p - ( r2 - s1 );
R3 = P + ( R2 - S2 );
S3 = P - ( R3 - S2 );

//============== IBUY ==============
Ibuy =  Cross( RSI( 14 ), EMA( RSI( 14 ), 9 ) );
Isell = Cross( EMA( RSI( 14 ), 9 ), RSI( 14 ) );
Ibuy = ExRem( Ibuy, ISell );
Isell = ExRem( ISell, Ibuy );
BlRSI = RSI( 14 ) > EMA( RSI( 14 ), 9 );
BrRSI = RSI( 14 ) < EMA( RSI( 14 ), 9 );


//=================Trend & Signals & Market Index ===============================
/// Please replace "00DSEGEN" with your market index ticker and activate the codes

/// Market Bull Bear

Cg = Foreign("^GSPC", "C");
Cgo= Ref(Cg,-1);

//Longterm Bullish or Bearish
Bullg = Cg > WMA(Cg,200);
Bearg= Cg <WMA(Cg,200);

//Midterm Bullish or Bearish
mBullg = Cg >WMA(Cg,50);
mBearg= Cg <WMA(Cg,50);

//Shortterm Bullish or Bearish
sBullg = Cg >WMA(Cg,15);
sBearg= Cg <WMA(Cg,15);
////////////////////////////////



xChange1=Cg - Ref(Cg,-1);
Change1 = StrFormat("%1.2f% ",xChange1);    
barche1= xChange1>=0;  
Comche1= xChange1<0;   
xperchange1 = xChange1/100;
perchange1 = StrFormat("%1.2f% ",xperchange1);   
positivechange1 = xperchange1>0;  
negativechange1 = xperchange1<0;

//=================Trend & Signals & Market Index END===============================



//============== TITLE ==============
_SECTION_BEGIN( "Title" );
printf( "Open : " + O );

if ( Status( "action" ) == actionIndicator )
    (
        Title = EncodeColor( colorGold ) + "Kenzie SR System" + EncodeColor( ColorRGB( 111, 208, 255 ) )  + " (" +  Name() + ") " + EncodeColor( colorGold ) + Interval( 2 ) +
                "  " + Date() + " " + "  Open " + O + "  " + "Hi " +  H + "  " + "Lo " + L + "  " + EncodeColor( ColorRGB( 111, 208, 255 ) ) +
                "Close " + C + " ( " + WriteVal( ( C - Ref( C, -1 ) ) * 100 / Ref( C, -1 ), 1.1 ) + " %)" + EncodeColor( colorGold ) + "  Vol= " + WriteVal( V, 1.0 )
                + " " + WriteIf( V > Vp2, EncodeColor( colorBrightGreen ) + "(Very High)", WriteIf( V > Vp1, EncodeColor( colorLime ) + "(High)", WriteIf( V > Vrg, EncodeColor( colorLime ) + "(Above Average)",
                                 WriteIf( V<Vrg AND V>Vn1, EncodeColor( ColorRGB( 255, 0, 128 ) ) + "(Less than Average)", WriteIf( V < Vn1, "(Low)", "" ) ) ) ) ) + EncodeColor( colorGold ) + "  "
                + EncodeColor( colorGold ) + "EMA(Close,13) = " +EncodeColor( colorBrightGreen ) + WriteVal( EMA( C, 13 ), 1.2 )


                + "\n\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) +
                WriteIf ( Buy , "Signal: Go Long -" +EncodeColor( colorBrightGreen ) + " Entry Price: " + WriteVal( C, 1.2 ) + EncodeColor( ColorRGB( 111, 208, 255 ) ) +" - Last Exit Price: " +  EncodeColor( colorBrightGreen) +WriteVal( SellPrice, 1.2 )
                          + " (" + WriteVal( ( BuyPrice - SellPrice ), 1.2 ) + ")" + EncodeColor( ColorRGB( 111, 208, 255 ) ) +" - StopLoss: " + EncodeColor( colorBrightGreen ) + WriteVal( C * .95, 1.2 ) + EncodeColor( ColorRGB( 111, 208, 255 ) ) + " - Reward Risk Ratio: " + EncodeColor( colorBrightGreen) + WriteVal( ( profittaker - C ) / ( C - C * 0.95 ), 1.2 ) + " - " + EncodeColor( colorGold ) + "Strong Buy!"
                          , "" ) +

                EncodeColor( ColorRGB( 111, 208, 255 ) ) +
                WriteIf ( Sell , "Signal: Go Short -" + EncodeColor( colorBrightGreen ) +"Exit Price: " + WriteVal( C, 1.2 ) + EncodeColor( ColorRGB( 111, 208, 255 ) )  +
                " - Profit: " +  EncodeColor( colorBrightGreen ) + WriteVal( ( SellPrice - BuyPrice ), 1.2 ) + " (" + WriteVal( ( ( SellPrice - BuyPrice ) * 100 / BuyPrice ), 1.1 ) + "%)" + EncodeColor( colorGold ) + " - Profit Taking!", "" ) +
                EncodeColor( ColorRGB( 111, 208, 255 ) ) +
                WriteIf( Long AND NOT Buy, "Trade:"  + EncodeColor( colorBrightGreen) +" Long" + EncodeColor( ColorRGB( 111, 208, 255 ) )  +"- Entry Price: " + EncodeColor( colorBrightGreen) + WriteVal( ( BuyPrice ), 1.2 ) +EncodeColor( ColorRGB( 111, 208, 255 ) ) + " - Profit: " + EncodeColor( colorBrightGreen) + WriteVal( ( C - BuyPrice ), 1.2 ) + " (" + WriteVal( ( ( C - BuyPrice ) * 100 / BuyPrice ), 1.1 ) + "%)" +
                EncodeColor( ColorRGB( 111, 208, 255 ) ) + " - StopLoss: " + EncodeColor( colorBrightGreen) + WriteVal( ( BuyPrice * .95 ), 1.2 ) + EncodeColor( ColorRGB( 111, 208, 255 ) ) + " - Reward Risk Ratio: " + EncodeColor( colorBrightGreen) + WriteVal( ( profittaker - BuyPrice ) / ( BuyPrice - BuyPrice * 0.95 ), 1.2 ) + " - " + EncodeColor( colorGold ) + " Let your profit's run !", "" ) +
                WriteIf( shrt AND NOT Sell, "Trade: Short - Exit Price: " + WriteVal( ( SellPrice ), 1.2 ) + " - Profit: " + WriteVal( ( SellPrice - BuyPrice ), 1.2 ) + " (" + WriteVal( ( ( SellPrice - BuyPrice ) * 100 / BuyPrice ), 1.1 ) + "%) - " + EncodeColor( colorBrightGreen ) + "Watch for a Strong Buy Signal !", "" )

                + "\n\n" +  EncodeColor( colorBlack ) + ":::::::::::::::::::::::::::::::::::::::::::::"

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Short Term: " +
                WriteIf( TS > 0 AND TS < 0.3, EncodeColor( colorLime ) + "Weak Up Trend",
                         WriteIf( TS >= 0.3 AND TS < 0.6 , EncodeColor( colorGold ) + "Medium Up Trend",
                                  WriteIf( TS >= 0.6, EncodeColor( colorGreen ) + "Strong Up Trend",
                                           WriteIf( TS<0 AND TS> -0.3, EncodeColor( colorPink ) + "Weak Down Trend",
                                                    WriteIf( TS <= -0.3 AND TS > -0.6 , EncodeColor( ColorRGB( 255, 0, 128 ) ) + "Medium Down Trend",
                                                             WriteIf( TS <= -0.6, EncodeColor( colorRed ) + "Strong Down Trend", EncodeColor( colorGold ) + "Sideways" ) ) ) ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Mid Term: " +
                WriteIf( TM > 0 AND TM < 0.3, EncodeColor( colorLime ) + "Weak Up Trend",
                         WriteIf( TM >= 0.3 AND TM < 0.6 , EncodeColor( colorGold ) + "Medium Up Trend",
                                  WriteIf( TM >= 0.6, EncodeColor( colorGreen ) + "Strong Up Trend",
                                           WriteIf( TM<0 AND TM> -0.3, EncodeColor( colorPink ) + "Weak Down Trend",
                                                    WriteIf( TM <= -0.3 AND TM > -0.6 , EncodeColor( ColorRGB( 255, 0, 128 ) ) + "Medium Down Trend",
                                                             WriteIf( TM <= -0.6, EncodeColor( colorRed ) + "Strong Down Trend", EncodeColor( colorGold ) + "Sideways" ) ) ) ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Long Term: " +
                WriteIf( TL > 0 AND TL < 0.3, EncodeColor( colorLime ) + "Weak Up Trend",
                         WriteIf( TL >= 0.3 AND TL < 0.6 , EncodeColor( colorGold ) + "Medium Up Trend",
                                  WriteIf( TL >= 0.6, EncodeColor( colorGreen ) + "Strong Up Trend",
                                           WriteIf( TL<0 AND TL> -0.3, EncodeColor( colorPink ) + "Weak Down Trend",
                                                    WriteIf( TL <= -0.3 AND TL > -0.6 , EncodeColor( ColorRGB( 255, 0, 128 ) ) + "Medium Down Trend",
                                                             WriteIf( TL <= -0.6, EncodeColor( colorRed ) + "Strong Down Trend", EncodeColor( colorGold ) + "Sideways" ) ) ) ) ) )

                + "\n" +  EncodeColor( colorBlack ) +":::::::::::::::::::::::::::::::::::::::::::::"




                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "AccDist (): " + WriteIf( wu, EncodeColor( colorGold ) + "Accumulation", WriteIf( wd, EncodeColor( colorRed ) + "Distribution", "Neutral" ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "RSI (14): " + WriteIf( RSI( 14 ) > 30 AND RSI( 14 ) < 70, EncodeColor( colorGold ), WriteIf( RSI( 14 ) < 30 , EncodeColor( colorBlue ), EncodeColor( colorRed ) ) ) + WriteVal( RSI( 14 ), format = 1.1 )
                + WriteIf( RSI( 14 ) > 30 AND RSI( 14 ) < 70, " Range" + EncodeColor( colorGold ), WriteIf( RSI( 14 ) < 30 , " OverSold" + EncodeColor( colorBlue ), " OverBought" + EncodeColor( colorRed ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "CCI (14): " + WriteIf( CCI( 14 ) > -100 AND CCI( 14 ) < 100, EncodeColor( colorGold ), WriteIf( CCI( 14 ) < -100 , EncodeColor( colorBlue ), EncodeColor( colorRed ) ) ) + WriteVal( CCI( 14 ), format = 1.1 )
                + WriteIf( CCI( 14 ) > -100 AND CCI( 14 ) < 100, " Range" + EncodeColor( colorGold ), WriteIf( CCI( 14 ) < -100 , " OverSold" + EncodeColor( colorBlue ), " OverBought" + EncodeColor( colorRed ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "ROC (C,14): " + WriteIf( ROC( C, 14 ) > -10 AND ROC( C, 14 ) < 10, EncodeColor( colorGold ), WriteIf( ROC( C, 14 ) < -10 , EncodeColor( colorBlue ), EncodeColor( colorRed ) ) ) + WriteVal( ROC( C, 14 ), format = 1.1 )
                + WriteIf( ROC( C, 14 ) > -10 AND ROC( C, 14 ) < 10, " Range" + EncodeColor( colorGold ), WriteIf( ROC( C, 14 ) < -10 , " OverSold" + EncodeColor( colorBlue ), " OverBought" + EncodeColor( colorRed ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Wm%R (14): " + WriteIf( WR > -80 AND WR < -20, EncodeColor( colorGold ), WriteIf( WR < -80 , EncodeColor( colorBlue ), EncodeColor( colorRed ) ) ) + WriteVal( WR, format = 1.1 )
                + WriteIf( WR > -80 AND WR < -20, " Range" + EncodeColor(colorGold ), WriteIf( WR < -80 , " OverSold" + EncodeColor( colorBlue ), " OverBought" + EncodeColor( colorRed ) ) )


                + "\n" +  EncodeColor( colorBlack ) +":::::::::::::::::::::::::::::::::::::::::::::"




                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Signal (RSI):  " + WriteIf( Ibuy, EncodeColor( colorDarkRed) + "BuyWarning", WriteIf( Isell, EncodeColor( colorRed ) + "SellWarning", WriteIf( BlRSI, EncodeColor( colorGold ) + "BullishZone", WriteIf( BrRSI, EncodeColor( colorRed ) + "BearishZone", "Neutral" ) ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Signal (Z Lag):  " + WriteIf( TMBuy, EncodeColor(colorDarkRed ) + "Buy", WriteIf( TMSell, EncodeColor( colorRed ) + "Sell", WriteIf( TMBuy1, EncodeColor( colorGold ) + "Bullish", WriteIf( TMSell1, EncodeColor( colorRed ) + "Bearish", "Neutral" ) ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Signal (MACD):  " + WriteIf( MB, EncodeColor( colorDarkRed ) + "Buy", WriteIf( MS, EncodeColor( colorRed ) + "Sell", WriteIf( MB1, EncodeColor( colorGold ) + "Bullish", WriteIf( MS1, EncodeColor( colorRed ) + "Bearish", "Neutral" ) ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Signal (Stoch):  " + WriteIf( StochBuy, EncodeColor( colorDarkRed) + "Buy", WriteIf( StochSell, EncodeColor( colorRed ) + "Sell", WriteIf( StBuy, EncodeColor( colorGold ) + "Bullish", WriteIf( StSell, EncodeColor( colorRed ) + "Bearish", "Neutral" ) ) ) )

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Signal (ADX):  " + WriteIf( adxBuy, EncodeColor( colorDarkRed) + "Buy", WriteIf( adxSell, EncodeColor( colorRed ) + "Sell", WriteIf( adxBuy1, EncodeColor( colorGold ) + "Bullish", WriteIf( adxSell1, EncodeColor( colorRed ) + "Bearish", "Neutral" ) ) ) )

                +  "\n" +  EncodeColor( colorBlack ) +":::::::::::::::::::::::::::::::::::::::::::::"



             

                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Trigger Stop : " + EncodeColor( colorYellow ) + WriteVal( TrailStop, format = 1.0 )
                + "\n" + EncodeColor( ColorRGB( 111, 208, 255 ) ) + "Trigger Price Profit : " + EncodeColor( colorYellow ) + WriteVal( Profittaker, format = 1.0 )
                + "\n" +EncodeColor( ColorRGB( 111, 208, 255 ) ) + "R1: " + EncodeColor( colorDarkRed) + WriteVal( r1, format = 1.0 )
                + EncodeColor( ColorRGB( 111, 208, 255 ) ) + " R2: " + EncodeColor( colorDarkRed ) + WriteVal( r2, format = 1.0 )
                + EncodeColor( ColorRGB( 111, 208, 255 ) ) + " R3: " + EncodeColor( colorDarkRed ) + WriteVal( r3, format = 1.0 )
                + "\n" +EncodeColor( ColorRGB( 111, 208, 255 ) ) + "S1: " + EncodeColor( colorDarkGreen ) + WriteVal( s1, format = 1.0 )
                + EncodeColor( ColorRGB( 111, 208, 255 ) ) + " S2: " + EncodeColor( colorDarkGreen ) + WriteVal( s2, format = 1.0 )
                +EncodeColor( ColorRGB( 111, 208, 255 ) ) + " S3: " + EncodeColor( colorDarkGreen ) + WriteVal( s3, format = 1.0 )

                +  "\n" +  EncodeColor( colorBlack ) + ":::::::::::::::::::::::::::::::::::::::::::::"



                + "\n\n\n\n\n\n" +  EncodeColor( ColorRGB( 111, 208, 255 ) ) + "¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤"

                 
                +"\n"+EncodeColor(colorPink)+"S&P 500 Index: "+ WriteIf(Cg>Cgo,EncodeColor(08),EncodeColor(04))+WriteVal(Cg,format=1.2)+WriteIf(positivechange1, EncodeColor(colorBrightGreen),"")+WriteIf(negativechange1,EncodeColor(colorRed), "")+" ( "+WriteIf(barche1,"\\c08"+Change1,"")+WriteIf(barche1,"\\c08 ","")+WriteIf(Comche1,"\\c04"+Change1,"")+ WriteIf(Comche1,"\\c04 ","")+""+") " 

                +"\n"+EncodeColor(colorPink)+"Market Trend: "+ WriteIf(sBullg,EncodeColor(colorBrightGreen)+"WMA (15) UP",WriteIf(sBearg,EncodeColor(colorRed)+"WMA (15) Down",EncodeColor(colorYellow)+"WMA (15) Flat"))  
               
                +"\n"+EncodeColor(colorPink)+"Market Trend: "+ WriteIf(mBullg,EncodeColor(colorBrightGreen)+"WMA (50) UP",WriteIf(mBearg,EncodeColor(colorRed)+"WMA (50) Down",EncodeColor(colorYellow)+"WMA (50) Flat"))  
                
                + "\n"+EncodeColor(colorPink)+"Market Trend: " +WriteIf(Bullg,EncodeColor(colorBrightGreen)+"WMA (200) UP",WriteIf(Bearg,EncodeColor(colorRed)+"WMA (200) Down",EncodeColor(colorYellow)+"WMA (200) Flat"))  

                + "\n" +  EncodeColor( ColorRGB( 111, 208, 255 ) ) + "¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤"
               
              


                 );


//============== BACKGROUND NAME ==============

pxwidth = Status( "pxwidth" );

pxheight = Status( "pxheight" );

GfxSetOverlayMode( 1 );

GfxSetBkMode( 0 ); // transparent

GfxSelectFont( "Amienne", Status( "pxheight" ) / 15 );

//GfxSetTextColor( colorGrey40 );

//GfxTextOut( "Kenzie Sebastian", Status("pxwidth")/5.3, Status("pxheight")/5 );

//============================

////BACKGROUND COLOR////////////////////////////////////////////////////////
//SetChartBkColor(ColorRGB(255,200,255));
//SetChartBkGradientFill( colorPlum, colorPlum);
/////////////////////////////////////////////////////////////////////////////////////



_SECTION_END();

_SECTION_BEGIN( "Keltner Bands" );

P = ParamField( "Price field", -1 );

Periods = Param( "Periods", 15, 2, 300, 1 );

Width = Param( "Width", 2, 0, 10, 0.05 );

Color = ParamColor( "Color", colorBlue );

Style = ParamStyle( "Style" );

CenterLine = MA( P, Periods );

KTop   = CenterLine + Width * ATR( Periods );

KBot = CenterLine - Width * ATR( Periods );

Plot( KTop, "KBTop" + _PARAM_VALUES(), Color, Style );

Plot( KBot, "KBBot" + _PARAM_VALUES(), Color, Style );
_SECTION_END();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Name : MA Ribbon
// Date   : 20100112
// Release : 1.0
// Language : AFL for AmiBroker

// Author :       TAMS
// http://www.traderslaboratory.com/forums/f46/ma-ribbon-6992.html
// Adapted from Easy Language : aaa 
// Color : Head2K

// Public license
// Terms of use:
// You are required to include this header if you incorporate 
// all or parts of the concepts or codes of this indicator.

// The MA Ribbon cuts through the noise to display the strength of a trend
//
// The strength of the trend is shown via the difference between current bar's MA
// and the MA of X bars ago. (usually half of the MA length)
//
// When the trend strength is positive and increasing, 
// the indicator will display an upward ribbon that is increasing in width.
// When the trend strength is decreasing, the ribbon narrows.
// Eventually when the market reverses, the ribbon will change color,
// then reverses direction

// Differences with TradeStation or Multicharts
// 2) The lenght is adjustable with a slider in a floating window in Real Time
// 1) The loocback is projected in the future

Plot(C,"Price",colorBlack,styleBar);

Periods      = Param("Periods",      20, 2, 150, 1, 0 );
Displacement = Param("Displacement", 10, -150, 150 );

MAactual = MA( C, Periods );
MAdisp   = Ref( MA( C, Periods ), -Displacement);

ColorMA = MA( C , Periods ) < Ref( MA( C , Periods ), -1) ;

Plot( MA( C , Periods ), "MA", 
IIf( ColorMA, ParamColor( "ColorUp.MA", colorBlue ), ParamColor( "ColorDn.MA", colorYellow ) ), 
ParamStyle( "Style.MA") | styleNoRescale ); 

Plot( MA( Close, Periods ), "MA2", 
IIf( ColorMA, ParamColor( "ColorUp.MA", colorBlue ), ParamColor( "ColorDn.MA", colorYellow ) ), 
ParamStyle( "StyleMA2") | styleNoRescale , Null, Null, Displacement  );

PlotOHLC( MAdisp , MAactual, MAdisp, MAactual, "Cloud", 
IIf( MAactual > MAdisp, ParamColor( "ColorCloud.Up", ColorRGB( 193 , 189 , 152 )), ParamColor( "ColorCloud.Dn", ColorRGB( 125 , 151 , 192 ))), 
styleCloud | styleNoRescale | styleNoLabel );

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

AlertIf( Buy, "SOUND C:\\Windows\\Media\\tada.wav", "Buy",2);
AlertIf( Short, "SOUND C:\\Windows\\Media\\notify.wav", "Short", 2 );

_SECTION_BEGIN("Ramesh Swing1");
no=Param( "Swing", 10, 1, 55 );
tsl_col=ParamColor( "Color", colorYellow );

res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);

// line code
Plot(tsl, _DEFAULT_NAME(), colorYellow, styleStaircase); 

// ribbon code
//  Plot( 2, "Ribbon",IIf(C>tsl,colorBlue,colorRed),styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

// arrows code
Buy=Cross(C,Ref(res,-1));
Sell=Cross(Ref(sup,-1),C);
shape=Buy*shapeUpArrow + Sell*shapeDownArrow;
PlotShapes(shape,colorBlack,0,IIf(Buy,Low,High));

// scanner code
Buy=Cross(C,tsl);
Sell=Cross(tsl,C);

_SECTION_END();

_SECTION_BEGIN("_trend detection bands (overlay)");

BDay= Optimize("upper-lines",Param("upper-lines",1,1,28,1),1,21,1);//2
SDay= Optimize("lower-lines",Param("lower-lines",1,1,28,1),1,21,1);//7

Hc=(O+H+L+C)/4;

Ho = AMA( Ref( Hc,  -1 ),  0.5 );
Hh = Max( H,  Max( Hc,  Ho ) );
Hl = Min( L,  Min( Hc,  Ho ) );

HHb = HHV(hH,Bday);
HCb = HHV(hC,Bday);
LLb = LLV(hL,Bday);
LCb = LLV(hC,Bday);

HHs = HHV(hH,Sday);
HCs = HHV(hC,Sday);
LLs = LLV(hL,Sday);
LCs = LLV(hC,Sday);

for( i = 1+Max(Bday,Sday); i < BarCount; i++ )
{
if ((HHb[i-1] - LCb[i-1]) >= (HCb[i-1] - LLb[i-1])) Buyrange[i] = HHb[i-1] - LCb[i-1];
else
Buyrange[i] = HCb[i-1] - LLb[i-1];

if ((HHs[i-1] - LCs[i-1]) >= (HCs[i-1] - LLs[i-1])) Sellrange[i] = HHs[i-1] - LCs[i-1];
else
Sellrange[i] = HCs[i-1] - LLs[i-1];
};


Band_TopSMOOTH=AMA( Ho+Buyrange,  0.5 );
Band_BotSMOOTH=AMA( Ho-Sellrange,  0.5 );
Band_MidSMOOTH=(Band_TopSMOOTH+Band_BotSMOOTH)/2;

Band_Top=Ho+Buyrange;
Band_Bot=Ho-Sellrange;
Band_Mid=(Band_Top+Band_Bot)/2;

BS = ParamToggle("bands smooth","No|Yes",1);
BN = ParamToggle("bands normal","No|Yes",1);
DUL = ParamToggle("upper/lower bands","No|Yes",0); //disable upper/lower bands

if (BS AND DUL) Plot( Band_TopSMOOTH, " Band_TopSMOOTH ", IIf( Band_TopSMOOTH>Ref(Band_TopSMOOTH,-1), colorOrange, colorRed ));
if (BS) Plot( Band_MidSMOOTH, " Band_MidSMOOTH ", IIf( Band_MidSMOOTH>Ref(Band_MidSMOOTH,-1), colorGreen, colorRed ));
if (BS AND DUL) Plot( Band_BotSMOOTH, " Band_BotSMOOTH ", IIf( Band_BotSMOOTH>Ref(Band_BotSMOOTH,-1), colorDarkGreen, colorGreen ));
if (BS) Plot( 1, "ribbon", IIf( Band_MidSMOOTH>Ref(Band_MidSMOOTH,-1), colorGreen, IIf( Band_MidSMOOTH<Ref(Band_MidSMOOTH,-1), colorRed, colorPink )), styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

if (BN AND DUL)Plot( Band_Top, " Band_Top ", IIf( Band_Top>Ref(Band_Top,-1), colorOrange, colorRed ));
if (BN) Plot( Band_Mid, " Band_Mid ", IIf( Band_Mid>Ref(Band_Mid,-1), colorGreen, colorRed ));
if (BN AND DUL) Plot( Band_Bot, " Band_Bot ", IIf( Band_Bot>Ref(Band_Bot,-1), colorDarkGreen, colorGreen ));
if (BN) Plot( 2, "ribbon", IIf( Band_Mid>Ref(Band_Mid,-1), colorGreen, IIf( Band_Mid<Ref(Band_Mid,-1), colorRed, colorPink )), styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

_SECTION_END();