 Cup with Handle formation calculations using Pine.

First of all, ignore all other lines in the example chart except the two FAT lines. The two fat lines are the ones that define the Cup With handle or in the example chart: a Reversed Cup With Handle.

Note: Handle does not always develop and sometimes the final target price is reached without forming any handle.
This script can calculate both Cup With Handle ( CH ) and Reversed Cup With Handle ( RCH ). Just order the input values accordingly.

The script need two input parameters: The highest price in the Cup formation and the lowest price in the cup formation or vice versa for the Reversed Cup formation.

Best regards,
/Hull, 2015.05.20.16:31
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

Want to use this script on a chart?
```study(title="Cup With Handle", shorttitle="CH/RCH", overlay=true)

// Inputs should be: The edges of the potential cup, and the bottom of that potential cup formation.
// Usually the highest and lowest values of price inside a cup with handle formation.
// This code can do both Cup with handle(CH) and Reversed Cup with handle (RCH). Just use the correct order of the inputs.
// Note: There is not always a defined handle. Sometimes the final target is reached immediately without any noticable "handle".
Cup_edge = input(title="Cup edge", type=float, defval=0.0, minval=0.0)
Cup_bottom = input(title="Cup bottom", type=float, defval=0.0, minval=0.0)

// simple function for fibonacci calculations, if ever needed. Can be ignored for CH/RCH.
fib1618(x) => x*1.618
fib0618(x) => x*0.618
fib0382(x) => x*0.382

// These below calculations are not used in this CH/RCH example, but I leave them in here in case for experimentations.

A = Cup_bottom
B = fib0618(Cup_edge - Cup_bottom) + A
C = B - fib1618(Cup_edge - Cup_bottom)
D = C - fib0382(C - B)
E = D - Cup_edge + Cup_bottom

// Experimental plotting of CH/RCH fibowaves.
//plot(B,title='B wave end', color=green,linewidth=2,offset=15)
//plot(C,title='C wave end', color=blue,linewidth=2,offset=30)
//plot(D,title='D wave end', color=purple,linewidth=2,offset=45)
//plot(E,title='E wave end', color=red,linewidth=2,offset=60)

// This is what the example is going to plot. Simple Cup with Handle. Handle target and Final target.
Depth = Cup_edge - Cup_bottom
Handle = Cup_edge - (Depth*0.5)
Target = Cup_edge + (Depth*0.79)

// As the code below explains: blue is the final target price, red is the potential "handle" price.
plot(Handle, title='Handle', color=red, linewidth=2, offset=0 )
plot(Target, title='Target', color=blue, linewidth=2, offset=0 )
// Best regards, Hull
```