Perfect Trend Support Resistance



Perfect Trend Support Resistance
Perfect Trend Support Resistance


//www.aflcode.com
_SECTION_BEGIN("ADX Candle stick ");
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 ) ) ));
 if (SetBarFillColor( IIf( (C < O) AND (C < Ref( C, -1)), colorPlum, colorDarkGreen  ) ) );
else
(SetBarFillColor( IIf( (C > O)AND (C < Ref( C, -1)), colorWhite,colorWhite)));
Plot( C, "Close",IIf( Close < Ref( Close, -1), colorRed, colorGreen ), styleCandle );
_SECTION_END();

_SECTION_BEGIN("Detecting double tops and bottoms");
/* Detecting double tops and bottoms (for come into view, by Isfandi)*/ 
percdiff = 5; /* peak detection threshold */ 
fwdcheck = 5; /* forward validity check */ 
mindistance = 10; 
validdiff = percdiff/400; 

PK= Peak( H, percdiff, 1 ) == High; 
TR= Trough( L, percdiff, 1 ) == Low; 
x = Cum( 1 ); 
XPK1 = ValueWhen( PK, x, 1 ); 
XPK2 = ValueWhen( PK, x, 2 ); 
xTR1 = ValueWhen( Tr, x, 1 ); 
xTr2 = ValueWhen( Tr, x, 2 ); 

peakdiff = ValueWhen( PK, H, 1 )/ValueWhen( PK, H, 2 ); 
Troughdiff=ValueWhen( tr, L, 1 )/ValueWhen( tr, L, 2 ); 

doubletop = PK AND abs( peakdiff - 1 ) < validdiff AND (Xpk1 -Xpk2)>mindistance AND High > HHV( Ref( H, fwdcheck ), fwdcheck - 1 ); 
doubleBot=tr AND abs( troughdiff - 1 ) < validdiff AND (Xtr1 -Xtr2)>mindistance AND Low < LLV( Ref( L, fwdcheck ), fwdcheck - 1 ); 

Buy = doublebot; 
Sell = doubletop; 
for( i = 0; i < BarCount; i++ ) 
{ 
if( Buy[i] ) PlotText( "BOT " , i, L[ i ],colorYellow ); 
if( Sell[i] ) PlotText( "TOP" , i, H[ i ], colorWhite ); 
} 

WriteIf( Highest( doubletop ) == 1, "AmiBroker has detected some possible 
double top patterns for " + Name() + "\nLook for green arrows on the price 
chart.", "There are no double top patterns for " + Name() ); 

WriteIf(Lowest( doublebot)==1,"AmiBroker has detected some possible double 
bottom patterns for " + Name() + "\nLook for red arrows on the price 
chart.", "There are no double bottom patterns for " + Name() ); 
_SECTION_END();

_SECTION_BEGIN("My Name");
GfxSetBkColor(colorBlack);
GfxSetTextColor( colorGrey40 ); 
GfxSelectFont("Impact", 12, 500, True );
GfxTextOut("Isfandi Trading System II", 10 , 848 );
_SECTION_END();

_SECTION_BEGIN("Wall Indicator System_Isfandi");
//---- pivot points
DayH = TimeFrameGetPrice("H", inDaily, -1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);//low
DayC = TimeFrameGetPrice("C", inDaily, -1);//close
DayO = TimeFrameGetPrice("O", inDaily);// current day open
HiDay = TimeFrameGetPrice("H", inDaily);
LoDay = TimeFrameGetPrice("L", inDaily);
PP = (DayH + DayL + DayO + DayO) / 4 ;
R1 = (2 * PP) - DayL;
S1 = (2 * PP) - DayH;
R2 = PP + R1 - S1;
S2 = PP + S1 - R1;
R3 = R2 + (R1 - PP);
S3 = S2 - (PP - S1);
// OTHER INDICATOR
MOMETUM =RSI(14);
radius = 0.1 * Status("pxheight"); // get pixel height of the chart and use 45% for pie chart radius
textoffset = 0.1 * radius;
GfxSelectFont("Arial", 9, 500, True );
GfxSetTextColor( colorGrey50 );
GfxTextOut("PIVOT POINT", textoffset , 288 );
GfxSetTextColor( colorGrey50 );
GfxTextOut("SIGNAL", textoffset , 188 );
GfxSetTextColor( colorGrey50);
GfxTextOut( "TREND", textoffset ,386 );
GfxSelectFont("Arial", 9 );
GfxSetTextColor( colorGreen);
GfxTextOut( "R1 =" + R1, textoffset + 1, 320 );
GfxSetTextColor( colorLime );
GfxTextOut( "R2 = " +R2, textoffset + 1, 305 );
GfxSetTextColor( colorWhite );
GfxTextOut( "PP = " +PP, textoffset + 1, 335 );
GfxSetTextColor( colorOrange );
GfxTextOut( "S1 = " +S1, textoffset + 1, 350 );
GfxSetTextColor( colorRed );
GfxTextOut( "S2 = " +S2, textoffset + 1, 365 );
GfxSelectFont("Arial", 9 );
_SECTION_END();
//////////////////////////////////////////////////////////////////////
_SECTION_BEGIN("Elder Impulse");
GfxSelectFont("Arial", 8, 500, True );
///////////////////////////////////////////////////
SetChartOptions(0,chartShowArrows|chartShowDates);
EnableTextOutput(False);
// User-defined parameter for EMA periods
EMA_Type = Param("EMA-1, TEMA-2, JMA-3", 2, 1, 3, 1);
EMA_prds = Param("EMA_periods", 7, 1, 30, 1);
Std_MACD = Param("Standard MACD? No-0, Yes-1", 1, 0, 1, 1);
Plot_fashion = Param("Bar+Arrows-1, Impulse Bars-2", 2, 1, 2, 1);
// Allow user to define Weekly and Monthly Ribbon Location and Height
WR_P1 = Param("Weekly Ribbon Location", 5.2, -1000, 1000, 0.1);
WR_P2 = Param("Weekly Ribbon Height", 199, -0.001, 500, 0.1);
//MR_P1 = Param("Monthly Ribbon Location", 5.2, -1000, 1000, 0.1);
//MR_P2 = Param("Monthly Ribbon Height", 199, -0.001, 500, 0.1);
// Compute EMA and MACD Histogram
if(EMA_Type == 1)
{
DayEMA = EMA(Close, EMA_prds);
}
if (EMA_Type == 2)
{
DayEMA = TEMA(Close, EMA_prds);
}
Histogram = MACD() - Signal();
// Determine if we have an Impulse UP, DOWN or None
Impulse_Up = DayEMA > Ref(DayEMA, -1) AND Histogram > Ref(Histogram, -1);
Impulse_Down = DayEMA < Ref(DayEMA, -1) AND Histogram < Ref(Histogram, -1);
Impulse_None = (NOT Impulse_UP) AND (NOT Impulse_Down);
// Compute Weekly MACD and determine whether rising or falling
TimeFrameSet(inWeekly);
if (Std_MACD == 0)
{
MACD_val = MACD(5, Cool);
Signal_val = Signal(5, 8, 5);
}
else
{
MACD_val = MACD(12, 26);
Signal_val = Signal(12, 26, 9);
}
Hist_in_w = MACD_val - Signal_val;
wh_rising = Hist_in_w > Ref(Hist_in_w, -1);
wh_falling = Hist_in_w < Ref(Hist_in_w, -1);
wh_none = (NOT wh_rising) AND (NOT wh_falling);
TimeFrameRestore();
// Now get Monthly MACD Histogram....
TimeFrameSet(inMonthly);
MACD_val = MACD(5, 8);
Signal_val = Signal(5, 8, 5);
Hist_in_m = MACD_val - Signal_val;
mh_rising = Hist_in_m > Ref(Hist_in_m, -1);
mh_falling = Hist_in_m < Ref(Hist_in_m, -1);
TimeFrameRestore();
wh_rising = TimeFrameExpand( wh_rising, inWeekly, expandLast );
wh_falling = TimeFrameExpand( wh_falling, inWeekly, expandLast);
wh_none = TimeFrameExpand( wh_none, inWeekly, expandLast);
mh_rising = TimeFrameExpand(mh_rising, inMonthly, expandLast);
mh_falling = TimeFrameExpand(mh_falling, inMonthly, expandLast);
kol = IIf( wh_rising, colorBrightGreen, IIf(wh_falling, colorRed, IIf(wh_none, colorCustom11, colorLightGrey)));
mkol = IIf( mh_rising, colorBlue, IIf(mh_falling, colorYellow, colorLightGrey));
if (Plot_fashion == 1)
{
}
else
{
bar_kol = IIf(impulse_UP, colorBrightGreen, IIf(impulse_Down, colorRed, colorCustom11));
}
Impulse_State = WriteIf(Impulse_Up, "Bulllish", WriteIf(Impulse_Down, "Bearish", "Neutral"));
GfxSetTextColor( colorPink );
GfxTextOut( "IMPULSE STATE = " +Impulse_State, textoffset + 1, 402 );
// Set the background color for Impulse Status Column
Impulse_Col = IIf(Impulse_Up, colorGreen, IIf(Impulse_Down, colorRed, colorGrey40));
// Determine Weekly Trend. Display as Text Column
Weekly_Trend = WriteIf(wh_rising, "Rising", WriteIf(wh_falling, "Falling", "Flat!"));
Weekly_Col = IIf(wh_rising, colorGreen, IIf(wh_falling, colorRed, colorLightGrey));
GfxSetTextColor( colorPink);
GfxTextOut( "WEEKLT TREND = " +Weekly_Trend, textoffset + 1, 417 );
// Determine Monthly Trend. Display as Text Column
Monthly_Trend = WriteIf(mh_rising, "Rising", WriteIf(mh_falling, "Falling", "Flat!"));
Monthly_Col = IIf(mh_rising, colorGreen, IIf(mh_falling, colorRed, colorLightGrey));
GfxSetTextColor( colorPink );
GfxTextOut( "MONTHLY TREND = " +Monthly_Trend, textoffset + 1, 432 );
// Determine how many bars has the current state existed
bars_in_bull = Min(BarsSince(impulse_none), BarsSince(impulse_down));
bars_in_bear = Min(BarsSince(impulse_up), BarsSince(impulse_none));
bars_in_neut = Min(BarsSince(impulse_down), BarsSince(impulse_up));
// Set a single variable to show number of bars in current state depending upon
// actual Impulse Status - Bullish, Bearish or Neutral
bars_in_state = IIf(Impulse_Up, bars_in_bull, IIf(Impulse_down, bars_in_bear, bars_in_neut));
//weekly pivot
k1=-1;
SetChartBkColor(16 ) ;
k=IIf(ParamList("select type","daily|next day")=="daily",-1,0);
k1=-1;
TimeFrameSet(inDaily);
day_h= LastValue(Ref(H,K));
day_l= LastValue(Ref(L,K));
day_c= LastValue(Ref(C,K));
TimeFrameRestore();
// day
DH=Day_h;
DL=Day_L;
DC=Day_C;
// DAY PIVOT Calculation
pd = ( DH+ DL + DC )/3;
sd1 = (2*pd)-DH;
sd2 = pd -(DH - DL);
sd3 = Sd1 - (DH-DL);
rd1 = (2*pd)-DL;
rd2 = pd +(DH -DL);
rd3 = rd1 +(DH-DL);
WriteVal( StochK(39) - StochK(12) );
GfxSelectFont("Arial", 8 );
_SECTION_END();

_SECTION_BEGIN("STOCHASTIC");
periods = Param( "Periods", 15, 1, 200, 1 );
Ksmooth = Param( "%K avg", 3, 1, 200, 1 );
a = StochK( periods , Ksmooth);
Dsmooth = Param( "%D avg", 3, 1, 200, 1 );
b = StochD( periods , Ksmooth, DSmooth );
Buy = Cross( a, b );
Sell = Cross( b, a );
STOCH =WriteIf(a>b , "BUY","SELL");
GfxSetTextColor( colorYellow );
GfxTextOut( "STOCH = " +STOCH, textoffset + 1, 229 );
GfxSelectFont("Arial", 8 );

_SECTION_BEGIN("Van K Tharp Efficient Stocks");
// Mark Keitel 12-05
// Van K Tharp Efficient Stocks
GfxSelectFont("Arial", 8, 500, True );
ATRD180 = ATR(180);
ATRD90 = ATR(90);
ATRD45 = ATR(45);
ATRD20 = ATR(20);
ATRD5 = ATR(5);
DifferenceD180 = Close - Ref(Close,-180);
DifferenceD90 = Close - Ref(Close,-90);
DifferenceD45 = Close - Ref(Close,-45);
DifferenceD20 = Close - Ref(Close,-20);
DifferenceD5 = Close - Ref(Close,-5);
EfficiencyD180 = IIf(ATRD180!=0,DifferenceD180/ATRD180,1);
EfficiencyD90 = IIf(ATRD90!=0,DifferenceD90/ATRD90,1);
EfficiencyD45 = IIf(ATRD45!=0,DifferenceD45/ATRD45,1);
EfficiencyD20 = IIf(ATRD20!=0,DifferenceD20/ATRD20,1);
EfficiencyD5 = IIf(ATRD5!=0,DifferenceD5/ATRD5,1);
Averageeff = (EfficiencyD180 + EfficiencyD90 + EfficiencyD45 +
EfficiencyD20 ) / 4;
//Plot( averageeff, "AvgEff", colorLightBlue, styleLine );
x = Param ( "Add Results to a Watchlist? Yes = 1, No = 2" , 2 , 1 , 2 , 1 ) ;  // select whether to add results to watchlist or not
y = Param("Set Watchlist Number", 25, 2, 60,1);    // sets the watchlist number, but reserves the first 2 and last 4 watchlists
//  --------  Parameter Variables for Exploration  --------------------------------
TCH = Param("High close value ", 20, 5, 300, 0.5);
TCL  = Param("Low close value " , 5, 1, 10, 0.25);
AVP = Param("Period for Avg Vol " , 21, 10, 240, 1);
SV = Param("Stock minimum Avg Vol " , 125000, 50000, 1000000, 500000);
//  --------------  Organize the exploration results ------------------------------------
P = Param("Period for Price Delta Comparisons", 1, 1, 120, 1);
P1 = Param("Period for Other Delta Comparisons", 3, 1, 120, 1);
PATR = Param("Period for ATR", 5,1,21,1);
PADX = Param("Period for ADX", 14, 3, 34,1);
PRSI = Param("Period for RSI", 13, 3, 34,1);
ADX_TREND =WriteIf(PDI(PADX)>MDI(PADX),"_ TREND UP","_TREND DOWN");
GfxSetTextColor( colorRed );
GfxTextOut( "RSI(14) = " + MOMETUM +  ADX_TREND , textoffset + 1,465 );
ADX_14 = ADX(14);
GfxSetTextColor( colorLightBlue );
GfxTextOut( "ADX_TREND = " +ADX_14+ ADX_TREND, textoffset + 1, 452 );
_SECTION_END();


_SECTION_BEGIN("Gann HiLo");
/*Gann HiLo*/
pds=Param("Stop Period",4,2,10,0.25);

barcolor=
 IIf(Outside(),colorYellow,
   IIf(L<Ref(L,-1) && H<=Ref(H,-1),colorRed,
     IIf(H>Ref(H,-1) && L>=Ref(L,-1),colorBrightGreen,colorBlue)));

Hld = IIf(C > Ref(MA(H, 2), -1), 1, IIf(C < Ref(MA(L, 2), -1), -1, 0));
Hlv = ValueWhen(Hld != 0, Hld, 1);
Hilo = IIf(Hlv == -1, MA(H, 2), MA(L, 2));
Trigger = IIf(C>Hilo, colorBrightGreen, colorRed);
Buy = Close > Hilo;
Sell = Close < Hilo;
GannHiLo =WriteIf(C>Hilo,"BUY","SELL");
GfxSetTextColor( colorBrightGreen );
GfxTextOut( "GannHiLo = " +GannHiLo, textoffset + 1, 255 );
GfxSelectFont("Arial", 8 );
_SECTION_END();

_SECTION_BEGIN("Indicator ZeroLag W%R");
/*ZeroLag W%R*/
"========";
R = ((HHV(H,12) - C) /(HHV (H,14) -LLV (L,14))) *-100;
Period= 10;
EMA1= EMA(R,Period);
EMA2= EMA(EMA1,5);
Difference= EMA1 - EMA2;
ZeroLagEMA= EMA1 + Difference;
PR=100-abs(ZeroLagEMA);
Buy=Cross(R,ZeroLagEMA);
Sell=Cross(ZeroLagEMA,R);
ZeroLagEMA =WriteIf(R > ZeroLagEMA, "BUY","SELL");
GfxSetTextColor( colorOrange );
GfxTextOut( "William%R = " +ZeroLagEMA, textoffset + 1, 216 );
GfxSelectFont("Arial", 8 );
_SECTION_END();

_SECTION_BEGIN("PVR");
"PVR";
P1=Ref(C,-1);
V1=Ref(V,-1);
PVR=IIf(C>P1 AND V>V1,4,
    IIf(C>P1 AND V<V1,3,
    IIf(C<P1 AND V<V1,2,1)));
GraphXSpace=10;
A=EMA(PVR,5);
B=EMA(A,3);
Buy = Cross(A, B);
Sell = Cross(B, A);
PVR =WriteIf(A > B, "BUY","SELL");
GfxSetTextColor( colorRed );
GfxTextOut( "PVR = " +PVR, textoffset + 1, 203 );
GfxSelectFont("Arial", 8 );
_SECTION_END();

_SECTION_BEGIN("ASHISHDA MACD");
a=TSF(C,3)+TSF(C,5)+TSF(C,8)+TSF(C,13)+TSF(C,17)+TSF(C,26);
b=TSF(C,8)+TSF(C,13)+TSF(C,26)+TSF(C,35)+TSF(C,50)+TSF(C,56);
mova= TSF(a,3);
movb=TSF(b,26);
diff= a + mova;
diff1= b + movb;
m= diff - diff1;
m1= TSF(m,5)*.5;
Buy = Cross(m1,Ref(m1, -1));
Sell = Cross(Ref(m1, -1),m1);
QuickMACD =WriteIf(m1>Ref(m1, -1), "BUY","SELL");
GfxSetTextColor( colorGreen);
GfxTextOut( "MACD = " +QuickMACD, textoffset + 1, 242 );
GfxSelectFont("Arial", 8 );
_SECTION_END();

_SECTION_BEGIN("SECONDARY LANDIS");
x = ((StochK(9,3)*0.05)+(StochK(182,9)*0.43)+(StochK(34,13)*0.26)+(StochK(34,8)*0.16)+(StochK(21,5)*0.10));
y = MA(x,15);

Buy= Cross (x,y);
Sell= Cross (y,x);
LANDIS =WriteIf(x > y, "SECONDARY UPTREND","SECONDARY DOWNTREND");
GfxSetTextColor( colorPink );
GfxTextOut( "LANDIS = " +LANDIS, textoffset + 1, 491 );
GfxSelectFont("Arial", 8 );

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
_SECTION_BEGIN("Average");
P = ParamField("Field");
Type = ParamList("Type", "Weighted,Lagless-21,Hull-26,Linear Regression-45,Exponential,Double Exponential,Tripple Exponential,Wilders,Simple");
Periods = Param("Periods", 14, 2, 100 );
Displacement = Param("Displacement", 1, -50, 50 );
m = 0;

if( Type == "Weighted" )     m=  WMA( P, Periods );
if( Type == "Lagless-21"  )    m=  2*EMA(P, Periods)-EMA(EMA(P, Periods), Periods); 
if( Type == "Hull-26"  )    m=  WMA(2*(WMA(P, Periods/2))-WMA(P, Periods) ,4 );
if( Type == "Linear Regression-45"  ) m=  LinearReg( P, Periods );
if( Type == "Exponential" )    m = EMA( P, Periods );
if( Type == "Double Exponential" )  m = DEMA( P, Periods );
if( Type == "Tripple Exponential" )  m = TEMA( P, Periods );
if( Type == "Wilders" )     m = Wilders( P, Periods );
if( Type == "Simple" )      m = MA( P, Periods );

Buy=Cover=Cross(C,m);
Sell=Short=Cross(m,C);
AveragePrice =WriteIf(C > m, "ABOVE AVERAGE","BELOW AVERAGE");
GfxSetTextColor( colorDarkYellow );
GfxTextOut( "PRICE = " +averageprice, textoffset + 1, 504 );
GfxSelectFont("Arial", 8 );

_SECTION_BEGIN("SAR ARROW");
Plot( Close, "C", colorBlack, styleCandle ); 
acc = Param("Acceleration", 0.02, 0, 1, 0.001 ); 
accm = Param("Max. acceleration", 0.2, 0, 1, 0.001 ); 
S = SAR(); 
Buy = Cross( Close, S ); 
Sell = Cross( S, Close ); 
SARPhase =WriteIf(Close>S,"BUY","SELL");
GfxSetTextColor( colorRed);
GfxTextOut( "S A R = " +SARPhase, textoffset + 1, 517 );
GfxSelectFont("Arial", 8 );
_SECTION_END();

_SECTION_BEGIN("Stochastic Momentum Index");
period1 = 12;
period2 = 6;
period3 = 6;
SMI = 
100 * ( EMA( EMA( C - (0.5 * ( HHV(H,period1) + LLV(L,period1))),period2),period3)/(0.5*EMA( EMA( HHV(H,period1) - LLV(L,period1),period2),period3)));
Buy= Cross (SMI,MA( SMI, 6 ));
Sell= Cross (MA( SMI, 6 ),SMI);
Momentum =WriteIf(SMI > MA( SMI, 6 ), "BUY","SELL");
GfxSetTextColor( colorOrange );
GfxTextOut( "Stoch Momentum = " +Momentum, textoffset + 1, 530 );
GfxSelectFont("Arial", 8 );
_SECTION_END();

_SECTION_BEGIN("JMAHiLo");
function JMA( array, per )
{ 
TN1=MA(array,per); 
s1=0;
for( i = 0; i < per; i=i+1 ) 
{
s1=s1+((per-(2*i)-1)/2)*Ref(array,-i); 
} 
return TN1+(((per/2)+1)*S1)/((per+1)*per);
} 
JMAperiods = Param( "JMA Periods", 10, 1, 200, 1 );
_SECTION_BEGIN("RWIHILO");
minperiods = Param( "Min Periods", 2, 1, 200, 1 );
maxperiods = Param( "Max Periods", 8, 1, 200, 1 );
varLo = RWILo( minperiods, maxperiods);
minperiods = Param( "Min Periods", 2, 1, 200, 1 );
maxperiods = Param( "Max Periods", 8, 1, 200, 1 );
varHi = RWIHi( minperiods, maxperiods) ;
Buy= Cross (JMA(varHi ,JMAperiods),JMA(varLo ,JMAperiods));
Sell=Cross (JMA(varLo ,JMAperiods),JMA(varHi ,JMAperiods));
JMAHiLo =WriteIf(JMA(varHi ,JMAperiods) > JMA(varLo ,JMAperiods), "BUY","SELL");
GfxSetTextColor( colorGreen );
GfxTextOut( "JMAHiLo = " +JMAHiLo, textoffset + 1, 543 );
GfxSelectFont("Arial", 8 );
_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[i] < NW[i-1])                 
  {
   Trend[i] = -1;                   
   NW[i] = j[i] + Revers[i];        
  }
  else                              
  {
   Trend[i] = 1;
   if((j[i] - Revers[i]) > NW[i-1])
   {
    NW[i] = j[i] - Revers[i]; 
   }
   else
   {
    NW[i] = NW[i-1];
   }
  } 
 }
 if(Trend[i-1] == -1)               
 {
  if(j[i] > NW[i-1])                
  {
   Trend[i] = 1;                    
   NW[i] = j[i] - Revers[i];        
  }
  else                              
  {
   Trend[i] = -1;
   if((j[i] + Revers[i]) < NW[i-1]) 
   {
    NW[i] = j[i] + Revers[i]; 
   }
   else
   {
    NW[i] = 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 );
GfxTextOut( "SWING = " +SWING, textoffset + 1, 268 );
GfxSelectFont("Arial", 8 );
shape = Buy * shapeCircle + Sell * shapeCircle;
_SECTION_END();

_SECTION_BEGIN("Sigma Bands");
A = Param("Periods?",21,1,300,1);

x = MA(C,a);

x1  = x+1*StDev(C,a);
x2  = x+2*StDev(C,a);
x3  = x+3*StDev(C,a);

y1 = x-1*StDev(C,a);
y2 = x-2*StDev(C,a);
y3 = x-3*StDev(C,a);

Plot(x1, "", colorDarkGreen, styleDashed);
Plot(x2, "", colorDarkGreen, styleThick | styleDashed);
Plot(x3, "", colorDarkGreen, styleThick);

Plot(y1, "", colorDarkRed, styleDashed);
Plot(y2, "", colorDarkRed, styleThick | styleDashed);
Plot(y3, "", colorDarkRed, styleThick);
_SECTION_END();


_SECTION_BEGIN("Pivot_Finder");
/* **********************************

Code to automatically identify pivots

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

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


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, shapeHollowSquare, shapeNone), colorRed, 0, H, 15);

PlotShapes(

IIf(aLPivs==1, shapeHollowSquare , shapeNone), colorLime, 0, L, -15);

_SECTION_BEGIN("Graphics");
GrpPrm=Param("Graphic Space",1,0,10);
GraphXSpace=GrpPrm;
_SECTION_END();

_SECTION_BEGIN("channels");
//Pattern Recognition graph
//with shading
/*TRENDLINES BREAKOUT*/
 
 
x = Cum(1);
per = Param("Percent",3,1,5,1);
s1=L;
s11=H;
pS = TroughBars( s1, per, 1 ) == 0;
endt= LastValue(ValueWhen( pS, x, 1 ));
startt=LastValue(ValueWhen( pS, x, 2 ));
endS = LastValue(ValueWhen( pS, s1, 1 ) );
startS = LastValue( ValueWhen( pS, s1, 2 ));
dtS =endt-startt;
aS = (endS-startS)/dtS;
bS = endS;
trendlineS = aS * ( x -endt ) + bS;
 
pR = PeakBars( s11, per, 1 ) == 0;
endt1= LastValue(ValueWhen( pR, x, 1 ));
startt1=LastValue(ValueWhen( pR, x, 2 ));
endR = LastValue(ValueWhen( pR, s11, 1 ) );
startR = LastValue( ValueWhen( pR, s11, 2 ));
DTR=ENDT1-STARTT1;
aR = (endR-startR)/dtR;
bR = endR;
trendlineR = aR * ( x -endt1 ) + bR;
BEGIN=Min(STARTT,STARTT1);
TRS = IIf(x>BEGIN-10,trendlineS,-1e10);
TRR = IIf(x>BEGIN-10,trendlineR,-1e10);
 
Condcolor=(Cross(C,trendlineR) AND X>ENDT1) OR (Cross(trendlineS,C) AND
X>ENDT);
BarColor=IIf(Condcolor,7,1);
Plot(C,"Close",BarColor,styleCandle); 
Plot(TRS,"Support",colorYellow,styleLine);
Plot(TRR,"Resist",colorYellow,styleLine);
/////////////////////////////////////
// Shading // 
////////////////////////////////////
fill=Param("style",2,1,2,1);
style=IIf(fill==1,styleHistogram,IIf(fill==2,styleArea,Null));
x=IIf(trs > trr,trr,trs);
Plot(x,"",colorBlack,style);
Plot(trr,"",colorDarkTeal,style);
////////////////////////////////////////////
// Pattern Recognition in title bar // 
///////////////////////////////////////////
Lowline=Ends-starts;
Highline=endr-startr;
Wedge=IIf(Highline <0 AND Lowline > 0,1,0);
DecendingTriangle=IIf(Highline < 0 AND Lowline==0,1,0);
AscendingTriangle=IIf(Highline==0 AND Lowline > 0,1,0);
DownChannel=IIf(Highline<0 AND Lowline<0,1,0);
UpChannel=IIf(Highline>0 AND Lowline>0,1,0);
BroadeningWedge=IIf(Highline > 0 AND Lowline < 0,1,0);
///////////////////////////////////
// Bullish or Bearish breakout //
//////////////////////////////////
BullishBreakout=Cross(C,trendlineR);
BearishBreakout=Cross(trendlineS,C);
Breakout =WriteIf(C > trendlineR, "Bullish","Bearish");
GfxSetTextColor( colorWhite );
GfxTextOut( "Breakout = " +Breakout, textoffset + 1, 558 );
GfxSelectFont("Arial", 8 );
 
Title=Name()+ WriteIf(wedge==1," Wedge","")+WriteIf(DecendingTriangle==1," Decending Triangle","")+WriteIf(AscendingTriangle==1,"Ascending Triangle","")/*+WriteVal(Lowline)*/+WriteIf(DownChannel==1," Downward Sloping Channel","")+WriteIf(UpChannel==1," Upward Sloping Channel","")+WriteIf(broadeningWedge==1," Broadening Wedge","")+"\n"+EncodeColor(colorGreen)+WriteIf(bullishbreakout,"Bullish Breakout",EncodeColor(colorRed)+WriteIf(bearishbreakout,"Bearish Breakout",""))/*+WriteVal(highline)*/; 
/////////////////////////////////////
// Automatic Analysis //
////////////////////////////////////
Filter=AscendingTriangle OR DecendingTriangle OR Wedge OR DownChannel OR UpChannel OR BroadeningWedge OR Bullishbreakout OR bearishbreakout;
AddTextColumn(WriteIf(bullishbreakout,"bullish Breakout",""),"BullBreak"); 
AddTextColumn(WriteIf(bearishBreakout,"Bearish Breakout",""),"BearBreak"); 
AddTextColumn(WriteIf(ascendingtriangle,"Ascending Triangle",WriteIf(decendingtriangle,"Decending Triangle",WriteIf(Wedge,"Wedge",WriteIf(DownChannel,"Down Channel",WriteIf(UpChannel,"Up Channel",WriteIf(BroadeningWedge,"Broadening Wedge","")))))),"Pattern");
_SECTION_END();

_SECTION_BEGIN("Calculation Bar");
Version(4.70); // needs AmiBroker 4.70
function msPattPos( element )
{
   Value1 = Ref( H, -1 );
   Value2 = Ref( L, -1 );
   Value3 = ( Value1 + Value2 )/2;
   Dist = Ref( ATR( 10 ), -1 );
   Value4 = Value1 + Dist;
   Value5 = Value2 - Dist;
   result = IIf( element < Value5, 0,
            IIf( element < Value2, 1,
            IIf( element < Value3, 2,
            IIf( element < Value1, 3,
            IIf( element < Value4, 4,
                              5 ) ) ) ) );
  return result;
}
function msPattToText( patt )
{
 result =
 WriteIf( patt == 0, " is below previous (Low - ATR 10) ",
 WriteIf( patt == 1, " is above (at) previous (Low - ATR 10) and below previous Low ",
 WriteIf( patt == 2, " is above (at) previous Low and below previous Midpoint ",
 WriteIf( patt == 3, " is above (at) previous Midpoint and below previous High ",
 WriteIf( patt == 4, " is above (at) previous High and below (High + ATR 10) ",
 " is above (at) previous (High + ATR(10) " ) ) ) ) );
 return result;
}
function msRecognize()
{
   return 1000 * msPattPos( Open ) + 100 * msPattPos( High ) +
          10 * msPattPos( Low ) + msPattPos( Close );
}
function msPatternDescription( patt )
{
   return "Open: " + msPattToText( round( ( patt / 1000 ) % 10 ) ) + "\n" +
          "High: " + msPattToText( round( ( patt / 100 ) % 10 ) ) + "\n" +
          "Low: " + msPattToText( round( ( patt / 10 ) % 10 ) ) + "\n" +
          "Close: " + msPattToText( round( patt % 10 ) );
}
patts = msRecognize();
// by default use pattern occuring at selected bar
DesiredPattern = SelectedValue( patts );
// if you want manual-entry of pattern code from parameter dialog
// then uncomment the line below
//DesiredPattern=Param("Pattern to look for", 3434, 0, 5555, 0 );
Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g", O, H, L, C);
Title = Title + "\nPattern code is : " + DesiredPattern + "\n" + msPatternDescription( DesiredPattern  );
//Plot(C, "Price", ParamColor("Color", colorGrey40), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
PattCloseAbove = DesiredPattern == patts AND Ref( Close > Open, 1 );
PattCloseBelow = DesiredPattern == patts AND Ref( Close < Open, 1 );
//PlotShapes(    ( DesiredPattern == patts ) * shapeCircle , IIf( PattCloseAbove, colorDarkGreen, IIf( PattCloseBelow, colorDarkRed, colorDarkBlue ) ), 0, High, 30 );
NumPatterns = LastValue( Cum( DesiredPattern == patts ) );
NumPattCloseAbove = LastValue( Cum( PattCloseAbove ) );
NumPattCloseBelow = LastValue( Cum( PattCloseBelow ) );
NumPattCloseEqual = NumPatterns - NumPattCloseAbove - NumPattCloseBelow;
Title = Title +
"\n\nTotal number of Patterns: " + NumPatterns +
"\n% on Total Bars: " + 100 * NumPatterns/BarCount +
"\nIn the next bar\n" +
EncodeColor(colorGreen) +
"Close has been above the open " + NumPattCloseAbove +
" (" + NumPattCloseAbove / NumPatterns + "%) times\n" +
EncodeColor(colorRed) +
"Close has been below the open " + NumPattCloseBelow + " (" +
NumPattCloseBelow / NumPatterns +" %) times\n" +
EncodeColor(colorGrey50) +
"Close has been equal to the open " + NumPattCloseEqual + " (" +
NumPattCloseEqual / NumPatterns +" %)times";
_SECTION_END();

Previous
Next Post »

1 Comments:

Write Comments