Traders' Tip text
We’ve coded Ehlers Autocorrelation Periodogram from September 2016 issue of Stocks & Commodities for Wealth-Lab 6 (.NET). To view the picture of spectral activity enhanced by using MESA with the same conditions, drag respective "parameter slider" on the bottom left of the screen.
The dominant cycle periods and periods where there were no useful cycles are represented by differently colored ranges: from yellow at the maximum amplitude to black as ice cold.
Figure 1 shows how the measured cycle periods change over time on the Daily chart of AXP (American Express).
Revision A 08/01/2016using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
namespace WealthLab.Strategies
{
public class TASC201609 : WealthScript
{
private StrategyParameter paramEnhance;
public TASC201609()
{
paramEnhance = CreateParameter("Enhance Resolution", 0, 0, 1, 1);
}
protected override void Execute()
{
bool EnhanceResolution = paramEnhance.ValueInt == 0 ? false : true;
DataSeries HP = new DataSeries(Bars, "HP");
DataSeries Filt = new DataSeries(Bars, "Filt");
DataSeries DominantCycle = new DataSeries(Bars, "DominantCycle");
double Deg2Rad = Math.PI / 180.0;
double cosInDegrees = Math.Cos((.707 * 360 / 48d) * Deg2Rad);
double sinInDegrees = Math.Sin((.707 * 360 / 48d) * Deg2Rad);
double alpha1 = (cosInDegrees + sinInDegrees - 1) / cosInDegrees;
double a1 = Math.Exp(-1.414 * Math.PI / 8.0);
double b1 = 2.0 * a1 * Math.Cos((1.414 * 180d / 8.0) * Deg2Rad);
double c2 = b1;
double c3 = -a1 * a1;
double c1 = 1 - c2 - c3;
for (int bar = 2; bar < Bars.Count; bar++)
{
HPbar = 0.5*(1 + alpha1)*(Closebar - Closebar-1) + alpha1*HPbar-1;
//Smooth with a SuperSmoother Filter
Filtbar = c1*(HPbar + HPbar-1) / 2 + c2*Filtbar-1 + c3*Filtbar-2;
}
DataSeries">48"> ds = new DataSeries[48;
ChartPane tp = CreatePane(50,false,false);
for( int n = 0; n < 48; n++ )
{
dsn = new DataSeries(Bars,n.ToString());
for( int bar = 0; bar < Bars.Count; bar++ )
dsnbar = n;
PlotSeries(tp, dsn, Color.Black, LineStyle.Solid, 16);
}
HideVolume();
for( int bar = 0; bar < Bars.Count; bar++)
{
SetPaneBackgroundColor(PricePane,bar,Color.Black);
int AvgLength = 3, M = 0;
double X = 0, Y = 0, Sx = 0, Sy = 0, Sxx = 0, Syy = 0, Sxy = 0;
double">70"> Corr = new double[70;
double">70"> CosinePart = new double[70;
double">70"> SinePart = new double[70;
double">70"> SqSum = new double[70;
double, R = new double70,2;
double">70"> Pwr = new double[70;
//Pearson correlation for each value of lag
for(int Lag = 0; Lag <= 48; Lag++)
{
//Set the averaging length as M
M = AvgLength;
if( AvgLength == 0 )
M = Lag;
Sx = 0; Sy = 0; Sxx = 0; Syy = 0; Sxy = 0;
for(int count = 0; count <= M-1; count++)
{
X = bar-count < 0 ? 0 : Filtbar-count;
//Y = bar-Lag+count < 0 ? 0 : Filtbar-Lag+count;
Y = bar-(Lag+count) < 0 ? 0 : Filtbar-(Lag+count); // Rev.A
Sx = Sx + X;
Sy = Sy + Y;
Sxx = Sxx + X*X;
Sxy = Sxy + X*Y;
Syy = Syy + Y*Y;
}
if( (M*Sxx - Sx*Sx)*(M*Syy - Sy*Sy) > 0 )
CorrLag = (M*Sxy-Sx*Sy)/Math.Sqrt((M*Sxx - Sx*Sx)*(M*Syy - Sy*Sy));
}
//Compute the Fourier Transform for each Correlation
for(int Period = 0; Period <= 48; Period++)
{
CosinePartPeriod = 0;
SinePartPeriod = 0;
for(int N = 3; N <= 48; N++)
{
double _cosInDegrees = Math.Cos(((double)N * 360 / (double)Period) * Deg2Rad);
double _sinInDegrees = Math.Sin(((double)N * 360 / (double)Period) * Deg2Rad);
CosinePartPeriod = CosinePartPeriod + CorrN*_cosInDegrees;
SinePartPeriod = SinePartPeriod + CorrN*_sinInDegrees;
}
SqSumPeriod = CosinePartPeriod*CosinePartPeriod + SinePartPeriod*SinePartPeriod;
}
for(int Period = 8; Period <= 48; Period++)
{
RPeriod, 1 = RPeriod, 0;
RPeriod, 0 = 0.2*SqSumPeriod*SqSumPeriod + 0.8*RPeriod,1;
}
//Find Maximum Power Level for Normalization
double MaxPwr = 0;
for(int Period = 8; Period <= 48; Period++)
{
if( RPeriod, 0 > MaxPwr )
MaxPwr = RPeriod, 0;
}
for(int Period = 8; Period <= 48; Period++)
{
PwrPeriod = RPeriod, 0 / MaxPwr;
}
//Optionally increase Display Resolution by raising the NormPwr to
//a higher mathematically power (since the maximum amplitude is
//unity, cubing all amplitudes further reduces the smaller ones)
if( EnhanceResolution )
{
for(int Period = 8; Period <= 48; Period++)
{
PwrPeriod = Math.Pow(PwrPeriod, 3);
}
}
//Compute the dominant cycle using the CG of the spectrum
double PeakPwr = 0, Spx = 0, Sp = 0;
for(int Period = 8; Period <= 48; Period++)
{
if( PwrPeriod > PeakPwr )
PeakPwr = PwrPeriod;
}
//Plot as a Heatmap
double Color1 = 255, Color2 = 0, Color3 = 0;
for(int Period = 8; Period < 48; Period++)
{
if( PwrPeriod > 0.5 )
{
Color1 = 255;
Color2 = 255*(2*PwrPeriod - 1);
}
else
{
Color1 = 2*255*PwrPeriod;
Color2 = 0;
}
Color1 = Math.Min((int)Color1,255); Color1 = Math.Max((int)Color1,0);
Color2 = Math.Min((int)Color2,255); Color2 = Math.Max((int)Color2,0);
Color3 = Math.Min((int)Color3,255); Color3 = Math.Max((int)Color3,0);
SetSeriesBarColor(bar, dsPeriod, Color.FromArgb(100,(int)Color1,(int)Color2,(int)Color3) );
}
}
}
}
}
Eugene
Wealth-Lab team
www.wealth-lab.com