Convexity, Concavity and their use

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
 

Attachments

The bullish movement begins with an ascending convex. [darkgreen area]
Before the end, the bulish pressure diminishes and the smoothing line becomes concave.[turquoise area]
The Inflection point gives the signal for the curvature change.
Exit two days later for a quick profit taking.
Since you know the exit day in advance, you may sell at Close, delay=0.
^NDX is the module array to apply the IIR2 smoothing and the concavity/convexity principle.
96/101 N100 stocks were profitable for this [bullish] year


Settings

Initial Equity: 10000 Periodicity/Positions: Daily/Long
Commissions: 0.25 % Annual interest rate: 0.00%
Range: 1/1/2003 00:00:00 - 30/9/2003 Apply to: Filter
Include Filter Exclude Filter
Market - Market -
Group Group 254 Group -
Sector - Sector -
Industry - Industry -
Watch list - Watch list -
Index - Index -
Favourite - Favourite -
Margin requirement: 100 Futures mode: No
Def. round lot size: 0 Def. Tick Size 0
Drawdowns based on: High/Low prices
Long trades
Buy price: Open Sell price: Close
Buy delay: 1 Sell delay: 0
Short trades
Short price: Open Cover price: Open
Short delay: 1 Cover delay: 1
Stops
Maximum loss: disabled Profit target: disabled
Value: 50.00 Value: 20.00
Exit at stop? yes Exit at stop? yes

Trailing stop: disabled
Value: 5.00
Exit at stop? yes

Formula


Z1=LastValue(Cum(1));
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;
}
SetForeign("^NDX");
C1=C;
k=0.45;
RD=IIR2( C1, 0.3, 1.2+K, -0.5-K);
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);
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)));
Buy=BULLSTART AND Ref(BEAREND,-1);
Sell=Ref(BULLEND AND Ref(BULLSTART,-1),-2);



Overall performance summary

Total net profit: 397055.54 Total commissions paid: 86943.91
Return on account: 39.31 % Open position gain/loss 0.00
Buy&Hold profit: 377566.31 Bars (avg. days) in test: 18887 (271)
Buy&Hold % return: 37.38% System to Buy&Hold index: 5.16%

Annual system % return: 56.29% Annual B&H % return: 53.38%

System drawdown: -3667.89 B&H drawdown: -4183.06
Max. system drawdown: -4803.97 B&H max. drawdown: -10382.31
Max. system % drawdown: -43.14% B&H max. % drawdown: -49.10%
Max. trade drawdown: -4531.67
Max. trade % drawdown: -33.83%
Trade drawdown: -3544.00

Total number of trades: 1515 Percent profitable: 63.1%
Number winning trades: 956 Number losing trades: 559
Profit of winners: 668304.02 Loss of losers: -271248.49
Total # of bars in winners: 5267 Total # of bars in losers: 2914
Commissions paid in winners: 55873.75 Commissions paid in losers: 31070.16

Largest winning trade: 4316.82 Largest losing trade: -2928.74
# of bars in largest winner: 6 # bars in largest loser: 5
Commission paid in largest winner: 101.96 Commission paid in largest loser: 43.27

Average winning trade: 699.06 Average losing trade: -485.24
Avg. # of bars in winners: 5.5 Avg. # bars in losers: 5.2
Avg. commission paid in winner: 58.45 Avg. commission paid in loser: 55.58
Max consec. winners: 12 Max consec. losers: 4

Bars out of the market: 10706 Interest earned: 0.00

Exposure: 43.3% Risk adjusted ann. return: 129.96%
Ratio avg win/avg loss: 1.44 Avg. trade (win & loss): 262.08
Profit factor: 2.46


For IB use the
Z1=LastValue(Cum(1));
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;
}
SetForeign("^NDX");
C1=C;
k=Optimize("k",0.45,0.1,0.5,0.01);
RD=IIR2( C1, 0.3, 1.2+K, -0.5-K);
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);
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,"y",Color,8);
Plot(C,"C",1,64);
PlotShapes((BULLEND AND Ref(BULLSTART,-1))*shapeHollowSmallDownTriangle,colorOrange);
Buy=BULLSTART AND Ref(BEAREND,-1);
Sell=Ref(BULLEND AND Ref(BULLSTART,-1),-2);
PlotShapes(shapeDownArrow*Sell,colorRed);Graphxspace=2;

to see the basic ^NDX chart, the Inflection points [small orange triangle] and the SellTheMarket exits.
Dimitris Tsokakis
 

Attachments

Back
Top