One half of cup formation is ready in afl- what about second half code?

rvlv

Active Member
#1
Hi friends
here is code by TJ for first half of cup formation.
Question is how to code the second half of cup formation??
Can somebody do the coding of second half of cup n handle formation please!

HTML:
//AmiBroker code
//AMIBROKER: IDENTIFYING CUP FORMATIONS EARLY-http://traders.com/Documentation/FEEDbk_docs/2011/04/TradersTips.html#TT6
/*
In “Identifying Cup Formations Early” in this issue, author Giorgos Siligardos presents a pattern-detection formula for cup formations.

Our AmiBroker Formula Language implementation is based on the algorithm given in Siligardos’ article; 
however, it operates NOT only on the last bar but also on user-selected bars.
 We have prepared a ready-to-use AmiBroker formula, which can be viewed below as well as at www.amibroker.com. 
To use the formula, enter the code set in the Afl Editor, then press “Insert Indicator” OR the “Send to Automatic Analysis” button.
 In the indicator mode, you can move the vertical selection line to check for the cup formation on any Date (NOT only the last bar). 
In the exploration mode, you simply run the exploration AND it will list detected cup formations if any are present for the Date selected as the end of the exploration range.
*/

Parameter = Param( "parameter", 1.5, 1, 10, 0.1 ); 

FilC = ( log( C ) ); 

eps = 0.0000000001; 
// basic Definitions 
semicupperiod = SelectedValue( Max( BarsSince( C >= SelectedValue( C * parameter ) ), 1 ) ) + 1; 

Ptop = SelectedValue( HHV( FilC, Semicupperiod ) ); 
Pbot = SelectedValue( LLV( FilC, Semicupperiod ) ); 
boxheight = SelectedValue( abs( Ptop - Pbot ) / 5 ); 
boxlength = SelectedValue( Max( int( semicupperiod / 5 ), 1 ) ); 

//   Grid Nodes 
bar = Cum( 1 ); 
b0 = SelectedValue( bar - semicupperiod + 1 ); 
b5 = SelectedValue( bar ); 
b1 = SelectedValue( Min( b0 + boxlength, b5 ) ); 
b2 = SelectedValue( Min( b1 + boxlength, b5 ) ); 
b3 = SelectedValue( Min( b2 + boxlength, b5 ) ); 
b4 = SelectedValue( Min( b3 + boxlength, b5 ) ); 
L2 = Pbot + 2 * boxheight; 
L3 = Pbot + 3 * boxheight; 

// Directional Strength 
Diff = FilC - Ref( FilC, -1 ); 
UpSum2 = Sum( Max( Diff, 0 ), 2 * boxlength ); 
DnSum2 = Sum( Max( -Diff, 0 ), 2 * boxlength ); 
DSX1 = abs( UpSum2 - DnSum2 ) / ( eps + UpSum2 + DnSum2 ) * 100; 
UpSum3 = Sum( Max( Diff, 0 ), 3 * boxlength ); 
DnSum3 = Sum( Max( -Diff, 0 ), 3 * boxlength ); 
DSX2 = abs( UpSum3 - DnSum3 ) / ( eps + UpSum3 + DnSum3 ) * 100; 

// Coditions 
isSemicup = ( semicupperiod >= 20 ) AND 
            ( Ref( DSX1, -( b5 - b2 ) ) > 25 ) AND 
            ( DSX2 < 25 ) AND 
            ( Cum( IIf( bar >= b2, FilC > L3, 0 ) ) == 0 ) AND 
            ( Cum( IIf( bar >= b4, FilC > L2, 0 ) ) == 0 ); 

LIS = SelectedValue( isSemicup ); 
Lastbar = SelectedValue( Cum( bar ) ); 


Line = LIS * ( ValueWhen( LIS * bar == b0, 1 ) * 
       ( ( exp( Ptop ) - exp( Pbot ) ) / 
         ( bar - b0 + 2 ) * 2 + 0.98 * exp( Pbot ) ) ); 

if( LIS ) Plot( Line , "IsSemiCupPlot", colorRed, styleThick ); 
Plot( C, Date() + " Close", ParamColor("Color", colorBlack ), styleBar ); 
MA50 =MA(C,50);

Filter = isSemicup; 
AddColumn( semicupperiod, "Semicup period", 1.0 );
AddColumn( Close, " close", 1.2 );
AddColumn( ATR(14), " ATR(14)", 1.2 );
AddColumn( RSI(14), " RSI(14)", 1.1 );
AddColumn( Close-MA50, " C-MA50", 1.1 );
AddColumn( MA(V,20)/1000, " MA(V,20)/1000", 1.0 );






how do you handle it in a simple shortcut way?
make some assumptions first.
1. cup pattern begins at a high and ends at almost same level high on rightside
2.LHS and RHS of cup remain under a resistance line
3.Afl code makes half the cup line or curve. we need the right side half.
4 click INSERT-ARC- and try to draw a matching arc along with leftside afl drawn arc and extend same to rightside.
now you know where the handle will begin.
all you do is wait for a breakout above rightside arc top or resistance line above price breakout
 
Last edited:

rvlv

Active Member
#2
Hi friends

here is the afl code for leftside of cup and a base.
The question is
how can we modify this to code a base and rightside of cup?

HTML:
Parameter = Param( "parameter", 1.5, 1, 10, 0.1 ); 

FilC = ( log( C ) ); 

eps = 0.0000000001; 
// basic Definitions 
semicupperiod = SelectedValue( Max( BarsSince( C >= SelectedValue( C * parameter ) ), 1 ) ) + 1; 

Ptop = SelectedValue( HHV( FilC, Semicupperiod ) ); 
Pbot = SelectedValue( LLV( FilC, Semicupperiod ) ); 
boxheight = SelectedValue( abs( Ptop - Pbot ) / 5 ); 
boxlength = SelectedValue( Max( int( semicupperiod / 5 ), 1 ) ); 

//   Grid Nodes 
bar = Cum( 1 ); 
b0 = SelectedValue( bar - semicupperiod + 1 ); 
b5 = SelectedValue( bar ); 
b1 = SelectedValue( Min( b0 + boxlength, b5 ) ); 
b2 = SelectedValue( Min( b1 + boxlength, b5 ) ); 
b3 = SelectedValue( Min( b2 + boxlength, b5 ) ); 
b4 = SelectedValue( Min( b3 + boxlength, b5 ) ); 
L2 = Pbot + 2 * boxheight; 
L3 = Pbot + 3 * boxheight; 

// Directional Strength 
Diff = FilC - Ref( FilC, -1 ); 
UpSum2 = Sum( Max( Diff, 0 ), 2 * boxlength ); 
DnSum2 = Sum( Max( -Diff, 0 ), 2 * boxlength ); 
DSX1 = abs( UpSum2 - DnSum2 ) / ( eps + UpSum2 + DnSum2 ) * 100; 
UpSum3 = Sum( Max( Diff, 0 ), 3 * boxlength ); 
DnSum3 = Sum( Max( -Diff, 0 ), 3 * boxlength ); 
DSX2 = abs( UpSum3 - DnSum3 ) / ( eps + UpSum3 + DnSum3 ) * 100; 

// Coditions 
isSemicup = ( semicupperiod >= 20 ) AND 
            ( Ref( DSX1, -( b5 - b2 ) ) > 25 ) AND 
            ( DSX2 < 25 ) AND 
            ( Cum( IIf( bar >= b2, FilC > L3, 0 ) ) == 0 ) AND 
            ( Cum( IIf( bar >= b4, FilC > L2, 0 ) ) == 0 ); 

LIS = SelectedValue( isSemicup ); 
Lastbar = SelectedValue( Cum( bar ) ); 


Line = LIS * ( ValueWhen( LIS * bar == b0, 1 ) * 
       ( ( exp( Ptop ) - exp( Pbot ) ) / 
         ( bar - b0 + 2 ) * 2 + 0.98 * exp( Pbot ) ) ); 

if( LIS ) Plot( Line , "IsSemiCupPlot", colorBrightGreen, styleThick ); //RED
Plot( C, Date() + " Close", ParamColor("Color", colorBlack ), styleBar );