Cup & handle loop

I am trying to create a loop in order to detect a cup & handle but there is probably a problem with the loop code. For example on 8/18/20 the system detect a cup with the following properties: Cup duration =140 days , Right Lip high=150.01 Left Lip high=148.75

//CUP WITH HANDLE  

timeexit =Optimize("exit",50,50,50,5); 
 // LOOP TO FIND  CUP DURATION  
   CUPDUR=0; 
for (i = 220; i >= 90; i=i-10 )  
{HR=Ref(HHV(C,i/3),-1); HRB=Ref(HHVBARS(C,i/3),-1)+1;// RIGHT CUP HIGH
  CL=LLV(C,i-10); CLB=LLVBars(C,i-10);   //CUP LOW 
  HL=Ref(HHV(C,i-CLB),-CLB); HLB=Ref(HHVBARS(C,i-CLB),-CLB)+CLB; //LEFT CUP HIGH  
   CUPDUR=IIf( HLB>1.1*CLB AND HLB<i AND HLB>i-10 AND HRB<30 //CHECK DISTANCES
    AND Min(HR,HL)>CL+5*ATR(30) //CUP DEPTH   
AND abs((HL/HR-1)*100)<5 // CUP LIPS  
AND CUPDUR==0,i,CUPDUR) ;  }  

BUY=CUPDUR>=90 AND  Cross(C,Ref(HHV(C,30),-1)) ;
Sell=False;Short=False;Cover=False;
ApplyStop( stopTypeNBar, stopModeBars, timeexit ); // time exit  
 
//PLOT 
SetChartOptions( 0, chartShowArrows | chartShowDates ); 
Equity(1); // evaluate stops, all quotes 
SetOption("EveryBarNullCheck", True ); 
Plot( C, "Price", colorDefault, stylebar | styleThick );  
PlotShapes( IIf(  Buy, shapeUpArrow, 0 ), colorBlue, 0, Low );  
PlotShapes( IIf( Sell, shapeDOWNTriangle, 0 ), colorRed, 0, High );  
// Custom text labels displayed with PlotText 
dist = 1.4*ATR(10);  
for( i = 0; i < BarCount; i++ )  
{  
if( BUY[i] ) PlotTextSetFont("Cup1", "ARIAL", 12, i-1, L[ i ]-dist[i], colorblue, colorDefault, 0 );  
 if( SELL[i]==5 ) PlotText( "TIME EXIT" , i-4, H[ i ]+dist[i], colorRED, colorDefaulT ); 
  }  

Title = Name() + " " + Date() + " Low = " + Low + " Price = " + Close + " BarIndex= " + BarIndex()+ " Signal : " + WriteIf( Buy, "Buy Cup",  
          WriteIf( Sell == 1, "Sell",  
                  WriteIf( Sell == 5, "Time stop", "None" ) ) ) ;  
         //EXPLORATION 
Filter=Buy; 
AddTextColumn( FullName(), "FullName"); 
AddColumn(Close,"Close",1.2); 
AddColumn(CUPDUR,"CUP DUR",1.0);  //AddColumn(i,"i",1.0);    
AddColumn(HR,"RIGHT LIP",1.2);  AddColumn(HRB,"R LIP BARS",1);  
AddColumn(HL,"LEFT LIP",1.2); 
AddColumn(HLB,"L LIP BARS",1.0); 
AddColumn(CL,"CUP LOW",1.2); 
AddColumn(CLB,"CUP LOW BARS",1.0); 
AddColumn(ATR(30),"ATR",1.2); 
AddColumn(BarIndex(),"BARINDEX",1); 
AddColumn((Ref(O,TIMEEXIT+1)/Ref(O,1)-1)*100,"PERF",1.1); 

cup

1 Like

Your user account isn't marked with License verified badge. Did you purchase AmiBroker license?

Only users with License verified badge are allowed to post on this forum.

So before posting, make sure you verify your license as explained here.

1 Like

I forgot to mention that in the chart of JNJ in my previous post the exploration produced the following
Ticker Date Close Cup Duration Right Lip Left Lip Cup Low
JNJ 18/08/20 150.09 140 150.01 148.75 137.81

The Left Lip cup high during the last 140 days is obviously wrong as this is the high of the last 80 days and not 140 days indicated by the cup duration.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.