An ideal sinusoidal has 4 phases:
Convex AND Ascending;// colorDarkGreen
Concave AND Ascending;// colorTurquoise
Concave AND Descending;// colorDarkRed
Convex AND Descending;// colorPink
The AFL* code illustrates the example
// The 4 phases of an ideal sinusoidal
freq = 1;
y=sin( Cum( freq/10 ) );
t=1;
Convex=(y-Ref(y,-t))/t>=(y-Ref(y,-(t+1)))/(t+1);
Concave=NOT(Convex);
Ascending=y>=Ref(y,-1);
Descending=NOT(ascending);
Bullstart=Convex AND Ascending;
Bullend=Concave AND ascending;
Bearstart=Concave AND Descending;
Bearend=Convex AND Descending;
Color=IIf(Bullstart,colorDarkGreen,IIf(Bullend,colorTurquoise,IIf(Bearstart,colorDarkRed,colorPink)));
Plot(y,"",Color,8);
[see sin.gif]
We shall use a smoothing procedure [IIR2 filter **] as close to the sinusoidal as possible. [see IIR2.gif]
Of course the trend characteristics do not give the [-1, +1] oscillation, but the convexity/concavity are still detectable.
The superimpose of actual price candles gives a more descriptive picture of the 4 phases.
[see 4phases.gif]
The full AFL* code is
// The 4 phases of a stock graph
// A. Smothing procedure
function IIR2( input, f0, f1, f2 )
{
result[ 0 ] = input[ 0 ];result[ 1 ] = input[ 1 ];
for( i = 2; i < BarCount; i++ )
{
result[ i ] = f0 * input[ i ] + f1 * result[ i - 1 ] + f2 * result[ i - 2 ];
}
return result;
}
C1=C;
k=0.3;
RD=IIR2( C1, 0.3, 1.2+K, -0.5-K);
// B. Convexity definition
y=RD;
t=1;
Convex=(y-Ref(y,-t))/t>=(y-Ref(y,-(t+1)))/(t+1);
Concave=NOT(Convex);
Ascending=y>=Ref(y,-1);
Descending=NOT(ascending);
// C. Trend phases
Bullstart=Convex AND Ascending;// from A to B
Bullend=Concave AND ascending;// from B to C
Bearstart=Concave AND Descending;// from C to D
Bearend=Convex AND Descending;// from D to E
// D. Application
Color=IIf(Bullstart,colorDarkGreen,IIf(Bullend,colorTurquoise,IIf(Bearstart,colorDarkRed,colorPink)));
Plot(y,"",Color,8+styleThick);
Plot(C,"",47,64);GraphXSpace=5;
*AFL stands for Amibroker Formula Language, from amibroker.com
**IIR2 stands for Infinite Impulse filter 2nd order, as described in Amibroker 4.40 User´s guide
Convex AND Ascending;// colorDarkGreen
Concave AND Ascending;// colorTurquoise
Concave AND Descending;// colorDarkRed
Convex AND Descending;// colorPink
The AFL* code illustrates the example
// The 4 phases of an ideal sinusoidal
freq = 1;
y=sin( Cum( freq/10 ) );
t=1;
Convex=(y-Ref(y,-t))/t>=(y-Ref(y,-(t+1)))/(t+1);
Concave=NOT(Convex);
Ascending=y>=Ref(y,-1);
Descending=NOT(ascending);
Bullstart=Convex AND Ascending;
Bullend=Concave AND ascending;
Bearstart=Concave AND Descending;
Bearend=Convex AND Descending;
Color=IIf(Bullstart,colorDarkGreen,IIf(Bullend,colorTurquoise,IIf(Bearstart,colorDarkRed,colorPink)));
Plot(y,"",Color,8);
[see sin.gif]
We shall use a smoothing procedure [IIR2 filter **] as close to the sinusoidal as possible. [see IIR2.gif]
Of course the trend characteristics do not give the [-1, +1] oscillation, but the convexity/concavity are still detectable.
The superimpose of actual price candles gives a more descriptive picture of the 4 phases.
[see 4phases.gif]
The full AFL* code is
// The 4 phases of a stock graph
// A. Smothing procedure
function IIR2( input, f0, f1, f2 )
{
result[ 0 ] = input[ 0 ];result[ 1 ] = input[ 1 ];
for( i = 2; i < BarCount; i++ )
{
result[ i ] = f0 * input[ i ] + f1 * result[ i - 1 ] + f2 * result[ i - 2 ];
}
return result;
}
C1=C;
k=0.3;
RD=IIR2( C1, 0.3, 1.2+K, -0.5-K);
// B. Convexity definition
y=RD;
t=1;
Convex=(y-Ref(y,-t))/t>=(y-Ref(y,-(t+1)))/(t+1);
Concave=NOT(Convex);
Ascending=y>=Ref(y,-1);
Descending=NOT(ascending);
// C. Trend phases
Bullstart=Convex AND Ascending;// from A to B
Bullend=Concave AND ascending;// from B to C
Bearstart=Concave AND Descending;// from C to D
Bearend=Convex AND Descending;// from D to E
// D. Application
Color=IIf(Bullstart,colorDarkGreen,IIf(Bullend,colorTurquoise,IIf(Bearstart,colorDarkRed,colorPink)));
Plot(y,"",Color,8+styleThick);
Plot(C,"",47,64);GraphXSpace=5;
*AFL stands for Amibroker Formula Language, from amibroker.com
**IIR2 stands for Infinite Impulse filter 2nd order, as described in Amibroker 4.40 User´s guide